packer-cn/builder/profitbricks/ssh.go

48 lines
1.1 KiB
Go
Raw Normal View History

2016-06-28 22:35:41 -04:00
package profitbricks
import (
"fmt"
2017-04-04 16:39:01 -04:00
"github.com/hashicorp/packer/communicator/ssh"
2016-06-28 22:35:41 -04:00
"github.com/mitchellh/multistep"
2016-11-15 18:17:30 -05:00
gossh "golang.org/x/crypto/ssh"
2016-06-28 22:35:41 -04:00
)
func commHost(state multistep.StateBag) (string, error) {
ipAddress := state.Get("server_ip").(string)
return ipAddress, nil
}
2016-11-15 18:17:30 -05:00
func sshConfig(state multistep.StateBag) (*gossh.ClientConfig, error) {
2016-06-28 22:35:41 -04:00
config := state.Get("config").(*Config)
2016-11-15 18:17:30 -05:00
var privateKey string
2016-06-28 22:35:41 -04:00
2016-11-15 18:17:30 -05:00
var auth []gossh.AuthMethod
if config.Comm.SSHPassword != "" {
auth = []gossh.AuthMethod{
gossh.Password(config.Comm.SSHPassword),
gossh.KeyboardInteractive(
ssh.PasswordKeyboardInteractive(config.Comm.SSHPassword)),
}
2016-06-28 22:35:41 -04:00
}
2016-11-15 18:17:30 -05:00
if config.Comm.SSHPrivateKey != "" {
if priv, ok := state.GetOk("privateKey"); ok {
privateKey = priv.(string)
}
signer, err := gossh.ParsePrivateKey([]byte(privateKey))
if err != nil {
return nil, fmt.Errorf("Error setting up SSH config: %s", err)
}
if err != nil {
return nil, err
}
auth = append(auth, gossh.PublicKeys(signer))
}
return &gossh.ClientConfig{
2016-06-28 22:35:41 -04:00
User: config.Comm.SSHUsername,
2016-11-15 18:17:30 -05:00
Auth: auth,
2016-06-28 22:35:41 -04:00
}, nil
}