2013-12-08 17:37:36 -05:00
|
|
|
package googlecompute
|
|
|
|
|
|
|
|
import (
|
2018-01-22 18:32:33 -05:00
|
|
|
"context"
|
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
|
|
|
|
2020-12-17 16:29:25 -05:00
|
|
|
"github.com/hashicorp/packer-plugin-sdk/multistep"
|
|
|
|
packersdk "github.com/hashicorp/packer-plugin-sdk/packer"
|
2013-12-08 17:37:36 -05:00
|
|
|
)
|
|
|
|
|
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.
|
2019-03-29 11:50:02 -04:00
|
|
|
func (s *StepCreateImage) Run(ctx context.Context, state multistep.StateBag) multistep.StepAction {
|
2019-05-05 11:12:30 -04:00
|
|
|
config := state.Get("config").(*Config)
|
2014-11-24 11:36:14 -05:00
|
|
|
driver := state.Get("driver").(Driver)
|
2020-11-19 14:54:31 -05:00
|
|
|
ui := state.Get("ui").(packersdk.Ui)
|
2013-12-13 21:26:00 -05:00
|
|
|
|
2020-10-16 18:42:30 -04:00
|
|
|
if config.SkipCreateImage {
|
2020-10-15 17:56:57 -04:00
|
|
|
ui.Say("Skipping image creation...")
|
|
|
|
return multistep.ActionContinue
|
|
|
|
}
|
|
|
|
|
2016-09-23 08:21:43 -04:00
|
|
|
if config.PackerForce && config.imageAlreadyExists {
|
|
|
|
ui.Say("Deleting previous image...")
|
|
|
|
|
|
|
|
errCh := driver.DeleteImage(config.ImageName)
|
|
|
|
err := <-errCh
|
|
|
|
if err != nil {
|
|
|
|
err := fmt.Errorf("Error deleting image: %s", err)
|
|
|
|
state.Put("error", err)
|
|
|
|
ui.Error(err.Error())
|
|
|
|
return multistep.ActionHalt
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-12-13 21:26:00 -05:00
|
|
|
ui.Say("Creating image...")
|
2016-05-24 20:13:36 -04:00
|
|
|
|
2019-05-05 11:12:30 -04:00
|
|
|
imageCh, errCh := driver.CreateImage(
|
|
|
|
config.ImageName, config.ImageDescription, config.ImageFamily, config.Zone,
|
2020-06-02 13:42:33 -04:00
|
|
|
config.DiskName, config.ImageLabels, config.ImageLicenses, config.ImageEncryptionKey.ComputeType(),
|
|
|
|
config.ImageStorageLocations)
|
2014-11-24 11:36:14 -05:00
|
|
|
var err error
|
|
|
|
select {
|
|
|
|
case err = <-errCh:
|
2019-10-31 10:49:34 -04:00
|
|
|
case <-time.After(config.StateTimeout):
|
2014-11-24 11:36:14 -05:00
|
|
|
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
|
|
|
|
2016-05-24 20:13:36 -04:00
|
|
|
state.Put("image", <-imageCh)
|
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) {}
|