refactor ssh connection

This commit is contained in:
Michael Kuzmin 2017-08-31 15:05:26 +03:00 committed by Michael Kuzmin
parent d97c3ebaf0
commit ebacc4c2fa
7 changed files with 64 additions and 18 deletions

View File

@ -67,8 +67,7 @@ Hardware customization:
Provisioning:
* `ssh_username` - [**mandatory**] username in guest OS.
* `ssh_password` - [**mandatory if ssh_private_key_file is not present**] password in guest OS.
* `ssh_private_key_file` - [**mandatory if ssh_password is not present**] password in guest OS.
* `ssh_password` or `ssh_private_key_file` - [**mandatory**] password or SSH-key filename to access a guest OS.
Post-processing:
* `shutdown_command` - VMware guest tools are used by default.

View File

@ -43,7 +43,7 @@ func (b *Builder) Run(ui packer.Ui, hook packer.Hook, cache packer.Cache) (packe
},
&StepRun{},
&communicator.StepConnect{
Config: &b.config.Config,
Config: &b.config.Comm,
Host: commHost,
SSHConfig: sshConfig,
},

View File

@ -277,7 +277,7 @@ func checkLinkedClone(t *testing.T) builderT.TestCheckFunc {
t.Fatalf("Cannot read VM properties: %v", err)
}
if len(vmInfo.LayoutEx.Disk[0].Chain) != 3 {
if len(vmInfo.LayoutEx.Disk[0].Chain) != 2 {
t.Error("Not a linked clone")
}
@ -378,6 +378,21 @@ func checkRAMReservation(t *testing.T) builderT.TestCheckFunc {
}
}
func TestBuilderAcc_sshKey(t *testing.T) {
builderT.Test(t, builderT.TestCase{
Builder: &Builder{},
Template: sshKeyConfig(),
})
}
func sshKeyConfig() string {
config := defaultConfig()
config["ssh_password"] = ""
config["ssh_private_key_file"] = "test-key.pem"
config["linked_clone"] = true // speed up
return renderConfig(config)
}
func TestBuilderAcc_snapshot(t *testing.T) {
builderT.Test(t, builderT.TestCase{
Builder: &Builder{},

View File

@ -13,7 +13,7 @@ type Config struct {
ConnectConfig `mapstructure:",squash"`
CloneConfig `mapstructure:",squash"`
HardwareConfig `mapstructure:",squash"`
communicator.Config `mapstructure:",squash"`
Comm communicator.Config `mapstructure:",squash"`
ShutdownConfig `mapstructure:",squash"`
CreateSnapshot bool `mapstructure:"create_snapshot"`
ConvertToTemplate bool `mapstructure:"convert_to_template"`
@ -34,7 +34,7 @@ func NewConfig(raws ...interface{}) (*Config, []string, error) {
}
errs := new(packer.MultiError)
errs = packer.MultiErrorAppend(errs, c.Config.Prepare(&c.ctx)...)
errs = packer.MultiErrorAppend(errs, c.Comm.Prepare(&c.ctx)...)
errs = packer.MultiErrorAppend(errs, c.ConnectConfig.Prepare()...)
errs = packer.MultiErrorAppend(errs, c.CloneConfig.Prepare()...)
errs = packer.MultiErrorAppend(errs, c.HardwareConfig.Prepare()...)

28
ssh.go
View File

@ -16,18 +16,10 @@ func commHost(state multistep.StateBag) (string, error) {
func sshConfig(state multistep.StateBag) (*ssh.ClientConfig, error) {
config := state.Get("config").(*Config)
clientConfig := &ssh.ClientConfig{
User: config.Config.SSHUsername,
Auth: []ssh.AuthMethod{
ssh.Password(config.Config.SSHPassword),
ssh.KeyboardInteractive(
packerssh.PasswordKeyboardInteractive(config.Config.SSHPassword)),
},
HostKeyCallback: ssh.InsecureIgnoreHostKey(),
}
var auth []ssh.AuthMethod
if config.Config.SSHPrivateKey != "" {
privateKey, err := ioutil.ReadFile(config.Config.SSHPrivateKey)
if config.Comm.SSHPrivateKey != "" {
privateKey, err := ioutil.ReadFile(config.Comm.SSHPrivateKey)
if err != nil {
return nil, fmt.Errorf("Error loading configured private key file: %s", err)
}
@ -37,8 +29,20 @@ func sshConfig(state multistep.StateBag) (*ssh.ClientConfig, error) {
return nil, fmt.Errorf("Error setting up SSH config: %s", err)
}
clientConfig.Auth = []ssh.AuthMethod{ssh.PublicKeys(signer)}
auth = []ssh.AuthMethod{ssh.PublicKeys(signer)}
} else {
auth = []ssh.AuthMethod{
ssh.Password(config.Comm.SSHPassword),
ssh.KeyboardInteractive(
packerssh.PasswordKeyboardInteractive(config.Comm.SSHPassword)),
}
}
clientConfig := &ssh.ClientConfig{
User: config.Comm.SSHUsername,
HostKeyCallback: ssh.InsecureIgnoreHostKey(),
}
clientConfig.Auth = auth
return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