add retry logic when attach keypair failed

This commit is contained in:
zhuzhih2017 2017-05-26 09:54:07 +08:00
parent a26c72f38e
commit 7b9f93c75f
5 changed files with 35 additions and 27 deletions

View File

@ -31,16 +31,16 @@ func TestBuilderAcc_basic(t *testing.T) {
// }) // })
//} //}
func TestBuilderAcc_regionCopy(t *testing.T) { //func TestBuilderAcc_regionCopy(t *testing.T) {
builderT.Test(t, builderT.TestCase{ // builderT.Test(t, builderT.TestCase{
PreCheck: func() { // PreCheck: func() {
testAccPreCheck(t) // testAccPreCheck(t)
}, // },
Builder: &Builder{}, // Builder: &Builder{},
Template: testBuilderAccRegionCopy, // Template: testBuilderAccRegionCopy,
Check: checkRegionCopy([]string{"cn-hangzhou", "cn-shenzhen"}), // Check: checkRegionCopy([]string{"cn-hangzhou", "cn-shenzhen"}),
}) // })
} //}
func TestBuilderAcc_forceDelete(t *testing.T) { func TestBuilderAcc_forceDelete(t *testing.T) {
// Build the same alicloud image twice, with ecs_image_force_delete on the second run // Build the same alicloud image twice, with ecs_image_force_delete on the second run
@ -230,7 +230,7 @@ const testBuilderAccBasic = `
"type": "test", "type": "test",
"region": "cn-beijing", "region": "cn-beijing",
"instance_type": "ecs.n1.tiny", "instance_type": "ecs.n1.tiny",
"source_image":"centos_7_2_64_40G_base_20170222.vhd", "source_image":"ubuntu_16_0402_64_40G_base_20170222.vhd",
"ssh_username": "ubuntu", "ssh_username": "ubuntu",
"io_optimized":"true", "io_optimized":"true",
"ssh_username":"root", "ssh_username":"root",
@ -244,7 +244,7 @@ const testBuilderAccRegionCopy = `
"type": "test", "type": "test",
"region": "cn-beijing", "region": "cn-beijing",
"instance_type": "ecs.n1.tiny", "instance_type": "ecs.n1.tiny",
"source_image":"centos_7_2_64_40G_base_20170222.vhd", "source_image":"ubuntu_16_0402_64_40G_base_20170222.vhd",
"io_optimized":"true", "io_optimized":"true",
"ssh_username":"root", "ssh_username":"root",
"image_name": "packer-test_{{timestamp}}", "image_name": "packer-test_{{timestamp}}",
@ -259,7 +259,7 @@ const testBuilderAccForceDelete = `
"type": "test", "type": "test",
"region": "cn-beijing", "region": "cn-beijing",
"instance_type": "ecs.n1.tiny", "instance_type": "ecs.n1.tiny",
"source_image":"centos_7_2_64_40G_base_20170222.vhd", "source_image":"ubuntu_16_0402_64_40G_base_20170222.vhd",
"io_optimized":"true", "io_optimized":"true",
"ssh_username":"root", "ssh_username":"root",
"image_force_delete": "%s", "image_force_delete": "%s",
@ -274,7 +274,7 @@ const testBuilderAccForceDeleteSnapshot = `
"type": "test", "type": "test",
"region": "cn-beijing", "region": "cn-beijing",
"instance_type": "ecs.n1.tiny", "instance_type": "ecs.n1.tiny",
"source_image":"centos_7_2_64_40G_base_20170222.vhd", "source_image":"ubuntu_16_0402_64_40G_base_20170222.vhd",
"io_optimized":"true", "io_optimized":"true",
"ssh_username":"root", "ssh_username":"root",
"image_force_delete_snapshots": "%s", "image_force_delete_snapshots": "%s",
@ -291,7 +291,7 @@ const testBuilderAccSharing = `
"type": "test", "type": "test",
"region": "cn-beijing", "region": "cn-beijing",
"instance_type": "ecs.n1.tiny", "instance_type": "ecs.n1.tiny",
"source_image":"centos_7_2_64_40G_base_20170222.vhd", "source_image":"ubuntu_16_0402_64_40G_base_20170222.vhd",
"io_optimized":"true", "io_optimized":"true",
"ssh_username":"root", "ssh_username":"root",
"image_name": "packer-test_{{timestamp}}", "image_name": "packer-test_{{timestamp}}",

View File

@ -20,15 +20,23 @@ func (s *stepAttachKeyPar) Run(state multistep.StateBag) multistep.StepAction {
client := state.Get("client").(*ecs.Client) client := state.Get("client").(*ecs.Client)
config := state.Get("config").(Config) config := state.Get("config").(Config)
instance := state.Get("instance").(*ecs.InstanceAttributesType) instance := state.Get("instance").(*ecs.InstanceAttributesType)
retry_times := 3
for{
err := client.AttachKeyPair(&ecs.AttachKeyPairArgs{RegionId: common.Region(config.AlicloudRegion), KeyPairName: keyPairName, err := client.AttachKeyPair(&ecs.AttachKeyPairArgs{RegionId: common.Region(config.AlicloudRegion), KeyPairName: keyPairName,
InstanceIds: "[\"" + instance.InstanceId + "\"]"}) InstanceIds: "[\"" + instance.InstanceId + "\"]"})
if err != nil { if err != nil {
e, _ := err.(*common.Error)
if( (!(e.Code == "MissingParameter" || e.Code == "DependencyViolation.WindowsInstance" || e.Code == "InvalidKeyPairName.NotFound" || e.Code == "InvalidRegionId.NotFound"))&& retry_times>0){
retry_times=retry_times-1
continue
}
err := fmt.Errorf("Error attaching keypair %s to instance %s : %s", keyPairName, instance.InstanceId, err) err := fmt.Errorf("Error attaching keypair %s to instance %s : %s", keyPairName, instance.InstanceId, err)
state.Put("error", err) state.Put("error", err)
ui.Error(err.Error()) ui.Error(err.Error())
return multistep.ActionHalt return multistep.ActionHalt
} }
break
}
ui.Message(fmt.Sprintf("Attach keypair %s to instance: %s", keyPairName, instance.InstanceId)) ui.Message(fmt.Sprintf("Attach keypair %s to instance: %s", keyPairName, instance.InstanceId))

View File

@ -60,7 +60,7 @@ func (s *StepConfigAlicloudKeyPair) Run(state multistep.StateBag) multistep.Step
client := state.Get("client").(*ecs.Client) client := state.Get("client").(*ecs.Client)
ui.Say(fmt.Sprintf("Start create temporary keypair: %s", s.TemporaryKeyPairName)) ui.Say(fmt.Sprintf("Start creating temporary keypair: %s", s.TemporaryKeyPairName))
keyResp, err := client.CreateKeyPair(&ecs.CreateKeyPairArgs{ keyResp, err := client.CreateKeyPair(&ecs.CreateKeyPairArgs{
KeyPairName: s.TemporaryKeyPairName, KeyPairName: s.TemporaryKeyPairName,
RegionId: common.Region(s.RegionId), RegionId: common.Region(s.RegionId),

View File

@ -44,7 +44,7 @@ func (s *stepConfigAlicloudVPC) Run(state multistep.StateBag) multistep.StepActi
return multistep.ActionHalt return multistep.ActionHalt
} }
ui.Say("Start create alicloud vpc") ui.Say("Start creating alicloud vpc")
vpc, err := client.CreateVpc(&ecs.CreateVpcArgs{ vpc, err := client.CreateVpc(&ecs.CreateVpcArgs{
RegionId: common.Region(config.AlicloudRegion), RegionId: common.Region(config.AlicloudRegion),
CidrBlock: s.CidrBlock, CidrBlock: s.CidrBlock,

View File

@ -9,7 +9,7 @@
"secret_key":"{{user `secret_key`}}", "secret_key":"{{user `secret_key`}}",
"region":"cn-beijing", "region":"cn-beijing",
"image_name":"packer_with_data_disk", "image_name":"packer_with_data_disk",
"source_image":"centos_7_2_64_40G_base_20170222.vhd", "source_image":"ubuntu_16_0402_64_40G_base_20170222.vhd",
"ssh_username":"root", "ssh_username":"root",
"instance_type":"ecs.n1.tiny", "instance_type":"ecs.n1.tiny",
"io_optimized":"true", "io_optimized":"true",
@ -19,7 +19,7 @@
"type": "shell", "type": "shell",
"inline": [ "inline": [
"sleep 30", "sleep 30",
"yum install redis.x86_64 -y" "apt-get update -yy"
] ]
}] }]
} }