forked from tsuru/tsuru
-
Notifications
You must be signed in to change notification settings - Fork 0
/
dockermachine.go
357 lines (339 loc) · 9.76 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
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
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
// Copyright 2016 tsuru authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package dockermachine
import (
"encoding/json"
"io"
"io/ioutil"
"os"
"path/filepath"
"strconv"
"strings"
"github.com/docker/machine/libmachine"
"github.com/docker/machine/libmachine/drivers"
"github.com/docker/machine/libmachine/drivers/rpc"
"github.com/docker/machine/libmachine/engine"
"github.com/docker/machine/libmachine/host"
"github.com/docker/machine/libmachine/log"
"github.com/docker/machine/libmachine/mcnflag"
"github.com/docker/machine/libmachine/mcnutils"
"github.com/pkg/errors"
"github.com/tsuru/tsuru/iaas"
)
type DockerMachine struct {
io.Closer
client libmachine.API
StorePath string
CertsPath string
temp bool
}
type DockerMachineConfig struct {
CaPath string
OutWriter io.Writer
ErrWriter io.Writer
StorePath string
IsDebug bool
}
type DockerMachineAPI interface {
io.Closer
CreateMachine(CreateMachineOpts) (*Machine, error)
DeleteMachine(*iaas.Machine) error
RegisterMachine(RegisterMachineOpts) (*Machine, error)
List() ([]*Machine, error)
DeleteAll() error
}
type CreateMachineOpts struct {
Name string
DriverName string
Params map[string]interface{}
InsecureRegistry string
DockerEngineInstallURL string
RegistryMirror string
DockerEngineStorageDriver string
ArbitraryFlags []string
}
type RegisterMachineOpts struct {
Base *iaas.Machine
DriverName string
SSHPrivateKey []byte
}
type Machine struct {
Base *iaas.Machine
Host *host.Host
}
func NewDockerMachine(config DockerMachineConfig) (DockerMachineAPI, error) {
storePath := config.StorePath
temp := false
if storePath == "" {
tempPath, err := ioutil.TempDir("", "")
if err != nil {
return nil, errors.Wrap(err, "failed to create temp dir")
}
storePath = tempPath
temp = true
}
certsPath := filepath.Join(storePath, "certs")
if _, err := os.Stat(certsPath); os.IsNotExist(err) {
err := os.MkdirAll(certsPath, 0700)
if err != nil {
return nil, errors.WithMessage(err, "failed to create certs dir")
}
}
if config.CaPath != "" {
err := mcnutils.CopyFile(filepath.Join(config.CaPath, "ca.pem"), filepath.Join(certsPath, "ca.pem"))
if err != nil {
return nil, errors.Wrap(err, "failed to copy ca file")
}
err = mcnutils.CopyFile(filepath.Join(config.CaPath, "ca-key.pem"), filepath.Join(certsPath, "ca-key.pem"))
if err != nil {
return nil, errors.Wrap(err, "failed to copy ca key file")
}
}
if config.OutWriter != nil {
log.SetOutWriter(config.OutWriter)
} else {
log.SetOutWriter(ioutil.Discard)
}
if config.ErrWriter != nil {
log.SetOutWriter(config.ErrWriter)
} else {
log.SetOutWriter(ioutil.Discard)
}
log.SetDebug(config.IsDebug)
client := libmachine.NewClient(storePath, certsPath)
client.IsDebug = config.IsDebug
if _, err := os.Stat(client.GetMachinesDir()); os.IsNotExist(err) {
err := os.MkdirAll(client.GetMachinesDir(), 0700)
if err != nil {
return nil, errors.WithMessage(err, "failed to create machines dir")
}
}
return &DockerMachine{
StorePath: storePath,
CertsPath: certsPath,
client: client,
temp: temp,
}, nil
}
func (d *DockerMachine) Close() error {
if d.temp {
os.RemoveAll(d.StorePath)
}
return d.client.Close()
}
func (d *DockerMachine) CreateMachine(opts CreateMachineOpts) (*Machine, error) {
rawDriver, err := json.Marshal(&drivers.BaseDriver{
MachineName: opts.Name,
StorePath: d.StorePath,
})
if err != nil {
return nil, errors.Wrap(err, "failed to marshal base driver")
}
h, err := d.client.NewHost(opts.DriverName, rawDriver)
if err != nil {
return nil, errors.Wrap(err, "failed to initialize host")
}
err = configureDriver(h.Driver, opts.Params)
if err != nil {
return nil, errors.WithMessage(err, "failed to configure driver")
}
engineOpts := h.HostOptions.EngineOptions
if opts.InsecureRegistry != "" {
engineOpts.InsecureRegistry = []string{opts.InsecureRegistry}
}
if opts.DockerEngineInstallURL != "" {
engineOpts.InstallURL = opts.DockerEngineInstallURL
}
if opts.RegistryMirror != "" {
engineOpts.RegistryMirror = []string{opts.RegistryMirror}
}
if opts.DockerEngineStorageDriver != "" {
engineOpts.StorageDriver = opts.DockerEngineStorageDriver
}
engineOpts.ArbitraryFlags = opts.ArbitraryFlags
if h.AuthOptions() != nil {
h.AuthOptions().StorePath = d.StorePath
}
errCreate := d.client.Create(h)
machine, err := newMachine(h)
if errCreate != nil {
return machine, errors.Wrap(errCreate, "failed to create host")
}
return machine, errors.Wrap(err, "failed to create machine")
}
func (d *DockerMachine) DeleteMachine(m *iaas.Machine) error {
rawDriver, err := json.Marshal(m.CustomData)
if err != nil {
return errors.Wrap(err, "failed to marshal machine data")
}
host, err := d.client.NewHost(m.CreationParams["driver"], rawDriver)
if err != nil {
return errors.Wrap(err, "failed to initialize host")
}
err = host.Driver.Remove()
if err != nil {
return errors.Wrap(err, "failed to remove host")
}
return d.client.Remove(m.Id)
}
func (d *DockerMachine) DeleteAll() error {
hosts, err := d.client.List()
if err != nil {
return err
}
for _, n := range hosts {
h, errLoad := d.client.Load(n)
if errLoad != nil {
return errLoad
}
err = h.Driver.Remove()
if err != nil {
return err
}
}
return os.RemoveAll(d.StorePath)
}
// RegisterMachine registers an iaas.Machine as an Machine and a host on
// the current running DockerMachine. It expects all data needed to Marshal
// the host/driver to be available on CustomData.
func (d *DockerMachine) RegisterMachine(opts RegisterMachineOpts) (*Machine, error) {
if !d.temp {
return nil, errors.New("register is only available without user defined StorePath")
}
if opts.Base.CustomData == nil {
return nil, errors.New("custom data is required")
}
opts.Base.CustomData["SSHKeyPath"] = filepath.Join(d.client.GetMachinesDir(), opts.Base.Id, "id_rsa")
rawDriver, err := json.Marshal(opts.Base.CustomData)
if err != nil {
return nil, errors.WithMessage(err, "failed to marshal driver data")
}
h, err := d.client.NewHost(opts.DriverName, rawDriver)
if err != nil {
return nil, errors.WithStack(err)
}
err = ioutil.WriteFile(h.Driver.GetSSHKeyPath(), opts.SSHPrivateKey, 0700)
if err != nil {
return nil, errors.WithStack(err)
}
err = ioutil.WriteFile(h.AuthOptions().CaCertPath, opts.Base.CaCert, 0700)
if err != nil {
return nil, errors.WithStack(err)
}
err = ioutil.WriteFile(h.AuthOptions().ClientCertPath, opts.Base.ClientCert, 0700)
if err != nil {
return nil, errors.WithStack(err)
}
err = ioutil.WriteFile(h.AuthOptions().ClientKeyPath, opts.Base.ClientKey, 0700)
if err != nil {
return nil, errors.WithStack(err)
}
err = d.client.Save(h)
if err != nil {
return nil, errors.WithStack(err)
}
savedHost, err := d.client.Load(h.Name)
if err != nil {
return nil, errors.WithStack(err)
}
return &Machine{
Base: opts.Base,
Host: savedHost,
}, nil
}
func (d *DockerMachine) List() ([]*Machine, error) {
names, err := d.client.List()
if err != nil {
return nil, errors.WithStack(err)
}
var machines []*Machine
for _, n := range names {
h, err := d.client.Load(n)
if err != nil {
return nil, errors.WithStack(err)
}
m, err := newMachine(h)
if err != nil {
return nil, errors.WithStack(err)
}
machines = append(machines, m)
}
return machines, nil
}
func newMachine(h *host.Host) (*Machine, error) {
rawDriver, err := json.Marshal(h.Driver)
if err != nil {
return nil, errors.Wrap(err, "failed to marshal host driver")
}
var driverData map[string]interface{}
err = json.Unmarshal(rawDriver, &driverData)
if err != nil {
return nil, errors.Wrap(err, "failed to unmarshal host driver")
}
m := &Machine{
Base: &iaas.Machine{
Id: h.Name,
Port: engine.DefaultPort,
Protocol: "https",
CustomData: driverData,
CreationParams: map[string]string{
"driver": h.DriverName,
},
},
Host: h,
}
address, err := h.Driver.GetIP()
if err != nil || address == "" {
return nil, errors.Wrap(err, "failed to retrive host ip")
}
m.Base.Address = address
if h.AuthOptions() != nil {
m.Base.CaCert, err = ioutil.ReadFile(h.AuthOptions().CaCertPath)
if err != nil {
return m, errors.Wrap(err, "failed to read host ca cert")
}
m.Base.ClientCert, err = ioutil.ReadFile(h.AuthOptions().ClientCertPath)
if err != nil {
return m, errors.Wrap(err, "failed to read host client cert")
}
m.Base.ClientKey, err = ioutil.ReadFile(h.AuthOptions().ClientKeyPath)
if err != nil {
return m, errors.Wrap(err, "failed to read host client key")
}
}
return m, nil
}
func configureDriver(driver drivers.Driver, driverOpts map[string]interface{}) error {
opts := &rpcdriver.RPCFlags{Values: driverOpts}
for _, c := range driver.GetCreateFlags() {
val, ok := opts.Values[c.String()]
if !ok {
opts.Values[c.String()] = c.Default()
if c.Default() == nil {
opts.Values[c.String()] = false
}
} else {
if strVal, ok := val.(string); ok {
switch c.(type) {
case *mcnflag.StringSliceFlag, mcnflag.StringSliceFlag:
opts.Values[c.String()] = strings.Split(strVal, ",")
case *mcnflag.IntFlag, mcnflag.IntFlag:
v, err := strconv.Atoi(strVal)
if err != nil {
return errors.Wrapf(err, "failed to set %s flag: %s is not an int", c.String(), strVal)
}
opts.Values[c.String()] = v
case *mcnflag.BoolFlag, mcnflag.BoolFlag:
v, err := strconv.ParseBool(strVal)
if err != nil {
return errors.Wrapf(err, "failed to set %s flag: %s is not a bool", c.String(), strVal)
}
opts.Values[c.String()] = v
}
}
}
}
err := driver.SetConfigFromFlags(opts)
return errors.Wrap(err, "failed to set driver configuration")
}