From 0cbd3ff0f3c8b8b56479e70b8031e519c8055677 Mon Sep 17 00:00:00 2001 From: Megan Marsh Date: Thu, 26 Sep 2019 12:30:04 -0700 Subject: [PATCH 1/3] make sure amazon builders respect ssh_host option --- builder/amazon/common/ssh.go | 9 +++++++-- builder/amazon/ebs/builder.go | 4 +++- builder/amazon/ebssurrogate/builder.go | 4 +++- builder/amazon/ebsvolume/builder.go | 4 +++- builder/amazon/instance/builder.go | 4 +++- 5 files changed, 19 insertions(+), 6 deletions(-) diff --git a/builder/amazon/common/ssh.go b/builder/amazon/common/ssh.go index 713f88629..f085a1dca 100644 --- a/builder/amazon/common/ssh.go +++ b/builder/amazon/common/ssh.go @@ -3,6 +3,7 @@ package common import ( "errors" "fmt" + "log" "time" "github.com/aws/aws-sdk-go/service/ec2" @@ -20,12 +21,16 @@ var ( // SSHHost returns a function that can be given to the SSH communicator // for determining the SSH address based on the instance DNS name. -func SSHHost(e ec2Describer, sshInterface string) func(multistep.StateBag) (string, error) { +func SSHHost(e ec2Describer, sshInterface string, host string) func(multistep.StateBag) (string, error) { return func(state multistep.StateBag) (string, error) { + if host != "" { + log.Printf("Using ssh_host value: %s", host) + return host, nil + } + const tries = 2 // <= with current structure to check result of describing `tries` times for j := 0; j <= tries; j++ { - var host string i := state.Get("instance").(*ec2.Instance) if sshInterface != "" { switch sshInterface { diff --git a/builder/amazon/ebs/builder.go b/builder/amazon/ebs/builder.go index dd87f5d4e..2cbcbcbf4 100644 --- a/builder/amazon/ebs/builder.go +++ b/builder/amazon/ebs/builder.go @@ -231,7 +231,9 @@ func (b *Builder) Run(ctx context.Context, ui packer.Ui, hook packer.Hook) (pack Config: &b.config.RunConfig.Comm, Host: awscommon.SSHHost( ec2conn, - b.config.SSHInterface), + b.config.SSHInterface, + b.config.Comm.SSHHost, + ), SSHConfig: b.config.RunConfig.Comm.SSHConfigFunc(), }, &common.StepProvision{}, diff --git a/builder/amazon/ebssurrogate/builder.go b/builder/amazon/ebssurrogate/builder.go index ce4b9433f..f1e2c8122 100644 --- a/builder/amazon/ebssurrogate/builder.go +++ b/builder/amazon/ebssurrogate/builder.go @@ -272,7 +272,9 @@ func (b *Builder) Run(ctx context.Context, ui packer.Ui, hook packer.Hook) (pack Config: &b.config.RunConfig.Comm, Host: awscommon.SSHHost( ec2conn, - b.config.SSHInterface), + b.config.SSHInterface, + b.config.Comm.SSHHost, + ), SSHConfig: b.config.RunConfig.Comm.SSHConfigFunc(), }, &common.StepProvision{}, diff --git a/builder/amazon/ebsvolume/builder.go b/builder/amazon/ebsvolume/builder.go index 342f1b9aa..a26a7ff85 100644 --- a/builder/amazon/ebsvolume/builder.go +++ b/builder/amazon/ebsvolume/builder.go @@ -239,7 +239,9 @@ func (b *Builder) Run(ctx context.Context, ui packer.Ui, hook packer.Hook) (pack Config: &b.config.RunConfig.Comm, Host: awscommon.SSHHost( ec2conn, - b.config.SSHInterface), + b.config.SSHInterface, + b.config.Comm.SSHHost, + ), SSHConfig: b.config.RunConfig.Comm.SSHConfigFunc(), }, &common.StepProvision{}, diff --git a/builder/amazon/instance/builder.go b/builder/amazon/instance/builder.go index d40a69600..bcdb00d91 100644 --- a/builder/amazon/instance/builder.go +++ b/builder/amazon/instance/builder.go @@ -324,7 +324,9 @@ func (b *Builder) Run(ctx context.Context, ui packer.Ui, hook packer.Hook) (pack Config: &b.config.RunConfig.Comm, Host: awscommon.SSHHost( ec2conn, - b.config.SSHInterface), + b.config.SSHInterface, + b.config.Comm.SSHHost, + ), SSHConfig: b.config.RunConfig.Comm.SSHConfigFunc(), }, &common.StepProvision{}, From 31bb1e818f5a12d66b7570ee231b1c2e31d10472 Mon Sep 17 00:00:00 2001 From: Megan Marsh Date: Thu, 26 Sep 2019 13:02:11 -0700 Subject: [PATCH 2/3] fix tests --- builder/amazon/common/ssh_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/builder/amazon/common/ssh_test.go b/builder/amazon/common/ssh_test.go index 02823d1d9..8fd4b3026 100644 --- a/builder/amazon/common/ssh_test.go +++ b/builder/amazon/common/ssh_test.go @@ -68,7 +68,7 @@ func testSSHHost(t *testing.T, allowTries int, vpcId string, sshInterface string publicDNS: publicDNS, } - f := SSHHost(e, sshInterface) + f := SSHHost(e, sshInterface, "") st := &multistep.BasicStateBag{} st.Put("instance", &ec2.Instance{ InstanceId: aws.String("instance-id"), From ac6e7a4800a82c947e89fbde173305cfe89174b3 Mon Sep 17 00:00:00 2001 From: Megan Marsh Date: Fri, 27 Sep 2019 09:19:23 -0700 Subject: [PATCH 3/3] updates tests to validate that ssh_host value supplied in the template overrides all else --- builder/amazon/common/ssh_test.go | 70 +++++++++++++++++-------------- 1 file changed, 39 insertions(+), 31 deletions(-) diff --git a/builder/amazon/common/ssh_test.go b/builder/amazon/common/ssh_test.go index 8fd4b3026..c8c7f9b8d 100644 --- a/builder/amazon/common/ssh_test.go +++ b/builder/amazon/common/ssh_test.go @@ -9,10 +9,11 @@ import ( ) const ( - privateIP = "10.0.0.1" - publicIP = "192.168.1.1" - privateDNS = "private.dns.test" - publicDNS = "public.dns.test" + privateIP = "10.0.0.1" + publicIP = "192.168.1.1" + privateDNS = "private.dns.test" + publicDNS = "public.dns.test" + sshHostTemplate = "custom.host.value" ) func TestSSHHost(t *testing.T) { @@ -25,39 +26,46 @@ func TestSSHHost(t *testing.T) { vpcId string sshInterface string - ok bool - wantHost string + ok bool + wantHost string + sshHostOverride string }{ - {1, "", "", true, publicDNS}, - {1, "", "private_ip", true, privateIP}, - {1, "vpc-id", "", true, publicIP}, - {1, "vpc-id", "private_ip", true, privateIP}, - {1, "vpc-id", "private_dns", true, privateDNS}, - {1, "vpc-id", "public_dns", true, publicDNS}, - {1, "vpc-id", "public_ip", true, publicIP}, - {2, "", "", true, publicDNS}, - {2, "", "private_ip", true, privateIP}, - {2, "vpc-id", "", true, publicIP}, - {2, "vpc-id", "private_ip", true, privateIP}, - {2, "vpc-id", "private_dns", true, privateDNS}, - {2, "vpc-id", "public_dns", true, publicDNS}, - {2, "vpc-id", "public_ip", true, publicIP}, - {3, "", "", false, ""}, - {3, "", "private_ip", false, ""}, - {3, "vpc-id", "", false, ""}, - {3, "vpc-id", "private_ip", false, ""}, - {3, "vpc-id", "private_dns", false, ""}, - {3, "vpc-id", "public_dns", false, ""}, - {3, "vpc-id", "public_ip", false, ""}, + {1, "", "", true, publicDNS, ""}, + {1, "", "private_ip", true, privateIP, ""}, + {1, "vpc-id", "", true, publicIP, ""}, + {1, "vpc-id", "private_ip", true, privateIP, ""}, + {1, "vpc-id", "private_dns", true, privateDNS, ""}, + {1, "vpc-id", "public_dns", true, publicDNS, ""}, + {1, "vpc-id", "public_ip", true, publicIP, ""}, + {2, "", "", true, publicDNS, ""}, + {2, "", "private_ip", true, privateIP, ""}, + {2, "vpc-id", "", true, publicIP, ""}, + {2, "vpc-id", "private_ip", true, privateIP, ""}, + {2, "vpc-id", "private_dns", true, privateDNS, ""}, + {2, "vpc-id", "public_dns", true, publicDNS, ""}, + {2, "vpc-id", "public_ip", true, publicIP, ""}, + {3, "", "", false, "", ""}, + {3, "", "private_ip", false, "", ""}, + {3, "vpc-id", "", false, "", ""}, + {3, "vpc-id", "private_ip", false, "", ""}, + {3, "vpc-id", "private_dns", false, "", ""}, + {3, "vpc-id", "public_dns", false, "", ""}, + {3, "vpc-id", "public_ip", false, "", ""}, + {1, "", "", true, sshHostTemplate, sshHostTemplate}, + {1, "vpc-id", "", true, sshHostTemplate, sshHostTemplate}, + {2, "vpc-id", "private_dns", true, sshHostTemplate, sshHostTemplate}, } for _, c := range cases { - testSSHHost(t, c.allowTries, c.vpcId, c.sshInterface, c.ok, c.wantHost) + testSSHHost(t, c.allowTries, c.vpcId, c.sshInterface, c.ok, c.wantHost, + c.sshHostOverride) } } -func testSSHHost(t *testing.T, allowTries int, vpcId string, sshInterface string, ok bool, wantHost string) { - t.Logf("allowTries=%d vpcId=%s sshInterface=%s ok=%t wantHost=%q", allowTries, vpcId, sshInterface, ok, wantHost) +func testSSHHost(t *testing.T, allowTries int, vpcId string, sshInterface string, + ok bool, wantHost string, sshHostOverride string) { + t.Logf("allowTries=%d vpcId=%s sshInterface=%s ok=%t wantHost=%q sshHostOverride=%s", + allowTries, vpcId, sshInterface, ok, wantHost, sshHostOverride) e := &fakeEC2Describer{ allowTries: allowTries, @@ -68,7 +76,7 @@ func testSSHHost(t *testing.T, allowTries int, vpcId string, sshInterface string publicDNS: publicDNS, } - f := SSHHost(e, sshInterface, "") + f := SSHHost(e, sshInterface, sshHostOverride) st := &multistep.BasicStateBag{} st.Put("instance", &ec2.Instance{ InstanceId: aws.String("instance-id"),