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