Skip to content

Commit

Permalink
Fix identifying tools removal from home directory
Browse files Browse the repository at this point in the history
  • Loading branch information
vapopov committed Oct 25, 2024
1 parent a4f88ec commit 48a1159
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 8 deletions.
17 changes: 11 additions & 6 deletions lib/autoupdate/tools/updater.go
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,9 @@ func (u *Updater) CheckLocal() (version string, reExec bool, err error) {
// If a version of client tools has already been downloaded to
// tools directory, return that.
toolsVersion, err := CheckToolVersion(u.toolsDir)
if trace.IsNotFound(err) {
return u.localVersion, false, nil
}
if err != nil {
return "", false, trace.Wrap(err)
}
Expand Down Expand Up @@ -185,6 +188,9 @@ func (u *Updater) CheckRemote(ctx context.Context, proxyAddr string) (version st
// If a version of client tools has already been downloaded to
// tools directory, return that.
toolsVersion, err := CheckToolVersion(u.toolsDir)
if trace.IsNotFound(err) {
return u.localVersion, false, nil
}
if err != nil {
return "", false, trace.Wrap(err)
}
Expand All @@ -203,7 +209,7 @@ func (u *Updater) CheckRemote(ctx context.Context, proxyAddr string) (version st

// UpdateWithLock acquires filesystem lock, downloads requested version package,
// unarchive and replace existing one.
func (u *Updater) UpdateWithLock(ctx context.Context, toolsVersion string) (err error) {
func (u *Updater) UpdateWithLock(ctx context.Context, updateToolsVersion string) (err error) {
// Create tools directory if it does not exist.
if err := os.MkdirAll(u.toolsDir, 0o755); err != nil {
return trace.Wrap(err)
Expand All @@ -220,21 +226,20 @@ func (u *Updater) UpdateWithLock(ctx context.Context, toolsVersion string) (err
// If the version of the running binary or the version downloaded to
// tools directory is the same as the requested version of client tools,
// nothing to be done, exit early.
teleportVersion, err := CheckToolVersion(u.toolsDir)
toolsVersion, err := CheckToolVersion(u.toolsDir)
if err != nil && !trace.IsNotFound(err) {
return trace.Wrap(err)

}
if toolsVersion == teleportVersion {
if updateToolsVersion == toolsVersion {
return nil
}

// Download and update client tools in tools directory.
if err := u.Update(ctx, toolsVersion); err != nil {
if err := u.Update(ctx, updateToolsVersion); err != nil {
return trace.Wrap(err)
}

return
return nil
}

// Update downloads requested version and replace it with existing one and cleanups the previous downloads
Expand Down
5 changes: 3 additions & 2 deletions lib/autoupdate/tools/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,15 +63,16 @@ func DefaultClientTools() []string {
}
}

// CheckToolVersion returns current installed client tools version
// CheckToolVersion returns current installed client tools version, must return NotFoundError if
// the client tools is not found in tools directory.
func CheckToolVersion(toolsDir string) (string, error) {
// Find the path to the current executable.
path, err := toolName(toolsDir)
if err != nil {
return "", trace.Wrap(err)
}
if _, err := os.Stat(path); errors.Is(err, os.ErrNotExist) {
return "", nil
return "", trace.NotFound("autoupdate tool not found in %q", toolsDir)
} else if err != nil {
return "", trace.Wrap(err)
}
Expand Down

0 comments on commit 48a1159

Please sign in to comment.