-
Notifications
You must be signed in to change notification settings - Fork 4.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Ingest Manager] Download snapshot artifacts from snapshots repo (#18685
- Loading branch information
1 parent
681da1c
commit a3b8a4a
Showing
5 changed files
with
155 additions
and
6 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
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
93 changes: 93 additions & 0 deletions
93
x-pack/elastic-agent/pkg/artifact/download/snapshot/downloader.go
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,93 @@ | ||
// 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 snapshot | ||
|
||
import ( | ||
"encoding/json" | ||
"fmt" | ||
gohttp "net/http" | ||
"strings" | ||
|
||
"github.com/elastic/beats/v7/x-pack/elastic-agent/pkg/artifact" | ||
"github.com/elastic/beats/v7/x-pack/elastic-agent/pkg/artifact/download" | ||
"github.com/elastic/beats/v7/x-pack/elastic-agent/pkg/artifact/download/http" | ||
"github.com/elastic/beats/v7/x-pack/elastic-agent/pkg/release" | ||
) | ||
|
||
// NewDownloader creates a downloader which first checks local directory | ||
// and then fallbacks to remote if configured. | ||
func NewDownloader(config *artifact.Config) (download.Downloader, error) { | ||
cfg, err := snapshotConfig(config) | ||
if err != nil { | ||
return nil, err | ||
} | ||
return http.NewDownloader(cfg), nil | ||
} | ||
|
||
func snapshotConfig(config *artifact.Config) (*artifact.Config, error) { | ||
snapshotURI, err := snapshotURI() | ||
if err != nil { | ||
return nil, fmt.Errorf("failed to detect remote snapshot repo, proceeding with configured: %v", err) | ||
} | ||
|
||
return &artifact.Config{ | ||
OperatingSystem: config.OperatingSystem, | ||
Architecture: config.Architecture, | ||
BeatsSourceURI: snapshotURI, | ||
TargetDirectory: config.TargetDirectory, | ||
Timeout: config.Timeout, | ||
PgpFile: config.PgpFile, | ||
InstallPath: config.InstallPath, | ||
DropPath: config.DropPath, | ||
}, nil | ||
} | ||
|
||
func snapshotURI() (string, error) { | ||
artifactsURI := fmt.Sprintf("https://artifacts-api.elastic.co/v1/search/%s-SNAPSHOT/elastic-agent", release.Version()) | ||
resp, err := gohttp.Get(artifactsURI) | ||
if err != nil { | ||
return "", err | ||
} | ||
defer resp.Body.Close() | ||
|
||
body := struct { | ||
Packages map[string]interface{} `json:"packages"` | ||
}{} | ||
|
||
dec := json.NewDecoder(resp.Body) | ||
if err := dec.Decode(&body); err != nil { | ||
return "", err | ||
} | ||
|
||
if len(body.Packages) == 0 { | ||
return "", fmt.Errorf("no packages found in snapshot repo") | ||
} | ||
|
||
for k, pkg := range body.Packages { | ||
pkgMap, ok := pkg.(map[string]interface{}) | ||
if !ok { | ||
return "", fmt.Errorf("content of '%s' is not a map", k) | ||
} | ||
|
||
uriVal, found := pkgMap["url"] | ||
if !found { | ||
return "", fmt.Errorf("item '%s' does not contain url", k) | ||
} | ||
|
||
uri, ok := uriVal.(string) | ||
if !ok { | ||
return "", fmt.Errorf("uri is not a string") | ||
} | ||
|
||
index := strings.Index(uri, "/elastic-agent/") | ||
if index == -1 { | ||
return "", fmt.Errorf("not an agent uri: '%s'", uri) | ||
} | ||
|
||
return uri[:index], nil | ||
} | ||
|
||
return "", fmt.Errorf("uri not detected") | ||
} |
21 changes: 21 additions & 0 deletions
21
x-pack/elastic-agent/pkg/artifact/download/snapshot/verifier.go
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,21 @@ | ||
// 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 snapshot | ||
|
||
import ( | ||
"github.com/elastic/beats/v7/x-pack/elastic-agent/pkg/artifact" | ||
"github.com/elastic/beats/v7/x-pack/elastic-agent/pkg/artifact/download" | ||
"github.com/elastic/beats/v7/x-pack/elastic-agent/pkg/artifact/download/http" | ||
) | ||
|
||
// NewVerifier creates a downloader which first checks local directory | ||
// and then fallbacks to remote if configured. | ||
func NewVerifier(config *artifact.Config, downloaders ...download.Downloader) (download.Verifier, error) { | ||
cfg, err := snapshotConfig(config) | ||
if err != nil { | ||
return nil, err | ||
} | ||
return http.NewVerifier(cfg) | ||
} |