mirror of
https://github.com/docker/docs.git
synced 2026-11-05 00:14:17 +03:00
Also, a few various cleanups are bundled: 1. Only call GetDriver() once to get the object in provision/utils.go 2. SSH command wrapper will return the error and let the consumer decide what to do with it instead of bailing automatically on non-255 Signed-off-by: Nathan LeClaire <nathan.leclaire@gmail.com>
70 lines
1.5 KiB
Go
70 lines
1.5 KiB
Go
package drivers
|
|
|
|
import (
|
|
"fmt"
|
|
|
|
"github.com/docker/machine/libmachine/log"
|
|
"github.com/docker/machine/libmachine/mcnutils"
|
|
"github.com/docker/machine/libmachine/ssh"
|
|
)
|
|
|
|
func GetSSHClientFromDriver(d Driver) (ssh.Client, error) {
|
|
addr, err := d.GetSSHHostname()
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
port, err := d.GetSSHPort()
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
auth := &ssh.Auth{
|
|
Keys: []string{d.GetSSHKeyPath()},
|
|
}
|
|
|
|
client, err := ssh.NewClient(d.GetSSHUsername(), addr, port, auth)
|
|
return client, err
|
|
|
|
}
|
|
|
|
func RunSSHCommandFromDriver(d Driver, command string) (string, error) {
|
|
client, err := GetSSHClientFromDriver(d)
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
|
|
log.Debugf("About to run SSH command:\n%s", command)
|
|
|
|
output, err := client.Output(command)
|
|
log.Debugf("SSH cmd err, output: %v: %s", err, output)
|
|
if err != nil {
|
|
returnedErr := fmt.Errorf(`Something went wrong running an SSH command!
|
|
command : %s
|
|
err : %v
|
|
output : %s
|
|
`, command, err, output)
|
|
return "", returnedErr
|
|
}
|
|
|
|
return output, nil
|
|
}
|
|
|
|
func sshAvailableFunc(d Driver) func() bool {
|
|
return func() bool {
|
|
log.Debug("Getting to WaitForSSH function...")
|
|
if _, err := RunSSHCommandFromDriver(d, "exit 0"); err != nil {
|
|
log.Debugf("Error getting ssh command 'exit 0' : %s", err)
|
|
return false
|
|
}
|
|
return true
|
|
}
|
|
}
|
|
|
|
func WaitForSSH(d Driver) error {
|
|
if err := mcnutils.WaitFor(sshAvailableFunc(d)); err != nil {
|
|
return fmt.Errorf("Too many retries waiting for SSH to be available. Last error: %s", err)
|
|
}
|
|
return nil
|
|
}
|