forked from docker/machine
-
Notifications
You must be signed in to change notification settings - Fork 0
/
coreos.go
132 lines (104 loc) · 3.71 KB
/
coreos.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
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/log"
"github.com/docker/machine/libmachine/provision/pkgaction"
"github.com/docker/machine/libmachine/swarm"
"github.com/docker/machine/libmachine/versioncmp"
)
const (
hostTmpl = `sudo tee /var/tmp/hostname.yml << EOF
#cloud-config
hostname: %s
EOF
`
)
func init() {
Register("CoreOS", &RegisteredProvisioner{
New: NewCoreOSProvisioner,
})
}
func NewCoreOSProvisioner(d drivers.Driver) Provisioner {
return &CoreOSProvisioner{
NewSystemdProvisioner("coreos", d),
}
}
type CoreOSProvisioner struct {
SystemdProvisioner
}
func (provisioner *CoreOSProvisioner) String() string {
return "coreOS"
}
func (provisioner *CoreOSProvisioner) SetHostname(hostname string) error {
log.Debugf("SetHostname: %s", hostname)
if _, err := provisioner.SSHCommand(fmt.Sprintf(hostTmpl, hostname)); err != nil {
return err
}
if _, err := provisioner.SSHCommand("sudo systemctl start system-cloudinit@var-tmp-hostname.yml.service"); err != nil {
return err
}
return nil
}
func (provisioner *CoreOSProvisioner) 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)
dockerVersion, err := DockerClientVersion(provisioner)
if err != nil {
return nil, err
}
arg := "daemon"
if versioncmp.GreaterThanOrEqualTo(dockerVersion, "1.12.0") {
arg = ""
}
engineConfigTmpl := `[Service]
Environment=TMPDIR=/var/tmp
ExecStart=
ExecStart=/usr/lib/coreos/dockerd ` + arg + ` --host=unix:///var/run/docker.sock --host=tcp://0.0.0.0:{{.DockerPort}} --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 }} \$DOCKER_OPTS \$DOCKER_OPT_BIP \$DOCKER_OPT_MTU \$DOCKER_OPT_IPMASQ
Environment={{range .EngineOptions.Env}}{{ 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 *CoreOSProvisioner) Package(name string, action pkgaction.PackageAction) error {
return nil
}
func (provisioner *CoreOSProvisioner) Provision(swarmOptions swarm.Options, authOptions auth.Options, engineOptions engine.Options) error {
provisioner.SwarmOptions = swarmOptions
provisioner.AuthOptions = authOptions
provisioner.EngineOptions = engineOptions
if err := provisioner.SetHostname(provisioner.Driver.GetMachineName()); err != nil {
return err
}
if err := makeDockerOptionsDir(provisioner); err != nil {
return err
}
log.Debugf("Preparing certificates")
provisioner.AuthOptions = setRemoteAuthOptions(provisioner)
log.Debugf("Setting up certificates")
if err := ConfigureAuth(provisioner); err != nil {
return err
}
log.Debug("Configuring swarm")
err := configureSwarm(provisioner, swarmOptions, provisioner.AuthOptions)
return err
}