2013-07-19 14:59:04 -04:00
|
|
|
package common
|
|
|
|
|
|
|
|
import (
|
2013-07-19 15:09:13 -04:00
|
|
|
"fmt"
|
2017-03-01 15:37:05 -05:00
|
|
|
"net/url"
|
2013-07-29 02:51:21 -04:00
|
|
|
"os"
|
2013-07-29 03:09:48 -04:00
|
|
|
"path/filepath"
|
2018-01-09 21:08:06 -05:00
|
|
|
"regexp"
|
2018-01-19 00:09:53 -05:00
|
|
|
"runtime"
|
2013-07-19 15:09:13 -04:00
|
|
|
"strings"
|
2017-01-14 20:56:04 -05:00
|
|
|
"time"
|
2013-07-19 14:59:04 -04:00
|
|
|
)
|
|
|
|
|
2017-01-14 20:56:04 -05:00
|
|
|
// PackerKeyEnv is used to specify the key interval (delay) between keystrokes
|
|
|
|
// sent to the VM, typically in boot commands. This is to prevent host CPU
|
|
|
|
// utilization from causing key presses to be skipped or repeated incorrectly.
|
|
|
|
const PackerKeyEnv = "PACKER_KEY_INTERVAL"
|
|
|
|
|
|
|
|
// PackerKeyDefault 100ms is appropriate for shared build infrastructure while a
|
|
|
|
// shorter delay (e.g. 10ms) can be used on a workstation. See PackerKeyEnv.
|
|
|
|
const PackerKeyDefault = 100 * time.Millisecond
|
|
|
|
|
2013-10-11 06:50:08 -04:00
|
|
|
// ScrubConfig is a helper that returns a string representation of
|
|
|
|
// any struct with the given values stripped out.
|
|
|
|
func ScrubConfig(target interface{}, values ...string) string {
|
|
|
|
conf := fmt.Sprintf("Config: %+v", target)
|
|
|
|
for _, value := range values {
|
2015-05-18 18:13:01 -04:00
|
|
|
if value == "" {
|
|
|
|
continue
|
|
|
|
}
|
2013-10-11 06:50:08 -04:00
|
|
|
conf = strings.Replace(conf, value, "<Filtered>", -1)
|
|
|
|
}
|
|
|
|
return conf
|
|
|
|
}
|
|
|
|
|
2014-04-22 00:30:49 -04:00
|
|
|
// ChooseString returns the first non-empty value.
|
|
|
|
func ChooseString(vals ...string) string {
|
2014-04-26 14:12:43 -04:00
|
|
|
for _, el := range vals {
|
|
|
|
if el != "" {
|
|
|
|
return el
|
|
|
|
}
|
|
|
|
}
|
2014-04-22 00:30:49 -04:00
|
|
|
|
2014-04-26 14:12:43 -04:00
|
|
|
return ""
|
2014-04-22 00:30:49 -04:00
|
|
|
}
|
|
|
|
|
2018-02-02 19:58:42 -05:00
|
|
|
// SupportedProtocol verifies that the url passed is actually supported or not
|
2018-01-09 21:08:06 -05:00
|
|
|
// This will also validate that the protocol is one that's actually implemented.
|
2018-02-02 19:58:42 -05:00
|
|
|
func SupportedProtocol(u *url.URL) bool {
|
2018-01-09 21:08:06 -05:00
|
|
|
// url.Parse shouldn't return nil except on error....but it can.
|
|
|
|
if u == nil {
|
|
|
|
return false
|
2013-07-29 02:51:21 -04:00
|
|
|
}
|
|
|
|
|
2018-01-09 21:08:06 -05:00
|
|
|
// build a dummy NewDownloadClient since this is the only place that valid
|
|
|
|
// protocols are actually exposed.
|
2018-08-10 16:08:28 -04:00
|
|
|
cli := NewDownloadClient(&DownloadConfig{}, nil)
|
2016-04-05 17:51:17 -04:00
|
|
|
|
2018-01-09 21:08:06 -05:00
|
|
|
// Iterate through each downloader to see if a protocol was found.
|
|
|
|
ok := false
|
2018-01-19 00:33:44 -05:00
|
|
|
for scheme := range cli.config.DownloaderMap {
|
2018-01-09 21:08:06 -05:00
|
|
|
if strings.ToLower(u.Scheme) == strings.ToLower(scheme) {
|
|
|
|
ok = true
|
2017-03-01 15:37:05 -05:00
|
|
|
}
|
2018-01-09 21:08:06 -05:00
|
|
|
}
|
|
|
|
return ok
|
|
|
|
}
|
|
|
|
|
|
|
|
// DownloadableURL processes a URL that may also be a file path and returns
|
|
|
|
// a completely valid URL representing the requested file. For example,
|
|
|
|
// the original URL might be "local/file.iso" which isn't a valid URL,
|
|
|
|
// and so DownloadableURL will return "file://local/file.iso"
|
|
|
|
// No other transformations are done to the path.
|
|
|
|
func DownloadableURL(original string) (string, error) {
|
2018-01-19 00:58:24 -05:00
|
|
|
var absPrefix, result string
|
|
|
|
|
|
|
|
absPrefix = ""
|
|
|
|
if runtime.GOOS == "windows" {
|
|
|
|
absPrefix = "/"
|
|
|
|
}
|
2016-04-05 17:51:17 -04:00
|
|
|
|
2018-01-18 23:43:08 -05:00
|
|
|
// Check that the user specified a UNC path, and promote it to an smb:// uri.
|
|
|
|
if strings.HasPrefix(original, "\\\\") && len(original) > 2 && original[2] != '?' {
|
|
|
|
result = filepath.ToSlash(original[2:])
|
|
|
|
return fmt.Sprintf("smb://%s", result), nil
|
|
|
|
}
|
|
|
|
|
2018-01-09 21:08:06 -05:00
|
|
|
// Fix the url if it's using bad characters commonly mistaken with a path.
|
|
|
|
original = filepath.ToSlash(original)
|
2016-04-05 17:51:17 -04:00
|
|
|
|
2018-01-18 23:43:08 -05:00
|
|
|
// Check to see that this is a parseable URL with a scheme and a host.
|
|
|
|
// If so, then just pass it through.
|
2018-01-09 21:08:06 -05:00
|
|
|
if u, err := url.Parse(original); err == nil && u.Scheme != "" && u.Host != "" {
|
2018-01-18 23:43:08 -05:00
|
|
|
return original, nil
|
2015-11-01 22:46:14 -05:00
|
|
|
}
|
|
|
|
|
2018-01-18 23:43:08 -05:00
|
|
|
// If it's a file scheme, then convert it back to a regular path so the next
|
|
|
|
// case which forces it to an absolute path, will correct it.
|
|
|
|
if u, err := url.Parse(original); err == nil && strings.ToLower(u.Scheme) == "file" {
|
|
|
|
original = u.Path
|
|
|
|
}
|
|
|
|
|
|
|
|
// If we're on Windows and we start with a slash, then this absolute path
|
|
|
|
// is wrong. Fix it up, so the next case can figure out the absolute path.
|
|
|
|
if rpath := strings.SplitN(original, "/", 2); rpath[0] == "" && runtime.GOOS == "windows" {
|
|
|
|
result = rpath[1]
|
|
|
|
} else {
|
|
|
|
result = original
|
|
|
|
}
|
|
|
|
|
|
|
|
// Since we should be some kind of path (relative or absolute), check
|
|
|
|
// that the file exists, then make it an absolute path so we can return an
|
|
|
|
// absolute uri.
|
|
|
|
if _, err := os.Stat(result); err == nil {
|
|
|
|
result, err = filepath.Abs(filepath.FromSlash(result))
|
2017-03-01 15:37:05 -05:00
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
2016-04-05 16:01:06 -04:00
|
|
|
|
2018-01-09 21:08:06 -05:00
|
|
|
result, err = filepath.EvalSymlinks(result)
|
2017-03-01 15:37:05 -05:00
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
2016-04-05 16:01:06 -04:00
|
|
|
|
2018-01-09 21:08:06 -05:00
|
|
|
result = filepath.Clean(result)
|
2018-01-19 00:58:24 -05:00
|
|
|
return fmt.Sprintf("file://%s%s", absPrefix, filepath.ToSlash(result)), nil
|
2018-01-18 23:43:08 -05:00
|
|
|
}
|
2018-01-09 21:08:06 -05:00
|
|
|
|
2018-01-18 23:43:08 -05:00
|
|
|
// Otherwise, check if it was originally an absolute path, and fix it if so.
|
|
|
|
if strings.HasPrefix(original, "/") {
|
2018-01-19 00:58:24 -05:00
|
|
|
return fmt.Sprintf("file://%s%s", absPrefix, result), nil
|
2013-07-29 02:51:21 -04:00
|
|
|
}
|
2016-04-05 16:01:06 -04:00
|
|
|
|
2018-01-18 23:43:08 -05:00
|
|
|
// Anything left should be a non-existent relative path. So fix it up here.
|
|
|
|
result = filepath.ToSlash(filepath.Clean(result))
|
|
|
|
return fmt.Sprintf("file://./%s", result), nil
|
2018-01-09 21:08:06 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// Force the parameter into a url. This will transform the parameter into
|
|
|
|
// a proper url, removing slashes, adding the proper prefix, etc.
|
|
|
|
func ValidatedURL(original string) (string, error) {
|
|
|
|
|
|
|
|
// See if the user failed to give a url
|
|
|
|
if ok, _ := regexp.MatchString("(?m)^[^[:punct:]]+://", original); !ok {
|
|
|
|
|
|
|
|
// So since no magic was found, this must be a path.
|
|
|
|
result, err := DownloadableURL(original)
|
|
|
|
if err == nil {
|
|
|
|
return ValidatedURL(result)
|
|
|
|
}
|
|
|
|
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Verify that the url is parseable...just in case.
|
|
|
|
u, err := url.Parse(original)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
// We should now have a url, so verify that it's a protocol we support.
|
2018-02-02 19:58:42 -05:00
|
|
|
if !SupportedProtocol(u) {
|
2018-01-09 21:08:06 -05:00
|
|
|
return "", fmt.Errorf("Unsupported protocol scheme! (%#v)", u)
|
|
|
|
}
|
2016-04-05 16:01:06 -04:00
|
|
|
|
2018-01-09 21:08:06 -05:00
|
|
|
// We should now have a properly formatted and supported url
|
|
|
|
return u.String(), nil
|
2013-07-29 02:51:21 -04:00
|
|
|
}
|
2018-01-03 19:53:47 -05:00
|
|
|
|
|
|
|
// FileExistsLocally takes the URL output from DownloadableURL, and determines
|
|
|
|
// whether it is present on the file system.
|
|
|
|
// example usage:
|
|
|
|
//
|
|
|
|
// myFile, err = common.DownloadableURL(c.SourcePath)
|
|
|
|
// ...
|
2018-01-10 19:11:17 -05:00
|
|
|
// fileExists := common.StatURL(myFile)
|
2018-01-03 19:53:47 -05:00
|
|
|
// possible output:
|
2018-01-10 19:11:17 -05:00
|
|
|
// true -- should occur if the file is present, or if the file is not present,
|
|
|
|
// but is not supposed to be (e.g. the schema is http://, not file://)
|
|
|
|
// false -- should occur if there was an error stating the file, so the
|
2018-01-03 19:53:47 -05:00
|
|
|
// file is not present when it should be.
|
|
|
|
|
2018-01-10 19:11:17 -05:00
|
|
|
func FileExistsLocally(original string) bool {
|
2018-01-04 13:51:59 -05:00
|
|
|
// original should be something like file://C:/my/path.iso
|
2018-01-19 00:18:55 -05:00
|
|
|
u, _ := url.Parse(original)
|
2018-01-09 17:27:09 -05:00
|
|
|
|
2018-01-19 00:18:55 -05:00
|
|
|
// First create a dummy downloader so we can figure out which
|
|
|
|
// protocol to use.
|
2018-08-10 16:08:28 -04:00
|
|
|
cli := NewDownloadClient(&DownloadConfig{}, nil)
|
2018-01-19 00:18:55 -05:00
|
|
|
d, ok := cli.config.DownloaderMap[u.Scheme]
|
|
|
|
if !ok {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check to see that it's got a Local way of doing things.
|
|
|
|
local, ok := d.(LocalDownloader)
|
|
|
|
if !ok {
|
2018-02-02 21:16:54 -05:00
|
|
|
return true // XXX: Remote URLs short-circuit this logic.
|
2018-01-19 00:18:55 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// Figure out where we're at.
|
|
|
|
wd, err := os.Getwd()
|
|
|
|
if err != nil {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
// Now figure out the real path to the file.
|
|
|
|
realpath, err := local.toPath(wd, *u)
|
|
|
|
if err != nil {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
// Finally we can seek the truth via os.Stat.
|
|
|
|
_, err = os.Stat(realpath)
|
|
|
|
if err != nil {
|
|
|
|
return false
|
2018-01-03 19:53:47 -05:00
|
|
|
}
|
2018-01-19 00:18:55 -05:00
|
|
|
return true
|
2018-01-03 19:53:47 -05:00
|
|
|
}
|