From ef4ca9c48e22ab0ca905fcfb3431c7ea0752dd4d Mon Sep 17 00:00:00 2001 From: Adrien Delorme Date: Tue, 28 Aug 2018 17:47:02 +0200 Subject: [PATCH] builder.amazon: use c.Comm for ssh --- builder/amazon/common/run_config.go | 4 ++-- builder/amazon/common/run_config_test.go | 10 +++++----- builder/amazon/common/step_key_pair.go | 19 +++++++++---------- builder/amazon/ebs/builder.go | 7 +++---- builder/amazon/ebssurrogate/builder.go | 7 +++---- builder/amazon/ebsvolume/builder.go | 7 +++---- builder/amazon/instance/builder.go | 7 +++---- 7 files changed, 28 insertions(+), 33 deletions(-) diff --git a/builder/amazon/common/run_config.go b/builder/amazon/common/run_config.go index aca5e0143..a4345b1db 100644 --- a/builder/amazon/common/run_config.go +++ b/builder/amazon/common/run_config.go @@ -66,10 +66,10 @@ func (c *RunConfig) Prepare(ctx *interpolate.Context) []error { // ssh_private_key_file, then create a temporary one, but only if the // temporary_key_pair_name has not been provided and we are not using // ssh_password. - if c.Comm.SSHKeyPairName == "" && c.TemporaryKeyPairName == "" && + if c.Comm.SSHKeyPairName == "" && c.Comm.SSHTemporaryKeyPairName == "" && c.Comm.SSHPrivateKeyFile == "" && c.Comm.SSHPassword == "" { - c.TemporaryKeyPairName = fmt.Sprintf("packer_%s", uuid.TimeOrderedUUID()) + c.Comm.SSHTemporaryKeyPairName = fmt.Sprintf("packer_%s", uuid.TimeOrderedUUID()) } if c.WindowsPasswordTimeout == 0 { diff --git a/builder/amazon/common/run_config_test.go b/builder/amazon/common/run_config_test.go index f43b9f12c..9228dbcba 100644 --- a/builder/amazon/common/run_config_test.go +++ b/builder/amazon/common/run_config_test.go @@ -206,27 +206,27 @@ func TestRunConfigPrepare_UserDataFile(t *testing.T) { func TestRunConfigPrepare_TemporaryKeyPairName(t *testing.T) { c := testConfig() - c.TemporaryKeyPairName = "" + c.Comm.SSHTemporaryKeyPairName = "" if err := c.Prepare(nil); len(err) != 0 { t.Fatalf("err: %s", err) } - if c.TemporaryKeyPairName == "" { + if c.Comm.SSHTemporaryKeyPairName == "" { t.Fatal("keypair name is empty") } // Match prefix and UUID, e.g. "packer_5790d491-a0b8-c84c-c9d2-2aea55086550". r := regexp.MustCompile(`\Apacker_(?:(?i)[a-f\d]{8}(?:-[a-f\d]{4}){3}-[a-f\d]{12}?)\z`) - if !r.MatchString(c.TemporaryKeyPairName) { + if !r.MatchString(c.Comm.SSHTemporaryKeyPairName) { t.Fatal("keypair name is not valid") } - c.TemporaryKeyPairName = "ssh-key-123" + c.Comm.SSHTemporaryKeyPairName = "ssh-key-123" if err := c.Prepare(nil); len(err) != 0 { t.Fatalf("err: %s", err) } - if c.TemporaryKeyPairName != "ssh-key-123" { + if c.Comm.SSHTemporaryKeyPairName != "ssh-key-123" { t.Fatal("keypair name does not match") } } diff --git a/builder/amazon/common/step_key_pair.go b/builder/amazon/common/step_key_pair.go index 71c286dad..93651a21c 100644 --- a/builder/amazon/common/step_key_pair.go +++ b/builder/amazon/common/step_key_pair.go @@ -14,10 +14,9 @@ import ( ) type StepKeyPair struct { - Debug bool - Comm *communicator.Config - DebugKeyPath string - TemporaryKeyPairName string + Debug bool + Comm *communicator.Config + DebugKeyPath string doCleanup bool } @@ -50,7 +49,7 @@ func (s *StepKeyPair) Run(_ context.Context, state multistep.StateBag) multistep return multistep.ActionContinue } - if s.TemporaryKeyPairName == "" { + if s.Comm.SSHTemporaryKeyPairName == "" { ui.Say("Not using temporary keypair") state.Put("keyPair", "") return multistep.ActionContinue @@ -58,9 +57,9 @@ func (s *StepKeyPair) Run(_ context.Context, state multistep.StateBag) multistep ec2conn := state.Get("ec2").(*ec2.EC2) - ui.Say(fmt.Sprintf("Creating temporary keypair: %s", s.TemporaryKeyPairName)) + ui.Say(fmt.Sprintf("Creating temporary keypair: %s", s.Comm.SSHTemporaryKeyPairName)) keyResp, err := ec2conn.CreateKeyPair(&ec2.CreateKeyPairInput{ - KeyName: &s.TemporaryKeyPairName}) + KeyName: &s.Comm.SSHTemporaryKeyPairName}) if err != nil { state.Put("error", fmt.Errorf("Error creating temporary keypair: %s", err)) return multistep.ActionHalt @@ -69,7 +68,7 @@ func (s *StepKeyPair) Run(_ context.Context, state multistep.StateBag) multistep s.doCleanup = true // Set some data for use in future steps - s.Comm.SSHKeyPairName = s.TemporaryKeyPairName + s.Comm.SSHKeyPairName = s.Comm.SSHTemporaryKeyPairName s.Comm.SSHPrivateKey = []byte(*keyResp.KeyMaterial) // If we're in debug mode, output the private key to the working @@ -111,10 +110,10 @@ func (s *StepKeyPair) Cleanup(state multistep.StateBag) { // Remove the keypair ui.Say("Deleting temporary keypair...") - _, err := ec2conn.DeleteKeyPair(&ec2.DeleteKeyPairInput{KeyName: &s.TemporaryKeyPairName}) + _, err := ec2conn.DeleteKeyPair(&ec2.DeleteKeyPairInput{KeyName: &s.Comm.SSHTemporaryKeyPairName}) if err != nil { ui.Error(fmt.Sprintf( - "Error cleaning up keypair. Please delete the key manually: %s", s.TemporaryKeyPairName)) + "Error cleaning up keypair. Please delete the key manually: %s", s.Comm.SSHTemporaryKeyPairName)) } // Also remove the physical key if we're debugging. diff --git a/builder/amazon/ebs/builder.go b/builder/amazon/ebs/builder.go index af906da75..75fe70cbf 100644 --- a/builder/amazon/ebs/builder.go +++ b/builder/amazon/ebs/builder.go @@ -179,10 +179,9 @@ func (b *Builder) Run(ui packer.Ui, hook packer.Hook, cache packer.Cache) (packe AmiFilters: b.config.SourceAmiFilter, }, &awscommon.StepKeyPair{ - Debug: b.config.PackerDebug, - Comm: &b.config.RunConfig.Comm, - DebugKeyPath: fmt.Sprintf("ec2_%s.pem", b.config.PackerBuildName), - TemporaryKeyPairName: b.config.TemporaryKeyPairName, + Debug: b.config.PackerDebug, + Comm: &b.config.RunConfig.Comm, + DebugKeyPath: fmt.Sprintf("ec2_%s.pem", b.config.PackerBuildName), }, &awscommon.StepSecurityGroup{ SecurityGroupIds: b.config.SecurityGroupIds, diff --git a/builder/amazon/ebssurrogate/builder.go b/builder/amazon/ebssurrogate/builder.go index 10dcf5441..6ed2fda51 100644 --- a/builder/amazon/ebssurrogate/builder.go +++ b/builder/amazon/ebssurrogate/builder.go @@ -196,10 +196,9 @@ func (b *Builder) Run(ui packer.Ui, hook packer.Hook, cache packer.Cache) (packe AmiFilters: b.config.SourceAmiFilter, }, &awscommon.StepKeyPair{ - Debug: b.config.PackerDebug, - Comm: &b.config.RunConfig.Comm, - DebugKeyPath: fmt.Sprintf("ec2_%s.pem", b.config.PackerBuildName), - TemporaryKeyPairName: b.config.TemporaryKeyPairName, + Debug: b.config.PackerDebug, + Comm: &b.config.RunConfig.Comm, + DebugKeyPath: fmt.Sprintf("ec2_%s.pem", b.config.PackerBuildName), }, &awscommon.StepSecurityGroup{ SecurityGroupIds: b.config.SecurityGroupIds, diff --git a/builder/amazon/ebsvolume/builder.go b/builder/amazon/ebsvolume/builder.go index 8724323c0..c9b99e37f 100644 --- a/builder/amazon/ebsvolume/builder.go +++ b/builder/amazon/ebsvolume/builder.go @@ -171,10 +171,9 @@ func (b *Builder) Run(ui packer.Ui, hook packer.Hook, cache packer.Cache) (packe AmiFilters: b.config.SourceAmiFilter, }, &awscommon.StepKeyPair{ - Debug: b.config.PackerDebug, - Comm: &b.config.RunConfig.Comm, - DebugKeyPath: fmt.Sprintf("ec2_%s.pem", b.config.PackerBuildName), - TemporaryKeyPairName: b.config.TemporaryKeyPairName, + Debug: b.config.PackerDebug, + Comm: &b.config.RunConfig.Comm, + DebugKeyPath: fmt.Sprintf("ec2_%s.pem", b.config.PackerBuildName), }, &awscommon.StepSecurityGroup{ SecurityGroupIds: b.config.SecurityGroupIds, diff --git a/builder/amazon/instance/builder.go b/builder/amazon/instance/builder.go index 2f8786be7..43c353c79 100644 --- a/builder/amazon/instance/builder.go +++ b/builder/amazon/instance/builder.go @@ -257,10 +257,9 @@ func (b *Builder) Run(ui packer.Ui, hook packer.Hook, cache packer.Cache) (packe AmiFilters: b.config.SourceAmiFilter, }, &awscommon.StepKeyPair{ - Debug: b.config.PackerDebug, - Comm: &b.config.RunConfig.Comm, - DebugKeyPath: fmt.Sprintf("ec2_%s.pem", b.config.PackerBuildName), - TemporaryKeyPairName: b.config.TemporaryKeyPairName, + Debug: b.config.PackerDebug, + Comm: &b.config.RunConfig.Comm, + DebugKeyPath: fmt.Sprintf("ec2_%s.pem", b.config.PackerBuildName), }, &awscommon.StepSecurityGroup{ CommConfig: &b.config.RunConfig.Comm,