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

Draft
wants to merge 3 commits into
base: main
Choose a base branch
from
Draft
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.
12 changes: 6 additions & 6 deletions orbit/pkg/installer/installer.go
Original file line number Diff line number Diff line change
Expand Up @@ -220,12 +220,6 @@ func (r *Runner) installSoftware(ctx context.Context, installID string) (*fleet.
return payload, fmt.Errorf("creating temporary directory: %w", err)
}

log.Debug().Str("install_id", installID).Msgf("about to download software installer")
installerPath, err := r.OrbitClient.DownloadSoftwareInstaller(installer.InstallerID, tmpDir)
if err != nil {
return payload, err
}

// remove tmp directory and installer
defer func() {
removeAllFn := r.removeAllFn
Expand All @@ -238,6 +232,12 @@ func (r *Runner) installSoftware(ctx context.Context, installID string) (*fleet.
}
}()

log.Debug().Str("install_id", installID).Msgf("about to download software installer")
installerPath, err := r.OrbitClient.DownloadSoftwareInstaller(installer.InstallerID, tmpDir)
if err != nil {
return payload, err
}

scriptExtension := ".sh"
if runtime.GOOS == "windows" {
scriptExtension = ".ps1"
Expand Down
41 changes: 40 additions & 1 deletion orbit/pkg/installer/installer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,24 @@ func TestPreconditionCheck(t *testing.T) {
require.Equal(t, "", output)
}

type mockOrbitClient struct {
getInstallerDetails func(installID string) (*fleet.SoftwareInstallDetails, error)
downloadSoftwareInstaller func(installerID uint, downloadDir string) (string, error)
saveInstallerResult func(payload *fleet.HostSoftwareInstallResultPayload) error
}

func (f *mockOrbitClient) GetInstallerDetails(installID string) (*fleet.SoftwareInstallDetails, error) {
return f.getInstallerDetails(installID)
}

func (f *mockOrbitClient) DownloadSoftwareInstaller(installerID uint, downloadDir string) (string, error) {
return f.downloadSoftwareInstaller(installerID, downloadDir)
}

func (f *mockOrbitClient) SaveInstallerResult(payload *fleet.HostSoftwareInstallResultPayload) error {
return f.saveInstallerResult(payload)
}

func TestInstallerRun(t *testing.T) {
oc := &TestOrbitClient{}

Expand Down Expand Up @@ -413,6 +431,28 @@ func TestInstallerRun(t *testing.T) {
numPostInstallMatches := strings.Count(*savedInstallerResult.PostInstallScriptOutput, string(execOutput))
assert.Equal(t, 2, numPostInstallMatches)
})

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

oc := &mockOrbitClient{
getInstallerDetails: func(installID string) (*fleet.SoftwareInstallDetails, error) {
return &fleet.SoftwareInstallDetails{}, nil
},
downloadSoftwareInstaller: func(installerID uint, downloadDir string) (string, error) {
return "", errors.New("failed to download installer")
},
saveInstallerResult: func(payload *fleet.HostSoftwareInstallResultPayload) error { return nil },
}
r.OrbitClient = oc

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

require.True(t, removeAllFnCalled)
require.True(t, tmpDirFnCalled)
require.Equal(t, tmpDir, removedDir)
})
}

func TestScriptsDisabled(t *testing.T) {
Expand All @@ -425,7 +465,6 @@ func TestScriptsDisabled(t *testing.T) {
}

qc.queryFn = func(ctx context.Context, s string) (*QueryResponse, error) {

queryFnResMap := make(map[string]string, 0)
queryFnResMap["col"] = "true"
queryFnResArr := []map[string]string{queryFnResMap}
Expand Down
Loading