From bd562dccda78c78eab248a2ca37574d2761245ac Mon Sep 17 00:00:00 2001 From: Shaunak Kashyap Date: Mon, 25 Sep 2023 11:13:14 -0700 Subject: [PATCH] `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 --- internal/pkg/agent/cmd/install.go | 4 ++-- internal/pkg/agent/install/install.go | 27 +++++++++++++++++++++++++++ 2 files changed, 29 insertions(+), 2 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..73257afdffc 100644 --- a/internal/pkg/agent/install/install.go +++ b/internal/pkg/agent/install/install.go @@ -28,6 +28,7 @@ func Install(cfgFile, topPath string) error { return errors.New(err, "failed to discover the source directory for installation", errors.TypeFilesystem) } +<<<<<<< HEAD // Uninstall current installation // // There is no uninstall token for "install" command. @@ -39,6 +40,26 @@ func Install(cfgFile, topPath string) error { 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. + s := pt.StepStart("Uninstalling current Elastic Agent") + err = Uninstall(cfgFile, topPath, "", s) + if err != nil { + s.Failed() + return errors.New( + err, + fmt.Sprintf("failed to uninstall Agent at (%s)", filepath.Dir(topPath)), + errors.M("directory", filepath.Dir(topPath))) + } + s.Succeeded() +>>>>>>> 0c43005695 (`elastic-agent install`: Only uninstall when Agent is installed (#3415)) } // ensure parent directory exists, copy source into install path @@ -49,6 +70,12 @@ func Install(cfgFile, topPath string) error { fmt.Sprintf("failed to create installation parent directory (%s)", filepath.Dir(topPath)), errors.M("directory", filepath.Dir(topPath))) } +<<<<<<< HEAD +======= + + // copy source into install path + s := pt.StepStart("Copying files") +>>>>>>> 0c43005695 (`elastic-agent install`: Only uninstall when Agent is installed (#3415)) err = copy.Copy(dir, topPath, copy.Options{ OnSymlink: func(_ string) copy.SymlinkAction { return copy.Shallow