forked from docker/machine
-
Notifications
You must be signed in to change notification settings - Fork 0
/
compute_util.go
387 lines (338 loc) · 9.16 KB
/
compute_util.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
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
package google
import (
"fmt"
"io/ioutil"
"net/url"
"regexp"
"strings"
"time"
"github.com/docker/machine/libmachine/log"
"github.com/docker/machine/libmachine/ssh"
raw "google.golang.org/api/compute/v1"
"golang.org/x/oauth2"
"golang.org/x/oauth2/google"
)
// ComputeUtil is used to wrap the raw GCE API code and store common parameters.
type ComputeUtil struct {
zone string
instanceName string
userName string
project string
diskTypeURL string
address string
preemptible bool
service *raw.Service
zoneURL string
globalURL string
ipAddress string
SwarmMaster bool
SwarmHost string
}
const (
apiURL = "https://www.googleapis.com/compute/v1/projects/"
imageName = "https://www.googleapis.com/compute/v1/projects/ubuntu-os-cloud/global/images/ubuntu-1404-trusty-v20150909a"
firewallRule = "docker-machines"
port = "2376"
firewallTargetTag = "docker-machine"
dockerStartCommand = "sudo service docker start"
dockerStopCommand = "sudo service docker stop"
)
// NewComputeUtil creates and initializes a ComputeUtil.
func newComputeUtil(driver *Driver) (*ComputeUtil, error) {
client, err := google.DefaultClient(oauth2.NoContext, raw.ComputeScope)
if err != nil {
return nil, err
}
service, err := raw.New(client)
if err != nil {
return nil, err
}
c := ComputeUtil{
zone: driver.Zone,
instanceName: driver.MachineName,
userName: driver.SSHUser,
project: driver.Project,
diskTypeURL: driver.DiskType,
address: driver.Address,
preemptible: driver.Preemptible,
service: service,
zoneURL: apiURL + driver.Project + "/zones/" + driver.Zone,
globalURL: apiURL + driver.Project + "/global",
SwarmMaster: driver.SwarmMaster,
SwarmHost: driver.SwarmHost,
}
return &c, nil
}
func (c *ComputeUtil) diskName() string {
return c.instanceName + "-disk"
}
func (c *ComputeUtil) diskType() string {
return apiURL + c.project + "/zones/" + c.zone + "/diskTypes/" + c.diskTypeURL
}
// disk returns the gce Disk.
func (c *ComputeUtil) disk() (*raw.Disk, error) {
return c.service.Disks.Get(c.project, c.zone, c.diskName()).Do()
}
// deleteDisk deletes the persistent disk.
func (c *ComputeUtil) deleteDisk() error {
log.Infof("Deleting disk.")
op, err := c.service.Disks.Delete(c.project, c.zone, c.diskName()).Do()
if err != nil {
return err
}
log.Infof("Waiting for disk to delete.")
return c.waitForRegionalOp(op.Name)
}
// staticAddress returns the external static IP address.
func (c *ComputeUtil) staticAddress() (string, error) {
// is the address a name?
isName, err := regexp.MatchString("[a-z]([-a-z0-9]*[a-z0-9])?", c.address)
if err != nil {
return "", err
}
if !isName {
return c.address, nil
}
// resolve the address by name
externalAddress, err := c.service.Addresses.Get(c.project, c.region(), c.address).Do()
if err != nil {
return "", err
}
return externalAddress.Address, nil
}
func (c *ComputeUtil) region() string {
return c.zone[:len(c.zone)-2]
}
func (c *ComputeUtil) firewallRule() (*raw.Firewall, error) {
return c.service.Firewalls.Get(c.project, firewallRule).Do()
}
func (c *ComputeUtil) createFirewallRule() error {
log.Infof("Creating firewall rule.")
allowed := []*raw.FirewallAllowed{
{
IPProtocol: "tcp",
Ports: []string{
port,
},
},
}
if c.SwarmMaster {
u, err := url.Parse(c.SwarmHost)
if err != nil {
return fmt.Errorf("error authorizing port for swarm: %s", err)
}
parts := strings.Split(u.Host, ":")
swarmPort := parts[1]
allowed = append(allowed, &raw.FirewallAllowed{
IPProtocol: "tcp",
Ports: []string{
swarmPort,
},
})
}
rule := &raw.Firewall{
Allowed: allowed,
SourceRanges: []string{
"0.0.0.0/0",
},
TargetTags: []string{
firewallTargetTag,
},
Name: firewallRule,
}
op, err := c.service.Firewalls.Insert(c.project, rule).Do()
if err != nil {
return err
}
return c.waitForGlobalOp(op.Name)
}
// instance retrieves the instance.
func (c *ComputeUtil) instance() (*raw.Instance, error) {
return c.service.Instances.Get(c.project, c.zone, c.instanceName).Do()
}
// createInstance creates a GCE VM instance.
func (c *ComputeUtil) createInstance(d *Driver) error {
log.Infof("Creating instance.")
// The rule will either exist or be nil in case of an error.
if rule, _ := c.firewallRule(); rule == nil {
if err := c.createFirewallRule(); err != nil {
return err
}
}
instance := &raw.Instance{
Name: c.instanceName,
Description: "docker host vm",
MachineType: c.zoneURL + "/machineTypes/" + d.MachineType,
Disks: []*raw.AttachedDisk{
{
Boot: true,
AutoDelete: false,
Type: "PERSISTENT",
Mode: "READ_WRITE",
},
},
NetworkInterfaces: []*raw.NetworkInterface{
{
AccessConfigs: []*raw.AccessConfig{
{Type: "ONE_TO_ONE_NAT"},
},
Network: c.globalURL + "/networks/default",
},
},
Tags: &raw.Tags{
Items: parseTags(d),
},
ServiceAccounts: []*raw.ServiceAccount{
{
Email: "default",
Scopes: strings.Split(d.Scopes, ","),
},
},
Scheduling: &raw.Scheduling{
Preemptible: c.preemptible,
},
}
if c.address != "" {
staticAddress, err := c.staticAddress()
if err != nil {
return err
}
instance.NetworkInterfaces[0].AccessConfigs[0].NatIP = staticAddress
}
disk, err := c.disk()
if disk == nil || err != nil {
instance.Disks[0].InitializeParams = &raw.AttachedDiskInitializeParams{
DiskName: c.diskName(),
SourceImage: imageName,
// The maximum supported disk size is 1000GB, the cast should be fine.
DiskSizeGb: int64(d.DiskSize),
DiskType: c.diskType(),
}
} else {
instance.Disks[0].Source = c.zoneURL + "/disks/" + c.instanceName + "-disk"
}
op, err := c.service.Instances.Insert(c.project, c.zone, instance).Do()
if err != nil {
return err
}
log.Infof("Waiting for Instance...")
if err = c.waitForRegionalOp(op.Name); err != nil {
return err
}
instance, err = c.instance()
if err != nil {
return err
}
// Update the SSH Key
sshKey, err := ioutil.ReadFile(d.GetSSHKeyPath() + ".pub")
if err != nil {
return err
}
log.Infof("Uploading SSH Key")
op, err = c.service.Instances.SetMetadata(c.project, c.zone, c.instanceName, &raw.Metadata{
Fingerprint: instance.Metadata.Fingerprint,
Items: []*raw.MetadataItems{
{
Key: "sshKeys",
Value: c.userName + ":" + string(sshKey) + "\n",
},
},
}).Do()
if err != nil {
return err
}
log.Infof("Waiting for SSH Key")
return c.waitForRegionalOp(op.Name)
}
// parseTags computes the tags for the instance.
func parseTags(d *Driver) []string {
tags := []string{firewallTargetTag}
if d.Tags != "" {
tags = append(tags, strings.Split(d.Tags, ",")...)
}
return tags
}
// deleteInstance deletes the instance, leaving the persistent disk.
func (c *ComputeUtil) deleteInstance() error {
log.Infof("Deleting instance.")
op, err := c.service.Instances.Delete(c.project, c.zone, c.instanceName).Do()
if err != nil {
return err
}
log.Infof("Waiting for instance to delete.")
return c.waitForRegionalOp(op.Name)
}
// stopInstance stops the instance.
func (c *ComputeUtil) stopInstance() error {
log.Infof("Stopping instance.")
op, err := c.service.Instances.Stop(c.project, c.zone, c.instanceName).Do()
if err != nil {
return err
}
log.Infof("Waiting for instance to stop.")
return c.waitForRegionalOp(op.Name)
}
// startInstance starts the instance.
func (c *ComputeUtil) startInstance() error {
log.Infof("Starting instance.")
op, err := c.service.Instances.Start(c.project, c.zone, c.instanceName).Do()
if err != nil {
return err
}
log.Infof("Waiting for instance to start.")
return c.waitForRegionalOp(op.Name)
}
func (c *ComputeUtil) executeCommands(commands []string, ip, sshKeyPath string) error {
for _, command := range commands {
auth := &ssh.Auth{
Keys: []string{sshKeyPath},
}
client, err := ssh.NewClient(c.userName, ip, 22, auth)
if err != nil {
return err
}
if _, err := client.Output(command); err != nil {
return err
}
}
return nil
}
func (c *ComputeUtil) waitForOp(opGetter func() (*raw.Operation, error)) error {
for {
op, err := opGetter()
if err != nil {
return err
}
log.Debugf("operation %q status: %s", op.Name, op.Status)
if op.Status == "DONE" {
if op.Error != nil {
return fmt.Errorf("Operation error: %v", *op.Error.Errors[0])
}
break
}
time.Sleep(1 * time.Second)
}
return nil
}
// waitForOp waits for the GCE Operation to finish.
func (c *ComputeUtil) waitForRegionalOp(name string) error {
return c.waitForOp(func() (*raw.Operation, error) {
return c.service.ZoneOperations.Get(c.project, c.zone, name).Do()
})
}
func (c *ComputeUtil) waitForGlobalOp(name string) error {
return c.waitForOp(func() (*raw.Operation, error) {
return c.service.GlobalOperations.Get(c.project, name).Do()
})
}
// ip retrieves and returns the external IP address of the instance.
func (c *ComputeUtil) ip() (string, error) {
if c.ipAddress == "" {
instance, err := c.service.Instances.Get(c.project, c.zone, c.instanceName).Do()
if err != nil {
return "", err
}
c.ipAddress = instance.NetworkInterfaces[0].AccessConfigs[0].NatIP
}
return c.ipAddress, nil
}