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