2013-09-02 23:23:52 -04:00
|
|
|
package qemu
|
|
|
|
|
|
|
|
import (
|
2014-04-26 14:23:26 -04:00
|
|
|
gossh "code.google.com/p/go.crypto/ssh"
|
2013-09-02 23:23:52 -04:00
|
|
|
"fmt"
|
|
|
|
"github.com/mitchellh/multistep"
|
|
|
|
"github.com/mitchellh/packer/communicator/ssh"
|
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
|
|
|
)
|
|
|
|
|
|
|
|
func sshAddress(state multistep.StateBag) (string, error) {
|
|
|
|
sshHostPort := state.Get("sshHostPort").(uint)
|
|
|
|
return fmt.Sprintf("127.0.0.1:%d", sshHostPort), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func sshConfig(state multistep.StateBag) (*gossh.ClientConfig, error) {
|
|
|
|
config := state.Get("config").(*config)
|
|
|
|
|
2014-04-10 04:48:55 -04:00
|
|
|
auth := []gossh.AuthMethod{
|
|
|
|
gossh.Password(config.SSHPassword),
|
|
|
|
gossh.KeyboardInteractive(
|
2013-09-02 23:23:52 -04:00
|
|
|
ssh.PasswordKeyboardInteractive(config.SSHPassword)),
|
|
|
|
}
|
|
|
|
|
|
|
|
if config.SSHKeyPath != "" {
|
2014-04-10 04:48:55 -04:00
|
|
|
signer, err := sshKeyToSigner(config.SSHKeyPath)
|
2013-09-02 23:23:52 -04:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2014-04-10 04:48:55 -04:00
|
|
|
auth = append(auth, gossh.PublicKeys(signer))
|
2013-09-02 23:23:52 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
return &gossh.ClientConfig{
|
|
|
|
User: config.SSHUser,
|
|
|
|
Auth: auth,
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2014-04-10 04:48:55 -04:00
|
|
|
func sshKeyToSigner(path string) (gossh.Signer, error) {
|
2013-09-02 23:23:52 -04:00
|
|
|
f, err := os.Open(path)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
defer f.Close()
|
|
|
|
|
|
|
|
keyBytes, err := ioutil.ReadAll(f)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2014-04-10 04:48:55 -04:00
|
|
|
signer, err := gossh.ParsePrivateKey(keyBytes)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("Error setting up SSH config: %s", err)
|
2013-09-02 23:23:52 -04:00
|
|
|
}
|
|
|
|
|
2014-04-10 04:48:55 -04:00
|
|
|
return signer, nil
|
2013-09-02 23:23:52 -04:00
|
|
|
}
|