github.com/homburg/packer@v0.6.1-0.20140528012651-1dcaf1716848/builder/googlecompute/step_create_instance.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  // StepCreateInstance represents a Packer build step that creates GCE instances.
    13  type StepCreateInstance struct {
    14  	Debug bool
    15  
    16  	instanceName string
    17  }
    18  
    19  // Run executes the Packer build step that creates a GCE instance.
    20  func (s *StepCreateInstance) Run(state multistep.StateBag) multistep.StepAction {
    21  	config := state.Get("config").(*Config)
    22  	driver := state.Get("driver").(Driver)
    23  	sshPublicKey := state.Get("ssh_public_key").(string)
    24  	ui := state.Get("ui").(packer.Ui)
    25  
    26  	ui.Say("Creating instance...")
    27  	name := config.InstanceName
    28  
    29  	errCh, err := driver.RunInstance(&InstanceConfig{
    30  		Description: "New instance created by Packer",
    31  		Image:       config.SourceImage,
    32  		MachineType: config.MachineType,
    33  		Metadata: map[string]string{
    34  			"sshKeys": fmt.Sprintf("%s:%s", config.SSHUsername, sshPublicKey),
    35  		},
    36  		Name:    name,
    37  		Network: config.Network,
    38  		Tags:    config.Tags,
    39  		Zone:    config.Zone,
    40  	})
    41  
    42  	if err == nil {
    43  		ui.Message("Waiting for creation operation to complete...")
    44  		select {
    45  		case err = <-errCh:
    46  		case <-time.After(config.stateTimeout):
    47  			err = errors.New("time out while waiting for instance to create")
    48  		}
    49  	}
    50  
    51  	if err != nil {
    52  		err := fmt.Errorf("Error creating instance: %s", err)
    53  		state.Put("error", err)
    54  		ui.Error(err.Error())
    55  		return multistep.ActionHalt
    56  	}
    57  
    58  	ui.Message("Instance has been created!")
    59  
    60  	if s.Debug {
    61  		if name != "" {
    62  			ui.Message(fmt.Sprintf("Instance: %s started in %s", name, config.Zone))
    63  		}
    64  	}
    65  
    66  	// Things succeeded, store the name so we can remove it later
    67  	state.Put("instance_name", name)
    68  	s.instanceName = name
    69  
    70  	return multistep.ActionContinue
    71  }
    72  
    73  // Cleanup destroys the GCE instance created during the image creation process.
    74  func (s *StepCreateInstance) Cleanup(state multistep.StateBag) {
    75  	if s.instanceName == "" {
    76  		return
    77  	}
    78  
    79  	config := state.Get("config").(*Config)
    80  	driver := state.Get("driver").(Driver)
    81  	ui := state.Get("ui").(packer.Ui)
    82  
    83  	ui.Say("Deleting instance...")
    84  	errCh, err := driver.DeleteInstance(config.Zone, s.instanceName)
    85  	if err == nil {
    86  		select {
    87  		case err = <-errCh:
    88  		case <-time.After(config.stateTimeout):
    89  			err = errors.New("time out while waiting for instance to delete")
    90  		}
    91  	}
    92  
    93  	if err != nil {
    94  		ui.Error(fmt.Sprintf(
    95  			"Error deleting instance. Please delete it manually.\n\n"+
    96  				"Name: %s\n"+
    97  				"Error: %s", s.instanceName, err))
    98  	}
    99  
   100  	s.instanceName = ""
   101  	return
   102  }