github.com/askholme/packer@v0.7.2-0.20140924152349-70d9566a6852/builder/amazon/common/step_key_pair.go (about)

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