packer-cn/builder/googlecompute/step_create_image.go

64 lines
1.7 KiB
Go
Raw Normal View History

package googlecompute
import (
"context"
"errors"
"fmt"
"time"
"github.com/hashicorp/packer/helper/multistep"
2017-04-04 16:39:01 -04:00
"github.com/hashicorp/packer/packer"
)
2013-12-13 21:26:00 -05:00
// StepCreateImage represents a Packer build step that creates GCE machine
// images.
2013-12-13 21:26:00 -05:00
type StepCreateImage int
// Run executes the Packer build step that creates a GCE machine image.
//
// 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.
func (s *StepCreateImage) Run(_ context.Context, state multistep.StateBag) multistep.StepAction {
2013-12-13 21:26:00 -05:00
config := state.Get("config").(*Config)
driver := state.Get("driver").(Driver)
2013-12-13 21:26:00 -05:00
ui := state.Get("ui").(packer.Ui)
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...")
Some googlecompute fixes and cleanup. Addresses https://github.com/mitchellh/packer/issues/3829. Changes: - startup scripts don't run for Windows since it is isn't implemented yet. - startup scripts use instance metadata instead of serial port output to flag when they are done. - added licenses to Image data type (to check if an Image is a Windows Image). - added GetImage and GetImageFromProject to googlecompute Drivers. - changed some of the builder/googlecompute tests to use github.com/stretchr/testify/assert. Tests: - (in the Packer directory) `go test .`, `go test ./builder/googlecompute`, and `go test ./post-processor/googlecompute-export` - manual run of `packer build packer_template.json` with the following files --packer_template.json-- { "builders": [ { "type": "googlecompute", "account_file": "creds.json", "project_id": "google.com:packer-test", "source_image": "debian-8-jessie-v20160629", "zone": "us-central1-a", "startup_script_file": "startup_script.sh", "metadata": { "startup-script": "#!/bin/sh\necho \"This should be overwritten.\"", "startup-script-log-dest": "gs://packer-test.google.com.a.appspot.com/startup-script.log" }, "image_name": "test-packer-modifications", "ssh_username": "foo" } ], "post-processors": [ { "type": "googlecompute-export", "paths": [ "gs://packer-test.google.com.a.appspot.com/foo.tar.gz", "gs://packer-test.google.com.a.appspot.com/bar.tar.gz" ], "keep_input_artifact": true } ] } --startup_script.sh-- \#!/bin/sh echo "Hi, my name is Scott. I'm waiting 60 seconds!" >> /scott sleep 60 echo "I'm done waiting!" >> /scott
2016-09-07 22:00:30 -04:00
imageCh, errCh := driver.CreateImage(
config.ImageName, config.ImageDescription, config.ImageFamily, config.Zone,
config.DiskName, config.ImageLabels, config.ImageLicenses)
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
}
if err != nil {
err := fmt.Errorf("Error waiting for image: %s", err)
state.Put("error", err)
ui.Error(err.Error())
return multistep.ActionHalt
}
2013-12-13 21:26:00 -05:00
state.Put("image", <-imageCh)
return multistep.ActionContinue
}
// Cleanup.
2013-12-13 21:26:00 -05:00
func (s *StepCreateImage) Cleanup(state multistep.StateBag) {}