2015-11-03 21:15:40 +00:00
|
|
|
package getter
|
|
|
|
|
|
|
|
import (
|
2015-11-03 21:16:17 +00:00
|
|
|
"fmt"
|
|
|
|
"net/url"
|
2016-03-18 22:33:01 +00:00
|
|
|
"path/filepath"
|
2017-03-11 23:11:40 +00:00
|
|
|
"strings"
|
2016-02-18 02:48:13 +00:00
|
|
|
"sync"
|
2015-11-03 21:15:40 +00:00
|
|
|
|
2015-11-03 21:16:17 +00:00
|
|
|
gg "github.com/hashicorp/go-getter"
|
2016-03-15 02:55:30 +00:00
|
|
|
"github.com/hashicorp/nomad/nomad/structs"
|
2015-11-03 21:15:40 +00:00
|
|
|
)
|
|
|
|
|
2016-02-18 02:48:13 +00:00
|
|
|
var (
|
|
|
|
// getters is the map of getters suitable for Nomad. It is initialized once
|
|
|
|
// and the lock is used to guard access to it.
|
|
|
|
getters map[string]gg.Getter
|
|
|
|
lock sync.Mutex
|
2016-02-23 18:33:58 +00:00
|
|
|
|
|
|
|
// supported is the set of download schemes supported by Nomad
|
2017-03-01 21:02:38 +00:00
|
|
|
supported = []string{"http", "https", "s3", "hg", "git"}
|
2016-02-18 02:48:13 +00:00
|
|
|
)
|
|
|
|
|
2017-03-11 23:11:40 +00:00
|
|
|
const (
|
2018-03-11 17:55:47 +00:00
|
|
|
// gitSSHPrefix is the prefix for downloading via git using ssh
|
2017-03-11 23:11:40 +00:00
|
|
|
gitSSHPrefix = "git@github.com:"
|
|
|
|
)
|
|
|
|
|
2017-05-26 23:52:47 +00:00
|
|
|
// EnvReplacer is an interface which can interpolate environment variables and
|
|
|
|
// is usually satisfied by env.TaskEnv.
|
|
|
|
type EnvReplacer interface {
|
|
|
|
ReplaceEnv(string) string
|
|
|
|
}
|
|
|
|
|
2016-03-15 02:55:30 +00:00
|
|
|
// getClient returns a client that is suitable for Nomad downloading artifacts.
|
2017-07-06 03:44:49 +00:00
|
|
|
func getClient(src string, mode gg.ClientMode, dst string) *gg.Client {
|
2016-02-18 02:48:13 +00:00
|
|
|
lock.Lock()
|
|
|
|
defer lock.Unlock()
|
|
|
|
|
|
|
|
// Return the pre-initialized client
|
|
|
|
if getters == nil {
|
2016-02-23 18:33:58 +00:00
|
|
|
getters = make(map[string]gg.Getter, len(supported))
|
|
|
|
for _, getter := range supported {
|
|
|
|
if impl, ok := gg.Getters[getter]; ok {
|
|
|
|
getters[getter] = impl
|
|
|
|
}
|
2016-02-18 02:48:13 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return &gg.Client{
|
|
|
|
Src: src,
|
|
|
|
Dst: dst,
|
2017-07-06 03:44:49 +00:00
|
|
|
Mode: mode,
|
2016-02-18 02:48:13 +00:00
|
|
|
Getters: getters,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-03-15 02:55:30 +00:00
|
|
|
// getGetterUrl returns the go-getter URL to download the artifact.
|
2017-05-26 23:52:47 +00:00
|
|
|
func getGetterUrl(taskEnv EnvReplacer, artifact *structs.TaskArtifact) (string, error) {
|
2017-03-11 23:11:40 +00:00
|
|
|
source := taskEnv.ReplaceEnv(artifact.GetterSource)
|
|
|
|
|
|
|
|
// Handle an invalid URL when given a go-getter url such as
|
|
|
|
// git@github.com:hashicorp/nomad.git
|
|
|
|
gitSSH := false
|
|
|
|
if strings.HasPrefix(source, gitSSHPrefix) {
|
|
|
|
gitSSH = true
|
|
|
|
source = source[len(gitSSHPrefix):]
|
|
|
|
}
|
|
|
|
|
|
|
|
u, err := url.Parse(source)
|
2015-11-03 21:16:17 +00:00
|
|
|
if err != nil {
|
2016-03-15 02:55:30 +00:00
|
|
|
return "", fmt.Errorf("failed to parse source URL %q: %v", artifact.GetterSource, err)
|
2015-11-03 21:16:17 +00:00
|
|
|
}
|
2015-11-03 21:15:40 +00:00
|
|
|
|
2016-03-15 02:55:30 +00:00
|
|
|
// Build the url
|
|
|
|
q := u.Query()
|
|
|
|
for k, v := range artifact.GetterOptions {
|
2016-04-12 01:46:16 +00:00
|
|
|
q.Add(k, taskEnv.ReplaceEnv(v))
|
2015-11-03 21:16:17 +00:00
|
|
|
}
|
2016-03-15 02:55:30 +00:00
|
|
|
u.RawQuery = q.Encode()
|
2017-03-11 23:11:40 +00:00
|
|
|
|
|
|
|
// Add the prefix back
|
|
|
|
url := u.String()
|
|
|
|
if gitSSH {
|
|
|
|
url = fmt.Sprintf("%s%s", gitSSHPrefix, url)
|
|
|
|
}
|
|
|
|
|
|
|
|
return url, nil
|
2016-03-15 02:55:30 +00:00
|
|
|
}
|
2015-11-03 21:15:40 +00:00
|
|
|
|
2016-03-18 22:33:01 +00:00
|
|
|
// GetArtifact downloads an artifact into the specified task directory.
|
2017-05-26 23:52:47 +00:00
|
|
|
func GetArtifact(taskEnv EnvReplacer, artifact *structs.TaskArtifact, taskDir string) error {
|
2016-04-12 01:46:16 +00:00
|
|
|
url, err := getGetterUrl(taskEnv, artifact)
|
2016-03-15 02:55:30 +00:00
|
|
|
if err != nil {
|
2017-03-24 22:26:05 +00:00
|
|
|
return newGetError(artifact.GetterSource, err, false)
|
2015-11-03 21:16:17 +00:00
|
|
|
}
|
2015-11-03 21:15:40 +00:00
|
|
|
|
2016-03-15 02:55:30 +00:00
|
|
|
// Download the artifact
|
2016-03-18 22:33:01 +00:00
|
|
|
dest := filepath.Join(taskDir, artifact.RelativeDest)
|
2017-07-06 03:44:49 +00:00
|
|
|
|
|
|
|
// Convert from string getter mode to go-getter const
|
|
|
|
mode := gg.ClientModeAny
|
|
|
|
switch artifact.GetterMode {
|
|
|
|
case structs.GetterModeFile:
|
|
|
|
mode = gg.ClientModeFile
|
|
|
|
case structs.GetterModeDir:
|
|
|
|
mode = gg.ClientModeDir
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := getClient(url, mode, dest).Get(); err != nil {
|
2017-03-24 22:26:05 +00:00
|
|
|
return newGetError(url, err, true)
|
2015-11-03 21:16:17 +00:00
|
|
|
}
|
2016-03-15 02:55:30 +00:00
|
|
|
|
|
|
|
return nil
|
2015-11-03 21:15:40 +00:00
|
|
|
}
|
2017-03-24 22:26:05 +00:00
|
|
|
|
|
|
|
// GetError wraps the underlying artifact fetching error with the URL. It
|
|
|
|
// implements the RecoverableError interface.
|
|
|
|
type GetError struct {
|
|
|
|
URL string
|
|
|
|
Err error
|
|
|
|
recoverable bool
|
|
|
|
}
|
|
|
|
|
|
|
|
func newGetError(url string, err error, recoverable bool) *GetError {
|
|
|
|
return &GetError{
|
|
|
|
URL: url,
|
|
|
|
Err: err,
|
|
|
|
recoverable: recoverable,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (g *GetError) Error() string {
|
|
|
|
return g.Err.Error()
|
|
|
|
}
|
|
|
|
|
2017-03-27 23:27:24 +00:00
|
|
|
func (g *GetError) IsRecoverable() bool {
|
2017-03-24 22:26:05 +00:00
|
|
|
return g.recoverable
|
|
|
|
}
|