2015-01-09 09:52:24 -08:00
|
|
|
package utils
|
|
|
|
|
|
|
|
|
|
import (
|
2015-04-06 18:55:29 -07:00
|
|
|
"crypto/rand"
|
|
|
|
|
"encoding/hex"
|
2015-03-11 14:44:33 -07:00
|
|
|
"encoding/json"
|
2015-03-02 16:59:18 -08:00
|
|
|
"fmt"
|
2015-01-09 09:52:24 -08:00
|
|
|
"io"
|
2015-02-16 23:16:36 +00:00
|
|
|
"net"
|
2015-01-09 09:52:24 -08:00
|
|
|
"os"
|
2015-01-23 21:27:29 -05:00
|
|
|
"path/filepath"
|
|
|
|
|
"runtime"
|
2015-04-06 18:55:29 -07:00
|
|
|
"strconv"
|
2015-03-02 16:59:18 -08:00
|
|
|
"time"
|
2015-03-11 14:44:33 -07:00
|
|
|
|
2015-04-06 18:55:29 -07:00
|
|
|
"github.com/docker/machine/log"
|
2015-01-09 09:52:24 -08:00
|
|
|
)
|
|
|
|
|
|
2015-01-31 14:24:06 -05:00
|
|
|
func GetHomeDir() string {
|
|
|
|
|
if runtime.GOOS == "windows" {
|
|
|
|
|
return os.Getenv("USERPROFILE")
|
|
|
|
|
}
|
|
|
|
|
return os.Getenv("HOME")
|
|
|
|
|
}
|
|
|
|
|
|
2015-01-31 11:42:58 -05:00
|
|
|
func GetBaseDir() string {
|
2015-02-25 12:32:41 -05:00
|
|
|
baseDir := os.Getenv("MACHINE_STORAGE_PATH")
|
2015-01-31 11:42:58 -05:00
|
|
|
if baseDir == "" {
|
2015-03-20 00:08:58 -04:00
|
|
|
baseDir = filepath.Join(GetHomeDir(), ".docker", "machine")
|
2015-01-23 21:27:29 -05:00
|
|
|
}
|
2015-01-31 11:42:58 -05:00
|
|
|
return baseDir
|
2015-01-23 21:27:29 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func GetDockerDir() string {
|
2015-02-25 12:32:41 -05:00
|
|
|
return filepath.Join(GetHomeDir(), ".docker")
|
|
|
|
|
}
|
|
|
|
|
|
2015-01-23 21:27:29 -05:00
|
|
|
func GetMachineDir() string {
|
2015-03-20 00:08:58 -04:00
|
|
|
return filepath.Join(GetBaseDir(), "machines")
|
2015-02-25 12:32:41 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func GetMachineCertDir() string {
|
2015-03-20 00:08:58 -04:00
|
|
|
return filepath.Join(GetBaseDir(), "certs")
|
2015-01-23 22:04:54 -05:00
|
|
|
}
|
|
|
|
|
|
2015-02-25 12:32:41 -05:00
|
|
|
func GetMachineCacheDir() string {
|
2015-03-20 00:08:58 -04:00
|
|
|
return filepath.Join(GetBaseDir(), "cache")
|
2015-01-23 21:27:29 -05:00
|
|
|
}
|
|
|
|
|
|
2015-01-27 10:28:55 -05:00
|
|
|
func GetUsername() string {
|
|
|
|
|
u := "unknown"
|
|
|
|
|
osUser := ""
|
|
|
|
|
|
|
|
|
|
switch runtime.GOOS {
|
|
|
|
|
case "darwin", "linux":
|
|
|
|
|
osUser = os.Getenv("USER")
|
|
|
|
|
case "windows":
|
|
|
|
|
osUser = os.Getenv("USERNAME")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if osUser != "" {
|
|
|
|
|
u = osUser
|
2015-01-26 17:56:33 -05:00
|
|
|
}
|
|
|
|
|
|
2015-01-27 10:28:55 -05:00
|
|
|
return u
|
2015-01-26 17:56:33 -05:00
|
|
|
}
|
|
|
|
|
|
2015-01-09 09:52:24 -08:00
|
|
|
func CopyFile(src, dst string) error {
|
|
|
|
|
in, err := os.Open(src)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
defer in.Close()
|
|
|
|
|
|
|
|
|
|
out, err := os.Create(dst)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if _, err = io.Copy(out, in); err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
|
2015-04-08 12:05:28 -04:00
|
|
|
fi, err := os.Stat(src)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if err := os.Chmod(dst, fi.Mode()); err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
|
2015-01-09 09:52:24 -08:00
|
|
|
return nil
|
|
|
|
|
}
|
2015-03-02 16:59:18 -08:00
|
|
|
|
2015-05-31 23:57:23 +02:00
|
|
|
func WaitForSpecificOrError(f func() (bool, error), maxAttempts int, waitInterval time.Duration) error {
|
2015-03-02 16:59:18 -08:00
|
|
|
for i := 0; i < maxAttempts; i++ {
|
2015-05-31 23:57:23 +02:00
|
|
|
stop, err := f()
|
|
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
if stop {
|
2015-03-02 16:59:18 -08:00
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
time.Sleep(waitInterval)
|
|
|
|
|
}
|
|
|
|
|
return fmt.Errorf("Maximum number of retries (%d) exceeded", maxAttempts)
|
|
|
|
|
}
|
|
|
|
|
|
2015-05-31 23:57:23 +02:00
|
|
|
func WaitForSpecific(f func() bool, maxAttempts int, waitInterval time.Duration) error {
|
|
|
|
|
return WaitForSpecificOrError(func() (bool, error) {
|
|
|
|
|
return f(), nil
|
|
|
|
|
}, maxAttempts, waitInterval)
|
|
|
|
|
}
|
|
|
|
|
|
2015-03-02 16:59:18 -08:00
|
|
|
func WaitFor(f func() bool) error {
|
|
|
|
|
return WaitForSpecific(f, 60, 3*time.Second)
|
|
|
|
|
}
|
2015-03-11 14:44:33 -07:00
|
|
|
|
2015-02-16 23:16:36 +00:00
|
|
|
func WaitForDocker(ip string, daemonPort int) error {
|
|
|
|
|
return WaitFor(func() bool {
|
|
|
|
|
conn, err := net.Dial("tcp", fmt.Sprintf("%s:%d", ip, daemonPort))
|
|
|
|
|
if err != nil {
|
2015-05-06 17:52:29 -07:00
|
|
|
log.Debugf("Daemon not responding yet: %s", err)
|
2015-02-16 23:16:36 +00:00
|
|
|
return false
|
|
|
|
|
}
|
|
|
|
|
conn.Close()
|
|
|
|
|
return true
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
2015-03-11 14:44:33 -07:00
|
|
|
func DumpVal(vals ...interface{}) {
|
|
|
|
|
for _, val := range vals {
|
|
|
|
|
prettyJSON, err := json.MarshalIndent(val, "", " ")
|
|
|
|
|
if err != nil {
|
|
|
|
|
log.Fatal(err)
|
|
|
|
|
}
|
|
|
|
|
log.Debug(string(prettyJSON))
|
|
|
|
|
}
|
|
|
|
|
}
|
2015-04-06 18:55:29 -07:00
|
|
|
|
|
|
|
|
// Following two functions are from github.com/docker/docker/utils module. It
|
|
|
|
|
// was way overkill to include the whole module, so we just have these bits
|
|
|
|
|
// that we're using here.
|
|
|
|
|
func TruncateID(id string) string {
|
|
|
|
|
shortLen := 12
|
|
|
|
|
if len(id) < shortLen {
|
|
|
|
|
shortLen = len(id)
|
|
|
|
|
}
|
|
|
|
|
return id[:shortLen]
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// GenerateRandomID returns an unique id
|
|
|
|
|
func GenerateRandomID() string {
|
|
|
|
|
for {
|
|
|
|
|
id := make([]byte, 32)
|
|
|
|
|
if _, err := io.ReadFull(rand.Reader, id); err != nil {
|
|
|
|
|
panic(err) // This shouldn't happen
|
|
|
|
|
}
|
|
|
|
|
value := hex.EncodeToString(id)
|
|
|
|
|
// if we try to parse the truncated for as an int and we don't have
|
|
|
|
|
// an error then the value is all numberic and causes issues when
|
|
|
|
|
// used as a hostname. ref #3869
|
|
|
|
|
if _, err := strconv.ParseInt(TruncateID(value), 10, 64); err == nil {
|
|
|
|
|
continue
|
|
|
|
|
}
|
|
|
|
|
return value
|
|
|
|
|
}
|
|
|
|
|
}
|