Skip to content

Commit

Permalink
Preserve component state on upgrade (#2207)
Browse files Browse the repository at this point in the history
Preserve component state on upgrade (#2207)

(cherry picked from commit 9c03fae)
  • Loading branch information
michalpristas authored and mergify[bot] committed Feb 1, 2023
1 parent 08a62e3 commit d80d671
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 0 deletions.
31 changes: 31 additions & 0 deletions changelog/fragments/1675085396-preserve-state-on-upgrade.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
# 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

# Change summary; a 80ish characters long description of the change.
summary: Preserve component state on upgrade

# Long description; in case the summary is not enough to describe the change
# this field accommodate a description without length limits.
#description:

# Affected component; a word indicating the component this changeset affects.
component:

# PR number; 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: 2207

# Issue number; 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: 2136
24 changes: 24 additions & 0 deletions internal/pkg/agent/application/upgrade/upgrade.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ const (
agentName = "elastic-agent"
hashLen = 6
agentCommitFile = ".elastic-agent.active.commit"
runDirMod = 0770
)

var (
Expand Down Expand Up @@ -150,6 +151,10 @@ func (u *Upgrader) Upgrade(ctx context.Context, version string, sourceURI string
return nil, errors.New(err, "failed to copy action store")
}

if err := copyRunDirectory(u.log, newHash); err != nil {
return nil, errors.New(err, "failed to copy run directory")
}

if err := ChangeSymlink(ctx, u.log, newHash); err != nil {
u.log.Errorw("Rolling back: changing symlink failed", "error.message", err)
rollbackInstall(ctx, u.log, newHash)
Expand Down Expand Up @@ -252,6 +257,25 @@ func copyActionStore(log *logger.Logger, newHash string) error {
return nil
}

func copyRunDirectory(log *logger.Logger, newHash string) error {
newRunPath := filepath.Join(filepath.Dir(paths.Home()), fmt.Sprintf("%s-%s", agentName, newHash), "run")
oldRunPath := filepath.Join(filepath.Dir(paths.Home()), fmt.Sprintf("%s-%s", agentName, release.ShortCommit()), "run")

log.Infow("Copying run directory", "new_run_path", newRunPath, "old_run_path", oldRunPath)

if err := os.MkdirAll(newRunPath, runDirMod); err != nil {
return errors.New(err, "failed to create run directory")
}

err := copyDir(oldRunPath, newRunPath)
if os.IsNotExist(err) {
// nothing to copy, operation ok
log.Debugw("Run directory not present", "old_run_path", oldRunPath)
return nil
}
return errors.New(err, "failed to copy %q to %q", oldRunPath, newRunPath)
}

// shutdownCallback returns a callback function to be executing during shutdown once all processes are closed.
// this goes through runtime directory of agent and copies all the state files created by processes to new versioned
// home directory with updated process name to match new version.
Expand Down

0 comments on commit d80d671

Please sign in to comment.