communicator/ssh: Adhere to new Communicator interface
This commit is contained in:
parent
a21fe8c484
commit
1cea606f12
|
@ -23,33 +23,19 @@ func New(c net.Conn, config *ssh.ClientConfig) (result *comm, err error) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *comm) Start(cmd string) (remote *packer.RemoteCommand, err error) {
|
func (c *comm) Start(cmd *packer.RemoteCmd) (err error) {
|
||||||
session, err := c.client.NewSession()
|
session, err := c.client.NewSession()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
// Create the buffers to store our stdin/stdout/stderr
|
|
||||||
stdin := new(bytes.Buffer)
|
|
||||||
stdout := new(bytes.Buffer)
|
|
||||||
stderr := new(bytes.Buffer)
|
|
||||||
|
|
||||||
// Setup our session
|
// Setup our session
|
||||||
session.Stdin = stdin
|
session.Stdin = cmd.Stdin
|
||||||
session.Stdout = stdout
|
session.Stdout = cmd.Stdout
|
||||||
session.Stderr = stderr
|
session.Stderr = cmd.Stderr
|
||||||
|
|
||||||
// Setup the remote command
|
log.Printf("starting remote command: %s", cmd.Command)
|
||||||
remote = &packer.RemoteCommand{
|
err = session.Start(cmd.Command + "\n")
|
||||||
Stdin: stdin,
|
|
||||||
Stdout: stdout,
|
|
||||||
Stderr: stderr,
|
|
||||||
Exited: false,
|
|
||||||
ExitStatus: -1,
|
|
||||||
}
|
|
||||||
|
|
||||||
log.Printf("starting remote command: %s", cmd)
|
|
||||||
err = session.Start(cmd + "\n")
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
@ -60,15 +46,15 @@ func (c *comm) Start(cmd string) (remote *packer.RemoteCommand, err error) {
|
||||||
defer session.Close()
|
defer session.Close()
|
||||||
|
|
||||||
err := session.Wait()
|
err := session.Wait()
|
||||||
remote.ExitStatus = 0
|
cmd.ExitStatus = 0
|
||||||
if err != nil {
|
if err != nil {
|
||||||
exitErr, ok := err.(*ssh.ExitError)
|
exitErr, ok := err.(*ssh.ExitError)
|
||||||
if ok {
|
if ok {
|
||||||
remote.ExitStatus = exitErr.ExitStatus()
|
cmd.ExitStatus = exitErr.ExitStatus()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
remote.Exited = true
|
cmd.Exited = true
|
||||||
}()
|
}()
|
||||||
|
|
||||||
return
|
return
|
||||||
|
|
|
@ -8,6 +8,7 @@ import (
|
||||||
"net"
|
"net"
|
||||||
"strings"
|
"strings"
|
||||||
"testing"
|
"testing"
|
||||||
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
// private key for mock server
|
// private key for mock server
|
||||||
|
@ -178,19 +179,24 @@ func TestStart(t *testing.T) {
|
||||||
t.Fatalf("error connecting to SSH: %s", err)
|
t.Fatalf("error connecting to SSH: %s", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
remote, err := client.Start("echo foo")
|
var cmd packer.RemoteCmd
|
||||||
|
stdout := new(bytes.Buffer)
|
||||||
|
cmd.Command = "echo foo"
|
||||||
|
cmd.Stdout = stdout
|
||||||
|
|
||||||
|
err = client.Start(&cmd)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatalf("error executing command: %s", err)
|
t.Fatalf("error executing command: %s", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Wait for it to complete
|
// Wait for it to complete
|
||||||
t.Log("Waiting for command to complete")
|
t.Log("Waiting for command to complete")
|
||||||
remote.Wait()
|
for !cmd.Exited {
|
||||||
|
time.Sleep(50 * time.Millisecond)
|
||||||
|
}
|
||||||
|
|
||||||
// Should have the correct output
|
// Should have the correct output
|
||||||
output := remote.Stdout.(*bytes.Buffer).String()
|
if stdout.String() != "ack: echo foo" {
|
||||||
|
t.Fatalf("unknown output: %#v", stdout.String())
|
||||||
if output != "ack: echo foo" {
|
|
||||||
t.Fatalf("unknown output: %#v", output)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue