Revert "Support creating a machine on an existing google VM"

This reverts commit 7cf9ae71be.

Signed-off-by: David Gageot <david@gageot.net>
This commit is contained in:
David Gageot 2016-01-11 15:46:04 +01:00
parent beb36cbcff
commit 52bd740ad3
3 changed files with 29 additions and 96 deletions

View File

@ -38,7 +38,7 @@ To create a machine instance, specify `--driver google`, the project id and the
### Options
- `--google-project`: **required** The id of your project to use when launching the instance.
- `--google-project`: **required** The id of your project to use when launching the instance.
- `--google-zone`: The zone to launch the instance.
- `--google-machine-type`: The type of instance.
- `--google-machine-image`: The absolute URL to a base VM image to instantiate.
@ -50,7 +50,6 @@ To create a machine instance, specify `--driver google`, the project id and the
- `--google-preemptible`: Instance preemptibility.
- `--google-tags`: Instance tags (comma-separated).
- `--google-use-internal-ip`: When this option is used during create it will make docker-machine use internal rather than public NATed IPs. The flag is persistent in the sense that a machine created with it retains the IP. It's useful for managing docker machines from another machine on the same network e.g. while deploying swarm.
- `--google-use-existing`: Don't create a new VM, use an existing one. This is useful when you'd like to provision Docker on a VM you created yourself, maybe because it uses create options not supported by this driver.
The GCE driver will use the `ubuntu-1510-wily-v20151114` instance image unless otherwise specified. To obtain a
list of image URLs run:
@ -73,4 +72,3 @@ Environment variables and default values:
| `--google-preemptible` | `GOOGLE_PREEMPTIBLE` | - |
| `--google-tags` | `GOOGLE_TAGS` | - |
| `--google-use-internal-ip` | `GOOGLE_USE_INTERNAL_IP` | - |
| `--google-use-existing` | `GOOGLE_USE_EXISTING` | - |

View File

