-
Notifications
You must be signed in to change notification settings - Fork 0
/
generic.go
138 lines (116 loc) · 3.75 KB
/
generic.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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
package provision
import (
"bytes"
"fmt"
"text/template"
"github.com/docker/machine/libmachine/auth"
"github.com/docker/machine/libmachine/drivers"
"github.com/docker/machine/libmachine/engine"
"github.com/docker/machine/libmachine/swarm"
)
type GenericProvisioner struct {
SSHCommander
OsReleaseID string
DockerOptionsDir string
DaemonOptionsFile string
Packages []string
OsReleaseInfo *OsRelease
Driver drivers.Driver
AuthOptions auth.Options
EngineOptions engine.Options
SwarmOptions swarm.Options
}
type GenericSSHCommander struct {
Driver drivers.Driver
}
func (sshCmder GenericSSHCommander) SSHCommand(args string) (string, error) {
return drivers.RunSSHCommandFromDriver(sshCmder.Driver, args)
}
func (provisioner *GenericProvisioner) Hostname() (string, error) {
return provisioner.SSHCommand("hostname")
}
func (provisioner *GenericProvisioner) SetHostname(hostname string) error {
if _, err := provisioner.SSHCommand(fmt.Sprintf(
"sudo hostname %s && echo %q | sudo tee /etc/hostname",
hostname,
hostname,
)); err != nil {
return err
}
// ubuntu/debian use 127.0.1.1 for non "localhost" loopback hostnames: https://www.debian.org/doc/manuals/debian-reference/ch05.en.html#_the_hostname_resolution
if _, err := provisioner.SSHCommand(fmt.Sprintf(`
if ! grep -xq '.*\s%s' /etc/hosts; then
if grep -xq '127.0.1.1\s.*' /etc/hosts; then
sudo sed -i 's/^127.0.1.1\s.*/127.0.1.1 %s/g' /etc/hosts;
else
echo '127.0.1.1 %s' | sudo tee -a /etc/hosts;
fi
fi`,
hostname,
hostname,
hostname,
)); err != nil {
return err
}
return nil
}
func (provisioner *GenericProvisioner) GetDockerOptionsDir() string {
return provisioner.DockerOptionsDir
}
func (provisioner *GenericProvisioner) CompatibleWithHost() bool {
return provisioner.OsReleaseInfo.ID == provisioner.OsReleaseID
}
func (provisioner *GenericProvisioner) GetAuthOptions() auth.Options {
return provisioner.AuthOptions
}
func (provisioner *GenericProvisioner) GetSwarmOptions() swarm.Options {
return provisioner.SwarmOptions
}
func (provisioner *GenericProvisioner) SetOsReleaseInfo(info *OsRelease) {
provisioner.OsReleaseInfo = info
}
func (provisioner *GenericProvisioner) GetOsReleaseInfo() (*OsRelease, error) {
return provisioner.OsReleaseInfo, nil
}
func (provisioner *GenericProvisioner) GenerateDockerOptions(dockerPort int) (*DockerOptions, error) {
var (
engineCfg bytes.Buffer
)
driverNameLabel := fmt.Sprintf("provider=%s", provisioner.Driver.DriverName())
provisioner.EngineOptions.Labels = append(provisioner.EngineOptions.Labels, driverNameLabel)
engineConfigTmpl := `
DOCKER_OPTS='
-H tcp://0.0.0.0:{{.DockerPort}}
-H unix:///var/run/docker.sock
--storage-driver {{.EngineOptions.StorageDriver}}
--tlsverify
--tlscacert {{.AuthOptions.CaCertRemotePath}}
--tlscert {{.AuthOptions.ServerCertRemotePath}}
--tlskey {{.AuthOptions.ServerKeyRemotePath}}
{{ range .EngineOptions.Labels }}--label {{.}}
{{ end }}{{ range .EngineOptions.InsecureRegistry }}--insecure-registry {{.}}
{{ end }}{{ range .EngineOptions.RegistryMirror }}--registry-mirror {{.}}
{{ end }}{{ range .EngineOptions.ArbitraryFlags }}--{{.}}
{{ end }}
'
{{range .EngineOptions.Env}}export \"{{ printf "%q" . }}\"
{{end}}
`
t, err := template.New("engineConfig").Parse(engineConfigTmpl)
if err != nil {
return nil, err
}
engineConfigContext := EngineConfigContext{
DockerPort: dockerPort,
AuthOptions: provisioner.AuthOptions,
EngineOptions: provisioner.EngineOptions,
}
t.Execute(&engineCfg, engineConfigContext)
return &DockerOptions{
EngineOptions: engineCfg.String(),
EngineOptionsPath: provisioner.DaemonOptionsFile,
}, nil
}
func (provisioner *GenericProvisioner) GetDriver() drivers.Driver {
return provisioner.Driver
}