mirror of
https://github.com/hashicorp/packer.git
synced 2026-03-03 14:03:01 -05:00
- Startup scripts can be provided through the instance creation metadata field 'startup-script'. - Script log can be copied to a GCS location by setting the metadata field 'startup-script-log-dest'. Added Retry method to googlecompute package. Added GetSerialPortOutput to googlecompute Drivers. Added StepWaitInstanceStartup (and associated test) which waits for an instance startup-script to finish. Changed the instance service account to use the same service account as the one provided in the Packer config template. It was the project default service account. Tested googlecompute package with 'go test' and also performed builds with a startup script and without a startup script.
50 lines
1.4 KiB
Go
50 lines
1.4 KiB
Go
package googlecompute
|
|
|
|
import(
|
|
"fmt"
|
|
"strings"
|
|
|
|
"github.com/mitchellh/multistep"
|
|
"github.com/mitchellh/packer/packer"
|
|
)
|
|
|
|
type StepWaitInstanceStartup int
|
|
|
|
// Run reads the instance serial port output and looks for the log entry indicating the startup script finished.
|
|
func (s *StepWaitInstanceStartup) Run(state multistep.StateBag) multistep.StepAction {
|
|
config := state.Get("config").(*Config)
|
|
driver := state.Get("driver").(Driver)
|
|
ui := state.Get("ui").(packer.Ui)
|
|
instanceName := state.Get("instance_name").(string)
|
|
|
|
ui.Say("Waiting for any running startup script to finish...")
|
|
|
|
// Keep checking the serial port output to see if the startup script is done.
|
|
err := Retry(10, 60, 0, func() (bool, error) {
|
|
output, err := driver.GetSerialPortOutput(config.Zone, instanceName)
|
|
|
|
if err != nil {
|
|
err := fmt.Errorf("Error getting serial port output: %s", err)
|
|
return false, err
|
|
}
|
|
|
|
done := strings.Contains(output, StartupScriptDoneLog)
|
|
if !done {
|
|
ui.Say("Startup script not finished yet. Waiting...")
|
|
}
|
|
|
|
return done, nil
|
|
})
|
|
|
|
if err != nil {
|
|
err := fmt.Errorf("Error waiting for startup script to finish: %s", err)
|
|
state.Put("error", err)
|
|
ui.Error(err.Error())
|
|
return multistep.ActionHalt
|
|
}
|
|
ui.Say("Startup script, if any, has finished running.")
|
|
return multistep.ActionContinue
|
|
}
|
|
|
|
// Cleanup.
|
|
func (s *StepWaitInstanceStartup) Cleanup(state multistep.StateBag) {}
|