2015-06-15 01:09:38 -04:00
|
|
|
package docker
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"io/ioutil"
|
|
|
|
|
2017-04-04 16:39:01 -04:00
|
|
|
"github.com/hashicorp/packer/communicator/ssh"
|
|
|
|
"github.com/hashicorp/packer/helper/communicator"
|
2018-01-19 19:18:44 -05:00
|
|
|
"github.com/hashicorp/packer/helper/multistep"
|
2015-06-15 01:09:38 -04:00
|
|
|
gossh "golang.org/x/crypto/ssh"
|
|
|
|
)
|
|
|
|
|
|
|
|
func commHost(state multistep.StateBag) (string, error) {
|
|
|
|
containerId := state.Get("container_id").(string)
|
|
|
|
driver := state.Get("driver").(Driver)
|
|
|
|
return driver.IPAddress(containerId)
|
|
|
|
}
|
|
|
|
|
|
|
|
func sshConfig(comm *communicator.Config) func(state multistep.StateBag) (*gossh.ClientConfig, error) {
|
|
|
|
return func(state multistep.StateBag) (*gossh.ClientConfig, error) {
|
|
|
|
if comm.SSHPrivateKey != "" {
|
|
|
|
// key based auth
|
|
|
|
bytes, err := ioutil.ReadFile(comm.SSHPrivateKey)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("Error setting up SSH config: %s", err)
|
|
|
|
}
|
|
|
|
privateKey := string(bytes)
|
|
|
|
|
|
|
|
signer, err := gossh.ParsePrivateKey([]byte(privateKey))
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("Error setting up SSH config: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return &gossh.ClientConfig{
|
|
|
|
User: comm.SSHUsername,
|
|
|
|
Auth: []gossh.AuthMethod{
|
|
|
|
gossh.PublicKeys(signer),
|
|
|
|
},
|
2017-06-21 14:59:57 -04:00
|
|
|
HostKeyCallback: gossh.InsecureIgnoreHostKey(),
|
2015-06-15 01:09:38 -04:00
|
|
|
}, nil
|
|
|
|
} else {
|
|
|
|
// password based auth
|
|
|
|
return &gossh.ClientConfig{
|
|
|
|
User: comm.SSHUsername,
|
|
|
|
Auth: []gossh.AuthMethod{
|
|
|
|
gossh.Password(comm.SSHPassword),
|
|
|
|
gossh.KeyboardInteractive(
|
|
|
|
ssh.PasswordKeyboardInteractive(comm.SSHPassword)),
|
|
|
|
},
|
2017-06-21 14:59:57 -04:00
|
|
|
HostKeyCallback: gossh.InsecureIgnoreHostKey(),
|
2015-06-15 01:09:38 -04:00
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|