github.com/rahart/packer@v0.12.2-0.20161229105310-282bb6ad370f/builder/amazon/common/step_key_pair.go (about)

     1  package common
     2  
     3  import (
     4  	"fmt"
     5  	"io/ioutil"
     6  	"os"
     7  	"runtime"
     8  
     9  	"github.com/aws/aws-sdk-go/service/ec2"
    10  	"github.com/mitchellh/multistep"
    11  	"github.com/mitchellh/packer/packer"
    12  )
    13  
    14  type StepKeyPair struct {
    15  	Debug                bool
    16  	SSHAgentAuth         bool
    17  	DebugKeyPath         string
    18  	TemporaryKeyPairName string
    19  	KeyPairName          string
    20  	PrivateKeyFile       string
    21  
    22  	keyName string
    23  }
    24  
    25  func (s *StepKeyPair) Run(state multistep.StateBag) multistep.StepAction {
    26  	ui := state.Get("ui").(packer.Ui)
    27  
    28  	if s.PrivateKeyFile != "" {
    29  		ui.Say("Using existing SSH private key")
    30  		privateKeyBytes, err := ioutil.ReadFile(s.PrivateKeyFile)
    31  		if err != nil {
    32  			state.Put("error", fmt.Errorf(
    33  				"Error loading configured private key file: %s", err))
    34  			return multistep.ActionHalt
    35  		}
    36  
    37  		state.Put("keyPair", s.KeyPairName)
    38  		state.Put("privateKey", string(privateKeyBytes))
    39  
    40  		return multistep.ActionContinue
    41  	}
    42  
    43  	if s.SSHAgentAuth && s.KeyPairName == "" {
    44  		ui.Say("Using SSH Agent with key pair in Source AMI")
    45  		return multistep.ActionContinue
    46  	}
    47  
    48  	if s.SSHAgentAuth && s.KeyPairName != "" {
    49  		ui.Say(fmt.Sprintf("Using SSH Agent for existing key pair %s", s.KeyPairName))
    50  		state.Put("keyPair", s.KeyPairName)
    51  		return multistep.ActionContinue
    52  	}
    53  
    54  	if s.TemporaryKeyPairName == "" {
    55  		ui.Say("Not using temporary keypair")
    56  		state.Put("keyPair", "")
    57  		return multistep.ActionContinue
    58  	}
    59  
    60  	ec2conn := state.Get("ec2").(*ec2.EC2)
    61  
    62  	ui.Say(fmt.Sprintf("Creating temporary keypair: %s", s.TemporaryKeyPairName))
    63  	keyResp, err := ec2conn.CreateKeyPair(&ec2.CreateKeyPairInput{
    64  		KeyName: &s.TemporaryKeyPairName})
    65  	if err != nil {
    66  		state.Put("error", fmt.Errorf("Error creating temporary keypair: %s", err))
    67  		return multistep.ActionHalt
    68  	}
    69  
    70  	// Set the keyname so we know to delete it later
    71  	s.keyName = s.TemporaryKeyPairName
    72  
    73  	// Set some state data for use in future steps
    74  	state.Put("keyPair", s.keyName)
    75  	state.Put("privateKey", *keyResp.KeyMaterial)
    76  
    77  	// If we're in debug mode, output the private key to the working
    78  	// directory.
    79  	if s.Debug {
    80  		ui.Message(fmt.Sprintf("Saving key for debug purposes: %s", s.DebugKeyPath))
    81  		f, err := os.Create(s.DebugKeyPath)
    82  		if err != nil {
    83  			state.Put("error", fmt.Errorf("Error saving debug key: %s", err))
    84  			return multistep.ActionHalt
    85  		}
    86  		defer f.Close()
    87  
    88  		// Write the key out
    89  		if _, err := f.Write([]byte(*keyResp.KeyMaterial)); err != nil {
    90  			state.Put("error", fmt.Errorf("Error saving debug key: %s", err))
    91  			return multistep.ActionHalt
    92  		}
    93  
    94  		// Chmod it so that it is SSH ready
    95  		if runtime.GOOS != "windows" {
    96  			if err := f.Chmod(0600); err != nil {
    97  				state.Put("error", fmt.Errorf("Error setting permissions of debug key: %s", err))
    98  				return multistep.ActionHalt
    99  			}
   100  		}
   101  	}
   102  
   103  	return multistep.ActionContinue
   104  }
   105  
   106  func (s *StepKeyPair) Cleanup(state multistep.StateBag) {
   107  	// If no key name is set, then we never created it, so just return
   108  	// If we used an SSH private key file, do not go about deleting
   109  	// keypairs
   110  	if s.PrivateKeyFile != "" || s.KeyPairName != "" {
   111  		return
   112  	}
   113  
   114  	ec2conn := state.Get("ec2").(*ec2.EC2)
   115  	ui := state.Get("ui").(packer.Ui)
   116  
   117  	// Remove the keypair
   118  	ui.Say("Deleting temporary keypair...")
   119  	_, err := ec2conn.DeleteKeyPair(&ec2.DeleteKeyPairInput{KeyName: &s.keyName})
   120  	if err != nil {
   121  		ui.Error(fmt.Sprintf(
   122  			"Error cleaning up keypair. Please delete the key manually: %s", s.keyName))
   123  	}
   124  
   125  	// Also remove the physical key if we're debugging.
   126  	if s.Debug {
   127  		if err := os.Remove(s.DebugKeyPath); err != nil {
   128  			ui.Error(fmt.Sprintf(
   129  				"Error removing debug key '%s': %s", s.DebugKeyPath, err))
   130  		}
   131  	}
   132  }