github.com/StackPointCloud/packer@v0.10.2-0.20180716202532-b28098e0f79b/builder/alicloud/ecs/step_config_key_pair.go (about)

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