diff --git a/builder/amazon/common/step_get_password.go b/builder/amazon/common/step_get_password.go index d40566620..291e97878 100644 --- a/builder/amazon/common/step_get_password.go +++ b/builder/amazon/common/step_get_password.go @@ -106,7 +106,7 @@ func (s *StepGetPassword) Cleanup(multistep.StateBag) { func (s *StepGetPassword) waitForPassword(state multistep.StateBag, cancel <-chan struct{}) (string, error) { ec2conn := state.Get("ec2").(*ec2.EC2) instance := state.Get("instance").(*ec2.Instance) - privateKey := state.Get("privateKey").(string) + privateKey := s.Comm.SSHPrivateKey for { select { diff --git a/builder/amazon/common/step_key_pair.go b/builder/amazon/common/step_key_pair.go index 927101fcf..0198fceb3 100644 --- a/builder/amazon/common/step_key_pair.go +++ b/builder/amazon/common/step_key_pair.go @@ -8,17 +8,17 @@ import ( "runtime" "github.com/aws/aws-sdk-go/service/ec2" + "github.com/hashicorp/packer/helper/communicator" "github.com/hashicorp/packer/helper/multistep" "github.com/hashicorp/packer/packer" ) type StepKeyPair struct { Debug bool + Comm *communicator.Config SSHAgentAuth bool DebugKeyPath string TemporaryKeyPairName string - KeyPairName string - PrivateKeyFile string doCleanup bool } @@ -26,29 +26,28 @@ type StepKeyPair struct { func (s *StepKeyPair) Run(_ context.Context, state multistep.StateBag) multistep.StepAction { ui := state.Get("ui").(packer.Ui) - if s.PrivateKeyFile != "" { + if s.Comm.SSHPrivateKeyFile != "" { ui.Say("Using existing SSH private key") - privateKeyBytes, err := ioutil.ReadFile(s.PrivateKeyFile) + privateKeyBytes, err := ioutil.ReadFile(s.Comm.SSHPrivateKeyFile) if err != nil { state.Put("error", fmt.Errorf( "Error loading configured private key file: %s", err)) return multistep.ActionHalt } - state.Put("keyPair", s.KeyPairName) - state.Put("privateKey", string(privateKeyBytes)) + s.Comm.SSHPrivateKey = privateKeyBytes return multistep.ActionContinue } - if s.SSHAgentAuth && s.KeyPairName == "" { + if s.SSHAgentAuth && s.Comm.SSHKeyPair == "" { ui.Say("Using SSH Agent with key pair in Source AMI") return multistep.ActionContinue } - if s.SSHAgentAuth && s.KeyPairName != "" { - ui.Say(fmt.Sprintf("Using SSH Agent for existing key pair %s", s.KeyPairName)) - state.Put("keyPair", s.KeyPairName) + if s.SSHAgentAuth && s.Comm.SSHKeyPair != "" { + ui.Say(fmt.Sprintf("Using SSH Agent for existing key pair %s", s.Comm.SSHKeyPair)) + state.Put("keyPair", s.Comm.SSHKeyPair) return multistep.ActionContinue } @@ -70,9 +69,9 @@ func (s *StepKeyPair) Run(_ context.Context, state multistep.StateBag) multistep s.doCleanup = true - // Set some state data for use in future steps - state.Put("keyPair", s.TemporaryKeyPairName) - state.Put("privateKey", *keyResp.KeyMaterial) + // Set some data for use in future steps + s.Comm.SSHKeyPair = s.TemporaryKeyPairName + s.Comm.SSHPrivateKey = []byte(*keyResp.KeyMaterial) // If we're in debug mode, output the private key to the working // directory. diff --git a/builder/amazon/ebs/builder.go b/builder/amazon/ebs/builder.go index 5733038a2..eb707654e 100644 --- a/builder/amazon/ebs/builder.go +++ b/builder/amazon/ebs/builder.go @@ -180,11 +180,10 @@ func (b *Builder) Run(ui packer.Ui, hook packer.Hook, cache packer.Cache) (packe }, &awscommon.StepKeyPair{ Debug: b.config.PackerDebug, + Comm: &b.config.RunConfig.Comm, SSHAgentAuth: b.config.Comm.SSHAgentAuth, DebugKeyPath: fmt.Sprintf("ec2_%s.pem", b.config.PackerBuildName), - KeyPairName: b.config.SSHKeyPairName, TemporaryKeyPairName: b.config.TemporaryKeyPairName, - PrivateKeyFile: b.config.RunConfig.Comm.SSHPrivateKeyFile, }, &awscommon.StepSecurityGroup{ SecurityGroupIds: b.config.SecurityGroupIds, diff --git a/builder/amazon/ebssurrogate/builder.go b/builder/amazon/ebssurrogate/builder.go index 657c49d5f..03a89603e 100644 --- a/builder/amazon/ebssurrogate/builder.go +++ b/builder/amazon/ebssurrogate/builder.go @@ -197,11 +197,10 @@ func (b *Builder) Run(ui packer.Ui, hook packer.Hook, cache packer.Cache) (packe }, &awscommon.StepKeyPair{ Debug: b.config.PackerDebug, + Comm: &b.config.RunConfig.Comm, SSHAgentAuth: b.config.Comm.SSHAgentAuth, DebugKeyPath: fmt.Sprintf("ec2_%s.pem", b.config.PackerBuildName), - KeyPairName: b.config.SSHKeyPairName, TemporaryKeyPairName: b.config.TemporaryKeyPairName, - PrivateKeyFile: b.config.RunConfig.Comm.SSHPrivateKeyFile, }, &awscommon.StepSecurityGroup{ SecurityGroupIds: b.config.SecurityGroupIds, diff --git a/builder/amazon/ebsvolume/builder.go b/builder/amazon/ebsvolume/builder.go index b31ecd23b..4d3cbd970 100644 --- a/builder/amazon/ebsvolume/builder.go +++ b/builder/amazon/ebsvolume/builder.go @@ -172,11 +172,10 @@ func (b *Builder) Run(ui packer.Ui, hook packer.Hook, cache packer.Cache) (packe }, &awscommon.StepKeyPair{ Debug: b.config.PackerDebug, + Comm: &b.config.RunConfig.Comm, SSHAgentAuth: b.config.Comm.SSHAgentAuth, DebugKeyPath: fmt.Sprintf("ec2_%s.pem", b.config.PackerBuildName), - KeyPairName: b.config.SSHKeyPairName, TemporaryKeyPairName: b.config.TemporaryKeyPairName, - PrivateKeyFile: b.config.RunConfig.Comm.SSHPrivateKeyFile, }, &awscommon.StepSecurityGroup{ SecurityGroupIds: b.config.SecurityGroupIds, diff --git a/builder/amazon/instance/builder.go b/builder/amazon/instance/builder.go index 11c91edc1..578fab40a 100644 --- a/builder/amazon/instance/builder.go +++ b/builder/amazon/instance/builder.go @@ -258,10 +258,9 @@ func (b *Builder) Run(ui packer.Ui, hook packer.Hook, cache packer.Cache) (packe }, &awscommon.StepKeyPair{ Debug: b.config.PackerDebug, + Comm: &b.config.RunConfig.Comm, SSHAgentAuth: b.config.Comm.SSHAgentAuth, DebugKeyPath: fmt.Sprintf("ec2_%s.pem", b.config.PackerBuildName), - KeyPairName: b.config.SSHKeyPairName, - PrivateKeyFile: b.config.RunConfig.Comm.SSHPrivateKeyFile, TemporaryKeyPairName: b.config.TemporaryKeyPairName, }, &awscommon.StepSecurityGroup{