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