github.com/sneal/packer@v0.5.2/builder/openstack/step_key_pair.go (about)

     1  package openstack
     2  
     3  import (
     4  	"fmt"
     5  	"github.com/mitchellh/multistep"
     6  	"github.com/mitchellh/packer/common/uuid"
     7  	"github.com/mitchellh/packer/packer"
     8  	"github.com/rackspace/gophercloud"
     9  	"log"
    10  	"os"
    11  	"runtime"
    12  )
    13  
    14  type StepKeyPair struct {
    15  	Debug        bool
    16  	DebugKeyPath string
    17  	keyName      string
    18  }
    19  
    20  func (s *StepKeyPair) Run(state multistep.StateBag) multistep.StepAction {
    21  	csp := state.Get("csp").(gophercloud.CloudServersProvider)
    22  	ui := state.Get("ui").(packer.Ui)
    23  
    24  	ui.Say("Creating temporary keypair for this instance...")
    25  	keyName := fmt.Sprintf("packer %s", uuid.TimeOrderedUUID())
    26  	log.Printf("temporary keypair name: %s", keyName)
    27  	keyResp, err := csp.CreateKeyPair(gophercloud.NewKeyPair{Name: keyName})
    28  	if err != nil {
    29  		state.Put("error", fmt.Errorf("Error creating temporary keypair: %s", err))
    30  		return multistep.ActionHalt
    31  	}
    32  
    33  	// If we're in debug mode, output the private key to the working
    34  	// directory.
    35  	if s.Debug {
    36  		ui.Message(fmt.Sprintf("Saving key for debug purposes: %s", s.DebugKeyPath))
    37  		f, err := os.Create(s.DebugKeyPath)
    38  		if err != nil {
    39  			state.Put("error", fmt.Errorf("Error saving debug key: %s", err))
    40  			return multistep.ActionHalt
    41  		}
    42  		defer f.Close()
    43  
    44  		// Write the key out
    45  		if _, err := f.Write([]byte(keyResp.PrivateKey)); err != nil {
    46  			state.Put("error", fmt.Errorf("Error saving debug key: %s", err))
    47  			return multistep.ActionHalt
    48  		}
    49  
    50  		// Chmod it so that it is SSH ready
    51  		if runtime.GOOS != "windows" {
    52  			if err := f.Chmod(0600); err != nil {
    53  				state.Put("error", fmt.Errorf("Error setting permissions of debug key: %s", err))
    54  				return multistep.ActionHalt
    55  			}
    56  		}
    57  	}
    58  
    59  	// Set the keyname so we know to delete it later
    60  	s.keyName = keyName
    61  
    62  	// Set some state data for use in future steps
    63  	state.Put("keyPair", keyName)
    64  	state.Put("privateKey", keyResp.PrivateKey)
    65  
    66  	return multistep.ActionContinue
    67  }
    68  
    69  func (s *StepKeyPair) Cleanup(state multistep.StateBag) {
    70  	// If no key name is set, then we never created it, so just return
    71  	if s.keyName == "" {
    72  		return
    73  	}
    74  
    75  	csp := state.Get("csp").(gophercloud.CloudServersProvider)
    76  	ui := state.Get("ui").(packer.Ui)
    77  
    78  	ui.Say("Deleting temporary keypair...")
    79  	err := csp.DeleteKeyPair(s.keyName)
    80  	if err != nil {
    81  		ui.Error(fmt.Sprintf(
    82  			"Error cleaning up keypair. Please delete the key manually: %s", s.keyName))
    83  	}
    84  }