Merge pull request #1631 from dgageot/features/startstop

FIX #676 - Support Start/Stop GCE instance
This commit is contained in:
Nathan LeClaire 2015-09-15 11:54:51 -07:00
commit 5e698f3d95
2 changed files with 54 additions and 20 deletions

View File

@ -284,10 +284,35 @@ func (c *ComputeUtil) deleteInstance() error {
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{

View File

@ -2,6 +2,7 @@ package google
import (
"fmt"
"strings"
"github.com/codegangsta/cli"
"github.com/docker/machine/drivers"
@ -226,32 +227,54 @@ func (d *Driver) GetState() (state.State, error) {
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 {
c, err := newComputeUtil(d)
if err != nil {
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()
return err
}
// Stop deletes the GCE instance, but keeps the disk.
// Stop stops an existing GCE instance.
func (d *Driver) Stop() error {
c, err := newComputeUtil(d)
if err != nil {
return err
}
if err = c.deleteInstance(); err != nil {
if err := c.stopInstance(); err != nil {
return err
}
d.IPAddress = ""
return nil
}
// 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)
@ -270,20 +293,6 @@ func (d *Driver) Remove() error {
return c.deleteDisk()
}
// Restart deletes and recreates the GCE instance, keeping the disk.
func (d *Driver) Restart() error {
c, err := newComputeUtil(d)
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()
return nil
}