2015-11-03 21:15:40 +00:00
|
|
|
package getter
|
|
|
|
|
|
|
|
import (
|
2015-11-03 21:16:17 +00:00
|
|
|
"fmt"
|
|
|
|
"log"
|
|
|
|
"net/url"
|
|
|
|
"path"
|
|
|
|
"path/filepath"
|
|
|
|
"runtime"
|
|
|
|
"strings"
|
2016-02-18 02:48:13 +00:00
|
|
|
"sync"
|
2015-11-03 21:16:17 +00:00
|
|
|
"syscall"
|
2015-11-03 21:15:40 +00:00
|
|
|
|
2015-11-03 21:16:17 +00:00
|
|
|
gg "github.com/hashicorp/go-getter"
|
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
|
|
|
|
supported = []string{"http", "https", "s3"}
|
2016-02-18 02:48:13 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// getClient returns a client that is suitable for Nomad.
|
|
|
|
func getClient(src, dst string) *gg.Client {
|
|
|
|
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,
|
|
|
|
Dir: false, // Only support a single file for now.
|
|
|
|
Getters: getters,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-11-03 21:15:40 +00:00
|
|
|
func GetArtifact(destDir, source, checksum string, logger *log.Logger) (string, error) {
|
2015-11-03 21:16:17 +00:00
|
|
|
if source == "" {
|
|
|
|
return "", fmt.Errorf("Source url is empty in Artifact Getter")
|
|
|
|
}
|
|
|
|
u, err := url.Parse(source)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
2015-11-03 21:15:40 +00:00
|
|
|
|
2015-11-03 21:16:17 +00:00
|
|
|
// if checksum is seperate, apply to source
|
|
|
|
if checksum != "" {
|
|
|
|
source = strings.Join([]string{source, fmt.Sprintf("checksum=%s", checksum)}, "?")
|
|
|
|
logger.Printf("[DEBUG] client.getter: Applying checksum to Artifact Source URL, new url: %s", source)
|
|
|
|
}
|
2015-11-03 21:15:40 +00:00
|
|
|
|
2015-11-03 21:16:17 +00:00
|
|
|
artifactFile := filepath.Join(destDir, path.Base(u.Path))
|
2016-02-18 02:48:13 +00:00
|
|
|
if err := getClient(source, artifactFile).Get(); err != nil {
|
2015-11-03 21:16:17 +00:00
|
|
|
return "", fmt.Errorf("Error downloading artifact: %s", err)
|
|
|
|
}
|
2015-11-03 21:15:40 +00:00
|
|
|
|
2015-11-03 21:16:17 +00:00
|
|
|
// Add execution permissions to the newly downloaded artifact
|
|
|
|
if runtime.GOOS != "windows" {
|
|
|
|
if err := syscall.Chmod(artifactFile, 0755); err != nil {
|
|
|
|
logger.Printf("[ERR] driver.raw_exec: Error making artifact executable: %s", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return artifactFile, nil
|
2015-11-03 21:15:40 +00:00
|
|
|
}
|