Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: remove temp dir if installer fails to download #25392

Merged
merged 4 commits into from
Jan 29, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions orbit/changes/25373-tmp-dir-fix
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
- Fixed a bug where fleetd was not properly cleaning up temporary directories during software
installation.
24 changes: 12 additions & 12 deletions orbit/pkg/installer/installer.go
Original file line number Diff line number Diff line change
Expand Up @@ -235,6 +235,18 @@ func (r *Runner) installSoftware(ctx context.Context, installID string) (*fleet.
return payload, fmt.Errorf("creating temporary directory: %w", err)
}

// remove tmp directory and installer
defer func() {
removeAllFn := r.removeAllFn
if removeAllFn == nil {
removeAllFn = os.RemoveAll
}
err := removeAllFn(tmpDir)
if err != nil {
log.Err(err)
}
}()

var installerPath string
if installer.SoftwareInstallerURL != nil && installer.SoftwareInstallerURL.URL != "" {
log.Debug().Str("install_id", installID).Msgf("about to download software installer from URL")
Expand All @@ -255,18 +267,6 @@ func (r *Runner) installSoftware(ctx context.Context, installID string) (*fleet.
}
}

// remove tmp directory and installer
defer func() {
removeAllFn := r.removeAllFn
if removeAllFn == nil {
removeAllFn = os.RemoveAll
}
err := removeAllFn(tmpDir)
if err != nil {
log.Err(err)
}
}()

scriptExtension := ".sh"
if runtime.GOOS == "windows" {
scriptExtension = ".ps1"
Expand Down
15 changes: 14 additions & 1 deletion orbit/pkg/installer/installer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -421,6 +421,20 @@ func TestInstallerRun(t *testing.T) {
assert.Equal(t, 2, numPostInstallMatches)
})

t.Run("failed installer download", func(t *testing.T) {
resetAll()

oc.downloadInstallerFn = func(installerID uint, downloadDir string) (string, error) {
return "", errors.New("failed to download installer")
}

err := r.run(context.Background(), &config)
require.Error(t, err)

require.True(t, removeAllFnCalled)
require.True(t, tmpDirFnCalled)
require.Equal(t, tmpDir, removedDir)
})
t.Run("failed results upload", func(t *testing.T) {
var retries int
// set a shorter interval to speed up tests
Expand Down Expand Up @@ -669,7 +683,6 @@ func TestInstallerRunWithInstallerFromURL(t *testing.T) {
assert.True(t, getInstallerDetailsFnCalled)
assert.Equal(t, installDetails.ExecutionID, installIdRequested)
})

}

func TestScriptsDisabled(t *testing.T) {
Expand Down
Loading