2013-12-08 17:37:36 -05:00
|
|
|
package googlecompute
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
|
|
|
|
"github.com/mitchellh/multistep"
|
|
|
|
"github.com/mitchellh/packer/packer"
|
|
|
|
)
|
|
|
|
|
2014-07-25 17:21:12 -04:00
|
|
|
// StepUpdateGcloud represents a Packer build step that updates the gsutil
|
2013-12-08 17:37:36 -05:00
|
|
|
// utility to the latest version available.
|
2014-07-25 17:21:12 -04:00
|
|
|
type StepUpdateGcloud int
|
2013-12-08 17:37:36 -05:00
|
|
|
|
|
|
|
// Run executes the Packer build step that updates the gsutil utility to the
|
|
|
|
// latest version available.
|
|
|
|
//
|
|
|
|
// This step is required to prevent the image creation process from hanging;
|
|
|
|
// the image creation process utilizes the gcimagebundle cli tool which will
|
|
|
|
// prompt to update gsutil if a newer version is available.
|
2014-07-25 17:21:12 -04:00
|
|
|
func (s *StepUpdateGcloud) Run(state multistep.StateBag) multistep.StepAction {
|
2013-12-13 21:19:21 -05:00
|
|
|
comm := state.Get("communicator").(packer.Communicator)
|
|
|
|
config := state.Get("config").(*Config)
|
|
|
|
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:19:21 -05:00
|
|
|
|
2014-06-26 07:50:06 -04:00
|
|
|
gsutilUpdateCmd := "/usr/local/bin/gcloud -q components update"
|
2013-12-08 17:37:36 -05:00
|
|
|
cmd := new(packer.RemoteCmd)
|
|
|
|
cmd.Command = fmt.Sprintf("%s%s", sudoPrefix, gsutilUpdateCmd)
|
2013-12-13 21:19:21 -05:00
|
|
|
|
2014-06-26 07:50:06 -04:00
|
|
|
ui.Say("Updating gcloud components...")
|
2013-12-08 17:37:36 -05:00
|
|
|
err := cmd.StartWithUi(comm, ui)
|
2013-12-13 21:19:21 -05:00
|
|
|
if err == nil && cmd.ExitStatus != 0 {
|
|
|
|
err = fmt.Errorf(
|
2014-06-26 07:50:06 -04:00
|
|
|
"gcloud components update exited with non-zero exit status: %d", cmd.ExitStatus)
|
2013-12-13 21:19:21 -05:00
|
|
|
}
|
2013-12-08 17:37:36 -05:00
|
|
|
if err != nil {
|
2014-06-26 07:50:06 -04:00
|
|
|
err := fmt.Errorf("Error updating gcloud components: %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:19:21 -05:00
|
|
|
|
2013-12-08 17:37:36 -05:00
|
|
|
return multistep.ActionContinue
|
|
|
|
}
|
|
|
|
|
|
|
|
// Cleanup.
|
2014-07-25 17:21:12 -04:00
|
|
|
func (s *StepUpdateGcloud) Cleanup(state multistep.StateBag) {}
|