Skip to content

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

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Sep 24, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 32 additions & 1 deletion cli/upgrade/upgrade.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,39 @@ import (
"github.com/arduino/arduino-cli/cli/instance"
"github.com/arduino/arduino-cli/cli/output"
"github.com/arduino/arduino-cli/commands"
"github.com/arduino/arduino-cli/configuration"
rpc "github.com/arduino/arduino-cli/rpc/commands"
"github.com/sirupsen/logrus"
"github.com/spf13/cobra"
)

var postInstallFlags struct {
runPostInstall bool
skipPostInstall bool
}

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)
}
if postInstallFlags.runPostInstall {
logrus.Info("Will run post-install by user request")
return false
}
if postInstallFlags.skipPostInstall {
logrus.Info("Will skip post-install by user request")
return true
}

if !configuration.IsInteractive {
logrus.Info("Not running from console, will skip post-install by default")
return true
}
logrus.Info("Running from console, will run post-install by default")
return false
}

// NewCommand creates a new `upgrade` command
func NewCommand() *cobra.Command {
upgradeCommand := &cobra.Command{
Expand All @@ -40,6 +68,8 @@ func NewCommand() *cobra.Command {
Run: runUpgradeCommand,
}

upgradeCommand.Flags().BoolVar(&postInstallFlags.runPostInstall, "run-post-install", false, "Force run of post-install scripts (if the CLI is not running interactively).")
upgradeCommand.Flags().BoolVar(&postInstallFlags.skipPostInstall, "skip-post-install", false, "Force skip of post-install scripts (if the CLI is running interactively).")
return upgradeCommand
}

Expand All @@ -53,7 +83,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: detectSkipPostInstallValue(),
}, output.NewDownloadProgressBarCB(), output.TaskProgress())

if err != nil {
Expand Down
19 changes: 18 additions & 1 deletion commands/instances.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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 {
Expand All @@ -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
}
Expand All @@ -558,13 +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()})
}
}

// 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"})
}
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion rpc/commands/board.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading