enable ssh key file

This commit is contained in:
bugbuilder 2017-08-25 02:08:08 -03:00 committed by Michael Kuzmin
parent a12121616d
commit d97c3ebaf0
3 changed files with 51 additions and 20 deletions

View file

@ -67,7 +67,8 @@ Hardware customization:
Provisioning:
* `ssh_username` - [**mandatory**] username in guest OS.
* `ssh_password` - [**mandatory**] password 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.
Post-processing:
* `shutdown_command` - VMware guest tools are used by default.

View file

@ -4,12 +4,10 @@ import (
"errors"
"github.com/hashicorp/packer/common"
"github.com/hashicorp/packer/packer"
"github.com/mitchellh/multistep"
"github.com/hashicorp/packer/helper/communicator"
gossh "golang.org/x/crypto/ssh"
"github.com/hashicorp/packer/communicator/ssh"
"github.com/hashicorp/packer/packer"
"github.com/jetbrains-infra/packer-builder-vsphere/driver"
"github.com/mitchellh/multistep"
)
type Builder struct {
@ -29,6 +27,7 @@ func (b *Builder) Prepare(raws ...interface{}) ([]string, error) {
func (b *Builder) Run(ui packer.Ui, hook packer.Hook, cache packer.Cache) (packer.Artifact, error) {
state := new(multistep.BasicStateBag)
state.Put("config", b.config)
state.Put("hook", hook)
state.Put("ui", ui)
@ -45,21 +44,8 @@ func (b *Builder) Run(ui packer.Ui, hook packer.Hook, cache packer.Cache) (packe
&StepRun{},
&communicator.StepConnect{
Config: &b.config.Config,
Host: func(state multistep.StateBag) (string, error) {
return state.Get("ip").(string), nil
},
SSHConfig: func(multistep.StateBag) (*gossh.ClientConfig, error) {
return &gossh.ClientConfig{
User: b.config.Config.SSHUsername,
Auth: []gossh.AuthMethod{
gossh.Password(b.config.Config.SSHPassword),
gossh.KeyboardInteractive(
ssh.PasswordKeyboardInteractive(b.config.Config.SSHPassword)),
},
// TODO: add a proper verification
HostKeyCallback: gossh.InsecureIgnoreHostKey(),
}, nil
},
Host: commHost,
SSHConfig: sshConfig,
},
&common.StepProvision{},
&StepShutdown{

44
ssh.go Normal file
View file

@ -0,0 +1,44 @@
package main
import (
"fmt"
"io/ioutil"
packerssh "github.com/hashicorp/packer/communicator/ssh"
"github.com/mitchellh/multistep"
"golang.org/x/crypto/ssh"
)
func commHost(state multistep.StateBag) (string, error) {
return state.Get("ip").(string), nil
}
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(),
}
if config.Config.SSHPrivateKey != "" {
privateKey, err := ioutil.ReadFile(config.Config.SSHPrivateKey)
if err != nil {
return nil, fmt.Errorf("Error loading configured private key file: %s", err)
}
signer, err := ssh.ParsePrivateKey(privateKey)
if err != nil {
return nil, fmt.Errorf("Error setting up SSH config: %s", err)
}
clientConfig.Auth = []ssh.AuthMethod{ssh.PublicKeys(signer)}
}
return clientConfig, nil
}