From 5c7c0a6ee209c7da6ac57cb21296c156b88fa87d Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Sat, 20 Jul 2013 20:03:00 -0700 Subject: [PATCH] builder/amazon: extract SSH connect funcs --- builder/amazon/common/ssh.go | 33 ++++++++++++++++++++++++++++ builder/amazon/ebs/builder.go | 6 ++--- builder/amazon/ebs/ssh.go | 35 ------------------------------ builder/amazon/instance/builder.go | 5 +++++ 4 files changed, 40 insertions(+), 39 deletions(-) create mode 100644 builder/amazon/common/ssh.go delete mode 100644 builder/amazon/ebs/ssh.go diff --git a/builder/amazon/common/ssh.go b/builder/amazon/common/ssh.go new file mode 100644 index 000000000..77e1d6c53 --- /dev/null +++ b/builder/amazon/common/ssh.go @@ -0,0 +1,33 @@ +package common + +import ( + gossh "code.google.com/p/go.crypto/ssh" + "fmt" + "github.com/mitchellh/goamz/ec2" + "github.com/mitchellh/packer/communicator/ssh" +) + +func SSHAddress(port int) func(map[string]interface{}) (string, error) { + return func(state map[string]interface{}) (string, error) { + instance := state["instance"].(*ec2.Instance) + return fmt.Sprintf("%s:%d", instance.DNSName, port), nil + } +} + +func SSHConfig(username string) func(map[string]interface{}) (*gossh.ClientConfig, error) { + return func(state map[string]interface{}) (*gossh.ClientConfig, error) { + privateKey := state["privateKey"].(string) + + keyring := new(ssh.SimpleKeychain) + if err := keyring.AddPEMKey(privateKey); err != nil { + return nil, fmt.Errorf("Error setting up SSH config: %s", err) + } + + return &gossh.ClientConfig{ + User: username, + Auth: []gossh.ClientAuth{ + gossh.ClientAuthKeyring(keyring), + }, + }, nil + } +} diff --git a/builder/amazon/ebs/builder.go b/builder/amazon/ebs/builder.go index 03c23bf64..48658f636 100644 --- a/builder/amazon/ebs/builder.go +++ b/builder/amazon/ebs/builder.go @@ -24,8 +24,6 @@ const BuilderId = "mitchellh.amazonebs" type config struct { common.PackerConfig `mapstructure:",squash"` awscommon.AccessConfig `mapstructure:",squash"` - VpcId string `mapstructure:"vpc_id"` - SubnetId string `mapstructure:"subnet_id"` awscommon.RunConfig `mapstructure:",squash"` // Configuration of the resulting AMI @@ -101,8 +99,8 @@ func (b *Builder) Run(ui packer.Ui, hook packer.Hook, cache packer.Cache) (packe SourceAMI: b.config.SourceAmi, }, &common.StepConnectSSH{ - SSHAddress: sshAddress, - SSHConfig: sshConfig, + SSHAddress: awscommon.SSHAddress(b.config.SSHPort), + SSHConfig: awscommon.SSHConfig(b.config.SSHUsername), SSHWaitTimeout: b.config.SSHTimeout(), }, &common.StepProvision{}, diff --git a/builder/amazon/ebs/ssh.go b/builder/amazon/ebs/ssh.go deleted file mode 100644 index 599bf5ad9..000000000 --- a/builder/amazon/ebs/ssh.go +++ /dev/null @@ -1,35 +0,0 @@ -package ebs - -import ( - gossh "code.google.com/p/go.crypto/ssh" - "fmt" - "github.com/mitchellh/goamz/ec2" - "github.com/mitchellh/packer/communicator/ssh" -) - -func sshAddress(state map[string]interface{}) (string, error) { - config := state["config"].(config) - instance := state["instance"].(*ec2.Instance) - if config.VpcId == "" { - return fmt.Sprintf("%s:%d", instance.DNSName, config.SSHPort), nil - } else { - return fmt.Sprintf("%s:%d", instance.PrivateIpAddress, config.SSHPort), nil - } -} - -func sshConfig(state map[string]interface{}) (*gossh.ClientConfig, error) { - config := state["config"].(config) - privateKey := state["privateKey"].(string) - - keyring := new(ssh.SimpleKeychain) - if err := keyring.AddPEMKey(privateKey); err != nil { - return nil, fmt.Errorf("Error setting up SSH config: %s", err) - } - - return &gossh.ClientConfig{ - User: config.SSHUsername, - Auth: []gossh.ClientAuth{ - gossh.ClientAuthKeyring(keyring), - }, - }, nil -} diff --git a/builder/amazon/instance/builder.go b/builder/amazon/instance/builder.go index 908a23a53..0606de521 100644 --- a/builder/amazon/instance/builder.go +++ b/builder/amazon/instance/builder.go @@ -79,6 +79,11 @@ func (b *Builder) Run(ui packer.Ui, hook packer.Hook, cache packer.Cache) (packe InstanceType: b.config.InstanceType, SourceAMI: b.config.SourceAmi, }, + &common.StepConnectSSH{ + SSHAddress: awscommon.SSHAddress(b.config.SSHPort), + SSHConfig: awscommon.SSHConfig(b.config.SSHUsername), + SSHWaitTimeout: b.config.SSHTimeout(), + }, } // Run!