diff --git a/orbit/changes/25373-tmp-dir-fix b/orbit/changes/25373-tmp-dir-fix new file mode 100644 index 000000000000..d5ca96c97fb1 --- /dev/null +++ b/orbit/changes/25373-tmp-dir-fix @@ -0,0 +1,2 @@ +- Fixed a bug where fleetd was not properly cleaning up temporary directories during software + installation. \ No newline at end of file diff --git a/orbit/pkg/installer/installer.go b/orbit/pkg/installer/installer.go index 91a2e84a6b3f..d3aadff7116b 100644 --- a/orbit/pkg/installer/installer.go +++ b/orbit/pkg/installer/installer.go @@ -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") @@ -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" diff --git a/orbit/pkg/installer/installer_test.go b/orbit/pkg/installer/installer_test.go index 07b329b5efee..bb40296eb647 100644 --- a/orbit/pkg/installer/installer_test.go +++ b/orbit/pkg/installer/installer_test.go @@ -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 @@ -669,7 +683,6 @@ func TestInstallerRunWithInstallerFromURL(t *testing.T) { assert.True(t, getInstallerDetailsFnCalled) assert.Equal(t, installDetails.ExecutionID, installIdRequested) }) - } func TestScriptsDisabled(t *testing.T) {