github.com/kaixiang/packer@v0.5.2-0.20140114230416-1f5786b0d7f1/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 int
    14  
    15  // Run executes the Packer build step that gathers GCE instance info.
    16  func (s *StepInstanceInfo) Run(state multistep.StateBag) multistep.StepAction {
    17  	config := state.Get("config").(*Config)
    18  	driver := state.Get("driver").(Driver)
    19  	ui := state.Get("ui").(packer.Ui)
    20  
    21  	instanceName := state.Get("instance_name").(string)
    22  
    23  	ui.Say("Waiting for the instance to become running...")
    24  	errCh := driver.WaitForInstance("RUNNING", config.Zone, instanceName)
    25  	var err error
    26  	select {
    27  	case err = <-errCh:
    28  	case <-time.After(config.stateTimeout):
    29  		err = errors.New("time out while waiting for instance to become running")
    30  	}
    31  
    32  	if err != nil {
    33  		err := fmt.Errorf("Error waiting for instance: %s", err)
    34  		state.Put("error", err)
    35  		ui.Error(err.Error())
    36  		return multistep.ActionHalt
    37  	}
    38  
    39  	ip, err := driver.GetNatIP(config.Zone, instanceName)
    40  	if err != nil {
    41  		err := fmt.Errorf("Error retrieving instance nat ip address: %s", err)
    42  		state.Put("error", err)
    43  		ui.Error(err.Error())
    44  		return multistep.ActionHalt
    45  	}
    46  
    47  	ui.Message(fmt.Sprintf("IP: %s", ip))
    48  	state.Put("instance_ip", ip)
    49  	return multistep.ActionContinue
    50  }
    51  
    52  // Cleanup.
    53  func (s *StepInstanceInfo) Cleanup(state multistep.StateBag) {}