From cd235358921f4beb08c4f297d3ede0c79857061c Mon Sep 17 00:00:00 2001 From: "mergify[bot]" <37929162+mergify[bot]@users.noreply.github.com> Date: Thu, 12 Oct 2023 10:04:55 -0700 Subject: [PATCH] [8.10](backport #3415) `elastic-agent install`: Only uninstall when Agent is installed (#3472) * `elastic-agent install`: Only uninstall when Agent is installed (#3415) * Only uninstall if --force is present * Improve help text * Add missing newline * Don't pass force * Fix more conflicts * Fixing compile error introduced while resolving conflicts (cherry picked from commit 0c4300569583b3c44fd8b0acb9574ffcb7bae316) # Conflicts: # internal/pkg/agent/install/install.go * Addressing conflicts --------- Co-authored-by: Shaunak Kashyap --- internal/pkg/agent/cmd/install.go | 4 ++-- internal/pkg/agent/install/install.go | 26 +++++++++++++++----------- 2 files changed, 17 insertions(+), 13 deletions(-) diff --git a/internal/pkg/agent/cmd/install.go b/internal/pkg/agent/cmd/install.go index 90fd9eba10d..59fefaaa11a 100644 --- a/internal/pkg/agent/cmd/install.go +++ b/internal/pkg/agent/cmd/install.go @@ -40,7 +40,7 @@ would like the Agent to operate. }, } - cmd.Flags().BoolP("force", "f", false, "Force overwrite the current and do not prompt for confirmation") + cmd.Flags().BoolP("force", "f", false, "Force overwrite the current installation and do not prompt for confirmation") cmd.Flags().BoolP("non-interactive", "n", false, "Install Elastic Agent in non-interactive mode which will not prompt on missing parameters but fails instead.") cmd.Flags().String(flagInstallBasePath, paths.DefaultBasePath, "The path where the Elastic Agent will be installed. It must be an absolute path.") addEnrollFlags(cmd) @@ -83,7 +83,7 @@ func installCmd(streams *cli.IOStreams, cmd *cobra.Command) error { nonInteractive, _ := cmd.Flags().GetBool("non-interactive") if nonInteractive { - fmt.Fprintf(streams.Out, "Installing in non-interactive mode.") + fmt.Fprintln(streams.Out, "Installing in non-interactive mode.") } if status == install.PackageInstall { diff --git a/internal/pkg/agent/install/install.go b/internal/pkg/agent/install/install.go index b91c931afd6..c54c9213718 100644 --- a/internal/pkg/agent/install/install.go +++ b/internal/pkg/agent/install/install.go @@ -28,17 +28,21 @@ func Install(cfgFile, topPath string) error { return errors.New(err, "failed to discover the source directory for installation", errors.TypeFilesystem) } - // Uninstall current installation - // - // There is no uninstall token for "install" command. - // Uninstall will fail on protected agent. - // The protected Agent will need to be uninstalled first before it can be installed. - err = Uninstall(cfgFile, topPath, "") - if err != nil { - return errors.New( - err, - fmt.Sprintf("failed to uninstall Agent at (%s)", filepath.Dir(topPath)), - errors.M("directory", filepath.Dir(topPath))) + // We only uninstall Agent if it is currently installed. + status, _ := Status(topPath) + if status == Installed { + // Uninstall current installation + // + // There is no uninstall token for "install" command. + // Uninstall will fail on protected agent. + // The protected Agent will need to be uninstalled first before it can be installed. + err = Uninstall(cfgFile, topPath, "") + if err != nil { + return errors.New( + err, + fmt.Sprintf("failed to uninstall Agent at (%s)", filepath.Dir(topPath)), + errors.M("directory", filepath.Dir(topPath))) + } } // ensure parent directory exists, copy source into install path