Merge pull request #1951 from nathanleclaire/windows_ssh
Fix Windows SSH issues
This commit is contained in:
@@ -74,15 +74,6 @@ func (h *Host) CreateSSHClient() (ssh.Client, error) {
|
||||
return ssh.NewClient(h.Driver.GetSSHUsername(), addr, port, auth)
|
||||
}
|
||||
|
||||
func (h *Host) CreateSSHShell() error {
|
||||
client, err := h.CreateSSHClient()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return client.Shell()
|
||||
}
|
||||
|
||||
func (h *Host) runActionForState(action func() error, desiredState state.State) error {
|
||||
if drivers.MachineInState(h.Driver, desiredState)() {
|
||||
return fmt.Errorf("Machine %q is already %s.", h.Name, strings.ToLower(desiredState.String()))
|
||||
|
||||
@@ -16,7 +16,7 @@ import (
|
||||
|
||||
type Client interface {
|
||||
Output(command string) (string, error)
|
||||
Shell() error
|
||||
Shell(args ...string) error
|
||||
}
|
||||
|
||||
type ExternalClient struct {
|
||||
@@ -195,7 +195,7 @@ func (client NativeClient) OutputWithPty(command string) (string, error) {
|
||||
return string(output), err
|
||||
}
|
||||
|
||||
func (client NativeClient) Shell() error {
|
||||
func (client NativeClient) Shell(args ...string) error {
|
||||
var (
|
||||
termWidth, termHeight int
|
||||
)
|
||||
@@ -243,12 +243,15 @@ func (client NativeClient) Shell() error {
|
||||
return err
|
||||
}
|
||||
|
||||
if err := session.Shell(); err != nil {
|
||||
return err
|
||||
if len(args) == 0 {
|
||||
if err := session.Shell(); err != nil {
|
||||
return err
|
||||
}
|
||||
session.Wait()
|
||||
} else {
|
||||
session.Run(strings.Join(args, " "))
|
||||
}
|
||||
|
||||
session.Wait()
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -272,23 +275,21 @@ func NewExternalClient(sshBinaryPath, user, host string, port int, auth *Auth) (
|
||||
return client, nil
|
||||
}
|
||||
|
||||
func getSSHCmd(binaryPath string, args ...string) *exec.Cmd {
|
||||
return exec.Command(binaryPath, args...)
|
||||
}
|
||||
|
||||
func (client ExternalClient) Output(command string) (string, error) {
|
||||
// TODO: Ugh, gross hack. Replace with all instances using variadic
|
||||
// syntax
|
||||
args := append(client.BaseArgs, strings.Split(command, " ")...)
|
||||
|
||||
cmd := exec.Command(client.BinaryPath, args...)
|
||||
log.Debug(cmd)
|
||||
|
||||
// Allow piping of local things to remote commands.
|
||||
cmd.Stdin = os.Stdin
|
||||
|
||||
args := append(client.BaseArgs, command)
|
||||
cmd := getSSHCmd(client.BinaryPath, args...)
|
||||
output, err := cmd.CombinedOutput()
|
||||
return string(output), err
|
||||
}
|
||||
|
||||
func (client ExternalClient) Shell() error {
|
||||
cmd := exec.Command(client.BinaryPath, client.BaseArgs...)
|
||||
func (client ExternalClient) Shell(args ...string) error {
|
||||
args = append(client.BaseArgs, args...)
|
||||
cmd := getSSHCmd(client.BinaryPath, args...)
|
||||
|
||||
log.Debug(cmd)
|
||||
|
||||
cmd.Stdin = os.Stdin
|
||||
|
||||
45
libmachine/ssh/client_test.go
Normal file
45
libmachine/ssh/client_test.go
Normal file
@@ -0,0 +1,45 @@
|
||||
package ssh
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
func TestGetSSHCmdArgs(t *testing.T) {
|
||||
cases := []struct {
|
||||
binaryPath string
|
||||
args []string
|
||||
expectedArgs []string
|
||||
}{
|
||||
{
|
||||
binaryPath: "/usr/local/bin/ssh",
|
||||
args: []string{
|
||||
"docker@localhost",
|
||||
"apt-get install -y htop",
|
||||
},
|
||||
expectedArgs: []string{
|
||||
"/usr/local/bin/ssh",
|
||||
"docker@localhost",
|
||||
"apt-get install -y htop",
|
||||
},
|
||||
},
|
||||
{
|
||||
binaryPath: "C:\\Program Files\\Git\\bin\\ssh.exe",
|
||||
args: []string{
|
||||
"docker@localhost",
|
||||
"sudo /usr/bin/sethostname foobar && echo 'foobar' | sudo tee /var/lib/boot2docker/etc/hostname",
|
||||
},
|
||||
expectedArgs: []string{
|
||||
"C:\\Program Files\\Git\\bin\\ssh.exe",
|
||||
"docker@localhost",
|
||||
"sudo /usr/bin/sethostname foobar && echo 'foobar' | sudo tee /var/lib/boot2docker/etc/hostname",
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
for _, c := range cases {
|
||||
cmd := getSSHCmd(c.binaryPath, c.args...)
|
||||
assert.Equal(t, cmd.Args, c.expectedArgs)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user