diff --git a/builder/alicloud/ecs/builder.go b/builder/alicloud/ecs/builder.go index 476b3c4a2..d51f6bd0e 100644 --- a/builder/alicloud/ecs/builder.go +++ b/builder/alicloud/ecs/builder.go @@ -139,10 +139,12 @@ func (b *Builder) Run(ui packer.Ui, hook packer.Hook, cache packer.Cache) (packe RegionId: b.config.AlicloudRegion, InternetChargeType: b.config.InternetChargeType, InternetMaxBandwidthOut: b.config.InternetMaxBandwidthOut, + SSHPrivateIp: b.config.SSHPrivateIp, }) } else { steps = append(steps, &stepConfigAlicloudPublicIP{ - RegionId: b.config.AlicloudRegion, + RegionId: b.config.AlicloudRegion, + SSHPrivateIp: b.config.SSHPrivateIp, }) } steps = append(steps, diff --git a/builder/alicloud/ecs/run_config_test.go b/builder/alicloud/ecs/run_config_test.go index 67e1369f0..19dbb5e73 100644 --- a/builder/alicloud/ecs/run_config_test.go +++ b/builder/alicloud/ecs/run_config_test.go @@ -123,3 +123,27 @@ func TestRunConfigPrepare_TemporaryKeyPairName(t *testing.T) { t.Fatal("keypair name does not match") } } + +func TestRunConfigPrepare_SSHPrivateIp(t *testing.T) { + c := testConfig() + if err := c.Prepare(nil); len(err) != 0 { + t.Fatalf("err: %s", err) + } + if c.SSHPrivateIp != false { + t.Fatalf("invalid value, expected: %t, actul: %t", false, c.SSHPrivateIp) + } + c.SSHPrivateIp = true + if err := c.Prepare(nil); len(err) != 0 { + t.Fatalf("err: %s", err) + } + if c.SSHPrivateIp != true { + t.Fatalf("invalid value, expected: %t, actul: %t", true, c.SSHPrivateIp) + } + c.SSHPrivateIp = false + if err := c.Prepare(nil); len(err) != 0 { + t.Fatalf("err: %s", err) + } + if c.SSHPrivateIp != false { + t.Fatalf("invalid value, expected: %t, actul: %t", false, c.SSHPrivateIp) + } +} diff --git a/builder/alicloud/ecs/step_config_eip.go b/builder/alicloud/ecs/step_config_eip.go index f747068f2..922161c28 100644 --- a/builder/alicloud/ecs/step_config_eip.go +++ b/builder/alicloud/ecs/step_config_eip.go @@ -16,12 +16,24 @@ type stepConfigAlicloudEIP struct { InternetChargeType string InternetMaxBandwidthOut int allocatedId string + SSHPrivateIp bool } func (s *stepConfigAlicloudEIP) Run(_ context.Context, state multistep.StateBag) multistep.StepAction { client := state.Get("client").(*ecs.Client) ui := state.Get("ui").(packer.Ui) instance := state.Get("instance").(*ecs.InstanceAttributesType) + + if s.SSHPrivateIp { + ipaddress := instance.VpcAttributes.PrivateIpAddress.IpAddress + if len(ipaddress) == 0 { + ui.Say("Failed to get private ip of instance") + return multistep.ActionHalt + } + state.Put("ipaddress", ipaddress[0]) + return multistep.ActionContinue + } + ui.Say("Allocating eip") ipaddress, allocateId, err := client.AllocateEipAddress(&ecs.AllocateEipAddressArgs{ RegionId: common.Region(s.RegionId), InternetChargeType: common.InternetChargeType(s.InternetChargeType), diff --git a/builder/alicloud/ecs/step_config_public_ip.go b/builder/alicloud/ecs/step_config_public_ip.go index 594f85603..3dc21a620 100644 --- a/builder/alicloud/ecs/step_config_public_ip.go +++ b/builder/alicloud/ecs/step_config_public_ip.go @@ -12,6 +12,7 @@ import ( type stepConfigAlicloudPublicIP struct { publicIPAddress string RegionId string + SSHPrivateIp bool } func (s *stepConfigAlicloudPublicIP) Run(_ context.Context, state multistep.StateBag) multistep.StepAction { @@ -19,6 +20,16 @@ func (s *stepConfigAlicloudPublicIP) Run(_ context.Context, state multistep.Stat ui := state.Get("ui").(packer.Ui) instance := state.Get("instance").(*ecs.InstanceAttributesType) + if s.SSHPrivateIp { + ipaddress := instance.InnerIpAddress.IpAddress + if len(ipaddress) == 0 { + ui.Say("Failed to get private ip of instance") + return multistep.ActionHalt + } + state.Put("ipaddress", ipaddress[0]) + return multistep.ActionContinue + } + ipaddress, err := client.AllocatePublicIpAddress(instance.InstanceId) if err != nil { state.Put("error", err)