Skip to content

Commit

Permalink
[Elastic Agent] Remove symlink.prev from previously failed upgrade (#…
Browse files Browse the repository at this point in the history
…26785) (#26806)

[Elastic Agent] Remove symlink.prev from previously failed upgrade (#26785)
  • Loading branch information
michalpristas authored Jul 9, 2021
1 parent 67c8b09 commit a89c16c
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 3 deletions.
1 change: 1 addition & 0 deletions x-pack/elastic-agent/CHANGELOG.next.asciidoc
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
21 changes: 18 additions & 3 deletions x-pack/elastic-agent/pkg/agent/application/upgrade/step_relink.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,22 +21,37 @@ 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")
}

// 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)
}

0 comments on commit a89c16c

Please sign in to comment.