From d773305c316e069a445f998d69b676478ecd0c6a Mon Sep 17 00:00:00 2001 From: Michal Pristas Date: Tue, 4 May 2021 14:43:12 +0200 Subject: [PATCH] Do not use unversioned home with watcher binary path (#25423) (#25459) --- x-pack/elastic-agent/CHANGELOG.next.asciidoc | 1 + .../pkg/agent/application/paths/common.go | 11 ++++++----- .../pkg/agent/application/upgrade/rollback.go | 3 ++- .../pkg/agent/application/upgrade/service.go | 4 ++-- .../pkg/agent/application/upgrade/service_darwin.go | 4 ++-- .../pkg/agent/application/upgrade/service_windows.go | 4 ++-- 6 files changed, 15 insertions(+), 12 deletions(-) diff --git a/x-pack/elastic-agent/CHANGELOG.next.asciidoc b/x-pack/elastic-agent/CHANGELOG.next.asciidoc index 922a7e11044..eebc7aceb1b 100644 --- a/x-pack/elastic-agent/CHANGELOG.next.asciidoc +++ b/x-pack/elastic-agent/CHANGELOG.next.asciidoc @@ -61,6 +61,7 @@ - Reduce log level for listener cleanup to debug {pull}25274[25274] - Delay the restart of application when a status report of failure is given {pull}25339[25339] - Don't log when upgrade capability doesn't apply {pull}25386[25386] +- Fixed issue when unversioned home is set and invoked watcher failing with ENOENT {issue}25371[25371] ==== New features 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 d5d56d5d000..b445cb0c40b 100644 --- a/x-pack/elastic-agent/pkg/agent/application/paths/common.go +++ b/x-pack/elastic-agent/pkg/agent/application/paths/common.go @@ -75,7 +75,7 @@ func Home() string { if unversionedHome { return topPath } - return versionedHome(topPath) + return VersionedHome(topPath) } // IsVersionHome returns true if the Home path is versioned based on build. @@ -134,6 +134,11 @@ func SetLogs(path string) { logsPath = path } +// VersionedHome returns a versioned path based on a TopPath and used commit. +func VersionedHome(base string) string { + return filepath.Join(base, "data", fmt.Sprintf("elastic-agent-%s", release.ShortCommit())) +} + // initialTop returns the initial top-level path for the binary // // When nested in top-level/data/elastic-agent-${hash}/ the result is top-level/. @@ -163,7 +168,3 @@ func insideData(exePath string) bool { expectedPath := filepath.Join("data", fmt.Sprintf("elastic-agent-%s", release.ShortCommit())) return strings.HasSuffix(exePath, expectedPath) } - -func versionedHome(base string) string { - return filepath.Join(base, "data", fmt.Sprintf("elastic-agent-%s", release.ShortCommit())) -} 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 be81b8df9df..129fde5b691 100644 --- a/x-pack/elastic-agent/pkg/agent/application/upgrade/rollback.go +++ b/x-pack/elastic-agent/pkg/agent/application/upgrade/rollback.go @@ -101,7 +101,8 @@ func InvokeWatcher(log *logger.Logger) error { return nil } - cmd := invokeCmd() + versionedHome := paths.VersionedHome(paths.Top()) + cmd := invokeCmd(versionedHome) 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 901a7884a8e..09d87a82a5d 100644 --- a/x-pack/elastic-agent/pkg/agent/application/upgrade/service.go +++ b/x-pack/elastic-agent/pkg/agent/application/upgrade/service.go @@ -200,8 +200,8 @@ func (p *noopPidProvider) Name() string { return "noop" } func (p *noopPidProvider) PID(ctx context.Context) (int, error) { return 0, nil } -func invokeCmd() *exec.Cmd { - homeExePath := filepath.Join(paths.Home(), agentName) +func invokeCmd(topPath string) *exec.Cmd { + homeExePath := filepath.Join(topPath, agentName) cmd := exec.Command(homeExePath, watcherSubcommand, "--path.config", paths.Config(), 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 17bb28bd176..77c3f7d5f4f 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 @@ -113,8 +113,8 @@ func (p *darwinPidProvider) piderFromCmd(ctx context.Context, name string, args } } -func invokeCmd() *exec.Cmd { - homeExePath := filepath.Join(paths.Home(), agentName) +func invokeCmd(topPath string) *exec.Cmd { + homeExePath := filepath.Join(topPath, agentName) cmd := exec.Command(homeExePath, watcherSubcommand, "--path.config", paths.Config(), 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 68c69979c78..61821e69572 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 @@ -61,8 +61,8 @@ func (p *pidProvider) PID(ctx context.Context) (int, error) { return int(status.ProcessId), nil } -func invokeCmd() *exec.Cmd { - homeExePath := filepath.Join(paths.Home(), agentName) +func invokeCmd(topPath string) *exec.Cmd { + homeExePath := filepath.Join(topPath, agentName) cmd := exec.Command(homeExePath, watcherSubcommand, "--path.config", paths.Config(),