@ -88,11 +88,6 @@ func (c *ComputeUtil) disk() (*raw.Disk, error) {
// deleteDisk deletes the persistent disk.
func (c *ComputeUtil) deleteDisk() error {
disk, _ := c.disk()
if disk == nil {
return nil
}
log.Infof("Deleting disk.")
op, err := c.service.Disks.Delete(c.project, c.zone, c.diskName()).Do()
if err != nil {
@ -167,9 +162,8 @@ func (c *ComputeUtil) portsUsed() ([]string, error) {
return ports, nil
}
// openFirewallPorts configures the firewall to open docker and swarm ports.
func (c *ComputeUtil) openFirewallPorts() error {
log.Infof("Opening firewall ports")
func (c *ComputeUtil) createFirewallRule() error {
log.Infof("Opening firewall ports.")
create := false
rule, _ := c.firewallRule()
@ -219,7 +213,11 @@ func (c *ComputeUtil) instance() (*raw.Instance, error) {
// createInstance creates a GCE VM instance.
func (c *ComputeUtil) createInstance(d *Driver) error {
log.Infof("Creating instance")
log.Infof("Creating instance.")
if err := c.createFirewallRule(); err != nil {
return err
}
instance := &raw.Instance{
Name: c.instanceName,
@ -282,7 +280,7 @@ func (c *ComputeUtil) createInstance(d *Driver) error {
return err
}
log.Infof("Waiting for Instance")
log.Infof("Waiting for Instance...")
if err = c.waitForRegionalOp(op.Name); err != nil {
return err
}
@ -292,58 +290,15 @@ func (c *ComputeUtil) createInstance(d *Driver) error {
return err
}
return c.uploadSSHKey(instance, d.GetSSHKeyPath())
}
// configureInstance configures an existing instance for use with Docker Machine.
func (c *ComputeUtil) configureInstance(d *Driver) error {
log.Infof("Configuring instance")
instance, err := c.instance()
// Update the SSH Key
sshKey, err := ioutil.ReadFile(d.GetSSHKeyPath() + ".pub")
if err != nil {
return err
}
if err := c.addFirewallTag(instance); err != nil {
return err
}
return c.uploadSSHKey(instance, d.GetSSHKeyPath())
}
// addFirewallTag adds a tag to the instance to match the firewall rule.
func (c *ComputeUtil) addFirewallTag(instance *raw.Instance) error {
log.Infof("Adding tag for the firewall rule")
tags := instance.Tags
for _, tag := range tags.Items {
if tag == firewallTargetTag {
return nil
}
}
tags.Items = append(tags.Items, firewallTargetTag)
op, err := c.service.Instances.SetTags(c.project, c.zone, instance.Name, tags).Do()
if err != nil {
return err
}
return c.waitForRegionalOp(op.Name)
}
// uploadSSHKey updates the instance metadata with the given ssh key.
func (c *ComputeUtil) uploadSSHKey(instance *raw.Instance, sshKeyPath string) error {
log.Infof("Uploading SSH Key")
sshKey, err := ioutil.ReadFile(sshKeyPath + ".pub")
if err != nil {
return err
}
metaDataValue := fmt.Sprintf("%s:%s %s\n", c.userName, strings.TrimSpace(string(sshKey)), c.userName)
op, err := c.service.Instances.SetMetadata(c.project, c.zone, c.instanceName, &raw.Metadata{
op, err = c.service.Instances.SetMetadata(c.project, c.zone, c.instanceName, &raw.Metadata{
Fingerprint: instance.Metadata.Fingerprint,
Items: []*raw.MetadataItems{
{
@ -352,6 +307,10 @@ func (c *ComputeUtil) uploadSSHKey(instance *raw.Instance, sshKeyPath string) er
},
},
}).Do()
if err != nil {
return err
}
log.Infof("Waiting for SSH Key")
return c.waitForRegionalOp(op.Name)
}
@ -401,7 +360,6 @@ func (c *ComputeUtil) startInstance() error {
return c.waitForRegionalOp(op.Name)
}
// waitForOp waits for the operation to finish.
func (c *ComputeUtil) waitForOp(opGetter func() (*raw.Operation, error)) error {
for {
op, err := opGetter()
@ -409,7 +367,7 @@ func (c *ComputeUtil) waitForOp(opGetter func() (*raw.Operation, error)) error {
return err
}
log.Debugf("Operation %q status: %s", op.Name, op.Status)
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])
@ -421,7 +379,7 @@ func (c *ComputeUtil) waitForOp(opGetter func() (*raw.Operation, error)) error {
return nil
}
// waitForRegionalOp waits for the regional operation to finish.
// waitForOp waits for the 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()

View File

@ -26,7 +26,6 @@ type Driver struct {
DiskSize int
Project string
Tags string
UseExisting bool
}
const (
@ -111,11 +110,6 @@ func (d *Driver) GetCreateFlags() []mcnflag.Flag {
Usage: "Use internal GCE Instance IP rather than public one",
EnvVar: "GOOGLE_USE_INTERNAL_IP",
},
mcnflag.BoolFlag{
Name: "google-use-existing",
Usage: "Don't create a new VM, use an existing one",
EnvVar: "GOOGLE_USE_EXISTING",
},
}
}
@ -162,18 +156,15 @@ func (d *Driver) SetConfigFromFlags(flags drivers.DriverOptions) error {
}
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.DiskSize = flags.Int("google-disk-size")
d.DiskType = flags.String("google-disk-type")
d.Address = flags.String("google-address")
d.Preemptible = flags.Bool("google-preemptible")
d.UseInternalIP = flags.Bool("google-use-internal-ip")
d.Scopes = flags.String("google-scopes")
d.Tags = flags.String("google-tags")
}
d.MachineType = flags.String("google-machine-type")
d.MachineImage = flags.String("google-machine-image")
d.DiskSize = flags.Int("google-disk-size")
d.DiskType = flags.String("google-disk-type")
d.Address = flags.String("google-address")
d.Preemptible = flags.Bool("google-preemptible")
d.UseInternalIP = flags.Bool("google-use-internal-ip")
d.Scopes = flags.String("google-scopes")
d.Tags = flags.String("google-tags")
d.SSHUser = flags.String("google-username")
d.SSHPort = 22
d.SetSwarmConfigFromFlags(flags)
@ -200,15 +191,8 @@ func (d *Driver) PreCreateCheck() error {
// 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)
}
if instance, _ := c.instance(); instance != nil {
return fmt.Errorf("Instance %v already exists.", d.MachineName)
}
return nil
@ -229,13 +213,6 @@ func (d *Driver) Create() error {
return err
}
if err := c.openFirewallPorts(); err != nil {
return err
}
if d.UseExisting {
return c.configureInstance(d)
}
return c.createInstance(d)
}