-
Notifications
You must be signed in to change notification settings - Fork 145
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Update will cleanup unneeded artifacts.
The update process will cleanup unneeded artifacts. When an update starts all artifacts that do not have the current version number in it's name will be removed. If artifact retrieval fails, downloaded artifacts are removed. On a successful upgrade, all contents of the downloads dir will be removed.
- Loading branch information
1 parent
8ef98f1
commit d608090
Showing
4 changed files
with
128 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
// 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" | ||
"strings" | ||
|
||
"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 err | ||
} | ||
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 { | ||
return err | ||
} | ||
} | ||
} | ||
return nil | ||
} | ||
|
||
// cleanAllDownloads will remove all files from the downloads directory | ||
func cleanAllDownloads() error { | ||
files, err := os.ReadDir(paths.Downloads()) | ||
if err != nil { | ||
return err | ||
} | ||
for _, file := range files { | ||
if file.IsDir() { | ||
continue | ||
} | ||
if err := os.Remove(filepath.Join(paths.Downloads(), file.Name())); err != nil { | ||
return err | ||
} | ||
} | ||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
// 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!"), 0640) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
err = os.WriteFile(filepath.Join(dir, "test-8.4.0-file"), []byte("hello, world!"), 0640) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
err = os.WriteFile(filepath.Join(dir, "test-8.5.0-file"), []byte("hello, world!"), 0640) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
err = os.WriteFile(filepath.Join(dir, "test-hash-file"), []byte("hello, world!"), 0640) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
} | ||
|
||
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()) | ||
fi, err := files[0].Info() | ||
require.NoError(t, err) | ||
require.Greater(t, fi.Size(), int64(0)) | ||
} | ||
|
||
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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters