2013-12-08 17:37:36 -05:00
|
|
|
package googlecompute
|
|
|
|
|
|
|
|
import (
|
2013-12-13 00:38:34 -05:00
|
|
|
"errors"
|
2013-12-08 17:37:36 -05:00
|
|
|
"fmt"
|
2013-12-13 00:38:34 -05:00
|
|
|
"time"
|
2013-12-08 17:37:36 -05:00
|
|
|
|
|
|
|
"github.com/mitchellh/multistep"
|
|
|
|
"github.com/mitchellh/packer/common/uuid"
|
|
|
|
"github.com/mitchellh/packer/packer"
|
|
|
|
)
|
|
|
|
|
2013-12-13 00:38:34 -05:00
|
|
|
// StepCreateInstance represents a Packer build step that creates GCE instances.
|
|
|
|
type StepCreateInstance struct {
|
2013-12-08 17:37:36 -05:00
|
|
|
instanceName string
|
|
|
|
}
|
|
|
|
|
|
|
|
// Run executes the Packer build step that creates a GCE instance.
|
2013-12-13 00:38:34 -05:00
|
|
|
func (s *StepCreateInstance) Run(state multistep.StateBag) multistep.StepAction {
|
|
|
|
config := state.Get("config").(*Config)
|
|
|
|
driver := state.Get("driver").(Driver)
|
|
|
|
sshPublicKey := state.Get("ssh_public_key").(string)
|
|
|
|
ui := state.Get("ui").(packer.Ui)
|
|
|
|
|
2013-12-08 17:37:36 -05:00
|
|
|
ui.Say("Creating instance...")
|
|
|
|
name := fmt.Sprintf("packer-%s", uuid.TimeOrderedUUID())
|
2013-12-13 00:38:34 -05:00
|
|
|
|
|
|
|
errCh, err := driver.RunInstance(&InstanceConfig{
|
2013-12-08 17:37:36 -05:00
|
|
|
Description: "New instance created by Packer",
|
2013-12-13 00:38:34 -05:00
|
|
|
Image: config.SourceImage,
|
|
|
|
MachineType: config.MachineType,
|
|
|
|
Metadata: map[string]string{
|
|
|
|
"sshKeys": fmt.Sprintf("%s:%s", config.SSHUsername, sshPublicKey),
|
|
|
|
},
|
|
|
|
Name: name,
|
|
|
|
Network: config.Network,
|
|
|
|
Tags: config.Tags,
|
|
|
|
Zone: config.Zone,
|
|
|
|
})
|
2013-12-08 17:37:36 -05:00
|
|
|
|
2013-12-13 00:38:34 -05:00
|
|
|
if err == nil {
|
|
|
|
select {
|
|
|
|
case err = <-errCh:
|
|
|
|
case <-time.After(config.stateTimeout):
|
|
|
|
err = errors.New("time out while waiting for instance to create")
|
|
|
|
}
|
2013-12-08 17:37:36 -05:00
|
|
|
}
|
2013-12-13 00:38:34 -05:00
|
|
|
|
2013-12-08 17:37:36 -05:00
|
|
|
if err != nil {
|
|
|
|
err := fmt.Errorf("Error creating instance: %s", err)
|
|
|
|
state.Put("error", err)
|
|
|
|
ui.Error(err.Error())
|
|
|
|
return multistep.ActionHalt
|
|
|
|
}
|
2013-12-13 00:38:34 -05:00
|
|
|
|
|
|
|
ui.Message("Instance has been created!")
|
|
|
|
|
|
|
|
// Things succeeded, store the name so we can remove it later
|
2013-12-08 17:37:36 -05:00
|
|
|
state.Put("instance_name", name)
|
|
|
|
s.instanceName = name
|
2013-12-13 00:38:34 -05:00
|
|
|
|
2013-12-08 17:37:36 -05:00
|
|
|
return multistep.ActionContinue
|
|
|
|
}
|
|
|
|
|
|
|
|
// Cleanup destroys the GCE instance created during the image creation process.
|
2013-12-13 00:38:34 -05:00
|
|
|
func (s *StepCreateInstance) Cleanup(state multistep.StateBag) {
|
2013-12-08 17:37:36 -05:00
|
|
|
if s.instanceName == "" {
|
|
|
|
return
|
|
|
|
}
|
2013-12-13 00:38:34 -05:00
|
|
|
/*
|
|
|
|
var (
|
|
|
|
client = state.Get("client").(*GoogleComputeClient)
|
|
|
|
config = state.Get("config").(*Config)
|
|
|
|
ui = state.Get("ui").(packer.Ui)
|
|
|
|
)
|
|
|
|
ui.Say("Destroying instance...")
|
|
|
|
operation, err := client.DeleteInstance(config.Zone, s.instanceName)
|
2013-12-08 17:37:36 -05:00
|
|
|
if err != nil {
|
|
|
|
ui.Error(fmt.Sprintf("Error destroying instance. Please destroy it manually: %v", s.instanceName))
|
|
|
|
}
|
2013-12-13 00:38:34 -05:00
|
|
|
ui.Say("Waiting for the instance to be deleted...")
|
|
|
|
for {
|
|
|
|
status, err := client.ZoneOperationStatus(config.Zone, operation.Name)
|
|
|
|
if err != nil {
|
|
|
|
ui.Error(fmt.Sprintf("Error destroying instance. Please destroy it manually: %v", s.instanceName))
|
|
|
|
}
|
|
|
|
if status == "DONE" {
|
|
|
|
break
|
|
|
|
}
|
2013-12-08 17:37:36 -05:00
|
|
|
}
|
2013-12-13 00:38:34 -05:00
|
|
|
*/
|
2013-12-08 17:37:36 -05:00
|
|
|
return
|
|
|
|
}
|