Skip to content

Commit c8d311c

Browse files
mergify[bot]swiatekmpkoutsovasilis
authored
Cache binaries downloaded for packaging locally (#9133) (#9907)
* Add an env variable to keep the download archive * Only download artifacts which aren't present * Document the KEEP_ARCHIVE env variable * Update README.md * fixup! Only download artifacts which aren't present --------- (cherry picked from commit 6aed8b1) Co-authored-by: Mikołaj Świątek <[email protected]> Co-authored-by: Panos Koutsovasilis <[email protected]>
1 parent f9be30d commit c8d311c

File tree

3 files changed

+33
-12
lines changed

3 files changed

+33
-12
lines changed

README.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,11 @@ To build a local version of the agent for development, run the command below. Th
108108
DEV=true EXTERNAL=true SNAPSHOT=true PLATFORMS=linux/amd64 PACKAGES=tar.gz mage -v package
109109
```
110110

111+
If you build the same agent package often (when running integration tests, for example),
112+
you can also set KEEP_ARCHIVE=true in your environment. The packaging step will then
113+
avoid deleting the binary archive after the package is generated, removing the need to
114+
re-download binaries on every invocation.
115+
111116
The resulting package will be produced in the build/distributions directory. The version is controlled by the value in [version.go](version/version.go).
112117
To install the agent extract the package and run the install command:
113118

dev-tools/mage/downloads/utils.go

Lines changed: 22 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -31,13 +31,7 @@ type downloadRequest struct {
3131
// It writes to the destination file as it downloads it, without
3232
// loading the entire file into memory.
3333
func downloadFile(downloadRequest *downloadRequest) error {
34-
targetFile, err := os.Create(downloadRequest.TargetPath)
35-
if err != nil {
36-
return fmt.Errorf("creating file: %w", err)
37-
}
38-
defer func() {
39-
_ = targetFile.Close()
40-
}()
34+
stat, _ := os.Stat(downloadRequest.TargetPath)
4135

4236
exp := getExponentialBackoff(3)
4337

@@ -47,6 +41,11 @@ func downloadFile(downloadRequest *downloadRequest) error {
4741
if err != nil {
4842
return fmt.Errorf("creating request: %w", err)
4943
}
44+
// if the target file already exists, add the If-Modified-Since header
45+
if stat != nil {
46+
req.Header.Add("If-Modified-Since", stat.ModTime().Format(http.TimeFormat))
47+
}
48+
5049
resp, err := http.DefaultClient.Do(req)
5150
if err != nil {
5251
retryCount++
@@ -55,23 +54,36 @@ func downloadFile(downloadRequest *downloadRequest) error {
5554
defer func() {
5655
_ = resp.Body.Close()
5756
}()
57+
58+
if resp.StatusCode == http.StatusNotModified {
59+
return nil
60+
}
61+
62+
targetFile, err := os.Create(downloadRequest.TargetPath)
63+
if err != nil {
64+
return fmt.Errorf("creating file: %w", err)
65+
}
66+
defer func() {
67+
_ = targetFile.Close()
68+
}()
69+
5870
_, err = io.Copy(targetFile, resp.Body)
5971
if err != nil {
6072
// try to drain the body before returning to ensure the connection can be reused
6173
_, _ = io.Copy(io.Discard, resp.Body)
6274
return fmt.Errorf("writing file %s: %w", targetFile.Name(), err)
6375
}
6476

77+
_ = os.Chmod(targetFile.Name(), 0666)
78+
6579
return nil
6680
}
6781

68-
err = backoff.Retry(download, exp)
82+
err := backoff.Retry(download, exp)
6983
if err != nil {
7084
return err
7185
}
7286

73-
_ = os.Chmod(targetFile.Name(), 0666)
74-
7587
return nil
7688
}
7789

magefile.go

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1183,12 +1183,16 @@ func packageAgent(ctx context.Context, platforms []string, dependenciesVersion s
11831183
log.Printf("dependencies extracted from package specs: %v", dependencies)
11841184
}
11851185

1186+
keepArchive := os.Getenv("KEEP_ARCHIVE") != ""
1187+
11861188
// download/copy all the necessary dependencies for packaging elastic-agent
11871189
archivePath, dropPath, dependencies := collectPackageDependencies(platforms, dependenciesVersion, packageTypes, dependencies)
11881190

11891191
// cleanup after build
1190-
defer os.RemoveAll(archivePath)
1191-
defer os.RemoveAll(dropPath)
1192+
if !keepArchive {
1193+
defer os.RemoveAll(archivePath)
1194+
defer os.RemoveAll(dropPath)
1195+
}
11921196
defer os.Unsetenv(agentDropPath)
11931197

11941198
// create flat dir

0 commit comments

Comments
 (0)