packer-cn/builder/virtualbox/common/ssh.go

40 lines
992 B
Go
Raw Normal View History

2013-12-22 12:08:09 -05:00
package common
import (
"fmt"
2013-12-22 12:08:09 -05:00
"github.com/mitchellh/multistep"
commonssh "github.com/mitchellh/packer/common/ssh"
2013-12-22 12:08:09 -05:00
"github.com/mitchellh/packer/communicator/ssh"
2015-06-09 00:34:20 -04:00
gossh "golang.org/x/crypto/ssh"
2013-12-22 12:08:09 -05:00
)
func SSHAddress(state multistep.StateBag) (string, error) {
sshHostPort := state.Get("sshHostPort").(uint)
return fmt.Sprintf("127.0.0.1:%d", sshHostPort), nil
}
func SSHConfigFunc(config SSHConfig) func(multistep.StateBag) (*gossh.ClientConfig, error) {
return func(state multistep.StateBag) (*gossh.ClientConfig, error) {
auth := []gossh.AuthMethod{
gossh.Password(config.Comm.SSHPassword),
gossh.KeyboardInteractive(
ssh.PasswordKeyboardInteractive(config.Comm.SSHPassword)),
2013-12-22 12:08:09 -05:00
}
if config.SSHKeyPath != "" {
signer, err := commonssh.FileSigner(config.Comm.SSHPrivateKey)
2013-12-22 12:08:09 -05:00
if err != nil {
return nil, err
}
2014-04-26 14:12:43 -04:00
auth = append(auth, gossh.PublicKeys(signer))
2013-12-22 12:08:09 -05:00
}
return &gossh.ClientConfig{
User: config.Comm.SSHUsername,
2013-12-22 12:08:09 -05:00
Auth: auth,
}, nil
}
}