forked from hashicorp/packer
/
ssh.go
56 lines (44 loc) · 1.43 KB
/
ssh.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
package cloudstack
import (
"fmt"
"io/ioutil"
"github.com/mitchellh/multistep"
packerssh "github.com/mitchellh/packer/communicator/ssh"
"github.com/xanzy/go-cloudstack/cloudstack"
"golang.org/x/crypto/ssh"
)
func commHost(state multistep.StateBag) (string, error) {
client := state.Get("client").(*cloudstack.CloudStackClient)
config := state.Get("config").(*Config)
if config.hostAddress == "" {
ipAddr, _, err := client.Address.GetPublicIpAddressByID(config.PublicIPAddress)
if err != nil {
return "", fmt.Errorf("Failed to retrieve IP address: %s", err)
}
config.hostAddress = ipAddr.Ipaddress
}
return config.hostAddress, nil
}
func sshConfig(state multistep.StateBag) (*ssh.ClientConfig, error) {
config := state.Get("config").(*Config)
clientConfig := &ssh.ClientConfig{
User: config.Comm.SSHUsername,
Auth: []ssh.AuthMethod{
ssh.Password(config.Comm.SSHPassword),
ssh.KeyboardInteractive(
packerssh.PasswordKeyboardInteractive(config.Comm.SSHPassword)),
},
}
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)
}
signer, err := ssh.ParsePrivateKey([]byte(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
}