forked from machine-drivers/machine
-
Notifications
You must be signed in to change notification settings - Fork 0
/
google.go
425 lines (379 loc) · 10.6 KB
/
google.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
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
package google
import (
"errors"
"fmt"
"net"
"strings"
"github.com/docker/machine/libmachine/drivers"
"github.com/docker/machine/libmachine/log"
"github.com/docker/machine/libmachine/mcnflag"
"github.com/docker/machine/libmachine/ssh"
"github.com/docker/machine/libmachine/state"
)
// Driver is a struct compatible with the docker.hosts.drivers.Driver interface.
type Driver struct {
*drivers.BaseDriver
Zone string
MachineType string
MachineImage string
DiskType string
Address string
Network string
Subnetwork string
Preemptible bool
UseInternalIP bool
UseInternalIPOnly bool
ServiceAccount string
Scopes string
DiskSize int
Project string
Tags string
UseExisting bool
OpenPorts []string
}
const (
defaultZone = "us-central1-a"
defaultUser = "docker-user"
defaultMachineType = "n1-standard-1"
defaultImageName = "ubuntu-os-cloud/global/images/ubuntu-1604-xenial-v20170721"
defaultServiceAccount = "default"
defaultScopes = "https://www.googleapis.com/auth/devstorage.read_only,https://www.googleapis.com/auth/logging.write,https://www.googleapis.com/auth/monitoring.write"
defaultDiskType = "pd-standard"
defaultDiskSize = 10
defaultNetwork = "default"
defaultSubnetwork = ""
)
// GetCreateFlags registers the flags this driver adds to
// "docker hosts create"
func (d *Driver) GetCreateFlags() []mcnflag.Flag {
return []mcnflag.Flag{
mcnflag.StringFlag{
Name: "google-zone",
Usage: "GCE Zone",
Value: defaultZone,
EnvVar: "GOOGLE_ZONE",
},
mcnflag.StringFlag{
Name: "google-machine-type",
Usage: "GCE Machine Type",
Value: defaultMachineType,
EnvVar: "GOOGLE_MACHINE_TYPE",
},
mcnflag.StringFlag{
Name: "google-machine-image",
Usage: "GCE Machine Image Absolute URL",
Value: defaultImageName,
EnvVar: "GOOGLE_MACHINE_IMAGE",
},
mcnflag.StringFlag{
Name: "google-username",
Usage: "GCE User Name",
Value: defaultUser,
EnvVar: "GOOGLE_USERNAME",
},
mcnflag.StringFlag{
Name: "google-project",
Usage: "GCE Project",
EnvVar: "GOOGLE_PROJECT",
},
mcnflag.StringFlag{
Name: "google-service-account",
Usage: "GCE Service Account for the VM (email address)",
Value: defaultServiceAccount,
EnvVar: "GOOGLE_SERVICE_ACCOUNT",
},
mcnflag.StringFlag{
Name: "google-scopes",
Usage: "GCE Scopes (comma-separated if multiple scopes)",
Value: defaultScopes,
EnvVar: "GOOGLE_SCOPES",
},
mcnflag.IntFlag{
Name: "google-disk-size",
Usage: "GCE Instance Disk Size (in GB)",
Value: defaultDiskSize,
EnvVar: "GOOGLE_DISK_SIZE",
},
mcnflag.StringFlag{
Name: "google-disk-type",
Usage: "GCE Instance Disk type",
Value: defaultDiskType,
EnvVar: "GOOGLE_DISK_TYPE",
},
mcnflag.StringFlag{
Name: "google-network",
Usage: "Specify network in which to provision vm",
Value: defaultNetwork,
EnvVar: "GOOGLE_NETWORK",
},
mcnflag.StringFlag{
Name: "google-subnetwork",
Usage: "Specify subnetwork in which to provision vm",
Value: defaultSubnetwork,
EnvVar: "GOOGLE_SUBNETWORK",
},
mcnflag.StringFlag{
Name: "google-address",
Usage: "GCE Instance External IP",
EnvVar: "GOOGLE_ADDRESS",
},
mcnflag.BoolFlag{
Name: "google-preemptible",
Usage: "GCE Instance Preemptibility",
EnvVar: "GOOGLE_PREEMPTIBLE",
},
mcnflag.StringFlag{
Name: "google-tags",
Usage: "GCE Instance Tags (comma-separated)",
EnvVar: "GOOGLE_TAGS",
Value: "",
},
mcnflag.BoolFlag{
Name: "google-use-internal-ip",
Usage: "Use internal GCE Instance IP rather than public one",
EnvVar: "GOOGLE_USE_INTERNAL_IP",
},
mcnflag.BoolFlag{
Name: "google-use-internal-ip-only",
Usage: "Configure GCE instance to not have an external IP address",
EnvVar: "GOOGLE_USE_INTERNAL_IP_ONLY",
},
mcnflag.BoolFlag{
Name: "google-use-existing",
Usage: "Don't create a new VM, use an existing one",
EnvVar: "GOOGLE_USE_EXISTING",
},
mcnflag.StringSliceFlag{
Name: "google-open-port",
Usage: "Make the specified port number accessible from the Internet, e.g, 8080/tcp",
},
}
}
// NewDriver creates a Driver with the specified storePath.
func NewDriver(machineName string, storePath string) *Driver {
return &Driver{
Zone: defaultZone,
DiskType: defaultDiskType,
DiskSize: defaultDiskSize,
MachineType: defaultMachineType,
MachineImage: defaultImageName,
Network: defaultNetwork,
Subnetwork: defaultSubnetwork,
ServiceAccount: defaultServiceAccount,
Scopes: defaultScopes,
BaseDriver: &drivers.BaseDriver{
SSHUser: defaultUser,
MachineName: machineName,
StorePath: storePath,
},
}
}
// GetSSHHostname returns hostname for use with ssh
func (d *Driver) GetSSHHostname() (string, error) {
return d.GetIP()
}
// GetSSHUsername returns username for use with ssh
func (d *Driver) GetSSHUsername() string {
if d.SSHUser == "" {
d.SSHUser = "docker-user"
}
return d.SSHUser
}
// DriverName returns the name of the driver
func (d *Driver) DriverName() string {
return "google"
}
// SetConfigFromFlags initializes the driver based on the command line flags.
func (d *Driver) SetConfigFromFlags(flags drivers.DriverOptions) error {
d.Project = flags.String("google-project")
if d.Project == "" {
return errors.New("no Google Cloud Project name specified (--google-project)")
}
d.Zone = flags.String("google-zone")
d.UseExisting = flags.Bool("google-use-existing")
if !d.UseExisting {
d.MachineType = flags.String("google-machine-type")
d.MachineImage = flags.String("google-machine-image")
d.MachineImage = strings.TrimPrefix(d.MachineImage, "https://www.googleapis.com/compute/v1/projects/")
d.DiskSize = flags.Int("google-disk-size")
d.DiskType = flags.String("google-disk-type")
d.Address = flags.String("google-address")
d.Network = flags.String("google-network")
d.Subnetwork = flags.String("google-subnetwork")
d.Preemptible = flags.Bool("google-preemptible")
d.UseInternalIP = flags.Bool("google-use-internal-ip") || flags.Bool("google-use-internal-ip-only")
d.UseInternalIPOnly = flags.Bool("google-use-internal-ip-only")
d.ServiceAccount = flags.String("google-service-account")
d.Scopes = flags.String("google-scopes")
d.Tags = flags.String("google-tags")
d.OpenPorts = flags.StringSlice("google-open-port")
}
d.SSHUser = flags.String("google-username")
d.SSHPort = 22
d.SetSwarmConfigFromFlags(flags)
return nil
}
// PreCreateCheck is called to enforce pre-creation steps
func (d *Driver) PreCreateCheck() error {
c, err := newComputeUtil(d)
if err != nil {
return err
}
// Check that the project exists. It will also check the credentials
// at the same time.
log.Infof("Check that the project exists")
if _, err = c.service.Projects.Get(d.Project).Do(); err != nil {
return fmt.Errorf("Project with ID %q not found. %v", d.Project, err)
}
// Check if the instance already exists. There will be an error if the instance
// doesn't exist, so just check instance for nil.
log.Infof("Check if the instance already exists")
instance, _ := c.instance()
if d.UseExisting {
if instance == nil {
return fmt.Errorf("unable to find instance %q in zone %q", d.MachineName, d.Zone)
}
} else {
if instance != nil {
return fmt.Errorf("instance %q already exists in zone %q", d.MachineName, d.Zone)
}
}
return nil
}
// Create creates a GCE VM instance acting as a docker host.
func (d *Driver) Create() error {
log.Infof("Generating SSH Key")
if err := ssh.GenerateSSHKey(d.GetSSHKeyPath()); err != nil {
return err
}
log.Infof("Creating host...")
c, err := newComputeUtil(d)
if err != nil {
return err
}
if err := c.openFirewallPorts(d); err != nil {
return err
}
if d.UseExisting {
return c.configureInstance(d)
}
return c.createInstance(d)
}
// GetURL returns the URL of the remote docker daemon.
func (d *Driver) GetURL() (string, error) {
ip, err := d.GetIP()
if err != nil {
return "", err
}
return fmt.Sprintf("tcp://%s", net.JoinHostPort(ip, "2376")), nil
}
// GetIP returns the IP address of the GCE instance.
func (d *Driver) GetIP() (string, error) {
c, err := newComputeUtil(d)
if err != nil {
return "", err
}
ip, err := c.ip()
if err != nil {
return "", err
}
if ip == "" {
return "", drivers.ErrHostIsNotRunning
}
return ip, nil
}
// GetState returns a docker.hosts.state.State value representing the current state of the host.
func (d *Driver) GetState() (state.State, error) {
c, err := newComputeUtil(d)
if err != nil {
return state.None, err
}
// All we care about is whether the disk exists, so we just check disk for a nil value.
// There will be no error if disk is not nil.
instance, _ := c.instance()
if instance == nil {
disk, _ := c.disk()
if disk == nil {
return state.None, nil
}
return state.Stopped, nil
}
switch instance.Status {
case "PROVISIONING", "STAGING":
return state.Starting, nil
case "RUNNING":
return state.Running, nil
case "STOPPING", "STOPPED", "TERMINATED":
return state.Stopped, nil
}
return state.None, nil
}
// Start starts an existing GCE instance or create an instance with an existing disk.
func (d *Driver) Start() error {
c, err := newComputeUtil(d)
if err != nil {
return err
}
instance, err := c.instance()
if err != nil {
if !isNotFound(err) {
return err
}
}
if instance == nil {
if err = c.createInstance(d); err != nil {
return err
}
} else {
if err := c.startInstance(); err != nil {
return err
}
}
d.IPAddress, err = d.GetIP()
return err
}
// Stop stops an existing GCE instance.
func (d *Driver) Stop() error {
c, err := newComputeUtil(d)
if err != nil {
return err
}
if err := c.stopInstance(); err != nil {
return err
}
d.IPAddress = ""
return nil
}
// Restart restarts a machine which is known to be running.
func (d *Driver) Restart() error {
if err := d.Stop(); err != nil {
return err
}
return d.Start()
}
// Kill stops an existing GCE instance.
func (d *Driver) Kill() error {
return d.Stop()
}
// Remove deletes the GCE instance and the disk.
func (d *Driver) Remove() error {
c, err := newComputeUtil(d)
if err != nil {
return err
}
if err := c.deleteInstance(); err != nil {
if isNotFound(err) {
log.Warn("Remote instance does not exist, proceeding with removing local reference")
} else {
return err
}
}
if err := c.deleteDisk(); err != nil {
if isNotFound(err) {
log.Warn("Remote disk does not exist, proceeding")
} else {
return err
}
}
return nil
}