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