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