From 16ec28cd3efbb05e18ea1672f4ff977819caffd6 Mon Sep 17 00:00:00 2001 From: Blake Rouse Date: Mon, 12 Aug 2024 14:35:08 -0400 Subject: [PATCH] Adjust Elastic Agent 7.17 to launch upgrade watcher version (#40452) * Adjust so 7.17 agent starts latest watcher. * Add changelog entry. * Fix code review. --- CHANGELOG.next.asciidoc | 4 ++++ x-pack/elastic-agent/pkg/agent/application/paths/common.go | 7 +++++++ .../pkg/agent/application/upgrade/rollback.go | 3 +-- .../elastic-agent/pkg/agent/application/upgrade/service.go | 6 ++---- .../pkg/agent/application/upgrade/service_darwin.go | 7 ++----- .../pkg/agent/application/upgrade/service_windows.go | 7 ++----- .../elastic-agent/pkg/agent/application/upgrade/upgrade.go | 1 + 7 files changed, 19 insertions(+), 16 deletions(-) diff --git a/CHANGELOG.next.asciidoc b/CHANGELOG.next.asciidoc index 2d6a41c8d698..62eb709991c8 100644 --- a/CHANGELOG.next.asciidoc +++ b/CHANGELOG.next.asciidoc @@ -31,6 +31,10 @@ Fix CVE-2023-25173 by updating github.com/containerd/containerd to v1.5.18 {pull *Functionbeat* +*Elastic Agent* + +- Fixes the issue where upgrading from 7.17 to a 8.x+ version only runs the 7.17 watcher for the latest 8.x release. + ==== Bugfixes *Affecting all Beats* diff --git a/x-pack/elastic-agent/pkg/agent/application/paths/common.go b/x-pack/elastic-agent/pkg/agent/application/paths/common.go index a9b80522f2de..d0627cd74946 100644 --- a/x-pack/elastic-agent/pkg/agent/application/paths/common.go +++ b/x-pack/elastic-agent/pkg/agent/application/paths/common.go @@ -169,6 +169,13 @@ func SetInstall(path string) { installPath = path } +// TopBinaryPath returns the path to the Elastic Agent binary that is inside the Top directory. +// +// This always points to the symlink that points to the latest Elastic Agent version. +func TopBinaryPath() string { + return filepath.Join(Top(), BinaryName) +} + // initialTop returns the initial top-level path for the binary // // When nested in top-level/data/elastic-agent-${hash}/ the result is top-level/. diff --git a/x-pack/elastic-agent/pkg/agent/application/upgrade/rollback.go b/x-pack/elastic-agent/pkg/agent/application/upgrade/rollback.go index 9267d019825e..d215a726e378 100644 --- a/x-pack/elastic-agent/pkg/agent/application/upgrade/rollback.go +++ b/x-pack/elastic-agent/pkg/agent/application/upgrade/rollback.go @@ -104,8 +104,7 @@ func InvokeWatcher(log *logger.Logger) error { return nil } - versionedHome := paths.VersionedHome(paths.Top()) - cmd := invokeCmd(versionedHome) + cmd := invokeCmd() defer func() { if cmd.Process != nil { log.Debugf("releasing watcher %v", cmd.Process.Pid) diff --git a/x-pack/elastic-agent/pkg/agent/application/upgrade/service.go b/x-pack/elastic-agent/pkg/agent/application/upgrade/service.go index 4601a1f74d95..6b067915e488 100644 --- a/x-pack/elastic-agent/pkg/agent/application/upgrade/service.go +++ b/x-pack/elastic-agent/pkg/agent/application/upgrade/service.go @@ -200,10 +200,8 @@ func (p *noopPidProvider) Name() string { return "noop" } func (p *noopPidProvider) PID(ctx context.Context) (int, error) { return 0, nil } -func invokeCmd(topPath string) *exec.Cmd { - homeExePath := filepath.Join(topPath, agentName) - - cmd := exec.Command(homeExePath, watcherSubcommand, +func invokeCmd() *exec.Cmd { + cmd := exec.Command(paths.TopBinaryPath(), watcherSubcommand, "--path.config", paths.Config(), "--path.home", paths.Top(), ) diff --git a/x-pack/elastic-agent/pkg/agent/application/upgrade/service_darwin.go b/x-pack/elastic-agent/pkg/agent/application/upgrade/service_darwin.go index 5db095ce7700..1a86dbdea69f 100644 --- a/x-pack/elastic-agent/pkg/agent/application/upgrade/service_darwin.go +++ b/x-pack/elastic-agent/pkg/agent/application/upgrade/service_darwin.go @@ -14,7 +14,6 @@ import ( "fmt" "os" "os/exec" - "path/filepath" "regexp" "strconv" "strings" @@ -114,10 +113,8 @@ func (p *darwinPidProvider) piderFromCmd(ctx context.Context, name string, args } } -func invokeCmd(topPath string) *exec.Cmd { - homeExePath := filepath.Join(topPath, agentName) - - cmd := exec.Command(homeExePath, watcherSubcommand, +func invokeCmd() *exec.Cmd { + cmd := exec.Command(paths.TopBinaryPath(), watcherSubcommand, "--path.config", paths.Config(), "--path.home", paths.Top(), ) diff --git a/x-pack/elastic-agent/pkg/agent/application/upgrade/service_windows.go b/x-pack/elastic-agent/pkg/agent/application/upgrade/service_windows.go index 28f92e93b3ed..bc3b4b2a12a2 100644 --- a/x-pack/elastic-agent/pkg/agent/application/upgrade/service_windows.go +++ b/x-pack/elastic-agent/pkg/agent/application/upgrade/service_windows.go @@ -10,7 +10,6 @@ package upgrade import ( "context" "os/exec" - "path/filepath" "time" "golang.org/x/sys/windows/svc/mgr" @@ -62,10 +61,8 @@ func (p *pidProvider) PID(ctx context.Context) (int, error) { return int(status.ProcessId), nil } -func invokeCmd(topPath string) *exec.Cmd { - homeExePath := filepath.Join(topPath, agentName) - - cmd := exec.Command(homeExePath, watcherSubcommand, +func invokeCmd() *exec.Cmd { + cmd := exec.Command(paths.TopBinaryPath(), watcherSubcommand, "--path.config", paths.Config(), "--path.home", paths.Top(), ) diff --git a/x-pack/elastic-agent/pkg/agent/application/upgrade/upgrade.go b/x-pack/elastic-agent/pkg/agent/application/upgrade/upgrade.go index 5c03cf83e10b..037a0aca966e 100644 --- a/x-pack/elastic-agent/pkg/agent/application/upgrade/upgrade.go +++ b/x-pack/elastic-agent/pkg/agent/application/upgrade/upgrade.go @@ -168,6 +168,7 @@ func (u *Upgrader) Upgrade(ctx context.Context, a Action, reexecNow bool, skipVe return nil, err } + // InvokeWatcher invokes the watcher using the symlink that is rotated above with ChangeSymlink if err := InvokeWatcher(u.log); err != nil { rollbackInstall(ctx, newHash) return nil, errors.New("failed to invoke rollback watcher", err)