From b3aae89a535565bb193150f6488d56b7ac838740 Mon Sep 17 00:00:00 2001 From: "bozhi.ch" Date: Sun, 16 Sep 2018 18:19:00 +0800 Subject: [PATCH 1/2] fix attaching keypair error due to missing keypair name --- builder/alicloud/ecs/builder.go | 4 +++- builder/alicloud/ecs/step_attach_keypair.go | 4 +++- 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/builder/alicloud/ecs/builder.go b/builder/alicloud/ecs/builder.go index 3b2688b1f..cc9c438a1 100644 --- a/builder/alicloud/ecs/builder.go +++ b/builder/alicloud/ecs/builder.go @@ -146,7 +146,9 @@ func (b *Builder) Run(ui packer.Ui, hook packer.Hook, cache packer.Cache) (packe }) } steps = append(steps, - &stepAttachKeyPair{}, + &stepAttachKeyPair{ + Comm: &b.config.Comm, + }, &stepRunAlicloudInstance{}, &stepMountAlicloudDisk{}, &communicator.StepConnect{ diff --git a/builder/alicloud/ecs/step_attach_keypair.go b/builder/alicloud/ecs/step_attach_keypair.go index 9f5b63676..88deabbb3 100644 --- a/builder/alicloud/ecs/step_attach_keypair.go +++ b/builder/alicloud/ecs/step_attach_keypair.go @@ -8,11 +8,13 @@ import ( "github.com/denverdino/aliyungo/common" "github.com/denverdino/aliyungo/ecs" + "github.com/hashicorp/packer/helper/communicator" "github.com/hashicorp/packer/helper/multistep" "github.com/hashicorp/packer/packer" ) type stepAttachKeyPair struct { + Comm *communicator.Config } func (s *stepAttachKeyPair) Run(_ context.Context, state multistep.StateBag) multistep.StepAction { @@ -21,7 +23,7 @@ func (s *stepAttachKeyPair) Run(_ context.Context, state multistep.StateBag) mul config := state.Get("config").(Config) instance := state.Get("instance").(*ecs.InstanceAttributesType) timeoutPoint := time.Now().Add(120 * time.Second) - keyPairName := config.Comm.SSHKeyPairName + keyPairName := s.Comm.SSHKeyPairName if keyPairName == "" { return multistep.ActionContinue } From 3c312eb2a0fd1cbd40f47d1d148be6506b6c5ebb Mon Sep 17 00:00:00 2001 From: "bozhi.ch" Date: Tue, 18 Sep 2018 21:40:57 +0800 Subject: [PATCH 2/2] change b.config to pointer --- builder/alicloud/ecs/builder.go | 6 ++---- builder/alicloud/ecs/builder_acc_test.go | 3 +-- builder/alicloud/ecs/step_attach_keypair.go | 8 +++----- builder/alicloud/ecs/step_check_source_image.go | 2 +- builder/alicloud/ecs/step_config_vpc.go | 2 +- builder/alicloud/ecs/step_config_vswitch.go | 2 +- builder/alicloud/ecs/step_create_image.go | 4 ++-- builder/alicloud/ecs/step_create_instance.go | 2 +- builder/alicloud/ecs/step_delete_images_snapshots.go | 2 +- builder/alicloud/ecs/step_mount_disk.go | 2 +- builder/alicloud/ecs/step_pre_validate.go | 2 +- 11 files changed, 15 insertions(+), 20 deletions(-) diff --git a/builder/alicloud/ecs/builder.go b/builder/alicloud/ecs/builder.go index cc9c438a1..2dc42c2c8 100644 --- a/builder/alicloud/ecs/builder.go +++ b/builder/alicloud/ecs/builder.go @@ -79,7 +79,7 @@ func (b *Builder) Run(ui packer.Ui, hook packer.Hook, cache packer.Cache) (packe return nil, err } state := new(multistep.BasicStateBag) - state.Put("config", b.config) + state.Put("config", &b.config) state.Put("client", client) state.Put("hook", hook) state.Put("ui", ui) @@ -146,9 +146,7 @@ func (b *Builder) Run(ui packer.Ui, hook packer.Hook, cache packer.Cache) (packe }) } steps = append(steps, - &stepAttachKeyPair{ - Comm: &b.config.Comm, - }, + &stepAttachKeyPair{}, &stepRunAlicloudInstance{}, &stepMountAlicloudDisk{}, &communicator.StepConnect{ diff --git a/builder/alicloud/ecs/builder_acc_test.go b/builder/alicloud/ecs/builder_acc_test.go index 415bfcf8b..58de14188 100644 --- a/builder/alicloud/ecs/builder_acc_test.go +++ b/builder/alicloud/ecs/builder_acc_test.go @@ -236,8 +236,7 @@ const testBuilderAccBasic = ` "type": "test", "region": "cn-beijing", "instance_type": "ecs.n1.tiny", - "source_image":"ubuntu_16_0402_64_40G_base_20170222.vhd", - "ssh_username": "ubuntu", + "source_image":"ubuntu_16_0402_64_20G_alibase_20180409.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 88deabbb3..08c52d0d8 100644 --- a/builder/alicloud/ecs/step_attach_keypair.go +++ b/builder/alicloud/ecs/step_attach_keypair.go @@ -8,22 +8,20 @@ import ( "github.com/denverdino/aliyungo/common" "github.com/denverdino/aliyungo/ecs" - "github.com/hashicorp/packer/helper/communicator" "github.com/hashicorp/packer/helper/multistep" "github.com/hashicorp/packer/packer" ) type stepAttachKeyPair struct { - Comm *communicator.Config } func (s *stepAttachKeyPair) Run(_ context.Context, state multistep.StateBag) multistep.StepAction { ui := state.Get("ui").(packer.Ui) client := state.Get("client").(*ecs.Client) - config := state.Get("config").(Config) + config := state.Get("config").(*Config) instance := state.Get("instance").(*ecs.InstanceAttributesType) timeoutPoint := time.Now().Add(120 * time.Second) - keyPairName := s.Comm.SSHKeyPairName + keyPairName := config.Comm.SSHKeyPairName if keyPairName == "" { return multistep.ActionContinue } @@ -54,7 +52,7 @@ func (s *stepAttachKeyPair) Run(_ context.Context, state multistep.StateBag) mul func (s *stepAttachKeyPair) Cleanup(state multistep.StateBag) { client := state.Get("client").(*ecs.Client) - config := state.Get("config").(Config) + config := state.Get("config").(*Config) ui := state.Get("ui").(packer.Ui) instance := state.Get("instance").(*ecs.InstanceAttributesType) keyPairName := config.Comm.SSHKeyPairName diff --git a/builder/alicloud/ecs/step_check_source_image.go b/builder/alicloud/ecs/step_check_source_image.go index f0fbf46e2..ebd4e56cb 100644 --- a/builder/alicloud/ecs/step_check_source_image.go +++ b/builder/alicloud/ecs/step_check_source_image.go @@ -16,7 +16,7 @@ type stepCheckAlicloudSourceImage struct { func (s *stepCheckAlicloudSourceImage) Run(_ context.Context, state multistep.StateBag) multistep.StepAction { client := state.Get("client").(*ecs.Client) - config := state.Get("config").(Config) + config := state.Get("config").(*Config) ui := state.Get("ui").(packer.Ui) args := &ecs.DescribeImagesArgs{ RegionId: common.Region(config.AlicloudRegion), diff --git a/builder/alicloud/ecs/step_config_vpc.go b/builder/alicloud/ecs/step_config_vpc.go index 8ce5663ed..c86746d1f 100644 --- a/builder/alicloud/ecs/step_config_vpc.go +++ b/builder/alicloud/ecs/step_config_vpc.go @@ -20,7 +20,7 @@ type stepConfigAlicloudVPC struct { } func (s *stepConfigAlicloudVPC) Run(_ context.Context, state multistep.StateBag) multistep.StepAction { - config := state.Get("config").(Config) + config := state.Get("config").(*Config) client := state.Get("client").(*ecs.Client) ui := state.Get("ui").(packer.Ui) diff --git a/builder/alicloud/ecs/step_config_vswitch.go b/builder/alicloud/ecs/step_config_vswitch.go index 4bbb19773..4daa81a69 100644 --- a/builder/alicloud/ecs/step_config_vswitch.go +++ b/builder/alicloud/ecs/step_config_vswitch.go @@ -24,7 +24,7 @@ func (s *stepConfigAlicloudVSwitch) Run(_ context.Context, state multistep.State client := state.Get("client").(*ecs.Client) ui := state.Get("ui").(packer.Ui) vpcId := state.Get("vpcid").(string) - config := state.Get("config").(Config) + config := state.Get("config").(*Config) if len(s.VSwitchId) != 0 { vswitchs, _, err := client.DescribeVSwitches(&ecs.DescribeVSwitchesArgs{ diff --git a/builder/alicloud/ecs/step_create_image.go b/builder/alicloud/ecs/step_create_image.go index ae5840c09..fedbfb272 100644 --- a/builder/alicloud/ecs/step_create_image.go +++ b/builder/alicloud/ecs/step_create_image.go @@ -15,7 +15,7 @@ type stepCreateAlicloudImage struct { } func (s *stepCreateAlicloudImage) Run(_ context.Context, state multistep.StateBag) multistep.StepAction { - config := state.Get("config").(Config) + config := state.Get("config").(*Config) client := state.Get("client").(*ecs.Client) ui := state.Get("ui").(packer.Ui) @@ -85,7 +85,7 @@ func (s *stepCreateAlicloudImage) Cleanup(state multistep.StateBag) { client := state.Get("client").(*ecs.Client) ui := state.Get("ui").(packer.Ui) - config := state.Get("config").(Config) + config := state.Get("config").(*Config) ui.Say("Deleting the image because of cancellation or error...") if err := client.DeleteImage(common.Region(config.AlicloudRegion), s.image.ImageId); err != nil { diff --git a/builder/alicloud/ecs/step_create_instance.go b/builder/alicloud/ecs/step_create_instance.go index ff41a3df6..082b39188 100644 --- a/builder/alicloud/ecs/step_create_instance.go +++ b/builder/alicloud/ecs/step_create_instance.go @@ -28,7 +28,7 @@ type stepCreateAlicloudInstance struct { func (s *stepCreateAlicloudInstance) Run(_ context.Context, state multistep.StateBag) multistep.StepAction { client := state.Get("client").(*ecs.Client) - config := state.Get("config").(Config) + config := state.Get("config").(*Config) ui := state.Get("ui").(packer.Ui) source_image := state.Get("source_image").(*ecs.ImageType) network_type := state.Get("networktype").(InstanceNetWork) diff --git a/builder/alicloud/ecs/step_delete_images_snapshots.go b/builder/alicloud/ecs/step_delete_images_snapshots.go index 9e294c45f..6718744d9 100644 --- a/builder/alicloud/ecs/step_delete_images_snapshots.go +++ b/builder/alicloud/ecs/step_delete_images_snapshots.go @@ -20,7 +20,7 @@ type stepDeleteAlicloudImageSnapshots struct { func (s *stepDeleteAlicloudImageSnapshots) Run(_ context.Context, state multistep.StateBag) multistep.StepAction { client := state.Get("client").(*ecs.Client) ui := state.Get("ui").(packer.Ui) - config := state.Get("config").(Config) + config := state.Get("config").(*Config) ui.Say("Deleting image snapshots.") // Check for force delete if s.AlicloudImageForceDelete { diff --git a/builder/alicloud/ecs/step_mount_disk.go b/builder/alicloud/ecs/step_mount_disk.go index 642605bee..40b3dc792 100644 --- a/builder/alicloud/ecs/step_mount_disk.go +++ b/builder/alicloud/ecs/step_mount_disk.go @@ -14,7 +14,7 @@ type stepMountAlicloudDisk struct { func (s *stepMountAlicloudDisk) Run(_ context.Context, state multistep.StateBag) multistep.StepAction { client := state.Get("client").(*ecs.Client) - config := state.Get("config").(Config) + config := state.Get("config").(*Config) ui := state.Get("ui").(packer.Ui) instance := state.Get("instance").(*ecs.InstanceAttributesType) alicloudDiskDevices := config.ECSImagesDiskMappings diff --git a/builder/alicloud/ecs/step_pre_validate.go b/builder/alicloud/ecs/step_pre_validate.go index 6bc3b1060..57c3546b6 100644 --- a/builder/alicloud/ecs/step_pre_validate.go +++ b/builder/alicloud/ecs/step_pre_validate.go @@ -23,7 +23,7 @@ func (s *stepPreValidate) Run(_ context.Context, state multistep.StateBag) multi } client := state.Get("client").(*ecs.Client) - config := state.Get("config").(Config) + config := state.Get("config").(*Config) ui.Say("Prevalidating image name...") images, _, err := client.DescribeImages(&ecs.DescribeImagesArgs{ ImageName: s.AlicloudDestImageName,