Skip to content

Commit 5e698f3

Browse files
Merge pull request docker-archive-public#1631 from dgageot/features/startstop
FIX docker-archive-public#676 - Support Start/Stop GCE instance
2 parents 207a647 + bb45f83 commit 5e698f3

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"
@@ -226,32 +227,54 @@ func (d *Driver) GetState() (state.State, error) {
226227
return state.None, nil
227228
}
228229

229-
// Start creates a GCE instance and attaches it to the existing disk.
230+
// Start starts an existing GCE instance or create an instance with an existing disk.
230231
func (d *Driver) Start() error {
231232
c, err := newComputeUtil(d)
232233
if err != nil {
233234
return err
234235
}
235-
if err = c.createInstance(d); err != nil {
236-
return err
236+
237+
instance, err := c.instance()
238+
if err != nil {
239+
if !strings.Contains(err.Error(), "notFound") {
240+
return err
241+
}
242+
}
243+
244+
if instance == nil {
245+
if err = c.createInstance(d); err != nil {
246+
return err
247+
}
248+
} else {
249+
if err := c.startInstance(); err != nil {
250+
return err
251+
}
237252
}
253+
238254
d.IPAddress, err = d.GetIP()
239255
return err
240256
}
241257

242-
// Stop deletes the GCE instance, but keeps the disk.
258+
// Stop stops an existing GCE instance.
243259
func (d *Driver) Stop() error {
244260
c, err := newComputeUtil(d)
245261
if err != nil {
246262
return err
247263
}
248-
if err = c.deleteInstance(); err != nil {
264+
265+
if err := c.stopInstance(); err != nil {
249266
return err
250267
}
268+
251269
d.IPAddress = ""
252270
return nil
253271
}
254272

273+
// Kill stops an existing GCE instance.
274+
func (d *Driver) Kill() error {
275+
return d.Stop()
276+
}
277+
255278
// Remove deletes the GCE instance and the disk.
256279
func (d *Driver) Remove() error {
257280
c, err := newComputeUtil(d)
@@ -270,20 +293,6 @@ func (d *Driver) Remove() error {
270293
return c.deleteDisk()
271294
}
272295

273-
// Restart deletes and recreates the GCE instance, keeping the disk.
274296
func (d *Driver) Restart() error {
275-
c, err := newComputeUtil(d)
276-
if err != nil {
277-
return err
278-
}
279-
if err := c.deleteInstance(); err != nil {
280-
return err
281-
}
282-
283-
return c.createInstance(d)
284-
}
285-
286-
// Kill deletes the GCE instance, but keeps the disk.
287-
func (d *Driver) Kill() error {
288-
return d.Stop()
297+
return nil
289298
}

0 commit comments

Comments
 (0)