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

84 lines
2.0 KiB
Go
Raw Normal View History

2013-12-24 01:27:01 -05:00
package common
import (
"errors"
"fmt"
"log"
2013-12-24 01:27:01 -05:00
2017-04-04 16:39:01 -04:00
commonssh "github.com/hashicorp/packer/common/ssh"
"github.com/hashicorp/packer/communicator/ssh"
"github.com/hashicorp/packer/helper/multistep"
2015-06-09 00:34:20 -04:00
gossh "golang.org/x/crypto/ssh"
2013-12-24 01:27:01 -05:00
)
func CommHost(config *SSHConfig) func(multistep.StateBag) (string, error) {
return func(state multistep.StateBag) (string, error) {
driver := state.Get("driver").(Driver)
vmxPath := state.Get("vmx_path").(string)
2015-06-13 18:52:44 -04:00
if config.Comm.SSHHost != "" {
return config.Comm.SSHHost, nil
}
log.Println("Lookup up IP information...")
vmxData, err := ReadVMX(vmxPath)
if err != nil {
return "", err
}
var ok bool
macAddress := ""
if macAddress, ok = vmxData["ethernet0.address"]; !ok || macAddress == "" {
if macAddress, ok = vmxData["ethernet0.generatedaddress"]; !ok || macAddress == "" {
return "", errors.New("couldn't find MAC address in VMX")
}
}
ipLookup := &DHCPLeaseGuestLookup{
Driver: driver,
Device: "vmnet8",
MACAddress: macAddress,
}
ipAddress, err := ipLookup.GuestIP()
if err != nil {
log.Printf("IP lookup failed: %s", err)
return "", fmt.Errorf("IP lookup failed: %s", err)
}
if ipAddress == "" {
log.Println("IP is blank, no IP yet.")
return "", errors.New("IP is blank")
}
log.Printf("Detected IP: %s", ipAddress)
return ipAddress, nil
}
}
2013-12-24 13:31:57 -05:00
func SSHConfigFunc(config *SSHConfig) func(multistep.StateBag) (*gossh.ClientConfig, error) {
return func(state multistep.StateBag) (*gossh.ClientConfig, error) {
auth := []gossh.AuthMethod{
2015-06-13 18:52:44 -04:00
gossh.Password(config.Comm.SSHPassword),
gossh.KeyboardInteractive(
2015-06-13 18:52:44 -04:00
ssh.PasswordKeyboardInteractive(config.Comm.SSHPassword)),
}
2015-06-13 18:52:44 -04:00
if config.Comm.SSHPrivateKey != "" {
signer, err := commonssh.FileSigner(config.Comm.SSHPrivateKey)
if err != nil {
return nil, err
}
auth = append(auth, gossh.PublicKeys(signer))
}
return &gossh.ClientConfig{
User: config.Comm.SSHUsername,
Auth: auth,
HostKeyCallback: gossh.InsecureIgnoreHostKey(),
}, nil
}
}