in dev-tools/mage/downloads/versions.go [350:484]
func FetchProjectBinaryForSnapshots(ctx context.Context, useCISnapshots bool, project string, artifactName string, artifact string, version string, timeoutFactor int, xpack bool, downloadPath string, downloadSHAFile bool) (string, error) {
if BeatsLocalPath != "" {
return "", fmt.Errorf("⚠️ Beats local path usage is deprecated and not used to fetch the binaries. Please use the packaging job to generate the artifacts to be consumed by these tests")
}
handleDownload := func(URL string) (string, error) {
name := artifactName
downloadRequest := downloadRequest{
DownloadPath: downloadPath,
URL: URL,
}
span, _ := apm.StartSpanOptions(ctx, "Fetching Project binary", "project.url.fetch-binary", apm.SpanOptions{
Parent: apm.SpanFromContext(ctx).TraceContext(),
})
span.Context.SetLabel("project", project)
defer span.End()
binariesMutex.RLock()
val, ok := binariesCache[URL]
binariesMutex.RUnlock()
if ok {
logger.Debug("Retrieving binary from local cache",
slog.String("URL", URL),
slog.String("path", val),
)
return val, nil
}
err := downloadFile(&downloadRequest)
if err != nil {
return downloadRequest.UnsanitizedFilePath, err
}
if strings.HasSuffix(URL, ".sha512") {
name = fmt.Sprintf("%s.sha512", name)
}
// use artifact name as file name to avoid having URL params in the name
sanitizedFilePath := filepath.Join(path.Dir(downloadRequest.UnsanitizedFilePath), name)
err = os.Rename(downloadRequest.UnsanitizedFilePath, sanitizedFilePath)
if err != nil {
logger.Warn("Could not sanitize downloaded file name. Keeping old name",
slog.String("fileName", downloadRequest.UnsanitizedFilePath),
slog.String("sanitizedFileName", sanitizedFilePath),
)
sanitizedFilePath = downloadRequest.UnsanitizedFilePath
}
binariesMutex.Lock()
binariesCache[URL] = sanitizedFilePath
binariesMutex.Unlock()
return sanitizedFilePath, nil
}
var downloadURL, downloadShaURL string
var err error
if useCISnapshots {
span, _ := apm.StartSpanOptions(ctx, "Fetching Beats binary", "beats.gcp.fetch-binary", apm.SpanOptions{
Parent: apm.SpanFromContext(ctx).TraceContext(),
})
defer span.End()
logger.Debug(fmt.Sprintf("Using CI snapshots for %s", artifact))
maxTimeout := time.Duration(timeoutFactor) * time.Minute
variant := ""
if strings.HasSuffix(artifact, "-ubi8") {
variant = "ubi8"
}
// look up the bucket in this particular order:
// 1. the project layout (elastic-agent, fleet-server)
// 2. the new beats layout (beats)
// 3. the legacy Beats layout (commits/snapshots)
resolvers := []BucketURLResolver{
NewProjectURLResolver(FleetCIArtifactsBase, project, artifactName, variant),
NewProjectURLResolver(BeatsCIArtifactsBase, project, artifactName, variant),
NewBeatsURLResolver(artifact, artifactName, variant),
NewBeatsLegacyURLResolver(artifact, artifactName, variant),
}
downloadURL, err = getObjectURLFromResolvers(resolvers, maxTimeout)
if err != nil {
return "", err
}
downloadLocation, err := handleDownload(downloadURL)
// check if sha file should be downloaded, else return
if !downloadSHAFile {
return downloadLocation, err
}
sha512ArtifactName := fmt.Sprintf("%s.sha512", artifactName)
sha512Resolvers := []BucketURLResolver{
NewProjectURLResolver(FleetCIArtifactsBase, project, sha512ArtifactName, variant),
NewProjectURLResolver(BeatsCIArtifactsBase, project, sha512ArtifactName, variant),
NewBeatsURLResolver(artifact, sha512ArtifactName, variant),
NewBeatsLegacyURLResolver(artifact, sha512ArtifactName, variant),
}
downloadURL, err = getObjectURLFromResolvers(sha512Resolvers, maxTimeout)
if err != nil {
return "", err
}
return handleDownload(downloadURL)
}
elasticAgentNamespace := project
if strings.EqualFold(elasticAgentNamespace, "elastic-agent") {
elasticAgentNamespace = "beats"
}
// look up the binaries, first checking releases, then artifacts
downloadURLResolvers := []DownloadURLResolver{
NewReleaseURLResolver(elasticAgentNamespace, artifactName, artifact),
NewArtifactURLResolver(artifactName, artifact, version),
NewArtifactSnapshotURLResolver(artifactName, artifact, project, version),
}
downloadURL, downloadShaURL, err = getDownloadURLFromResolvers(downloadURLResolvers)
if err != nil {
return "", err
}
fmt.Printf("Downloading from %s\n", downloadURL)
downloadLocation, err := handleDownload(downloadURL)
if err != nil {
return "", err
}
if downloadSHAFile && downloadShaURL != "" {
downloadLocation, err = handleDownload(downloadShaURL)
}
return downloadLocation, err
}