packer-cn/builder/googlecompute/step_create_image.go

53 lines
1.5 KiB
Go
Raw Normal View History

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
// images.
2013-12-13 21:26:00 -05:00
type StepCreateImage int
// 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 := ""
if config.SSHUsername != "root" {
sudoPrefix = "sudo "
}
2013-12-13 21:26:00 -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...")
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)
}
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
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) {}