|
|
|
|
@ -9,18 +9,16 @@ import (
|
|
|
|
|
|
|
|
|
|
"github.com/denverdino/aliyungo/common"
|
|
|
|
|
"github.com/denverdino/aliyungo/ecs"
|
|
|
|
|
"github.com/hashicorp/packer/helper/communicator"
|
|
|
|
|
"github.com/hashicorp/packer/helper/multistep"
|
|
|
|
|
"github.com/hashicorp/packer/packer"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
type stepConfigAlicloudKeyPair struct {
|
|
|
|
|
Debug bool
|
|
|
|
|
SSHAgentAuth bool
|
|
|
|
|
DebugKeyPath string
|
|
|
|
|
TemporaryKeyPairName string
|
|
|
|
|
KeyPairName string
|
|
|
|
|
PrivateKeyFile string
|
|
|
|
|
RegionId string
|
|
|
|
|
Debug bool
|
|
|
|
|
Comm *communicator.Config
|
|
|
|
|
DebugKeyPath string
|
|
|
|
|
RegionId string
|
|
|
|
|
|
|
|
|
|
keyName string
|
|
|
|
|
}
|
|
|
|
|
@ -29,33 +27,33 @@ func (s *stepConfigAlicloudKeyPair) Run(_ context.Context, state multistep.State
|
|
|
|
|
ui := state.Get("ui").(packer.Ui)
|
|
|
|
|
config := state.Get("config").(*Config)
|
|
|
|
|
|
|
|
|
|
if s.PrivateKeyFile != "" {
|
|
|
|
|
if s.Comm.SSHPrivateKeyFile != "" {
|
|
|
|
|
ui.Say("Using existing SSH private key")
|
|
|
|
|
privateKeyBytes, err := ioutil.ReadFile(s.PrivateKeyFile)
|
|
|
|
|
privateKeyBytes, err := ioutil.ReadFile(s.Comm.SSHPrivateKeyFile)
|
|
|
|
|
if err != nil {
|
|
|
|
|
state.Put("error", fmt.Errorf(
|
|
|
|
|
"Error loading configured private key file: %s", err))
|
|
|
|
|
return multistep.ActionHalt
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
config.Comm.SSHKeyPairName = s.KeyPairName
|
|
|
|
|
config.Comm.SSHKeyPairName = s.Comm.SSHKeyPairName
|
|
|
|
|
config.Comm.SSHPrivateKey = privateKeyBytes
|
|
|
|
|
|
|
|
|
|
return multistep.ActionContinue
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if s.SSHAgentAuth && s.KeyPairName == "" {
|
|
|
|
|
if s.Comm.SSHAgentAuth && s.Comm.SSHKeyPairName == "" {
|
|
|
|
|
ui.Say("Using SSH Agent with key pair in source image")
|
|
|
|
|
return multistep.ActionContinue
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if s.SSHAgentAuth && s.KeyPairName != "" {
|
|
|
|
|
ui.Say(fmt.Sprintf("Using SSH Agent for existing key pair %s", s.KeyPairName))
|
|
|
|
|
state.Put("keyPair", s.KeyPairName)
|
|
|
|
|
if s.Comm.SSHAgentAuth && s.Comm.SSHKeyPairName != "" {
|
|
|
|
|
ui.Say(fmt.Sprintf("Using SSH Agent for existing key pair %s", s.Comm.SSHKeyPairName))
|
|
|
|
|
state.Put("keyPair", s.Comm.SSHKeyPairName)
|
|
|
|
|
return multistep.ActionContinue
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if s.TemporaryKeyPairName == "" {
|
|
|
|
|
if s.Comm.SSHTemporaryKeyPairName == "" {
|
|
|
|
|
ui.Say("Not using temporary keypair")
|
|
|
|
|
state.Put("keyPair", "")
|
|
|
|
|
return multistep.ActionContinue
|
|
|
|
|
@ -63,9 +61,9 @@ func (s *stepConfigAlicloudKeyPair) Run(_ context.Context, state multistep.State
|
|
|
|
|
|
|
|
|
|
client := state.Get("client").(*ecs.Client)
|
|
|
|
|
|
|
|
|
|
ui.Say(fmt.Sprintf("Creating temporary keypair: %s", s.TemporaryKeyPairName))
|
|
|
|
|
ui.Say(fmt.Sprintf("Creating temporary keypair: %s", s.Comm.SSHTemporaryKeyPairName))
|
|
|
|
|
keyResp, err := client.CreateKeyPair(&ecs.CreateKeyPairArgs{
|
|
|
|
|
KeyPairName: s.TemporaryKeyPairName,
|
|
|
|
|
KeyPairName: s.Comm.SSHTemporaryKeyPairName,
|
|
|
|
|
RegionId: common.Region(s.RegionId),
|
|
|
|
|
})
|
|
|
|
|
if err != nil {
|
|
|
|
|
@ -74,7 +72,7 @@ func (s *stepConfigAlicloudKeyPair) Run(_ context.Context, state multistep.State
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Set the keyname so we know to delete it later
|
|
|
|
|
s.keyName = s.TemporaryKeyPairName
|
|
|
|
|
s.keyName = s.Comm.SSHTemporaryKeyPairName
|
|
|
|
|
|
|
|
|
|
// Set some state data for use in future steps
|
|
|
|
|
config.Comm.SSHKeyPairName = s.keyName
|
|
|
|
|
@ -113,7 +111,7 @@ func (s *stepConfigAlicloudKeyPair) Cleanup(state multistep.StateBag) {
|
|
|
|
|
// If no key name is set, then we never created it, so just return
|
|
|
|
|
// If we used an SSH private key file, do not go about deleting
|
|
|
|
|
// keypairs
|
|
|
|
|
if s.PrivateKeyFile != "" || (s.KeyPairName == "" && s.keyName == "") {
|
|
|
|
|
if s.Comm.SSHPrivateKeyFile != "" || (s.Comm.SSHKeyPairName == "" && s.keyName == "") {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|