2013-12-08 17:37:36 -05:00
|
|
|
package googlecompute
|
|
|
|
|
|
|
|
import (
|
2018-01-22 18:32:33 -05:00
|
|
|
"context"
|
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
|
|
|
|
2018-01-19 19:18:44 -05:00
|
|
|
"github.com/hashicorp/packer/helper/multistep"
|
2017-04-04 16:39:01 -04:00
|
|
|
"github.com/hashicorp/packer/packer"
|
2013-12-08 17:37:36 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
// 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-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.
|
2019-03-29 11:50:02 -04:00
|
|
|
func (s *StepInstanceInfo) Run(ctx context.Context, state multistep.StateBag) multistep.StepAction {
|
2013-12-13 16:01:28 -05:00
|
|
|
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) {}
|