Skip to content

Commit d12e5a9

Browse files
Restore downloads directory before downloading (#2222) (#2229)
(cherry picked from commit fdd1465) Co-authored-by: Michal Pristas <michal.pristas@gmail.com>
1 parent 973af90 commit d12e5a9

File tree

2 files changed

+12
-0
lines changed

2 files changed

+12
-0
lines changed

internal/pkg/agent/application/upgrade/cleanup.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,11 @@ func cleanNonMatchingVersionsFromDownloads(log *logger.Logger, version string) e
2222
log.Debugw("Cleaning up non-matching downloaded versions", "version", version, "downloads.path", downloadsPath)
2323

2424
files, err := os.ReadDir(downloadsPath)
25+
if os.IsNotExist(err) {
26+
// nothing to clean up
27+
return nil
28+
}
29+
2530
if err != nil {
2631
return fmt.Errorf("unable to read directory %q: %w", paths.Downloads(), err)
2732
}

internal/pkg/agent/application/upgrade/step_download.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,13 @@ package upgrade
66

77
import (
88
"context"
9+
"fmt"
10+
"os"
911
"strings"
1012

1113
"go.elastic.co/apm"
1214

15+
"github.com/elastic/elastic-agent/internal/pkg/agent/application/paths"
1316
"github.com/elastic/elastic-agent/internal/pkg/agent/application/upgrade/artifact"
1417
"github.com/elastic/elastic-agent/internal/pkg/agent/application/upgrade/artifact/download"
1518
"github.com/elastic/elastic-agent/internal/pkg/agent/application/upgrade/artifact/download/composed"
@@ -54,6 +57,10 @@ func (u *Upgrader) downloadArtifact(ctx context.Context, version, sourceURI stri
5457
return "", errors.New(err, "initiating fetcher")
5558
}
5659

60+
if err := os.MkdirAll(paths.Downloads(), 0750); err != nil {
61+
return "", errors.New(err, fmt.Sprintf("failed to create download directory at %s", paths.Downloads()))
62+
}
63+
5764
path, err := fetcher.Download(ctx, agentArtifact, version)
5865
if err != nil {
5966
return "", errors.New(err, "failed upgrade of agent binary")

0 commit comments

Comments
 (0)