1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81
|
package googlecompute
import (
"context"
"errors"
"fmt"
"time"
"github.com/hashicorp/packer/packer-plugin-sdk/multistep"
packersdk "github.com/hashicorp/packer/packer-plugin-sdk/packer"
)
// stepInstanceInfo represents a Packer build step that gathers GCE instance info.
type StepInstanceInfo struct {
Debug bool
}
// Run executes the Packer build step that gathers GCE instance info.
// This adds "instance_ip" to the multistep state.
func (s *StepInstanceInfo) Run(ctx context.Context, state multistep.StateBag) multistep.StepAction {
config := state.Get("config").(*Config)
driver := state.Get("driver").(Driver)
ui := state.Get("ui").(packersdk.Ui)
instanceName := state.Get("instance_name").(string)
ui.Say("Waiting for the instance to become running...")
errCh := driver.WaitForInstance("RUNNING", config.Zone, instanceName)
var err error
select {
case err = <-errCh:
case <-time.After(config.StateTimeout):
err = errors.New("time out while waiting for instance to become running")
}
if err != nil {
err := fmt.Errorf("Error waiting for instance: %s", err)
state.Put("error", err)
ui.Error(err.Error())
return multistep.ActionHalt
}
if config.UseInternalIP {
ip, err := driver.GetInternalIP(config.Zone, instanceName)
if err != nil {
err := fmt.Errorf("Error retrieving instance internal ip address: %s", err)
state.Put("error", err)
ui.Error(err.Error())
return multistep.ActionHalt
}
if s.Debug {
if ip != "" {
ui.Message(fmt.Sprintf("Internal IP: %s", ip))
}
}
ui.Message(fmt.Sprintf("IP: %s", ip))
state.Put("instance_ip", ip)
return multistep.ActionContinue
} else {
ip, err := driver.GetNatIP(config.Zone, instanceName)
if err != nil {
err := fmt.Errorf("Error retrieving instance nat ip address: %s", err)
state.Put("error", err)
ui.Error(err.Error())
return multistep.ActionHalt
}
if s.Debug {
if ip != "" {
ui.Message(fmt.Sprintf("Public IP: %s", ip))
}
}
ui.Message(fmt.Sprintf("IP: %s", ip))
state.Put("instance_ip", ip)
return multistep.ActionContinue
}
}
// Cleanup.
func (s *StepInstanceInfo) Cleanup(state multistep.StateBag) {}
|