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