github.com/sneal/packer@v0.5.2/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  func (s *StepInstanceInfo) Run(state multistep.StateBag) multistep.StepAction {
    21  	config := state.Get("config").(*Config)
    22  	driver := state.Get("driver").(Driver)
    23  	ui := state.Get("ui").(packer.Ui)
    24  
    25  	instanceName := state.Get("instance_name").(string)
    26  
    27  	ui.Say("Waiting for the instance to become running...")
    28  	errCh := driver.WaitForInstance("RUNNING", config.Zone, instanceName)
    29  	var err error
    30  	select {
    31  	case err = <-errCh:
    32  	case <-time.After(config.stateTimeout):
    33  		err = errors.New("time out while waiting for instance to become running")
    34  	}
    35  
    36  	if err != nil {
    37  		err := fmt.Errorf("Error waiting for instance: %s", err)
    38  		state.Put("error", err)
    39  		ui.Error(err.Error())
    40  		return multistep.ActionHalt
    41  	}
    42  
    43  	ip, err := driver.GetNatIP(config.Zone, instanceName)
    44  	if err != nil {
    45  		err := fmt.Errorf("Error retrieving instance nat 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("Public IP: %s", ip))
    54  		}
    55  	}
    56  
    57  	ui.Message(fmt.Sprintf("IP: %s", ip))
    58  	state.Put("instance_ip", ip)
    59  	return multistep.ActionContinue
    60  }
    61  
    62  // Cleanup.
    63  func (s *StepInstanceInfo) Cleanup(state multistep.StateBag) {}