Skip to content

Commit

Permalink
Uninstall finds and kills any running elastic-agent watch process (#…
Browse files Browse the repository at this point in the history
…3384)

* kill watcher on uninstall

* Empty commit.

* Fix killWatcher.

* Empty commit.

* Another fix for killWatcher.

* Empty commit.

* Catch ErrProcessDone.

* Empty commit.

* Empty commit

* Add changelog fragment.

* Make it work on Windows.

* Change killWatcher to be in a loop.

* Add loop to killWatcher.

* Revert "Skip TestStandaloneUpgradeFailsStatus to fix failing integration tests again (#3391)"

This reverts commit bf467e3.

* Revert "Fix integration tests by waiting for the watcher to finish during upgrade tests (#3370)"

This reverts commit 94764be.

* Fix test.

* Revert "Revert "Skip TestStandaloneUpgradeFailsStatus to fix failing integration tests again (#3391)""

This reverts commit 3b0f040.

* Add progress tracking for uninstall like install.

* Log when no watchers our found.

* Improve uninstall.

* Fix data race.

(cherry picked from commit 7e86d24)

# Conflicts:
#	internal/pkg/agent/cmd/install.go
#	internal/pkg/agent/install/install.go
#	internal/pkg/agent/install/progress.go
#	internal/pkg/agent/install/progress_test.go
  • Loading branch information
blakerouse authored and mergify[bot] committed Oct 2, 2023
1 parent a3f132e commit 324f813
Show file tree
Hide file tree
Showing 9 changed files with 660 additions and 78 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
# Kind can be one of:
# - breaking-change: a change to previously-documented behavior
# - deprecation: functionality that is being removed in a later release
# - bug-fix: fixes a problem in a previous version
# - enhancement: extends functionality but does not break or fix existing behavior
# - feature: new functionality
# - known-issue: problems that we are aware of in a given version
# - security: impacts on the security of a product or a user’s deployment.
# - upgrade: important information for someone upgrading from a prior version
# - other: does not fit into any of the other categories
kind: bug-fix

# Change summary; a 80ish characters long description of the change.
summary: Uninstall finds and kills any running watcher process

# Long description; in case the summary is not enough to describe the change
# this field accommodate a description without length limits.
# NOTE: This field will be rendered only for breaking-change and known-issue kinds at the moment.
#description:

# Affected component; usually one of "elastic-agent", "fleet-server", "filebeat", "metricbeat", "auditbeat", "all", etc.
component:

# PR URL; optional; the PR number that added the changeset.
# If not present is automatically filled by the tooling finding the PR where this changelog fragment has been added.
# NOTE: the tooling supports backports, so it's able to fill the original PR number instead of the backport PR number.
# Please provide it if you are adding a fragment for a different PR.
pr: https://github.com/elastic/elastic-agent/pull/3384

# Issue URL; optional; the GitHub issue related to this changeset (either closes or is part of).
# If not present is automatically filled by the tooling with the issue linked to the PR number.
issue: https://github.com/elastic/elastic-agent/issues/3371
62 changes: 61 additions & 1 deletion internal/pkg/agent/cmd/install.go
Original file line number Diff line number Diff line change
Expand Up @@ -179,20 +179,43 @@ func installCmd(streams *cli.IOStreams, cmd *cobra.Command) error {
}
}

<<<<<<< HEAD

Check failure on line 182 in internal/pkg/agent/cmd/install.go

View workflow job for this annotation

GitHub Actions / lint (ubuntu-latest)

