2013-05-20 18:47:41 -04:00
|
|
|
package ssh
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"code.google.com/p/go.crypto/ssh"
|
2013-05-24 11:23:48 -04:00
|
|
|
"fmt"
|
2013-05-20 18:47:41 -04:00
|
|
|
"github.com/mitchellh/packer/packer"
|
2013-05-20 18:52:34 -04:00
|
|
|
"io"
|
2013-05-20 18:47:41 -04:00
|
|
|
"log"
|
|
|
|
"net"
|
2013-05-24 11:23:48 -04:00
|
|
|
"path/filepath"
|
2013-05-20 18:47:41 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
type comm struct {
|
|
|
|
client *ssh.ClientConn
|
|
|
|
}
|
|
|
|
|
|
|
|
// Creates a new packer.Communicator implementation over SSH. This takes
|
|
|
|
// an already existing TCP connection and SSH configuration.
|
|
|
|
func New(c net.Conn, config *ssh.ClientConfig) (result *comm, err error) {
|
|
|
|
client, err := ssh.Client(c, config)
|
|
|
|
result = &comm{client}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2013-06-03 02:27:01 -04:00
|
|
|
func (c *comm) Start(cmd *packer.RemoteCmd) (err error) {
|
2013-05-20 18:47:41 -04:00
|
|
|
session, err := c.client.NewSession()
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Setup our session
|
2013-06-03 02:27:01 -04:00
|
|
|
session.Stdin = cmd.Stdin
|
|
|
|
session.Stdout = cmd.Stdout
|
|
|
|
session.Stderr = cmd.Stderr
|
2013-05-20 18:47:41 -04:00
|
|
|
|
2013-06-03 02:27:01 -04:00
|
|
|
log.Printf("starting remote command: %s", cmd.Command)
|
|
|
|
err = session.Start(cmd.Command + "\n")
|
2013-05-20 18:47:41 -04:00
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Start a goroutine to wait for the session to end and set the
|
|
|
|
// exit boolean and status.
|
|
|
|
go func() {
|
|
|
|
defer session.Close()
|
|
|
|
|
|
|
|
err := session.Wait()
|
2013-06-03 02:27:01 -04:00
|
|
|
cmd.ExitStatus = 0
|
2013-05-20 18:47:41 -04:00
|
|
|
if err != nil {
|
|
|
|
exitErr, ok := err.(*ssh.ExitError)
|
|
|
|
if ok {
|
2013-06-03 02:27:01 -04:00
|
|
|
cmd.ExitStatus = exitErr.ExitStatus()
|
2013-05-20 18:47:41 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-06-03 02:27:01 -04:00
|
|
|
cmd.Exited = true
|
2013-05-20 18:47:41 -04:00
|
|
|
}()
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
2013-05-20 18:52:34 -04:00
|
|
|
|
2013-05-24 11:23:48 -04:00
|
|
|
func (c *comm) Upload(path string, input io.Reader) error {
|
2013-05-28 01:05:33 -04:00
|
|
|
log.Println("Opening new SSH session")
|
2013-05-24 11:23:48 -04:00
|
|
|
session, err := c.client.NewSession()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
defer session.Close()
|
|
|
|
|
|
|
|
// Get a pipe to stdin so that we can send data down
|
|
|
|
w, err := session.StdinPipe()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2013-05-24 12:06:13 -04:00
|
|
|
// Set stderr/stdout to a bytes buffer
|
|
|
|
stderr := new(bytes.Buffer)
|
|
|
|
stdout := new(bytes.Buffer)
|
|
|
|
session.Stderr = stderr
|
|
|
|
session.Stdout = stdout
|
|
|
|
|
2013-05-24 11:23:48 -04:00
|
|
|
// We only want to close once, so we nil w after we close it,
|
|
|
|
// and only close in the defer if it hasn't been closed already.
|
|
|
|
defer func() {
|
|
|
|
if w != nil {
|
|
|
|
w.Close()
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
// The target directory and file for talking the SCP protocol
|
|
|
|
target_dir := filepath.Dir(path)
|
|
|
|
target_file := filepath.Base(path)
|
|
|
|
|
|
|
|
// Start the sink mode on the other side
|
|
|
|
// TODO(mitchellh): There are probably issues with shell escaping the path
|
|
|
|
log.Println("Starting remote scp process in sink mode")
|
2013-05-24 12:06:13 -04:00
|
|
|
if err = session.Start("scp -vt " + target_dir); err != nil {
|
2013-05-24 11:23:48 -04:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Determine the length of the upload content by copying it
|
|
|
|
// into an in-memory buffer. Note that this means what we upload
|
|
|
|
// must fit into memory.
|
|
|
|
log.Println("Copying input data into in-memory buffer so we can get the length")
|
|
|
|
input_memory := new(bytes.Buffer)
|
|
|
|
if _, err = io.Copy(input_memory, input); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Start the protocol
|
2013-05-28 01:05:33 -04:00
|
|
|
log.Println("Beginning file upload...")
|
2013-05-24 11:23:48 -04:00
|
|
|
fmt.Fprintln(w, "C0644", input_memory.Len(), target_file)
|
|
|
|
io.Copy(w, input_memory)
|
2013-05-24 12:40:20 -04:00
|
|
|
fmt.Fprint(w, "\x00")
|
|
|
|
|
|
|
|
// TODO(mitchellh): Each step above results in a 0/1/2 being sent by
|
|
|
|
// the remote side to confirm. We should check for those confirmations.
|
2013-05-24 11:23:48 -04:00
|
|
|
|
|
|
|
// Close the stdin, which sends an EOF, and then set w to nil so that
|
|
|
|
// our defer func doesn't close it again since that is unsafe with
|
|
|
|
// the Go SSH package.
|
2013-05-28 01:05:33 -04:00
|
|
|
log.Println("Upload complete, closing stdin pipe")
|
2013-05-24 11:23:48 -04:00
|
|
|
w.Close()
|
|
|
|
w = nil
|
|
|
|
|
|
|
|
// Wait for the SCP connection to close, meaning it has consumed all
|
|
|
|
// our data and has completed. Or has errored.
|
2013-05-28 01:05:33 -04:00
|
|
|
log.Println("Waiting for SSH session to complete")
|
2013-05-24 12:06:13 -04:00
|
|
|
err = session.Wait()
|
|
|
|
if err != nil {
|
2013-05-24 12:27:28 -04:00
|
|
|
if exitErr, ok := err.(*ssh.ExitError); ok {
|
|
|
|
// Otherwise, we have an ExitErorr, meaning we can just read
|
|
|
|
// the exit status
|
|
|
|
log.Printf("non-zero exit status: %d", exitErr.ExitStatus())
|
2013-05-24 12:06:13 -04:00
|
|
|
}
|
|
|
|
|
2013-05-24 12:27:28 -04:00
|
|
|
return err
|
2013-05-24 12:06:13 -04:00
|
|
|
}
|
|
|
|
|
2013-05-24 12:40:20 -04:00
|
|
|
log.Printf("scp stdout (length %d): %#v", stdout.Len(), stdout.Bytes())
|
|
|
|
log.Printf("scp stderr (length %d): %s", stderr.Len(), stderr.String())
|
2013-05-24 11:23:48 -04:00
|
|
|
|
2013-05-20 18:52:34 -04:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *comm) Download(string, io.Writer) error {
|
2013-05-24 12:48:24 -04:00
|
|
|
panic("not implemented yet")
|
2013-05-20 18:52:34 -04:00
|
|
|
return nil
|
|
|
|
}
|