This repository has been archived by the owner on May 28, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 20
/
dockermachine.go
79 lines (67 loc) · 1.86 KB
/
dockermachine.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
package main
import (
"encoding/json"
"fmt"
"os"
)
type SSHCredentials struct {
IPAddress string
SSHHostPort uint `json:"SSHPort"`
SSHGuestPort uint // 22 per default
SSHUser string
SSHKeyPath string
}
func needsProvisioning(machineName string, verbose bool) bool {
checkCommands := []string{
`which rsync attr`,
}
for _, command := range checkCommands {
if _, err := RunSSHCommand(machineName, command, verbose); err != nil {
if verbose {
fmt.Println("Provisioning the docker-machine")
}
return true
}
}
return false
}
func Provision(machineName string, verbose bool) {
if needsProvisioning(machineName, verbose) {
c := []string{
// install and run rsync daemon
`tce-load -wi rsync attr acl`,
// disable boot2dockers builtin vboxfs
// TODO bad idea, because you then can't use vboxfs anymore
// `sudo umount /Users || /bin/true`,
}
for _, v := range c {
out, err := RunSSHCommand(machineName, v, verbose)
if err != nil {
fmt.Println(err)
fmt.Printf("%s\n", out)
os.Exit(1)
}
}
}
}
func RunSSHCommand(machineName, command string, verbose bool) (out []byte, err error) {
if verbose {
fmt.Println(`docker-machine ssh ` + machineName + ` '` + command + `'`)
}
return Exec("docker-machine", "ssh", machineName, command).CombinedOutput()
}
func GetSSHCredentials(machineName string) (creds SSHCredentials, err error) {
out, err := Exec("docker-machine", "inspect", "--format='{{json .Driver}}'", machineName).CombinedOutput()
if err != nil {
return SSHCredentials{}, err
}
return CredentialsFromMachineJSON(out)
}
func CredentialsFromMachineJSON(jsonData []byte) (creds SSHCredentials, err error) {
var v SSHCredentials
if err := json.Unmarshal(jsonData, &v); err != nil {
return SSHCredentials{}, err
}
v.SSHGuestPort = uint(22) // assume that this is the default SSH port
return v, nil
}