builder/amazon: extract SSH connect funcs
This commit is contained in:
parent
64ced7ff2c
commit
2f9840a4cf
|
@ -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
|
||||||
|
}
|
||||||
|
}
|
|
@ -24,8 +24,6 @@ const BuilderId = "mitchellh.amazonebs"
|
||||||
type config struct {
|
type config struct {
|
||||||
common.PackerConfig `mapstructure:",squash"`
|
common.PackerConfig `mapstructure:",squash"`
|
||||||
awscommon.AccessConfig `mapstructure:",squash"`
|
awscommon.AccessConfig `mapstructure:",squash"`
|
||||||
VpcId string `mapstructure:"vpc_id"`
|
|
||||||
SubnetId string `mapstructure:"subnet_id"`
|
|
||||||
awscommon.RunConfig `mapstructure:",squash"`
|
awscommon.RunConfig `mapstructure:",squash"`
|
||||||
|
|
||||||
// Configuration of the resulting AMI
|
// 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,
|
SourceAMI: b.config.SourceAmi,
|
||||||
},
|
},
|
||||||
&common.StepConnectSSH{
|
&common.StepConnectSSH{
|
||||||
SSHAddress: sshAddress,
|
SSHAddress: awscommon.SSHAddress(b.config.SSHPort),
|
||||||
SSHConfig: sshConfig,
|
SSHConfig: awscommon.SSHConfig(b.config.SSHUsername),
|
||||||
SSHWaitTimeout: b.config.SSHTimeout(),
|
SSHWaitTimeout: b.config.SSHTimeout(),
|
||||||
},
|
},
|
||||||
&common.StepProvision{},
|
&common.StepProvision{},
|
||||||
|
|
|
@ -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
|
|
||||||
}
|
|
|
@ -79,6 +79,11 @@ func (b *Builder) Run(ui packer.Ui, hook packer.Hook, cache packer.Cache) (packe
|
||||||
InstanceType: b.config.InstanceType,
|
InstanceType: b.config.InstanceType,
|
||||||
SourceAMI: b.config.SourceAmi,
|
SourceAMI: b.config.SourceAmi,
|
||||||
},
|
},
|
||||||
|
&common.StepConnectSSH{
|
||||||
|
SSHAddress: awscommon.SSHAddress(b.config.SSHPort),
|
||||||
|
SSHConfig: awscommon.SSHConfig(b.config.SSHUsername),
|
||||||
|
SSHWaitTimeout: b.config.SSHTimeout(),
|
||||||
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
// Run!
|
// Run!
|
||||||
|
|
Loading…
Reference in New Issue