From 743be8a808b7cfaa50f381e9cc3bbe9fc5ff42c3 Mon Sep 17 00:00:00 2001 From: "bozhi.ch" Date: Tue, 11 Sep 2018 16:56:57 +0800 Subject: [PATCH 1/2] support ssh with private ip address --- builder/alicloud/ecs/builder.go | 4 +++- builder/alicloud/ecs/run_config_test.go | 24 +++++++++++++++++++ builder/alicloud/ecs/step_config_eip.go | 12 ++++++++++ builder/alicloud/ecs/step_config_public_ip.go | 11 +++++++++ 4 files changed, 50 insertions(+), 1 deletion(-) 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) From ff31665e380dc086f067a06c1423b7b5435fc5ca Mon Sep 17 00:00:00 2001 From: "bozhi.ch" Date: Tue, 11 Sep 2018 19:41:42 +0800 Subject: [PATCH 2/2] update document about ssh_private_ip option --- website/source/docs/builders/alicloud-ecs.html.md | 3 +++ 1 file changed, 3 insertions(+) diff --git a/website/source/docs/builders/alicloud-ecs.html.md b/website/source/docs/builders/alicloud-ecs.html.md index 266b59445..575e5ae77 100644 --- a/website/source/docs/builders/alicloud-ecs.html.md +++ b/website/source/docs/builders/alicloud-ecs.html.md @@ -190,6 +190,9 @@ builder. - `zone_id` (string) - ID of the zone to which the disk belongs. +- `ssh_private_ip` (boolean) - If this value is true, packer will connect to the ECS created through private ip + instead of allocating a public ip or an EIP. The default value is false. + ## Basic Example Here is a basic example for Alicloud.