FIX #676 - Support Start/Stop GCE instance

Signed-off-by: David Gageot <david@gageot.net>
This commit is contained in:
David Gageot 2015-08-01 10:12:20 +02:00
parent 1a8fb2a071
commit bb45f83319
2 changed files with 54 additions and 20 deletions

View File

@ -284,10 +284,35 @@ func (c *ComputeUtil) deleteInstance() error {
if err != nil { if err != nil {
return err return err
} }
log.Infof("Waiting for instance to delete.") log.Infof("Waiting for instance to delete.")
return c.waitForRegionalOp(op.Name) 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 { func (c *ComputeUtil) executeCommands(commands []string, ip, sshKeyPath string) error {
for _, command := range commands { for _, command := range commands {
auth := &ssh.Auth{ auth := &ssh.Auth{

View File

@ -2,6 +2,7 @@ package google
import ( import (
"fmt" "fmt"
"strings"
"github.com/codegangsta/cli" "github.com/codegangsta/cli"
"github.com/docker/machine/drivers" "github.com/docker/machine/drivers"
@ -216,32 +217,54 @@ func (d *Driver) GetState() (state.State, error) {
return state.None, nil return state.None, nil
} }
// Start creates a GCE instance and attaches it to the existing disk. // Start starts an existing GCE instance or create an instance with an existing disk.
func (d *Driver) Start() error { func (d *Driver) Start() error {
c, err := newComputeUtil(d) c, err := newComputeUtil(d)
if err != nil { if err != nil {
return err return err
} }
if err = c.createInstance(d); err != nil {
return err instance, err := c.instance()
if err != nil {
if !strings.Contains(err.Error(), "notFound") {
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() d.IPAddress, err = d.GetIP()
return err return err
} }
// Stop deletes the GCE instance, but keeps the disk. // Stop stops an existing GCE instance.
func (d *Driver) Stop() error { func (d *Driver) Stop() error {
c, err := newComputeUtil(d) c, err := newComputeUtil(d)
if err != nil { if err != nil {
return err return err
} }
if err = c.deleteInstance(); err != nil {
if err := c.stopInstance(); err != nil {
return err return err
} }
d.IPAddress = "" d.IPAddress = ""
return nil return nil
} }
// Kill stops an existing GCE instance.
func (d *Driver) Kill() error {
return d.Stop()
}
// Remove deletes the GCE instance and the disk. // Remove deletes the GCE instance and the disk.
func (d *Driver) Remove() error { func (d *Driver) Remove() error {
c, err := newComputeUtil(d) c, err := newComputeUtil(d)
@ -260,20 +283,6 @@ func (d *Driver) Remove() error {
return c.deleteDisk() return c.deleteDisk()
} }
// Restart deletes and recreates the GCE instance, keeping the disk.
func (d *Driver) Restart() error { func (d *Driver) Restart() error {
c, err := newComputeUtil(d) return nil
if err != nil {
return err
}
if err := c.deleteInstance(); err != nil {
return err
}
return c.createInstance(d)
}
// Kill deletes the GCE instance, but keeps the disk.
func (d *Driver) Kill() error {
return d.Stop()
} }