Skip to content

Commit

Permalink
cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
sclevine committed Nov 21, 2024
1 parent 8c9da6b commit 3e2d32d
Showing 1 changed file with 14 additions and 7 deletions.
21 changes: 14 additions & 7 deletions lib/autoupdate/agent/updater.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import (
"os/exec"
"path/filepath"
"runtime"
"slices"
"strings"
"time"

Expand Down Expand Up @@ -535,10 +536,10 @@ func (u *Updater) update(ctx context.Context, cfg *UpdateConfig, targetVersion s
return trace.Errorf("failed to install: %w", err)
}

// TODO(sclevine): if the target version has fewer binaries, this will
// leave old binaries linked. This may prevent the installation from
// being removed. To fix this, we should look for orphaned binaries
// and remove them, or alternatively, attempt to remove extra versions.
// If the target version has fewer binaries, this will leave old binaries linked.
// This may prevent the installation from being removed.
// Cleanup logic at the end of this function will ensure that they are removed
// eventually.

revert, err := u.Installer.Link(ctx, targetVersion)
if err != nil {
Expand Down Expand Up @@ -608,8 +609,15 @@ func (u *Updater) update(ctx context.Context, cfg *UpdateConfig, targetVersion s
u.Log.InfoContext(ctx, "Backup version set.", backupVersionKey, v)
}

// Cleanup orphans.
return trace.Wrap(u.cleanup(ctx, []string{
targetVersion,
activeVersion,
backupVersion,
}))
}

// cleanup orphan installations
func (u *Updater) cleanup(ctx context.Context, keep []string) error {
versions, err := u.Installer.List(ctx)
if err != nil {
u.Log.ErrorContext(ctx, "Failed to read installed versions.", errorKey, err)
Expand All @@ -620,8 +628,7 @@ func (u *Updater) update(ctx context.Context, cfg *UpdateConfig, targetVersion s
}
u.Log.WarnContext(ctx, "More than two versions of Teleport are installed. Removing unused versions.", "count", len(versions))
for _, v := range versions {
switch v {
case "", targetVersion, activeVersion, backupVersion:
if v == "" || slices.Contains(keep, v) {
continue
}
err := u.Installer.Remove(ctx, v)
Expand Down

0 comments on commit 3e2d32d

Please sign in to comment.