Unverified Commit 8c2f90a4 authored by Silvano Cerza's avatar Silvano Cerza Committed by GitHub

Add --run-post-install and --skip-post-install flags to upgrade command (#953)

* Add --run-post-install and --skip-post-install flags to upgrade command
parent be39aa9e
......@@ -43,7 +43,7 @@ func initInstallCommand() *cobra.Command {
Args: cobra.MinimumNArgs(1),
Run: runInstallCommand,
}
addPostInstallFlagsToCommand(installCommand)
AddPostInstallFlagsToCommand(installCommand)
return installCommand
}
......@@ -52,12 +52,15 @@ var postInstallFlags struct {
skipPostInstall bool
}
func addPostInstallFlagsToCommand(cmd *cobra.Command) {
// AddPostInstallFlagsToCommand adds flags that can be used to force running or skipping
// of post installation scripts
func AddPostInstallFlagsToCommand(cmd *cobra.Command) {
cmd.Flags().BoolVar(&postInstallFlags.runPostInstall, "run-post-install", false, "Force run of post-install scripts (if the CLI is not running interactively).")
cmd.Flags().BoolVar(&postInstallFlags.skipPostInstall, "skip-post-install", false, "Force skip of post-install scripts (if the CLI is running interactively).")
}
func detectSkipPostInstallValue() bool {
// DetectSkipPostInstallValue returns true if a post install script must be run
func DetectSkipPostInstallValue() bool {
if postInstallFlags.runPostInstall && postInstallFlags.skipPostInstall {
feedback.Errorf("The flags --run-post-install and --skip-post-install can't be both set at the same time.")
os.Exit(errorcodes.ErrBadArgument)
......@@ -100,7 +103,7 @@ func runInstallCommand(cmd *cobra.Command, args []string) {
PlatformPackage: platformRef.PackageName,
Architecture: platformRef.Architecture,
Version: platformRef.Version,
SkipPostInstall: detectSkipPostInstallValue(),
SkipPostInstall: DetectSkipPostInstallValue(),
}
_, err := core.PlatformInstall(context.Background(), platformInstallReq, output.ProgressBar(), output.TaskProgress())
if err != nil {
......
......@@ -42,7 +42,7 @@ func initUpgradeCommand() *cobra.Command {
" " + os.Args[0] + " core upgrade arduino:samd",
Run: runUpgradeCommand,
}
addPostInstallFlagsToCommand(upgradeCommand)
AddPostInstallFlagsToCommand(upgradeCommand)
return upgradeCommand
}
......@@ -92,7 +92,7 @@ func runUpgradeCommand(cmd *cobra.Command, args []string) {
Instance: inst,
PlatformPackage: platformRef.PackageName,
Architecture: platformRef.Architecture,
SkipPostInstall: detectSkipPostInstallValue(),
SkipPostInstall: DetectSkipPostInstallValue(),
}
_, err := core.PlatformUpgrade(context.Background(), r, output.ProgressBar(), output.TaskProgress())
......
......@@ -19,6 +19,7 @@ import (
"context"
"os"
"github.com/arduino/arduino-cli/cli/core"
"github.com/arduino/arduino-cli/cli/errorcodes"
"github.com/arduino/arduino-cli/cli/feedback"
"github.com/arduino/arduino-cli/cli/instance"
......@@ -40,6 +41,7 @@ func NewCommand() *cobra.Command {
Run: runUpgradeCommand,
}
core.AddPostInstallFlagsToCommand(upgradeCommand)
return upgradeCommand
}
......@@ -53,7 +55,8 @@ func runUpgradeCommand(cmd *cobra.Command, args []string) {
logrus.Info("Executing `arduino upgrade`")
err = commands.Upgrade(context.Background(), &rpc.UpgradeReq{
Instance: inst,
Instance: inst,
SkipPostInstall: core.DetectSkipPostInstallValue(),
}, output.NewDownloadProgressBarCB(), output.TaskProgress())
if err != nil {
......
......@@ -515,6 +515,7 @@ func Upgrade(ctx context.Context, req *rpc.UpgradeReq, downloadCB DownloadProgre
toolsToInstall := []*cores.ToolRelease{}
for _, tool := range tools {
if tool.IsInstalled() {
logrus.WithField("tool", tool).Warn("Tool already installed")
taskCB(&rpc.TaskProgress{Name: "Tool " + tool.String() + " already installed", Completed: true})
} else {
toolsToInstall = append(toolsToInstall, tool)
......@@ -536,7 +537,8 @@ func Upgrade(ctx context.Context, req *rpc.UpgradeReq, downloadCB DownloadProgre
return err
}
taskCB(&rpc.TaskProgress{Name: "Installing " + latest.String()})
logrus.Info("Updating platform " + installed.String())
taskCB(&rpc.TaskProgress{Name: "Updating " + latest.String()})
// Installs tools
for _, tool := range toolsToInstall {
......@@ -549,6 +551,7 @@ func Upgrade(ctx context.Context, req *rpc.UpgradeReq, downloadCB DownloadProgre
// Installs platform
err = pm.InstallPlatform(latest)
if err != nil {
logrus.WithError(err).Error("Cannot install platform")
taskCB(&rpc.TaskProgress{Message: "Error installing " + latest.String()})
return err
}
......@@ -558,12 +561,27 @@ func Upgrade(ctx context.Context, req *rpc.UpgradeReq, downloadCB DownloadProgre
// In case uninstall fails tries to rollback
if err != nil {
logrus.WithError(err).Error("Error updating platform.")
taskCB(&rpc.TaskProgress{Message: "Error upgrading platform: " + err.Error()})
// Rollback
if err := pm.UninstallPlatform(latest); err != nil {
logrus.WithError(err).Error("Error rolling-back changes.")
taskCB(&rpc.TaskProgress{Message: "Error rolling-back changes: " + err.Error()})
return err
}
}
// Perform post install
if !req.SkipPostInstall {
logrus.Info("Running post_install script")
taskCB(&rpc.TaskProgress{Message: "Configuring platform"})
if err := pm.RunPostInstallScript(latest); err != nil {
taskCB(&rpc.TaskProgress{Message: fmt.Sprintf("WARNING: cannot run post install: %s", err)})
}
} else {
logrus.Info("Skipping platform configuration (post_install run).")
taskCB(&rpc.TaskProgress{Message: "Skipping platform configuration"})
}
}
}
......
......@@ -16,7 +16,7 @@
// Code generated by protoc-gen-go. DO NOT EDIT.
// versions:
// protoc-gen-go v1.25.0
// protoc v3.11.3
// protoc v3.12.4
// source: commands/board.proto
package commands
......
This diff is collapsed.
......@@ -221,6 +221,9 @@ message OutdatedResp {
message UpgradeReq {
// Arduino Core Service instance from the Init response.
Instance instance = 1;
// Set to true to not run (eventual) post install scripts
bool skipPostInstall = 2;
}
message UpgradeResp {
......
......@@ -16,7 +16,7 @@
// Code generated by protoc-gen-go. DO NOT EDIT.
// versions:
// protoc-gen-go v1.25.0
// protoc v3.11.3
// protoc v3.12.4
// source: commands/common.proto
package commands
......
......@@ -16,7 +16,7 @@
// Code generated by protoc-gen-go. DO NOT EDIT.
// versions:
// protoc-gen-go v1.25.0
// protoc v3.11.3
// protoc v3.12.4
// source: commands/compile.proto
package commands
......
......@@ -16,7 +16,7 @@
// Code generated by protoc-gen-go. DO NOT EDIT.
// versions:
// protoc-gen-go v1.25.0
// protoc v3.11.3
// protoc v3.12.4
// source: commands/core.proto
package commands
......
......@@ -16,7 +16,7 @@
// Code generated by protoc-gen-go. DO NOT EDIT.
// versions:
// protoc-gen-go v1.25.0
// protoc v3.11.3
// protoc v3.12.4
// source: commands/lib.proto
package commands
......
......@@ -16,7 +16,7 @@
// Code generated by protoc-gen-go. DO NOT EDIT.
// versions:
// protoc-gen-go v1.25.0
// protoc v3.11.3
// protoc v3.12.4
// source: commands/upload.proto
package commands
......
......@@ -16,7 +16,7 @@
// Code generated by protoc-gen-go. DO NOT EDIT.
// versions:
// protoc-gen-go v1.25.0
// protoc v3.11.3
// protoc v3.12.4
// source: debug/debug.proto
package debug
......
......@@ -16,7 +16,7 @@
// Code generated by protoc-gen-go. DO NOT EDIT.
// versions:
// protoc-gen-go v1.25.0
// protoc v3.11.3
// protoc v3.12.4
// source: monitor/monitor.proto
package monitor
......
......@@ -16,7 +16,7 @@
// Code generated by protoc-gen-go. DO NOT EDIT.
// versions:
// protoc-gen-go v1.25.0
// protoc v3.11.3
// protoc v3.12.4
// source: settings/settings.proto
package settings
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment