2017-05-25 06:27:54 -04:00
|
|
|
package ecs
|
|
|
|
|
|
|
|
import (
|
2018-01-22 18:32:33 -05:00
|
|
|
"context"
|
2017-05-25 06:27:54 -04:00
|
|
|
"fmt"
|
2019-04-26 03:12:07 -04:00
|
|
|
|
2019-04-25 22:37:49 -04:00
|
|
|
"github.com/aliyun/alibaba-cloud-sdk-go/sdk/responses"
|
|
|
|
"github.com/aliyun/alibaba-cloud-sdk-go/services/ecs"
|
2018-01-19 19:18:44 -05:00
|
|
|
"github.com/hashicorp/packer/helper/multistep"
|
2017-05-25 06:27:54 -04:00
|
|
|
"github.com/hashicorp/packer/packer"
|
|
|
|
)
|
|
|
|
|
2018-06-15 00:32:53 -04:00
|
|
|
type stepAttachKeyPair struct {
|
2017-05-25 06:27:54 -04:00
|
|
|
}
|
|
|
|
|
2019-04-25 22:37:49 -04:00
|
|
|
var attachKeyPairNotRetryErrors = []string{
|
|
|
|
"MissingParameter",
|
|
|
|
"DependencyViolation.WindowsInstance",
|
|
|
|
"InvalidKeyPairName.NotFound",
|
|
|
|
"InvalidRegionId.NotFound",
|
|
|
|
}
|
|
|
|
|
2019-03-29 11:50:02 -04:00
|
|
|
func (s *stepAttachKeyPair) Run(ctx context.Context, state multistep.StateBag) multistep.StepAction {
|
2017-05-25 06:27:54 -04:00
|
|
|
ui := state.Get("ui").(packer.Ui)
|
2019-04-25 22:37:49 -04:00
|
|
|
client := state.Get("client").(*ClientWrapper)
|
2018-09-18 09:40:57 -04:00
|
|
|
config := state.Get("config").(*Config)
|
2019-04-25 22:37:49 -04:00
|
|
|
instance := state.Get("instance").(*ecs.Instance)
|
2018-09-18 09:40:57 -04:00
|
|
|
keyPairName := config.Comm.SSHKeyPairName
|
2018-08-27 09:58:00 -04:00
|
|
|
if keyPairName == "" {
|
|
|
|
return multistep.ActionContinue
|
|
|
|
}
|
2019-04-25 22:37:49 -04:00
|
|
|
|
|
|
|
_, err := client.WaitForExpected(&WaitForExpectArgs{
|
|
|
|
RequestFunc: func() (responses.AcsResponse, error) {
|
|
|
|
request := ecs.CreateAttachKeyPairRequest()
|
|
|
|
request.RegionId = config.AlicloudRegion
|
|
|
|
request.KeyPairName = keyPairName
|
|
|
|
request.InstanceIds = "[\"" + instance.InstanceId + "\"]"
|
|
|
|
return client.AttachKeyPair(request)
|
|
|
|
},
|
|
|
|
EvalFunc: client.EvalCouldRetryResponse(attachKeyPairNotRetryErrors, EvalNotRetryErrorType),
|
|
|
|
})
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return halt(state, err, fmt.Sprintf("Error attaching keypair %s to instance %s", keyPairName, instance.InstanceId))
|
2017-05-25 06:27:54 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
ui.Message(fmt.Sprintf("Attach keypair %s to instance: %s", keyPairName, instance.InstanceId))
|
|
|
|
return multistep.ActionContinue
|
|
|
|
}
|
|
|
|
|
2018-06-15 00:32:53 -04:00
|
|
|
func (s *stepAttachKeyPair) Cleanup(state multistep.StateBag) {
|
2019-04-25 22:37:49 -04:00
|
|
|
client := state.Get("client").(*ClientWrapper)
|
2018-09-18 09:40:57 -04:00
|
|
|
config := state.Get("config").(*Config)
|
2017-05-25 06:27:54 -04:00
|
|
|
ui := state.Get("ui").(packer.Ui)
|
2019-04-25 22:37:49 -04:00
|
|
|
instance := state.Get("instance").(*ecs.Instance)
|
2018-08-28 10:10:39 -04:00
|
|
|
keyPairName := config.Comm.SSHKeyPairName
|
2018-08-27 09:58:00 -04:00
|
|
|
if keyPairName == "" {
|
|
|
|
return
|
|
|
|
}
|
2017-05-25 06:27:54 -04:00
|
|
|
|
2019-04-25 22:37:49 -04:00
|
|
|
detachKeyPairRequest := ecs.CreateDetachKeyPairRequest()
|
|
|
|
detachKeyPairRequest.RegionId = config.AlicloudRegion
|
|
|
|
detachKeyPairRequest.KeyPairName = keyPairName
|
|
|
|
detachKeyPairRequest.InstanceIds = fmt.Sprintf("[\"%s\"]", instance.InstanceId)
|
|
|
|
_, err := client.DetachKeyPair(detachKeyPairRequest)
|
2017-05-25 06:27:54 -04:00
|
|
|
if err != nil {
|
2017-05-25 22:59:53 -04:00
|
|
|
err := fmt.Errorf("Error Detaching keypair %s to instance %s : %s", keyPairName,
|
|
|
|
instance.InstanceId, err)
|
2017-05-25 06:27:54 -04:00
|
|
|
state.Put("error", err)
|
|
|
|
ui.Error(err.Error())
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
ui.Message(fmt.Sprintf("Detach keypair %s from instance: %s", keyPairName, instance.InstanceId))
|
|
|
|
|
|
|
|
}
|