-
Notifications
You must be signed in to change notification settings - Fork 30
/
boot2docker.go
320 lines (259 loc) · 8.63 KB
/
boot2docker.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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
package provision
import (
"bytes"
"encoding/json"
"fmt"
"net"
"path"
"text/template"
"time"
"github.com/docker/machine/commands/mcndirs"
"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/mcnutils"
"github.com/docker/machine/libmachine/provision/pkgaction"
"github.com/docker/machine/libmachine/provision/serviceaction"
"github.com/docker/machine/libmachine/state"
"github.com/docker/machine/libmachine/swarm"
"github.com/docker/machine/libmachine/kubernetes"
)
func init() {
Register("boot2docker", &RegisteredProvisioner{
New: NewBoot2DockerProvisioner,
})
}
func NewBoot2DockerProvisioner(d drivers.Driver) Provisioner {
return &Boot2DockerProvisioner{
Driver: d,
}
}
type Boot2DockerProvisioner struct {
OsReleaseInfo *OsRelease
Driver drivers.Driver
AuthOptions auth.AuthOptions
EngineOptions engine.EngineOptions
SwarmOptions swarm.SwarmOptions
KubernetesOptions kubernetes.KubernetesOptions
}
func (provisioner *Boot2DockerProvisioner) Service(name string, action serviceaction.ServiceAction) error {
var (
err error
)
if _, err = provisioner.SSHCommand(fmt.Sprintf("sudo /etc/init.d/%s %s", name, action.String())); err != nil {
return err
}
return nil
}
func (provisioner *Boot2DockerProvisioner) upgradeIso() error {
log.Info("Stopping machine to do the upgrade...")
if err := provisioner.Driver.Stop(); err != nil {
return err
}
if err := mcnutils.WaitFor(drivers.MachineInState(provisioner.Driver, state.Stopped)); err != nil {
return err
}
machineName := provisioner.GetDriver().GetMachineName()
log.Infof("Upgrading machine %s...", machineName)
// TODO: Ideally, we should not read from mcndirs directory at all.
// The driver should be able to communicate how and where to place the
// relevant files.
b2dutils := mcnutils.NewB2dUtils(mcndirs.GetBaseDir())
//Check if the driver has specifed a custom b2d url
jsonDriver, err := json.Marshal(provisioner.GetDriver())
if err != nil {
return err
}
var d struct {
Boot2DockerURL string
}
json.Unmarshal(jsonDriver, &d)
// Usually we call this implicitly, but call it here explicitly to get
// the latest default boot2docker ISO.
if d.Boot2DockerURL == "" {
if err := b2dutils.DownloadLatestBoot2Docker(d.Boot2DockerURL); err != nil {
return err
}
}
// Either download the latest version of the b2d url that was explicitly
// specified when creating the VM or copy the (updated) default ISO
if err := b2dutils.CopyIsoToMachineDir(d.Boot2DockerURL, machineName); err != nil {
return err
}
log.Infof("Starting machine back up...")
if err := provisioner.Driver.Start(); err != nil {
return err
}
return mcnutils.WaitFor(drivers.MachineInState(provisioner.Driver, state.Running))
}
func (provisioner *Boot2DockerProvisioner) Package(name string, action pkgaction.PackageAction) error {
if name == "docker" && action == pkgaction.Upgrade {
if err := provisioner.upgradeIso(); err != nil {
return err
}
}
return nil
}
func (provisioner *Boot2DockerProvisioner) Hostname() (string, error) {
return provisioner.SSHCommand("hostname")
}
func (provisioner *Boot2DockerProvisioner) SetHostname(hostname string) error {
if _, err := provisioner.SSHCommand(fmt.Sprintf(
"sudo /usr/bin/sethostname %s && echo %q | sudo tee /var/lib/boot2docker/etc/hostname",
hostname,
hostname,
)); err != nil {
return err
}
return nil
}
func (provisioner *Boot2DockerProvisioner) GetDockerOptionsDir() string {
return "/var/lib/boot2docker"
}
func (provisioner *Boot2DockerProvisioner) GetAuthOptions() auth.AuthOptions {
return provisioner.AuthOptions
}
func (provisioner *Boot2DockerProvisioner) GetKubernetesOptions() kubernetes.KubernetesOptions {
return provisioner.KubernetesOptions
}
// CAB: Investigate how much additional work would be needed to integrate options here
func (provisioner *Boot2DockerProvisioner) Generatek8sOptions() (*k8sOptions, error) {
var (
k8sCfg bytes.Buffer
)
k8sConfigTmpl := `
{
"apiVersion": "v1beta3"
}
`
t, err := template.New("k8sConfig").Parse(k8sConfigTmpl)
if err != nil {
return nil, err
}
k8sContext := EngineConfigContext{
DockerPort: 1234,
AuthOptions: provisioner.AuthOptions,
EngineOptions: provisioner.EngineOptions,
}
t.Execute(&k8sCfg, k8sContext)
return &k8sOptions{
k8sOptions: k8sCfg.String(),
k8sOptionsPath: "/etc/foobar",
}, nil
}
func (provisioner *Boot2DockerProvisioner) 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 := `
EXTRA_ARGS='
{{ range .EngineOptions.Labels }}--label {{.}}
{{ end }}{{ range .EngineOptions.InsecureRegistry }}--insecure-registry {{.}}
{{ end }}{{ range .EngineOptions.RegistryMirror }}--registry-mirror {{.}}
{{ end }}{{ range .EngineOptions.ArbitraryFlags }}--{{.}}
{{ end }}
'
CACERT={{.AuthOptions.CaCertRemotePath}}
DOCKER_HOST='-H tcp://0.0.0.0:{{.DockerPort}}'
DOCKER_STORAGE={{.EngineOptions.StorageDriver}}
DOCKER_TLS=auto
SERVERKEY={{.AuthOptions.ServerKeyRemotePath}}
SERVERCERT={{.AuthOptions.ServerCertRemotePath}}
{{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)
daemonOptsDir := path.Join(provisioner.GetDockerOptionsDir(), "profile")
return &DockerOptions{
EngineOptions: engineCfg.String(),
EngineOptionsPath: daemonOptsDir,
}, nil
}
func (provisioner *Boot2DockerProvisioner) CompatibleWithHost() bool {
return provisioner.OsReleaseInfo.Id == "boot2docker"
}
func (provisioner *Boot2DockerProvisioner) SetOsReleaseInfo(info *OsRelease) {
provisioner.OsReleaseInfo = info
}
func (provisioner *Boot2DockerProvisioner) GetOsReleaseInfo() (*OsRelease, error) {
return provisioner.OsReleaseInfo, nil
}
func (provisioner *Boot2DockerProvisioner) AttemptIPContact(dockerPort int) {
ip, err := provisioner.Driver.GetIP()
if err != nil {
log.Warnf("Could not get IP address for created machine: %s", err)
return
}
if conn, err := net.DialTimeout("tcp", fmt.Sprintf("%s:%d", ip, dockerPort), 5*time.Second); err != nil {
log.Warn(`
This machine has been allocated an IP address, but Docker Machine could not
reach it successfully.
SSH for the machine should still work, but connecting to exposed ports, such as
the Docker daemon port (usually <ip>:2376), may not work properly.
You may need to add the route manually, or use another related workaround.
This could be due to a VPN, proxy, or host file configuration issue.
You also might want to clear any VirtualBox host only interfaces you are not using.`)
} else {
conn.Close()
}
}
func (provisioner *Boot2DockerProvisioner) Provision(k8sOptions kubernetes.KubernetesOptions, swarmOptions swarm.SwarmOptions, authOptions auth.AuthOptions, engineOptions engine.EngineOptions) error {
const (
dockerPort = 2376
)
var (
err error
)
defer func() {
if err == nil {
provisioner.AttemptIPContact(dockerPort)
}
}()
provisioner.SwarmOptions = swarmOptions
provisioner.AuthOptions = authOptions
provisioner.EngineOptions = engineOptions
provisioner.KubernetesOptions = k8sOptions
if provisioner.EngineOptions.StorageDriver == "" {
provisioner.EngineOptions.StorageDriver = "aufs"
}
if err = provisioner.SetHostname(provisioner.Driver.GetMachineName()); err != nil {
return err
}
// b2d hosts need to wait for the daemon to be up
// before continuing with provisioning
if err = waitForDocker(provisioner, dockerPort); err != nil {
return err
}
if err = makeDockerOptionsDir(provisioner); err != nil {
return err
}
provisioner.AuthOptions = setRemoteAuthOptions(provisioner)
if err = ConfigureAuth(provisioner); err != nil {
return err
}
if err = configureSwarm(provisioner, swarmOptions, provisioner.AuthOptions); err != nil {
return err
}
if err = configureKubernetes(provisioner, &provisioner.KubernetesOptions, authOptions); err != nil {
return err
}
return nil
}
func (provisioner *Boot2DockerProvisioner) SSHCommand(args string) (string, error) {
return drivers.RunSSHCommandFromDriver(provisioner.Driver, args)
}
func (provisioner *Boot2DockerProvisioner) GetDriver() drivers.Driver {
return provisioner.Driver
}