Skip to content

Commit

Permalink
[8.10](backport #3415) elastic-agent install: Only uninstall when A…
Browse files Browse the repository at this point in the history
…gent 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 0c43005)

# Conflicts:
#	internal/pkg/agent/install/install.go

* Addressing conflicts

---------

Co-authored-by: Shaunak Kashyap <[email protected]>
  • Loading branch information
mergify[bot] and ycombinator authored Oct 12, 2023
1 parent a92ca32 commit cd23535
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 13 deletions.
4 changes: 2 additions & 2 deletions internal/pkg/agent/cmd/install.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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 {
Expand Down
26 changes: 15 additions & 11 deletions internal/pkg/agent/install/install.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit cd23535

Please sign in to comment.