2013-12-08 17:37:36 -05:00
|
|
|
package googlecompute
|
|
|
|
|
|
|
|
import (
|
2014-11-24 11:36:14 -05:00
|
|
|
"errors"
|
2013-12-08 17:37:36 -05:00
|
|
|
"fmt"
|
2014-11-24 11:36:14 -05:00
|
|
|
"time"
|
2013-12-08 17:37:36 -05:00
|
|
|
|
|
|
|
"github.com/mitchellh/multistep"
|
|
|
|
"github.com/mitchellh/packer/packer"
|
|
|
|
)
|
|
|
|
|
2013-12-13 21:26:00 -05:00
|
|
|
// StepCreateImage represents a Packer build step that creates GCE machine
|
2013-12-08 17:37:36 -05:00
|
|
|
// images.
|
2013-12-13 21:26:00 -05:00
|
|
|
type StepCreateImage int
|
2013-12-08 17:37:36 -05:00
|
|
|
|
|
|
|
// Run executes the Packer build step that creates a GCE machine image.
|
|
|
|
//
|
2014-11-24 11:36:14 -05:00
|
|
|
// The image is created from the persistent disk used by the instance. The
|
|
|
|
// instance must be deleted and the disk retained before doing this step.
|
2013-12-13 21:26:00 -05:00
|
|
|
func (s *StepCreateImage) Run(state multistep.StateBag) multistep.StepAction {
|
|
|
|
config := state.Get("config").(*Config)
|
2014-11-24 11:36:14 -05:00
|
|
|
driver := state.Get("driver").(Driver)
|
2013-12-13 21:26:00 -05:00
|
|
|
ui := state.Get("ui").(packer.Ui)
|
|
|
|
|
|
|
|
ui.Say("Creating image...")
|
2016-05-06 07:43:39 -04:00
|
|
|
errCh := driver.CreateImage(config.ImageName, config.ImageDescription, config.ImageFamily, config.Zone, config.DiskName)
|
2014-11-24 11:36:14 -05:00
|
|
|
var err error
|
|
|
|
select {
|
|
|
|
case err = <-errCh:
|
|
|
|
case <-time.After(config.stateTimeout):
|
|
|
|
err = errors.New("time out while waiting for image to register")
|
2013-12-13 21:26:00 -05:00
|
|
|
}
|
2014-11-24 11:36:14 -05:00
|
|
|
|
2013-12-08 17:37:36 -05:00
|
|
|
if err != nil {
|
2014-11-24 11:36:14 -05:00
|
|
|
err := fmt.Errorf("Error waiting for image: %s", err)
|
2013-12-08 17:37:36 -05:00
|
|
|
state.Put("error", err)
|
|
|
|
ui.Error(err.Error())
|
|
|
|
return multistep.ActionHalt
|
|
|
|
}
|
2013-12-13 21:26:00 -05:00
|
|
|
|
2014-11-24 11:36:14 -05:00
|
|
|
state.Put("image_name", config.ImageName)
|
2013-12-08 17:37:36 -05:00
|
|
|
return multistep.ActionContinue
|
|
|
|
}
|
|
|
|
|
2014-11-24 11:36:14 -05:00
|
|
|
// Cleanup.
|
2013-12-13 21:26:00 -05:00
|
|
|
func (s *StepCreateImage) Cleanup(state multistep.StateBag) {}
|