From a89c16c1028d28402894aa9dc6686b745c82748e Mon Sep 17 00:00:00 2001 From: Michal Pristas Date: Fri, 9 Jul 2021 15:48:59 +0200 Subject: [PATCH] [Elastic Agent] Remove symlink.prev from previously failed upgrade (#26785) (#26806) [Elastic Agent] Remove symlink.prev from previously failed upgrade (#26785) --- x-pack/elastic-agent/CHANGELOG.next.asciidoc | 1 + .../pkg/agent/application/upgrade/rollback.go | 3 +++ .../agent/application/upgrade/step_relink.go | 21 ++++++++++++++++--- 3 files changed, 22 insertions(+), 3 deletions(-) diff --git a/x-pack/elastic-agent/CHANGELOG.next.asciidoc b/x-pack/elastic-agent/CHANGELOG.next.asciidoc index 0795c2e06df1..a443566ce808 100644 --- a/x-pack/elastic-agent/CHANGELOG.next.asciidoc +++ b/x-pack/elastic-agent/CHANGELOG.next.asciidoc @@ -75,6 +75,7 @@ - Fix add support for Logstash output. {pull}24305[24305] - Do not log Elasticsearch configuration for monitoring output when running with debug. {pull}26583[26583] - Fix issue where proxy enrollment options broke enrollment command. {pull}26749[26749] +- Remove symlink.prev from previously failed upgrade {pull}26785[26785] ==== New features 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 129fde5b6911..9267d019825e 100644 --- a/x-pack/elastic-agent/pkg/agent/application/upgrade/rollback.go +++ b/x-pack/elastic-agent/pkg/agent/application/upgrade/rollback.go @@ -73,6 +73,9 @@ func Cleanup(currentHash string, removeMarker bool) error { return err } + // remove symlink to avoid upgrade failures, ignore error + _ = os.Remove(prevSymlinkPath()) + dirPrefix := fmt.Sprintf("%s-", agentName) currentDir := fmt.Sprintf("%s-%s", agentName, currentHash) for _, dir := range subdirs { diff --git a/x-pack/elastic-agent/pkg/agent/application/upgrade/step_relink.go b/x-pack/elastic-agent/pkg/agent/application/upgrade/step_relink.go index bdcff79d840c..0636964e434a 100644 --- a/x-pack/elastic-agent/pkg/agent/application/upgrade/step_relink.go +++ b/x-pack/elastic-agent/pkg/agent/application/upgrade/step_relink.go @@ -21,18 +21,22 @@ func ChangeSymlink(ctx context.Context, targetHash string) error { // create symlink to elastic-agent-{hash} hashedDir := fmt.Sprintf("%s-%s", agentName, targetHash) - agentPrevName := agentName + ".prev" symlinkPath := filepath.Join(paths.Top(), agentName) newPath := filepath.Join(paths.Top(), "data", hashedDir, agentName) // handle windows suffixes if runtime.GOOS == "windows" { - agentPrevName = agentName + ".exe.prev" symlinkPath += ".exe" newPath += ".exe" } - prevNewPath := filepath.Join(paths.Top(), agentPrevName) + prevNewPath := prevSymlinkPath() + + // remove symlink to avoid upgrade failures + if err := os.Remove(prevNewPath); !os.IsNotExist(err) { + return err + } + if err := os.Symlink(newPath, prevNewPath); err != nil { return errors.New(err, errors.TypeFilesystem, "failed to update agent symlink") } @@ -40,3 +44,14 @@ func ChangeSymlink(ctx context.Context, targetHash string) error { // safely rotate return file.SafeFileRotate(symlinkPath, prevNewPath) } + +func prevSymlinkPath() string { + agentPrevName := agentName + ".prev" + + // handle windows suffixes + if runtime.GOOS == "windows" { + agentPrevName = agentName + ".exe.prev" + } + + return filepath.Join(paths.Top(), agentPrevName) +}