2017-05-25 18:27:54 +08:00
|
|
|
package ecs
|
|
|
|
|
|
|
|
import (
|
2018-01-22 15:32:33 -08:00
|
|
|
"context"
|
2017-05-25 18:27:54 +08:00
|
|
|
"fmt"
|
2017-05-25 18:20:20 -07:00
|
|
|
|
2018-01-22 17:21:10 -08:00
|
|
|
"time"
|
|
|
|
|
2017-05-25 18:27:54 +08:00
|
|
|
"github.com/denverdino/aliyungo/common"
|
|
|
|
"github.com/denverdino/aliyungo/ecs"
|
2018-01-19 16:18:44 -08:00
|
|
|
"github.com/hashicorp/packer/helper/multistep"
|
2017-05-25 18:27:54 +08:00
|
|
|
"github.com/hashicorp/packer/packer"
|
|
|
|
)
|
|
|
|
|
2018-06-15 11:32:53 +07:00
|
|
|
type stepAttachKeyPair struct {
|
2017-05-25 18:27:54 +08:00
|
|
|
}
|
|
|
|
|
2019-03-29 16:50:02 +01:00
|
|
|
func (s *stepAttachKeyPair) Run(ctx context.Context, state multistep.StateBag) multistep.StepAction {
|
2017-05-25 18:27:54 +08:00
|
|
|
ui := state.Get("ui").(packer.Ui)
|
|
|
|
client := state.Get("client").(*ecs.Client)
|
2018-09-18 21:40:57 +08:00
|
|
|
config := state.Get("config").(*Config)
|
2017-05-25 18:27:54 +08:00
|
|
|
instance := state.Get("instance").(*ecs.InstanceAttributesType)
|
2017-07-21 07:44:32 +08:00
|
|
|
timeoutPoint := time.Now().Add(120 * time.Second)
|
2018-09-18 21:40:57 +08:00
|
|
|
keyPairName := config.Comm.SSHKeyPairName
|
2018-08-27 15:58:00 +02:00
|
|
|
if keyPairName == "" {
|
|
|
|
return multistep.ActionContinue
|
|
|
|
}
|
2017-05-26 10:23:36 +08:00
|
|
|
for {
|
2017-05-26 10:59:53 +08:00
|
|
|
err := client.AttachKeyPair(&ecs.AttachKeyPairArgs{RegionId: common.Region(config.AlicloudRegion),
|
|
|
|
KeyPairName: keyPairName, InstanceIds: "[\"" + instance.InstanceId + "\"]"})
|
2017-05-26 09:54:07 +08:00
|
|
|
if err != nil {
|
|
|
|
e, _ := err.(*common.Error)
|
2017-05-26 10:59:53 +08:00
|
|
|
if (!(e.Code == "MissingParameter" || e.Code == "DependencyViolation.WindowsInstance" ||
|
|
|
|
e.Code == "InvalidKeyPairName.NotFound" || e.Code == "InvalidRegionId.NotFound")) &&
|
2017-07-21 07:44:32 +08:00
|
|
|
time.Now().Before(timeoutPoint) {
|
2017-07-20 11:13:59 +08:00
|
|
|
time.Sleep(5 * time.Second)
|
2017-05-26 09:54:07 +08:00
|
|
|
continue
|
|
|
|
}
|
2017-05-26 10:59:53 +08:00
|
|
|
err := fmt.Errorf("Error attaching keypair %s to instance %s : %s",
|
|
|
|
keyPairName, instance.InstanceId, err)
|
2017-05-26 09:54:07 +08:00
|
|
|
state.Put("error", err)
|
|
|
|
ui.Error(err.Error())
|
|
|
|
return multistep.ActionHalt
|
|
|
|
}
|
|
|
|
break
|
2017-05-25 18:27:54 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
ui.Message(fmt.Sprintf("Attach keypair %s to instance: %s", keyPairName, instance.InstanceId))
|
|
|
|
|
|
|
|
return multistep.ActionContinue
|
|
|
|
}
|
|
|
|
|
2018-06-15 11:32:53 +07:00
|
|
|
func (s *stepAttachKeyPair) Cleanup(state multistep.StateBag) {
|
2017-05-25 18:27:54 +08:00
|
|
|
client := state.Get("client").(*ecs.Client)
|
2018-09-18 21:40:57 +08:00
|
|
|
config := state.Get("config").(*Config)
|
2017-05-25 18:27:54 +08:00
|
|
|
ui := state.Get("ui").(packer.Ui)
|
|
|
|
instance := state.Get("instance").(*ecs.InstanceAttributesType)
|
2018-08-28 16:10:39 +02:00
|
|
|
keyPairName := config.Comm.SSHKeyPairName
|
2018-08-27 15:58:00 +02:00
|
|
|
if keyPairName == "" {
|
|
|
|
return
|
|
|
|
}
|
2017-05-25 18:27:54 +08:00
|
|
|
|
2017-05-26 10:59:53 +08:00
|
|
|
err := client.DetachKeyPair(&ecs.DetachKeyPairArgs{RegionId: common.Region(config.AlicloudRegion),
|
|
|
|
KeyPairName: keyPairName, InstanceIds: "[\"" + instance.InstanceId + "\"]"})
|
2017-05-25 18:27:54 +08:00
|
|
|
if err != nil {
|
2017-05-26 10:59:53 +08:00
|
|
|
err := fmt.Errorf("Error Detaching keypair %s to instance %s : %s", keyPairName,
|
|
|
|
instance.InstanceId, err)
|
2017-05-25 18:27:54 +08:00
|
|
|
state.Put("error", err)
|
|
|
|
ui.Error(err.Error())
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
ui.Message(fmt.Sprintf("Detach keypair %s from instance: %s", keyPairName, instance.InstanceId))
|
|
|
|
|
|
|
|
}
|