expected statement, found '<<' (typecheck)
cfgFile := paths.ConfigFile()
if status != install.PackageInstall {
err = install.Install(cfgFile, topPath)
=======

Check failure on line 186 in internal/pkg/agent/cmd/install.go

View workflow job for this annotation

GitHub Actions / lint (ubuntu-latest)

expected statement, found '==' (typecheck)
pt := install.NewProgressTracker(streams.Out)
s := pt.Start()
defer func() {
if err != nil {
s.Failed()
} else {
s.Succeeded()
}
}()

cfgFile := paths.ConfigFile()
if status != install.PackageInstall {
err = install.Install(cfgFile, topPath, s)
>>>>>>> 7e86d24cd3 (Uninstall finds and kills any running `elastic-agent watch` process (#3384))

Check failure on line 200 in internal/pkg/agent/cmd/install.go

View workflow job for this annotation

GitHub Actions / lint (ubuntu-latest)

expected statement, found '>>' (typecheck)
if err != nil {
return err
}

defer func() {
if err != nil {
_ = install.Uninstall(cfgFile, topPath, "")
uninstallStep := s.StepStart("Uninstalling")
innerErr := install.Uninstall(cfgFile, topPath, "", uninstallStep)
if innerErr != nil {
uninstallStep.Failed()
} else {
uninstallStep.Succeeded()
}
}
}()

if !delayEnroll {
<<<<<<< HEAD

Check failure on line 218 in internal/pkg/agent/cmd/install.go

View workflow job for this annotation

GitHub Actions / lint (ubuntu-latest)

expected statement, found '<<' (typecheck)
err = install.StartService(topPath)
if err != nil {
fmt.Fprintf(streams.Out, "Installation failed to start Elastic Agent service.\n")
Expand All @@ -202,6 +225,26 @@ func installCmd(streams *cli.IOStreams, cmd *cobra.Command) error {
defer func() {
if err != nil {
_ = install.StopService(topPath)
=======

Check failure on line 228 in internal/pkg/agent/cmd/install.go

View workflow job for this annotation

GitHub Actions / lint (ubuntu-latest)

expected statement, found '==' (typecheck)
startServiceStep := s.StepStart("Starting service")
err = install.StartService(topPath)
if err != nil {
startServiceStep.Failed()
fmt.Fprintf(streams.Out, "Installation failed to start Elastic Agent service.\n")
return err
}
startServiceStep.Succeeded()

defer func() {
if err != nil {
stoppingServiceStep := s.StepStart("Stopping service")
innerErr := install.StopService(topPath)
if innerErr != nil {
stoppingServiceStep.Failed()
} else {
stoppingServiceStep.Succeeded()
}
>>>>>>> 7e86d24cd3 (Uninstall finds and kills any running `elastic-agent watch` process (#3384))

Check failure on line 247 in internal/pkg/agent/cmd/install.go

View workflow job for this annotation

GitHub Actions / lint (ubuntu-latest)

expected statement, found '>>' (typecheck)
}
}()
}
Expand All @@ -214,12 +257,21 @@ func installCmd(streams *cli.IOStreams, cmd *cobra.Command) error {
enrollCmd.Stdin = os.Stdin
enrollCmd.Stdout = os.Stdout
enrollCmd.Stderr = os.Stderr
<<<<<<< HEAD

Check failure on line 260 in internal/pkg/agent/cmd/install.go

View workflow job for this annotation

GitHub Actions / lint (ubuntu-latest)

expected statement, found '<<' (typecheck)
err = enrollCmd.Start()
if err != nil {
=======

Check failure on line 263 in internal/pkg/agent/cmd/install.go

View workflow job for this annotation

GitHub Actions / lint (ubuntu-latest)

expected statement, found '==' (typecheck)

enrollStep := s.StepStart("Enrolling Elastic Agent with Fleet")
err = enrollCmd.Start()
if err != nil {
enrollStep.Failed()
>>>>>>> 7e86d24cd3 (Uninstall finds and kills any running `elastic-agent watch` process (#3384))

Check failure on line 269 in internal/pkg/agent/cmd/install.go

View workflow job for this annotation

GitHub Actions / lint (ubuntu-latest)

expected statement, found '>>' (typecheck)
return fmt.Errorf("failed to execute enroll command: %w", err)
}
err = enrollCmd.Wait()
if err != nil {
<<<<<<< HEAD
if status != install.PackageInstall {
var exitErr *exec.ExitError
_ = install.Uninstall(cfgFile, topPath, "")
Expand All @@ -229,6 +281,14 @@ func installCmd(streams *cli.IOStreams, cmd *cobra.Command) error {
}
return fmt.Errorf("enroll command failed for unknown reason: %w", err)
}
=======
enrollStep.Failed()
// uninstall doesn't need to be performed here the defer above will
// catch the error and perform the uninstall
return fmt.Errorf("enroll command failed for unknown reason: %w", err)
}
enrollStep.Succeeded()
>>>>>>> 7e86d24cd3 (Uninstall finds and kills any running `elastic-agent watch` process (#3384))
}

if err := info.CreateInstallMarker(topPath); err != nil {
Expand Down
8 changes: 7 additions & 1 deletion internal/pkg/agent/cmd/uninstall.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,9 +80,15 @@ func uninstallCmd(streams *cli.IOStreams, cmd *cobra.Command) error {
}
}

err = install.Uninstall(paths.ConfigFile(), paths.Top(), uninstallToken)
pt := install.NewProgressTracker(streams.Out)
s := pt.Start()

err = install.Uninstall(paths.ConfigFile(), paths.Top(), uninstallToken, s)
if err != nil {
s.Failed()
return err
} else {
s.Succeeded()
}
fmt.Fprintf(streams.Out, "Elastic Agent has been uninstalled.\n")

Expand Down
45 changes: 45 additions & 0 deletions internal/pkg/agent/install/install.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,11 @@ const (
)

// Install installs Elastic Agent persistently on the system including creating and starting its service.
<<<<<<< HEAD

Check failure on line 25 in internal/pkg/agent/install/install.go

View workflow job for this annotation

GitHub Actions / vulncheck (ubuntu-latest)

expected declaration, found '<<'

Check failure on line 25 in internal/pkg/agent/install/install.go

View workflow job for this annotation

GitHub Actions / macos

syntax error: non-declaration statement outside function body

Check failure on line 25 in internal/pkg/agent/install/install.go

View workflow job for this annotation

GitHub Actions / CodeCoverage

syntax error: non-declaration statement outside function body
func Install(cfgFile, topPath string) error {
=======

Check failure on line 27 in internal/pkg/agent/install/install.go

View workflow job for this annotation

GitHub Actions / macos

syntax error: unexpected ==, expected }

Check failure on line 27 in internal/pkg/agent/install/install.go

View workflow job for this annotation

GitHub Actions / CodeCoverage

syntax error: unexpected ==, expected }
func Install(cfgFile, topPath string, pt ProgressTrackerStep) error {
>>>>>>> 7e86d24cd3 (Uninstall finds and kills any running `elastic-agent watch` process (#3384))

Check failure on line 29 in internal/pkg/agent/install/install.go

View workflow job for this annotation

GitHub Actions / vulncheck (ubuntu-latest)

illegal character U+0023 '#'

Check failure on line 29 in internal/pkg/agent/install/install.go

View workflow job for this annotation

GitHub Actions / macos

syntax error: unexpected >>, expected }

Check failure on line 29 in internal/pkg/agent/install/install.go

View workflow job for this annotation

GitHub Actions / macos

invalid character U+0023 '#'

Check failure on line 29 in internal/pkg/agent/install/install.go

View workflow job for this annotation

GitHub Actions / CodeCoverage

syntax error: unexpected >>, expected }

Check failure on line 29 in internal/pkg/agent/install/install.go

View workflow job for this annotation

GitHub Actions / CodeCoverage

invalid character U+0023 '#'
dir, err := findDirectory()
if err != nil {
return errors.New(err, "failed to discover the source directory for installation", errors.TypeFilesystem)
Expand All @@ -33,13 +37,24 @@ func Install(cfgFile, topPath string) error {
// 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.
<<<<<<< HEAD
err = Uninstall(cfgFile, topPath, "")
if err != nil {
=======
s := pt.StepStart("Uninstalling current Elastic Agent")
err = Uninstall(cfgFile, topPath, "", s)
if err != nil {
s.Failed()
>>>>>>> 7e86d24cd3 (Uninstall finds and kills any running `elastic-agent watch` process (#3384))

Check failure on line 48 in internal/pkg/agent/install/install.go

View workflow job for this annotation

GitHub Actions / vulncheck (ubuntu-latest)

illegal character U+0023 '#'

Check failure on line 48 in internal/pkg/agent/install/install.go

View workflow job for this annotation

GitHub Actions / macos

invalid character U+0023 '#'

Check failure on line 48 in internal/pkg/agent/install/install.go

View workflow job for this annotation

GitHub Actions / CodeCoverage

invalid character U+0023 '#'
return errors.New(
err,
fmt.Sprintf("failed to uninstall Agent at (%s)", filepath.Dir(topPath)),
errors.M("directory", filepath.Dir(topPath)))
}
<<<<<<< HEAD
=======
s.Succeeded()
>>>>>>> 7e86d24cd3 (Uninstall finds and kills any running `elastic-agent watch` process (#3384))

Check failure on line 57 in internal/pkg/agent/install/install.go

View workflow job for this annotation

GitHub Actions / vulncheck (ubuntu-latest)

illegal character U+0023 '#'

Check failure on line 57 in internal/pkg/agent/install/install.go

View workflow job for this annotation

GitHub Actions / macos

invalid character U+0023 '#'

Check failure on line 57 in internal/pkg/agent/install/install.go

View workflow job for this annotation

GitHub Actions / CodeCoverage

invalid character U+0023 '#'

// ensure parent directory exists, copy source into install path
err = os.MkdirAll(filepath.Dir(topPath), 0755)
Expand All @@ -49,18 +64,32 @@ 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")
>>>>>>> 7e86d24cd3 (Uninstall finds and kills any running `elastic-agent watch` process (#3384))

Check failure on line 72 in internal/pkg/agent/install/install.go

View workflow job for this annotation

GitHub Actions / vulncheck (ubuntu-latest)

illegal character U+0023 '#'

Check failure on line 72 in internal/pkg/agent/install/install.go

View workflow job for this annotation

GitHub Actions / macos

invalid character U+0023 '#'

Check failure on line 72 in internal/pkg/agent/install/install.go

View workflow job for this annotation

GitHub Actions / CodeCoverage

invalid character U+0023 '#'
err = copy.Copy(dir, topPath, copy.Options{
OnSymlink: func(_ string) copy.SymlinkAction {

Check failure on line 74 in internal/pkg/agent/install/install.go

View workflow job for this annotation

GitHub Actions / macos

syntax error: unexpected ., expected (

Check failure on line 74 in internal/pkg/agent/install/install.go

View workflow job for this annotation

GitHub Actions / CodeCoverage

syntax error: unexpected ., expected (
return copy.Shallow
},

Check failure on line 76 in internal/pkg/agent/install/install.go

View workflow job for this annotation

GitHub Actions / macos

syntax error: unexpected } after top level declaration

Check failure on line 76 in internal/pkg/agent/install/install.go

View workflow job for this annotation

GitHub Actions / CodeCoverage

syntax error: unexpected } after top level declaration
Sync: true,
})
if err != nil {
<<<<<<< HEAD
=======
s.Failed()
>>>>>>> 7e86d24cd3 (Uninstall finds and kills any running `elastic-agent watch` process (#3384))

Check failure on line 83 in internal/pkg/agent/install/install.go

View workflow job for this annotation

GitHub Actions / vulncheck (ubuntu-latest)

illegal character U+0023 '#'

Check failure on line 83 in internal/pkg/agent/install/install.go

View workflow job for this annotation

GitHub Actions / macos

invalid character U+0023 '#'

Check failure on line 83 in internal/pkg/agent/install/install.go

View workflow job for this annotation

GitHub Actions / CodeCoverage

invalid character U+0023 '#'
return errors.New(
err,
fmt.Sprintf("failed to copy source directory (%s) to destination (%s)", dir, topPath),
errors.M("source", dir), errors.M("destination", topPath))
}
<<<<<<< HEAD
=======
s.Succeeded()
>>>>>>> 7e86d24cd3 (Uninstall finds and kills any running `elastic-agent watch` process (#3384))

Check failure on line 92 in internal/pkg/agent/install/install.go

View workflow job for this annotation

GitHub Actions / vulncheck (ubuntu-latest)

illegal character U+0023 '#'

// place shell wrapper, if present on platform
if paths.ShellWrapperPath != "" {
Expand Down Expand Up @@ -124,17 +153,33 @@ func Install(cfgFile, topPath string) error {
}

// install service
<<<<<<< HEAD
svc, err := newService(topPath)
if err != nil {
=======
s = pt.StepStart("Installing service")
svc, err := newService(topPath)
if err != nil {
s.Failed()
>>>>>>> 7e86d24cd3 (Uninstall finds and kills any running `elastic-agent watch` process (#3384))

Check failure on line 164 in internal/pkg/agent/install/install.go

View workflow job for this annotation

GitHub Actions / vulncheck (ubuntu-latest)

illegal character U+0023 '#'
return err
}
err = svc.Install()
if err != nil {
<<<<<<< HEAD
=======
s.Failed()
>>>>>>> 7e86d24cd3 (Uninstall finds and kills any running `elastic-agent watch` process (#3384))

Check failure on line 172 in internal/pkg/agent/install/install.go

View workflow job for this annotation

GitHub Actions / vulncheck (ubuntu-latest)

illegal character U+0023 '#'
return errors.New(
err,
fmt.Sprintf("failed to install service (%s)", paths.ServiceName),
errors.M("service", paths.ServiceName))
}
<<<<<<< HEAD
=======
s.Succeeded()

>>>>>>> 7e86d24cd3 (Uninstall finds and kills any running `elastic-agent watch` process (#3384))

Check failure on line 182 in internal/pkg/agent/install/install.go

View workflow job for this annotation

GitHub Actions / vulncheck (ubuntu-latest)

illegal character U+0023 '#'
return nil
}

Expand Down
Loading

0 comments on commit 324f813

Please sign in to comment.