Skip to content

Commit bb45f83

Browse files
committed
FIX docker-archive-public#676 - Support Start/Stop GCE instance
Signed-off-by: David Gageot <david@gageot.net>
1 parent 1a8fb2a commit bb45f83

File tree

2 files changed

+54
-20
lines changed

2 files changed

+54
-20
lines changed

drivers/google/compute_util.go

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -284,10 +284,35 @@ func (c *ComputeUtil) deleteInstance() error {
284284
if err != nil {
285285
return err
286286
}
287+
287288
log.Infof("Waiting for instance to delete.")
288289
return c.waitForRegionalOp(op.Name)
289290
}
290291

292+
// stopInstance stops the instance.
293+
func (c *ComputeUtil) stopInstance() error {
294+
log.Infof("Stopping instance.")
295+
op, err := c.service.Instances.Stop(c.project, c.zone, c.instanceName).Do()
296+
if err != nil {
297+
return err
298+
}
299+
300+
log.Infof("Waiting for instance to stop.")
301+
return c.waitForRegionalOp(op.Name)
302+
}
303+
304+
// startInstance starts the instance.
305+
func (c *ComputeUtil) startInstance() error {
306+
log.Infof("Starting instance.")
307+
op, err := c.service.Instances.Start(c.project, c.zone, c.instanceName).Do()
308+
if err != nil {
309+
return err
310+
}
311+
312+
log.Infof("Waiting for instance to start.")
313+
return c.waitForRegionalOp(op.Name)
314+
}
315+
291316
func (c *ComputeUtil) executeCommands(commands []string, ip, sshKeyPath string) error {
292317
for _, command := range commands {
293318
auth := &ssh.Auth{

drivers/google/google.go

Lines changed: 29 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package google
22

33
import (
44
"fmt"
5+
"strings"
56

67
"github.com/codegangsta/cli"
78
"github.com/docker/machine/drivers"
@@ -216,32 +217,54 @@ func (d *Driver) GetState() (state.State, error) {
216217
return state.None, nil
217218
}
218219

219-
// Start creates a GCE instance and attaches it to the existing disk.
220+
// Start starts an existing GCE instance or create an instance with an existing disk.
220221
func (d *Driver) Start() error {
221222
c, err := newComputeUtil(d)
222223
if err != nil {
223224
return err
224225
}
225-
if err = c.createInstance(d); err != nil {
226-
return err
226+
227+
instance, err := c.instance()
228+
if err != nil {
229+
if !strings.Contains(err.Error(), "notFound") {
230+
return err
231+
}
232+
}
233+
234+
if instance == nil {
235+
if err = c.createInstance(d); err != nil {
236+
return err
237+
}
238+
} else {
239+
if err := c.startInstance(); err != nil {
240+
return err
241+
}
227242
}
243+
228244
d.IPAddress, err = d.GetIP()
229245
return err
230246
}
231247

232-
// Stop deletes the GCE instance, but keeps the disk.
248+
// Stop stops an existing GCE instance.
233249
func (d *Driver) Stop() error {
234250
c, err := newComputeUtil(d)
235251
if err != nil {
236252
return err
237253
}
238-
if err = c.deleteInstance(); err != nil {
254+
255+
if err := c.stopInstance(); err != nil {
239256
return err
240257
}
258+
241259
d.IPAddress = ""
242260
return nil
243261
}
244262

263+
// Kill stops an existing GCE instance.
264+
func (d *Driver) Kill() error {
265+
return d.Stop()
266+
}
267+
245268
// Remove deletes the GCE instance and the disk.
246269
func (d *Driver) Remove() error {
247270
c, err := newComputeUtil(d)
@@ -260,20 +283,6 @@ func (d *Driver) Remove() error {
260283
return c.deleteDisk()
261284
}
262285

263-
// Restart deletes and recreates the GCE instance, keeping the disk.
264286
func (d *Driver) Restart() error {
265-
c, err := newComputeUtil(d)
266-
if err != nil {
267-
return err
268-
}
269-
if err := c.deleteInstance(); err != nil {
270-
return err
271-
}
272-
273-
return c.createInstance(d)
274-
}
275-
276-
// Kill deletes the GCE instance, but keeps the disk.
277-
func (d *Driver) Kill() error {
278-
return d.Stop()
287+
return nil
279288
}

0 commit comments

Comments
 (0)