Add SSH client

Signed-off-by: Simon Thulbourn <simon+github@thulbourn.com>
This commit is contained in:
Simon Thulbourn
2015-03-29 18:10:06 +01:00
parent 191564564e
commit 709b0a84e3
11 changed files with 276 additions and 184 deletions

View File

@@ -3,7 +3,6 @@ package drivers
import (
"errors"
"fmt"
"os/exec"
"sort"
log "github.com/Sirupsen/logrus"
@@ -174,21 +173,32 @@ type DriverOptions interface {
Bool(key string) bool
}
func GetSSHCommandFromDriver(d Driver, args ...string) (*exec.Cmd, error) {
func RunSSHCommandFromDriver(d Driver, args string) (ssh.Output, error) {
var output ssh.Output
host, err := d.GetSSHHostname()
if err != nil {
return nil, err
return output, err
}
port, err := d.GetSSHPort()
if err != nil {
return nil, err
return output, err
}
user := d.GetSSHUsername()
keyPath := d.GetSSHKeyPath()
return ssh.GetSSHCommand(host, port, user, keyPath, args...), nil
auth := &ssh.Auth{
Keys: []string{keyPath},
}
client, err := ssh.NewClient(user, host, port, auth)
if err != nil {
return output, err
}
return client.Run(args)
}
func MachineInState(d Driver, desiredState state.State) func() bool {

View File

@@ -234,9 +234,17 @@ func (c *ComputeUtil) deleteInstance() error {
func (c *ComputeUtil) executeCommands(commands []string, ip, sshKeyPath string) error {
for _, command := range commands {
cmd := ssh.GetSSHCommand(ip, 22, c.userName, sshKeyPath, command)
if err := cmd.Run(); err != nil {
return fmt.Errorf("error executing command: %v %v", command, err)
auth := &ssh.Auth{
Keys: []string{sshKeyPath},
}
client, err := ssh.NewClient(c.userName, ip, 22, auth)
if err != nil {
return err
}
if _, err := client.Run(command); err != nil {
return err
}
}
return nil

View File

@@ -452,19 +452,17 @@ func (d *Driver) GetIP() (string, error) {
if s != state.Running {
return "", drivers.ErrHostIsNotRunning
}
cmd, err := drivers.GetSSHCommandFromDriver(d, "ip addr show dev eth1")
output, err := drivers.RunSSHCommandFromDriver(d, "ip addr show dev eth1")
if err != nil {
return "", err
}
// reset to nil as if using from Host Stdout is already set when using DEBUG
cmd.Stdout = nil
b, err := cmd.Output()
if err != nil {
var buf bytes.Buffer
if _, err := buf.ReadFrom(output.Stdout); err != nil {
return "", err
}
out := string(b)
out := buf.String()
log.Debugf("SSH returned: %s\nEND SSH\n", out)
// parse to find: inet 192.168.59.103/24 brd 192.168.59.255 scope global eth1
lines := strings.Split(out, "\n")