2013-12-08 17:37:36 -05:00
|
|
|
package googlecompute
|
|
|
|
|
|
|
|
import (
|
2013-12-13 16:01:28 -05:00
|
|
|
"errors"
|
2013-12-08 17:37:36 -05:00
|
|
|
"fmt"
|
2013-12-13 16:01:28 -05:00
|
|
|
"time"
|
2013-12-08 17:37:36 -05:00
|
|
|
|
|
|
|
"github.com/mitchellh/multistep"
|
|
|
|
"github.com/mitchellh/packer/packer"
|
|
|
|
)
|
|
|
|
|
|
|
|
// stepInstanceInfo represents a Packer build step that gathers GCE instance info.
|
2014-01-31 07:33:23 -05:00
|
|
|
type StepInstanceInfo struct {
|
2014-01-31 11:55:58 -05:00
|
|
|
Debug bool
|
2014-02-21 18:10:09 -05:00
|
|
|
|
2014-02-21 18:11:56 -05:00
|
|
|
info int
|
2014-01-31 07:33:23 -05:00
|
|
|
}
|
2013-12-08 17:37:36 -05:00
|
|
|
|
|
|
|
// Run executes the Packer build step that gathers GCE instance info.
|
2016-05-24 20:13:36 -04:00
|
|
|
// This adds "instance_ip" to the multistep state.
|
2013-12-13 16:01:28 -05:00
|
|
|
func (s *StepInstanceInfo) Run(state multistep.StateBag) multistep.StepAction {
|
|
|
|
config := state.Get("config").(*Config)
|
|
|
|
driver := state.Get("driver").(Driver)
|
|
|
|
ui := state.Get("ui").(packer.Ui)
|
|
|
|
|
2013-12-08 17:37:36 -05:00
|
|
|
instanceName := state.Get("instance_name").(string)
|
2013-12-13 16:01:28 -05:00
|
|
|
|
|
|
|
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")
|
|
|
|
}
|
|
|
|
|
2013-12-08 17:37:36 -05:00
|
|
|
if err != nil {
|
2013-12-13 16:01:28 -05:00
|
|
|
err := fmt.Errorf("Error waiting for instance: %s", err)
|
2013-12-08 17:37:36 -05:00
|
|
|
state.Put("error", err)
|
|
|
|
ui.Error(err.Error())
|
|
|
|
return multistep.ActionHalt
|
|
|
|
}
|
2013-12-13 16:01:28 -05:00
|
|
|
|
2015-05-29 17:50:11 -04:00
|
|
|
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
|
|
|
|
}
|
2013-12-13 16:01:28 -05:00
|
|
|
|
2015-05-29 17:50:11 -04:00
|
|
|
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
|
2014-01-31 11:55:58 -05:00
|
|
|
}
|
2014-01-31 07:33:23 -05:00
|
|
|
|
2015-05-29 17:50:11 -04:00
|
|
|
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
|
|
|
|
}
|
2013-12-08 17:37:36 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// Cleanup.
|
2013-12-13 16:01:28 -05:00
|
|
|
func (s *StepInstanceInfo) Cleanup(state multistep.StateBag) {}
|