2017-05-25 06:27:54 -04:00
|
|
|
package ecs
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2017-05-25 21:20:20 -04:00
|
|
|
|
2018-01-22 20:21:10 -05:00
|
|
|
"time"
|
|
|
|
|
2017-05-25 06:27:54 -04:00
|
|
|
"github.com/denverdino/aliyungo/common"
|
|
|
|
"github.com/denverdino/aliyungo/ecs"
|
|
|
|
"github.com/hashicorp/packer/packer"
|
|
|
|
"github.com/mitchellh/multistep"
|
|
|
|
)
|
|
|
|
|
|
|
|
type stepAttachKeyPar struct {
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *stepAttachKeyPar) Run(state multistep.StateBag) multistep.StepAction {
|
|
|
|
keyPairName := state.Get("keyPair").(string)
|
|
|
|
if keyPairName == "" {
|
|
|
|
return multistep.ActionContinue
|
|
|
|
}
|
|
|
|
ui := state.Get("ui").(packer.Ui)
|
|
|
|
client := state.Get("client").(*ecs.Client)
|
|
|
|
config := state.Get("config").(Config)
|
|
|
|
instance := state.Get("instance").(*ecs.InstanceAttributesType)
|
2017-07-20 19:44:32 -04:00
|
|
|
timeoutPoint := time.Now().Add(120 * time.Second)
|
2017-05-25 22:23:36 -04:00
|
|
|
for {
|
2017-05-25 22:59:53 -04:00
|
|
|
err := client.AttachKeyPair(&ecs.AttachKeyPairArgs{RegionId: common.Region(config.AlicloudRegion),
|
|
|
|
KeyPairName: keyPairName, InstanceIds: "[\"" + instance.InstanceId + "\"]"})
|
2017-05-25 21:54:07 -04:00
|
|
|
if err != nil {
|
|
|
|
e, _ := err.(*common.Error)
|
2017-05-25 22:59:53 -04:00
|
|
|
if (!(e.Code == "MissingParameter" || e.Code == "DependencyViolation.WindowsInstance" ||
|
|
|
|
e.Code == "InvalidKeyPairName.NotFound" || e.Code == "InvalidRegionId.NotFound")) &&
|
2017-07-20 19:44:32 -04:00
|
|
|
time.Now().Before(timeoutPoint) {
|
2017-07-19 23:13:59 -04:00
|
|
|
time.Sleep(5 * time.Second)
|
2017-05-25 21:54:07 -04:00
|
|
|
continue
|
|
|
|
}
|
2017-05-25 22:59:53 -04:00
|
|
|
err := fmt.Errorf("Error attaching keypair %s to instance %s : %s",
|
|
|
|
keyPairName, instance.InstanceId, err)
|
2017-05-25 21:54:07 -04:00
|
|
|
state.Put("error", err)
|
|
|
|
ui.Error(err.Error())
|
|
|
|
return multistep.ActionHalt
|
|
|
|
}
|
|
|
|
break
|
2017-05-25 06:27:54 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
ui.Message(fmt.Sprintf("Attach keypair %s to instance: %s", keyPairName, instance.InstanceId))
|
|
|
|
|
|
|
|
return multistep.ActionContinue
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *stepAttachKeyPar) Cleanup(state multistep.StateBag) {
|
|
|
|
keyPairName := state.Get("keyPair").(string)
|
|
|
|
if keyPairName == "" {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
client := state.Get("client").(*ecs.Client)
|
|
|
|
config := state.Get("config").(Config)
|
|
|
|
ui := state.Get("ui").(packer.Ui)
|
|
|
|
instance := state.Get("instance").(*ecs.InstanceAttributesType)
|
|
|
|
|
2017-05-25 22:59:53 -04:00
|
|
|
err := client.DetachKeyPair(&ecs.DetachKeyPairArgs{RegionId: common.Region(config.AlicloudRegion),
|
|
|
|
KeyPairName: keyPairName, InstanceIds: "[\"" + instance.InstanceId + "\"]"})
|
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))
|
|
|
|
|
|
|
|
}
|