2013-12-08 17:37:36 -05:00
|
|
|
package googlecompute
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"path/filepath"
|
|
|
|
|
|
|
|
"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.
|
|
|
|
//
|
|
|
|
// Currently the only way to create a GCE image is to run the gcimagebundle
|
|
|
|
// command on the running GCE instance.
|
2013-12-13 21:26:00 -05:00
|
|
|
func (s *StepCreateImage) Run(state multistep.StateBag) multistep.StepAction {
|
|
|
|
config := state.Get("config").(*Config)
|
|
|
|
comm := state.Get("communicator").(packer.Communicator)
|
|
|
|
ui := state.Get("ui").(packer.Ui)
|
|
|
|
|
|
|
|
sudoPrefix := ""
|
2013-12-08 17:37:36 -05:00
|
|
|
if config.SSHUsername != "root" {
|
|
|
|
sudoPrefix = "sudo "
|
|
|
|
}
|
2013-12-13 21:26:00 -05:00
|
|
|
|
2013-12-08 17:37:36 -05:00
|
|
|
imageFilename := fmt.Sprintf("%s.tar.gz", config.ImageName)
|
|
|
|
imageBundleCmd := "/usr/bin/gcimagebundle -d /dev/sda -o /tmp/"
|
2013-12-13 21:26:00 -05:00
|
|
|
|
|
|
|
ui.Say("Creating image...")
|
2013-12-08 17:37:36 -05:00
|
|
|
cmd := new(packer.RemoteCmd)
|
|
|
|
cmd.Command = fmt.Sprintf("%s%s --output_file_name %s",
|
|
|
|
sudoPrefix, imageBundleCmd, imageFilename)
|
|
|
|
err := cmd.StartWithUi(comm, ui)
|
2013-12-13 21:26:00 -05:00
|
|
|
if err == nil && cmd.ExitStatus != 0 {
|
|
|
|
err = fmt.Errorf(
|
|
|
|
"gcimagebundle exited with non-zero exit status: %d", cmd.ExitStatus)
|
|
|
|
}
|
2013-12-08 17:37:36 -05:00
|
|
|
if err != nil {
|
|
|
|
err := fmt.Errorf("Error creating image: %s", err)
|
|
|
|
state.Put("error", err)
|
|
|
|
ui.Error(err.Error())
|
|
|
|
return multistep.ActionHalt
|
|
|
|
}
|
2013-12-13 21:26:00 -05:00
|
|
|
|
2013-12-08 17:37:36 -05:00
|
|
|
state.Put("image_file_name", filepath.Join("/tmp", imageFilename))
|
|
|
|
return multistep.ActionContinue
|
|
|
|
}
|
|
|
|
|
2013-12-13 21:26:00 -05:00
|
|
|
func (s *StepCreateImage) Cleanup(state multistep.StateBag) {}
|