Skip to content

Commit

Permalink
Restore downloads directory before downloading (#2222) (#2229)
Browse files Browse the repository at this point in the history
(cherry picked from commit fdd1465)

Co-authored-by: Michal Pristas <[email protected]>
  • Loading branch information
mergify[bot] and michalpristas authored Feb 6, 2023
1 parent 973af90 commit d12e5a9
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 0 deletions.
5 changes: 5 additions & 0 deletions internal/pkg/agent/application/upgrade/cleanup.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,11 @@ func cleanNonMatchingVersionsFromDownloads(log *logger.Logger, version string) e
log.Debugw("Cleaning up non-matching downloaded versions", "version", version, "downloads.path", downloadsPath)

files, err := os.ReadDir(downloadsPath)
if os.IsNotExist(err) {
// nothing to clean up
return nil
}

if err != nil {
return fmt.Errorf("unable to read directory %q: %w", paths.Downloads(), err)
}
Expand Down
7 changes: 7 additions & 0 deletions internal/pkg/agent/application/upgrade/step_download.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,13 @@ package upgrade

import (
"context"
"fmt"
"os"
"strings"

"go.elastic.co/apm"

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

if err := os.MkdirAll(paths.Downloads(), 0750); err != nil {
return "", errors.New(err, fmt.Sprintf("failed to create download directory at %s", paths.Downloads()))
}

path, err := fetcher.Download(ctx, agentArtifact, version)
if err != nil {
return "", errors.New(err, "failed upgrade of agent binary")
Expand Down

0 comments on commit d12e5a9

Please sign in to comment.