-
Notifications
You must be signed in to change notification settings - Fork 205
Update will cleanup unneeded artifacts. #752
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
Changes from 6 commits
02a4aa8
0f4b672
98e767e
dae475f
904752c
8017a3d
2098909
810a919
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,54 @@ | ||
| // Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
| // or more contributor license agreements. Licensed under the Elastic License; | ||
| // you may not use this file except in compliance with the Elastic License. | ||
|
|
||
| package upgrade | ||
|
|
||
| import ( | ||
| "fmt" | ||
| "os" | ||
| "path/filepath" | ||
| "strings" | ||
|
|
||
| "github.com/hashicorp/go-multierror" | ||
|
|
||
| "github.com/elastic/elastic-agent/internal/pkg/agent/application/paths" | ||
| ) | ||
|
|
||
| // preUpgradeCleanup will remove files that do not have the passed version number from the downloads directory. | ||
| func preUpgradeCleanup(version string) error { | ||
| files, err := os.ReadDir(paths.Downloads()) | ||
| if err != nil { | ||
| return fmt.Errorf("unable to read directory %q: %w", paths.Downloads(), err) | ||
| } | ||
| var rErr error | ||
| for _, file := range files { | ||
| if file.IsDir() { | ||
| continue | ||
| } | ||
| if !strings.Contains(file.Name(), version) { | ||
| if err := os.Remove(filepath.Join(paths.Downloads(), file.Name())); err != nil { | ||
| rErr = multierror.Append(rErr, fmt.Errorf("unable to remove file %q: %w", filepath.Join(paths.Downloads(), file.Name()), err)) | ||
| } | ||
| } | ||
| } | ||
| return rErr | ||
| } | ||
|
|
||
| // cleanAllDownloads will remove all files from the downloads directory | ||
| func cleanAllDownloads() error { | ||
| files, err := os.ReadDir(paths.Downloads()) | ||
| if err != nil { | ||
| return fmt.Errorf("unable to read directory %q: %w", paths.Downloads(), err) | ||
| } | ||
| var rErr error | ||
| for _, file := range files { | ||
| if file.IsDir() { | ||
| continue | ||
| } | ||
| if err := os.Remove(filepath.Join(paths.Downloads(), file.Name())); err != nil { | ||
| rErr = multierror.Append(rErr, fmt.Errorf("unable to remove file %q: %w", filepath.Join(paths.Downloads(), file.Name()), err)) | ||
| } | ||
| } | ||
| return rErr | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,53 @@ | ||
| // Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
| // or more contributor license agreements. Licensed under the Elastic License; | ||
| // you may not use this file except in compliance with the Elastic License. | ||
|
|
||
| package upgrade | ||
|
|
||
| import ( | ||
| "os" | ||
| "path/filepath" | ||
| "testing" | ||
|
|
||
| "github.com/elastic/elastic-agent/internal/pkg/agent/application/paths" | ||
|
|
||
| "github.com/stretchr/testify/require" | ||
| ) | ||
|
|
||
| func setupDir(t *testing.T) { | ||
| t.Helper() | ||
| dir := t.TempDir() | ||
| paths.SetDownloads(dir) | ||
|
|
||
| err := os.WriteFile(filepath.Join(dir, "test-8.3.0-file"), []byte("hello, world!"), 0600) | ||
| require.NoError(t, err) | ||
| err = os.WriteFile(filepath.Join(dir, "test-8.4.0-file"), []byte("hello, world!"), 0600) | ||
| require.NoError(t, err) | ||
| err = os.WriteFile(filepath.Join(dir, "test-8.5.0-file"), []byte("hello, world!"), 0600) | ||
| require.NoError(t, err) | ||
| err = os.WriteFile(filepath.Join(dir, "test-hash-file"), []byte("hello, world!"), 0600) | ||
| require.NoError(t, err) | ||
| } | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. A bit late to the discussion here, but there is an |
||
|
|
||
| func TestPreUpgradeCleanup(t *testing.T) { | ||
| setupDir(t) | ||
| err := preUpgradeCleanup("8.4.0") | ||
| require.NoError(t, err) | ||
|
|
||
| files, err := os.ReadDir(paths.Downloads()) | ||
| require.NoError(t, err) | ||
| require.Len(t, files, 1) | ||
| require.Equal(t, "test-8.4.0-file", files[0].Name()) | ||
| p, err := os.ReadFile(filepath.Join(paths.Downloads(), files[0].Name())) | ||
| require.NoError(t, err) | ||
| require.Equal(t, []byte("hello, world!"), p) | ||
| } | ||
|
|
||
| func TestCleanAllDownloads(t *testing.T) { | ||
| setupDir(t) | ||
| err := cleanAllDownloads() | ||
| require.NoError(t, err) | ||
| files, err := os.ReadDir(paths.Downloads()) | ||
| require.NoError(t, err) | ||
| require.Len(t, files, 0) | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -126,6 +126,11 @@ func (u *Upgrader) Upgrade(ctx context.Context, a Action, reexecNow bool) (_ ree | |
| "running under control of the systems supervisor") | ||
| } | ||
|
|
||
| err = preUpgradeCleanup(a.Version()) | ||
| if err != nil { | ||
| u.log.Errorf("Unable to clean downloads dir %q before update: %v", paths.Downloads(), err) | ||
| } | ||
|
|
||
| if u.caps != nil { | ||
| if _, err := u.caps.Apply(a); errors.Is(err, capabilities.ErrBlocked) { | ||
| return nil, nil | ||
|
|
@@ -137,6 +142,11 @@ func (u *Upgrader) Upgrade(ctx context.Context, a Action, reexecNow bool) (_ ree | |
| sourceURI := u.sourceURI(a.SourceURI()) | ||
| archivePath, err := u.downloadArtifact(ctx, a.Version(), sourceURI) | ||
| if err != nil { | ||
| // Run the same preUpgradeCleanup task to get rid of any newly downloaded files | ||
| // This may have an issue if users are upgrading to the same version number. | ||
| if dErr := preUpgradeCleanup(a.Version()); dErr != nil { | ||
| u.log.Errorf("Unable to remove file after verification failure: %v", dErr) | ||
| } | ||
| return nil, err | ||
AndersonQ marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| } | ||
|
|
||
|
|
@@ -184,6 +194,13 @@ func (u *Upgrader) Upgrade(ctx context.Context, a Action, reexecNow bool) (_ ree | |
| return nil, nil | ||
| } | ||
|
|
||
| // Clean everything from the downloads dir | ||
| // If we wanted to be a bit simpler we could call os.RemoveAll to remove the dir + children | ||
|
||
| err = cleanAllDownloads() | ||
| if err != nil { | ||
| u.log.Errorf("Unable to clean downloads dir %q after update: %v", paths.Downloads(), err) | ||
| } | ||
|
|
||
| return cb, nil | ||
| } | ||
|
|
||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.