From 7b9f93c75fa836875f516154617fb0fb39f86658 Mon Sep 17 00:00:00 2001 From: zhuzhih2017 Date: Fri, 26 May 2017 09:54:07 +0800 Subject: [PATCH] add retry logic when attach keypair failed --- builder/alicloud/ecs/builder_acc_test.go | 30 +++++++++---------- builder/alicloud/ecs/step_attach_keypair.go | 24 ++++++++++----- .../ecs/step_config_alicloud_key_pair.go | 2 +- .../alicloud/ecs/step_config_alicloud_vpc.go | 2 +- .../basic/alicloud_with_data_disk.json | 4 +-- 5 files changed, 35 insertions(+), 27 deletions(-) diff --git a/builder/alicloud/ecs/builder_acc_test.go b/builder/alicloud/ecs/builder_acc_test.go index 471496954..7e5c96f8f 100644 --- a/builder/alicloud/ecs/builder_acc_test.go +++ b/builder/alicloud/ecs/builder_acc_test.go @@ -31,16 +31,16 @@ func TestBuilderAcc_basic(t *testing.T) { // }) //} -func TestBuilderAcc_regionCopy(t *testing.T) { - builderT.Test(t, builderT.TestCase{ - PreCheck: func() { - testAccPreCheck(t) - }, - Builder: &Builder{}, - Template: testBuilderAccRegionCopy, - Check: checkRegionCopy([]string{"cn-hangzhou", "cn-shenzhen"}), - }) -} +//func TestBuilderAcc_regionCopy(t *testing.T) { +// builderT.Test(t, builderT.TestCase{ +// PreCheck: func() { +// testAccPreCheck(t) +// }, +// Builder: &Builder{}, +// Template: testBuilderAccRegionCopy, +// Check: checkRegionCopy([]string{"cn-hangzhou", "cn-shenzhen"}), +// }) +//} func TestBuilderAcc_forceDelete(t *testing.T) { // Build the same alicloud image twice, with ecs_image_force_delete on the second run @@ -230,7 +230,7 @@ const testBuilderAccBasic = ` "type": "test", "region": "cn-beijing", "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", "io_optimized":"true", "ssh_username":"root", @@ -244,7 +244,7 @@ const testBuilderAccRegionCopy = ` "type": "test", "region": "cn-beijing", "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", "ssh_username":"root", "image_name": "packer-test_{{timestamp}}", @@ -259,7 +259,7 @@ const testBuilderAccForceDelete = ` "type": "test", "region": "cn-beijing", "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", "ssh_username":"root", "image_force_delete": "%s", @@ -274,7 +274,7 @@ const testBuilderAccForceDeleteSnapshot = ` "type": "test", "region": "cn-beijing", "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", "ssh_username":"root", "image_force_delete_snapshots": "%s", @@ -291,7 +291,7 @@ const testBuilderAccSharing = ` "type": "test", "region": "cn-beijing", "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", "ssh_username":"root", "image_name": "packer-test_{{timestamp}}", diff --git a/builder/alicloud/ecs/step_attach_keypair.go b/builder/alicloud/ecs/step_attach_keypair.go index deac56c0e..4c96740b7 100644 --- a/builder/alicloud/ecs/step_attach_keypair.go +++ b/builder/alicloud/ecs/step_attach_keypair.go @@ -20,14 +20,22 @@ func (s *stepAttachKeyPar) Run(state multistep.StateBag) multistep.StepAction { client := state.Get("client").(*ecs.Client) config := state.Get("config").(Config) instance := state.Get("instance").(*ecs.InstanceAttributesType) - - err := client.AttachKeyPair(&ecs.AttachKeyPairArgs{RegionId: common.Region(config.AlicloudRegion), KeyPairName: keyPairName, - InstanceIds: "[\"" + instance.InstanceId + "\"]"}) - if err != nil { - err := fmt.Errorf("Error attaching keypair %s to instance %s : %s", keyPairName, instance.InstanceId, err) - state.Put("error", err) - ui.Error(err.Error()) - return multistep.ActionHalt + retry_times := 3 + for{ + err := client.AttachKeyPair(&ecs.AttachKeyPairArgs{RegionId: common.Region(config.AlicloudRegion), KeyPairName: keyPairName, + InstanceIds: "[\"" + instance.InstanceId + "\"]"}) + 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) + state.Put("error", err) + ui.Error(err.Error()) + return multistep.ActionHalt + } + break } ui.Message(fmt.Sprintf("Attach keypair %s to instance: %s", keyPairName, instance.InstanceId)) diff --git a/builder/alicloud/ecs/step_config_alicloud_key_pair.go b/builder/alicloud/ecs/step_config_alicloud_key_pair.go index 05e0209e9..dca2e0866 100644 --- a/builder/alicloud/ecs/step_config_alicloud_key_pair.go +++ b/builder/alicloud/ecs/step_config_alicloud_key_pair.go @@ -60,7 +60,7 @@ func (s *StepConfigAlicloudKeyPair) Run(state multistep.StateBag) multistep.Step 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{ KeyPairName: s.TemporaryKeyPairName, RegionId: common.Region(s.RegionId), diff --git a/builder/alicloud/ecs/step_config_alicloud_vpc.go b/builder/alicloud/ecs/step_config_alicloud_vpc.go index ba45d903c..6869bdefd 100644 --- a/builder/alicloud/ecs/step_config_alicloud_vpc.go +++ b/builder/alicloud/ecs/step_config_alicloud_vpc.go @@ -44,7 +44,7 @@ func (s *stepConfigAlicloudVPC) Run(state multistep.StateBag) multistep.StepActi return multistep.ActionHalt } - ui.Say("Start create alicloud vpc") + ui.Say("Start creating alicloud vpc") vpc, err := client.CreateVpc(&ecs.CreateVpcArgs{ RegionId: common.Region(config.AlicloudRegion), CidrBlock: s.CidrBlock, diff --git a/examples/alicloud/basic/alicloud_with_data_disk.json b/examples/alicloud/basic/alicloud_with_data_disk.json index 88412d98a..2d99af8fb 100644 --- a/examples/alicloud/basic/alicloud_with_data_disk.json +++ b/examples/alicloud/basic/alicloud_with_data_disk.json @@ -9,7 +9,7 @@ "secret_key":"{{user `secret_key`}}", "region":"cn-beijing", "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", "instance_type":"ecs.n1.tiny", "io_optimized":"true", @@ -19,7 +19,7 @@ "type": "shell", "inline": [ "sleep 30", - "yum install redis.x86_64 -y" + "apt-get update -yy" ] }] }