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/packer"
|
|
|
|
)
|
|
|
|
|
2013-12-13 00:38:34 -05:00
|
|
|
// StepCreateInstance represents a Packer build step that creates GCE instances.
|
|
|
|
type StepCreateInstance struct {
|
2014-02-21 18:10:09 -05:00
|
|
|
Debug bool
|
2013-12-08 17:37:36 -05:00
|
|
|
}
|
|
|
|
|
2014-09-02 12:55:06 -04:00
|
|
|
func (config *Config) getImage() Image {
|
2014-08-20 13:20:28 -04:00
|
|
|
project := config.ProjectId
|
|
|
|
if config.SourceImageProjectId != "" {
|
|
|
|
project = config.SourceImageProjectId
|
|
|
|
}
|
|
|
|
return Image{Name: config.SourceImage, ProjectId: project}
|
|
|
|
}
|
|
|
|
|
2014-10-06 17:18:34 -04:00
|
|
|
func (config *Config) getInstanceMetadata(sshPublicKey string) map[string]string {
|
|
|
|
instanceMetadata := make(map[string]string)
|
|
|
|
|
|
|
|
// Copy metadata from config
|
|
|
|
for k, v := range config.Metadata {
|
|
|
|
instanceMetadata[k] = v
|
|
|
|
}
|
|
|
|
|
|
|
|
// Merge any existing ssh keys with our public key
|
|
|
|
sshMetaKey := "sshKeys"
|
2015-06-13 18:30:16 -04:00
|
|
|
sshKeys := fmt.Sprintf("%s:%s", config.Comm.SSHUsername, sshPublicKey)
|
2014-10-06 17:18:34 -04:00
|
|
|
if confSshKeys, exists := instanceMetadata[sshMetaKey]; exists {
|
|
|
|
sshKeys = fmt.Sprintf("%s\n%s", sshKeys, confSshKeys)
|
|
|
|
}
|
|
|
|
instanceMetadata[sshMetaKey] = sshKeys
|
|
|
|
|
|
|
|
return instanceMetadata
|
|
|
|
}
|
|
|
|
|
2013-12-08 17:37:36 -05:00
|
|
|
// 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...")
|
2014-04-25 22:14:53 -04:00
|
|
|
name := config.InstanceName
|
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",
|
2014-08-07 15:34:08 -04:00
|
|
|
DiskSizeGb: config.DiskSizeGb,
|
2014-08-20 13:20:28 -04:00
|
|
|
Image: config.getImage(),
|
2013-12-13 00:38:34 -05:00
|
|
|
MachineType: config.MachineType,
|
2014-10-28 11:35:21 -04:00
|
|
|
Metadata: config.getInstanceMetadata(sshPublicKey),
|
|
|
|
Name: name,
|
|
|
|
Network: config.Network,
|
|
|
|
Tags: config.Tags,
|
|
|
|
Zone: config.Zone,
|
2013-12-13 00:38:34 -05:00
|
|
|
})
|
2013-12-08 17:37:36 -05:00
|
|
|
|
2013-12-13 00:38:34 -05:00
|
|
|
if err == nil {
|
2013-12-13 01:23:00 -05:00
|
|
|
ui.Message("Waiting for creation operation to complete...")
|
2013-12-13 00:38:34 -05:00
|
|
|
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!")
|
|
|
|
|
2014-01-31 11:55:58 -05:00
|
|
|
if s.Debug {
|
|
|
|
if name != "" {
|
|
|
|
ui.Message(fmt.Sprintf("Instance: %s started in %s", name, config.Zone))
|
|
|
|
}
|
|
|
|
}
|
2014-01-31 07:33:23 -05:00
|
|
|
|
2013-12-13 00:38:34 -05:00
|
|
|
// Things succeeded, store the name so we can remove it later
|
2013-12-08 17:37:36 -05:00
|
|
|
state.Put("instance_name", 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) {
|
2014-11-24 11:36:14 -05:00
|
|
|
nameRaw, ok := state.GetOk("instance_name")
|
|
|
|
if !ok {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
name := nameRaw.(string)
|
|
|
|
if name == "" {
|
2013-12-08 17:37:36 -05:00
|
|
|
return
|
|
|
|
}
|
2013-12-13 01:34:47 -05:00
|
|
|
|
|
|
|
config := state.Get("config").(*Config)
|
|
|
|
driver := state.Get("driver").(Driver)
|
|
|
|
ui := state.Get("ui").(packer.Ui)
|
|
|
|
|
|
|
|
ui.Say("Deleting instance...")
|
2014-11-24 11:36:14 -05:00
|
|
|
errCh, err := driver.DeleteInstance(config.Zone, name)
|
2013-12-13 01:34:47 -05:00
|
|
|
if err == nil {
|
|
|
|
select {
|
|
|
|
case err = <-errCh:
|
|
|
|
case <-time.After(config.stateTimeout):
|
|
|
|
err = errors.New("time out while waiting for instance to delete")
|
2013-12-08 17:37:36 -05:00
|
|
|
}
|
2013-12-13 01:34:47 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
ui.Error(fmt.Sprintf(
|
|
|
|
"Error deleting instance. Please delete it manually.\n\n"+
|
|
|
|
"Name: %s\n"+
|
2014-11-24 11:36:14 -05:00
|
|
|
"Error: %s", name, err))
|
2013-12-13 01:34:47 -05:00
|
|
|
}
|
|
|
|
|
2015-04-21 10:40:55 -04:00
|
|
|
ui.Message("Instance has been deleted!")
|
2014-11-24 11:36:14 -05:00
|
|
|
state.Put("instance_name", "")
|
2015-04-21 10:40:55 -04:00
|
|
|
|
|
|
|
// Deleting the instance does not remove the boot disk. This cleanup removes
|
|
|
|
// the disk.
|
|
|
|
ui.Say("Deleting disk...")
|
|
|
|
errCh, err = driver.DeleteDisk(config.Zone, config.DiskName)
|
|
|
|
if err == nil {
|
|
|
|
select {
|
|
|
|
case err = <-errCh:
|
|
|
|
case <-time.After(config.stateTimeout):
|
|
|
|
err = errors.New("time out while waiting for disk to delete")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
ui.Error(fmt.Sprintf(
|
|
|
|
"Error deleting disk. Please delete it manually.\n\n"+
|
|
|
|
"Name: %s\n"+
|
|
|
|
"Error: %s", config.InstanceName, err))
|
|
|
|
}
|
|
|
|
|
|
|
|
ui.Message("Disk has been deleted!")
|
|
|
|
|
2013-12-08 17:37:36 -05:00
|
|
|
return
|
|
|
|
}
|