2013-05-20 18:47:41 -04:00
|
|
|
package ssh
|
|
|
|
|
|
|
|
import (
|
2013-07-17 21:15:42 -04:00
|
|
|
"bufio"
|
2013-05-20 18:47:41 -04:00
|
|
|
"bytes"
|
|
|
|
"code.google.com/p/go.crypto/ssh"
|
2013-07-07 15:23:32 -04:00
|
|
|
"errors"
|
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-11-02 04:07:45 -04:00
|
|
|
"io/ioutil"
|
2013-05-20 18:47:41 -04:00
|
|
|
"log"
|
|
|
|
"net"
|
2013-08-24 20:14:15 -04:00
|
|
|
"os"
|
2013-05-24 11:23:48 -04:00
|
|
|
"path/filepath"
|
2013-08-28 01:12:21 -04:00
|
|
|
"sync"
|
|
|
|
"time"
|
2013-05-20 18:47:41 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
type comm struct {
|
|
|
|
client *ssh.ClientConn
|
2013-07-14 07:22:41 -04:00
|
|
|
config *Config
|
2013-07-14 07:55:27 -04:00
|
|
|
conn net.Conn
|
2013-07-14 07:22:41 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// Config is the structure used to configure the SSH communicator.
|
|
|
|
type Config struct {
|
|
|
|
// The configuration of the Go SSH connection
|
|
|
|
SSHConfig *ssh.ClientConfig
|
|
|
|
|
|
|
|
// Connection returns a new connection. The current connection
|
|
|
|
// in use will be closed as part of the Close method, or in the
|
|
|
|
// case an error occurs.
|
|
|
|
Connection func() (net.Conn, error)
|
2013-08-27 19:51:05 -04:00
|
|
|
|
|
|
|
// NoPty, if true, will not request a pty from the remote end.
|
|
|
|
NoPty bool
|
2013-05-20 18:47:41 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// Creates a new packer.Communicator implementation over SSH. This takes
|
|
|
|
// an already existing TCP connection and SSH configuration.
|
2013-07-14 07:22:41 -04:00
|
|
|
func New(config *Config) (result *comm, err error) {
|
|
|
|
// Establish an initial connection and connect
|
|
|
|
result = &comm{
|
|
|
|
config: config,
|
|
|
|
}
|
|
|
|
|
|
|
|
if err = result.reconnect(); err != nil {
|
|
|
|
result = nil
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2013-05-20 18:47:41 -04:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2013-06-03 02:27:01 -04:00
|
|
|
func (c *comm) Start(cmd *packer.RemoteCmd) (err error) {
|
2013-07-14 07:55:02 -04:00
|
|
|
session, err := c.newSession()
|
2013-05-20 18:47:41 -04:00
|
|
|
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-08-27 19:51:05 -04:00
|
|
|
if !c.config.NoPty {
|
|
|
|
// Request a PTY
|
|
|
|
termModes := ssh.TerminalModes{
|
|
|
|
ssh.ECHO: 0, // do not echo
|
|
|
|
ssh.TTY_OP_ISPEED: 14400, // input speed = 14.4kbaud
|
|
|
|
ssh.TTY_OP_OSPEED: 14400, // output speed = 14.4kbaud
|
|
|
|
}
|
2013-06-23 20:36:45 -04:00
|
|
|
|
2013-08-27 19:51:05 -04:00
|
|
|
if err = session.RequestPty("xterm", 80, 40, termModes); err != nil {
|
|
|
|
return
|
|
|
|
}
|
2013-06-23 20:36:45 -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
|
|
|
|
}
|
|
|
|
|
2013-08-28 01:12:21 -04:00
|
|
|
// A channel to keep track of our done state
|
|
|
|
doneCh := make(chan struct{})
|
|
|
|
sessionLock := new(sync.Mutex)
|
|
|
|
timedOut := false
|
|
|
|
|
2013-05-20 18:47:41 -04:00
|
|
|
// Start a goroutine to wait for the session to end and set the
|
|
|
|
// exit boolean and status.
|
|
|
|
go func() {
|
|
|
|
defer session.Close()
|
2013-08-28 01:12:21 -04:00
|
|
|
|
2013-05-20 18:47:41 -04:00
|
|
|
err := session.Wait()
|
2013-07-29 15:12:42 -04:00
|
|
|
exitStatus := 0
|
2013-05-20 18:47:41 -04:00
|
|
|
if err != nil {
|
|
|
|
exitErr, ok := err.(*ssh.ExitError)
|
|
|
|
if ok {
|
2013-07-29 15:12:42 -04:00
|
|
|
exitStatus = exitErr.ExitStatus()
|
2013-05-20 18:47:41 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-08-28 01:12:21 -04:00
|
|
|
sessionLock.Lock()
|
|
|
|
defer sessionLock.Unlock()
|
|
|
|
|
|
|
|
if timedOut {
|
|
|
|
// We timed out, so set the exit status to -1
|
|
|
|
exitStatus = -1
|
|
|
|
}
|
|
|
|
|
2013-07-30 20:06:22 -04:00
|
|
|
log.Printf("remote command exited with '%d': %s", exitStatus, cmd.Command)
|
2013-07-29 15:12:42 -04:00
|
|
|
cmd.SetExited(exitStatus)
|
2013-08-28 01:12:21 -04:00
|
|
|
close(doneCh)
|
|
|
|
}()
|
|
|
|
|
|
|
|
go func() {
|
|
|
|
failures := 0
|
|
|
|
for {
|
|
|
|
dummy, err := c.config.Connection()
|
|
|
|
if err == nil {
|
2013-08-28 01:14:31 -04:00
|
|
|
failures = 0
|
2013-08-28 01:12:21 -04:00
|
|
|
dummy.Close()
|
|
|
|
}
|
|
|
|
|
|
|
|
select {
|
|
|
|
case <-doneCh:
|
|
|
|
return
|
|
|
|
default:
|
|
|
|
}
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
log.Printf("background SSH connection checker failure: %s", err)
|
|
|
|
failures += 1
|
|
|
|
}
|
|
|
|
|
|
|
|
if failures < 5 {
|
|
|
|
time.Sleep(5 * time.Second)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
// Acquire a lock in order to modify session state
|
|
|
|
sessionLock.Lock()
|
|
|
|
defer sessionLock.Unlock()
|
|
|
|
|
|
|
|
// Kill the connection and mark that we timed out.
|
|
|
|
log.Printf("Too many SSH connection failures. Killing it!")
|
|
|
|
c.conn.Close()
|
|
|
|
timedOut = true
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
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 {
|
|
|
|
// The target directory and file for talking the SCP protocol
|
|
|
|
target_dir := filepath.Dir(path)
|
|
|
|
target_file := filepath.Base(path)
|
|
|
|
|
2013-07-25 12:28:04 -04:00
|
|
|
// On windows, filepath.Dir uses backslash seperators (ie. "\tmp").
|
|
|
|
// This does not work when the target host is unix. Switch to forward slash
|
|
|
|
// which works for unix and windows
|
|
|
|
target_dir = filepath.ToSlash(target_dir)
|
|
|
|
|
2013-08-23 22:52:02 -04:00
|
|
|
scpFunc := func(w io.Writer, stdoutR *bufio.Reader) error {
|
2013-08-24 20:14:15 -04:00
|
|
|
return scpUploadFile(target_file, input, w, stdoutR)
|
2013-05-24 12:06:13 -04:00
|
|
|
}
|
|
|
|
|
2013-08-23 22:52:02 -04:00
|
|
|
return c.scpSession("scp -vt "+target_dir, scpFunc)
|
2013-05-20 18:52:34 -04:00
|
|
|
}
|
|
|
|
|
2013-08-23 22:31:33 -04:00
|
|
|
func (c *comm) UploadDir(dst string, src string, excl []string) error {
|
2013-08-25 23:47:10 -04:00
|
|
|
log.Printf("Upload dir '%s' to '%s'", src, dst)
|
|
|
|
scpFunc := func(w io.Writer, r *bufio.Reader) error {
|
|
|
|
uploadEntries := func() error {
|
|
|
|
f, err := os.Open(src)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer f.Close()
|
2013-08-24 20:14:15 -04:00
|
|
|
|
2013-08-25 23:47:10 -04:00
|
|
|
entries, err := f.Readdir(-1)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2013-08-24 20:14:15 -04:00
|
|
|
|
2013-08-25 23:47:10 -04:00
|
|
|
return scpUploadDir(src, entries, w, r)
|
|
|
|
}
|
|
|
|
|
|
|
|
if src[len(src)-1] != '/' {
|
|
|
|
log.Printf("No trailing slash, creating the source directory name")
|
|
|
|
return scpUploadDirProtocol(filepath.Base(src), w, r, uploadEntries)
|
|
|
|
} else {
|
|
|
|
// Trailing slash, so only upload the contents
|
|
|
|
return uploadEntries()
|
|
|
|
}
|
2013-08-24 20:14:15 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
return c.scpSession("scp -rvt "+dst, scpFunc)
|
2013-08-23 22:31:33 -04:00
|
|
|
}
|
|
|
|
|
2013-05-20 18:52:34 -04:00
|
|
|
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
|
|
|
}
|
2013-07-14 07:22:41 -04:00
|
|
|
|
2013-08-12 18:22:31 -04:00
|
|
|
func (c *comm) newSession() (session *ssh.Session, err error) {
|
2013-07-14 07:55:02 -04:00
|
|
|
log.Println("opening new ssh session")
|
2013-08-12 18:22:31 -04:00
|
|
|
if c.client == nil {
|
|
|
|
err = errors.New("client not available")
|
|
|
|
} else {
|
|
|
|
session, err = c.client.NewSession()
|
|
|
|
}
|
|
|
|
|
2013-07-14 07:55:02 -04:00
|
|
|
if err != nil {
|
2013-07-14 08:07:32 -04:00
|
|
|
log.Printf("ssh session open error: '%s', attempting reconnect", err)
|
2013-07-14 07:55:02 -04:00
|
|
|
if err := c.reconnect(); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return c.client.NewSession()
|
|
|
|
}
|
|
|
|
|
|
|
|
return session, nil
|
|
|
|
}
|
|
|
|
|
2013-07-14 07:22:41 -04:00
|
|
|
func (c *comm) reconnect() (err error) {
|
|
|
|
if c.conn != nil {
|
|
|
|
c.conn.Close()
|
|
|
|
}
|
|
|
|
|
2013-08-12 18:22:31 -04:00
|
|
|
// Set the conn and client to nil since we'll recreate it
|
|
|
|
c.conn = nil
|
|
|
|
c.client = nil
|
|
|
|
|
2013-07-14 07:55:02 -04:00
|
|
|
log.Printf("reconnecting to TCP connection for SSH")
|
2013-07-14 07:22:41 -04:00
|
|
|
c.conn, err = c.config.Connection()
|
|
|
|
if err != nil {
|
2013-10-14 04:21:52 -04:00
|
|
|
// Explicitly set this to the REAL nil. Connection() can return
|
|
|
|
// a nil implementation of net.Conn which will make the
|
|
|
|
// "if c.conn == nil" check fail above. Read here for more information
|
|
|
|
// on this psychotic language feature:
|
|
|
|
//
|
|
|
|
// http://golang.org/doc/faq#nil_error
|
|
|
|
c.conn = nil
|
|
|
|
|
2013-07-14 07:55:02 -04:00
|
|
|
log.Printf("reconnection error: %s", err)
|
2013-07-14 07:22:41 -04:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2013-07-14 07:55:02 -04:00
|
|
|
log.Printf("handshaking with SSH")
|
2013-07-14 07:22:41 -04:00
|
|
|
c.client, err = ssh.Client(c.conn, c.config.SSHConfig)
|
2013-07-14 07:55:02 -04:00
|
|
|
if err != nil {
|
|
|
|
log.Printf("handshake error: %s", err)
|
|
|
|
}
|
|
|
|
|
2013-07-14 07:22:41 -04:00
|
|
|
return
|
|
|
|
}
|
2013-07-17 21:15:42 -04:00
|
|
|
|
2013-08-23 22:52:02 -04:00
|
|
|
func (c *comm) scpSession(scpCommand string, f func(io.Writer, *bufio.Reader) error) error {
|
|
|
|
session, err := c.newSession()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer session.Close()
|
|
|
|
|
|
|
|
// Get a pipe to stdin so that we can send data down
|
|
|
|
stdinW, err := session.StdinPipe()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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 stdinW != nil {
|
|
|
|
stdinW.Close()
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
// Get a pipe to stdout so that we can get responses back
|
|
|
|
stdoutPipe, err := session.StdoutPipe()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
stdoutR := bufio.NewReader(stdoutPipe)
|
|
|
|
|
|
|
|
// Set stderr to a bytes buffer
|
|
|
|
stderr := new(bytes.Buffer)
|
|
|
|
session.Stderr = stderr
|
|
|
|
|
|
|
|
// Start the sink mode on the other side
|
|
|
|
// TODO(mitchellh): There are probably issues with shell escaping the path
|
2013-09-05 20:22:37 -04:00
|
|
|
log.Println("Starting remote scp process: ", scpCommand)
|
2013-08-23 22:52:02 -04:00
|
|
|
if err := session.Start(scpCommand); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2013-09-05 22:55:09 -04:00
|
|
|
// Call our callback that executes in the context of SCP. We ignore
|
|
|
|
// EOF errors if they occur because it usually means that SCP prematurely
|
|
|
|
// ended on the other side.
|
2013-08-23 22:52:02 -04:00
|
|
|
log.Println("Started SCP session, beginning transfers...")
|
2013-09-05 22:55:09 -04:00
|
|
|
if err := f(stdinW, stdoutR); err != nil && err != io.EOF {
|
2013-08-23 22:52:02 -04:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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.
|
|
|
|
log.Println("SCP session complete, closing stdin pipe.")
|
|
|
|
stdinW.Close()
|
|
|
|
stdinW = nil
|
|
|
|
|
|
|
|
// Wait for the SCP connection to close, meaning it has consumed all
|
|
|
|
// our data and has completed. Or has errored.
|
|
|
|
log.Println("Waiting for SSH session to complete.")
|
|
|
|
err = session.Wait()
|
|
|
|
if err != nil {
|
|
|
|
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())
|
|
|
|
|
|
|
|
// If we exited with status 127, it means SCP isn't available.
|
|
|
|
// Return a more descriptive error for that.
|
|
|
|
if exitErr.ExitStatus() == 127 {
|
|
|
|
return errors.New(
|
|
|
|
"SCP failed to start. This usually means that SCP is not\n" +
|
|
|
|
"properly installed on the remote system.")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
log.Printf("scp stderr (length %d): %s", stderr.Len(), stderr.String())
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2013-07-19 14:16:39 -04:00
|
|
|
// checkSCPStatus checks that a prior command sent to SCP completed
|
|
|
|
// successfully. If it did not complete successfully, an error will
|
|
|
|
// be returned.
|
|
|
|
func checkSCPStatus(r *bufio.Reader) error {
|
|
|
|
code, err := r.ReadByte()
|
2013-07-17 21:15:42 -04:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2013-07-19 14:16:39 -04:00
|
|
|
|
|
|
|
if code != 0 {
|
2013-07-17 21:15:42 -04:00
|
|
|
// Treat any non-zero (really 1 and 2) as fatal errors
|
2013-07-19 14:16:39 -04:00
|
|
|
message, _, err := r.ReadLine()
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("Error reading error message: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return errors.New(string(message))
|
2013-07-17 21:15:42 -04:00
|
|
|
}
|
2013-07-19 14:16:39 -04:00
|
|
|
|
2013-07-17 21:15:42 -04:00
|
|
|
return nil
|
|
|
|
}
|
2013-08-24 20:14:15 -04:00
|
|
|
|
|
|
|
func scpUploadFile(dst string, src io.Reader, w io.Writer, r *bufio.Reader) error {
|
2013-11-02 04:07:45 -04:00
|
|
|
// Create a temporary file where we can copy the contents of the src
|
|
|
|
// so that we can determine the length, since SCP is length-prefixed.
|
|
|
|
tf, err := ioutil.TempFile("", "packer-upload")
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("Error creating temporary file for upload: %s", err)
|
|
|
|
}
|
|
|
|
defer os.Remove(tf.Name())
|
|
|
|
defer tf.Close()
|
|
|
|
|
|
|
|
log.Println("Copying input data into temporary file so we can read the length")
|
|
|
|
if _, err := io.Copy(tf, src); err != nil {
|
2013-08-24 20:14:15 -04:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2013-11-02 04:07:45 -04:00
|
|
|
// Sync the file so that the contents are definitely on disk, then
|
|
|
|
// read the length of it.
|
|
|
|
if err := tf.Sync(); err != nil {
|
|
|
|
return fmt.Errorf("Error creating temporary file for upload: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Seek the file to the beginning so we can re-read all of it
|
|
|
|
if _, err := tf.Seek(0, 0); err != nil {
|
|
|
|
return fmt.Errorf("Error creating temporary file for upload: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
fi, err := tf.Stat()
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("Error creating temporary file for upload: %s", err)
|
|
|
|
}
|
|
|
|
|
2013-08-24 20:14:15 -04:00
|
|
|
// Start the protocol
|
|
|
|
log.Println("Beginning file upload...")
|
2013-11-02 04:07:45 -04:00
|
|
|
fmt.Fprintln(w, "C0644", fi.Size(), dst)
|
|
|
|
if err := checkSCPStatus(r); err != nil {
|
2013-08-24 20:14:15 -04:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2013-11-02 04:07:45 -04:00
|
|
|
if _, err := io.Copy(w, tf); err != nil {
|
2013-08-24 20:14:15 -04:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
fmt.Fprint(w, "\x00")
|
2013-11-02 04:07:45 -04:00
|
|
|
if err := checkSCPStatus(r); err != nil {
|
2013-08-24 20:14:15 -04:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2013-08-25 23:47:10 -04:00
|
|
|
func scpUploadDirProtocol(name string, w io.Writer, r *bufio.Reader, f func() error) error {
|
|
|
|
log.Printf("SCP: starting directory upload: %s", name)
|
|
|
|
fmt.Fprintln(w, "D0755 0", name)
|
|
|
|
err := checkSCPStatus(r)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := f(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
fmt.Fprintln(w, "E")
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2013-08-24 20:14:15 -04:00
|
|
|
func scpUploadDir(root string, fs []os.FileInfo, w io.Writer, r *bufio.Reader) error {
|
|
|
|
for _, fi := range fs {
|
2013-08-25 23:29:50 -04:00
|
|
|
realPath := filepath.Join(root, fi.Name())
|
|
|
|
|
2013-09-25 04:42:49 -04:00
|
|
|
// Track if this is actually a symlink to a directory. If it is
|
|
|
|
// a symlink to a file we don't do any special behavior because uploading
|
|
|
|
// a file just works. If it is a directory, we need to know so we
|
|
|
|
// treat it as such.
|
|
|
|
isSymlinkToDir := false
|
2013-09-30 14:39:52 -04:00
|
|
|
if fi.Mode()&os.ModeSymlink == os.ModeSymlink {
|
2013-09-25 04:42:49 -04:00
|
|
|
symPath, err := filepath.EvalSymlinks(realPath)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
symFi, err := os.Lstat(symPath)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
isSymlinkToDir = symFi.IsDir()
|
|
|
|
}
|
|
|
|
|
|
|
|
if !fi.IsDir() && !isSymlinkToDir {
|
|
|
|
// It is a regular file (or symlink to a file), just upload it
|
2013-08-25 23:29:50 -04:00
|
|
|
f, err := os.Open(realPath)
|
2013-08-24 20:14:15 -04:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
err = func() error {
|
|
|
|
defer f.Close()
|
|
|
|
return scpUploadFile(fi.Name(), f, w, r)
|
|
|
|
}()
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2013-08-25 23:29:50 -04:00
|
|
|
continue
|
|
|
|
}
|
2013-08-24 20:14:15 -04:00
|
|
|
|
2013-08-25 23:29:50 -04:00
|
|
|
// It is a directory, recursively upload
|
2013-08-25 23:47:10 -04:00
|
|
|
err := scpUploadDirProtocol(fi.Name(), w, r, func() error {
|
|
|
|
f, err := os.Open(realPath)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2013-08-25 23:29:50 -04:00
|
|
|
defer f.Close()
|
2013-08-24 20:14:15 -04:00
|
|
|
|
2013-08-25 23:29:50 -04:00
|
|
|
entries, err := f.Readdir(-1)
|
2013-08-24 20:14:15 -04:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2013-08-25 23:29:50 -04:00
|
|
|
return scpUploadDir(realPath, entries, w, r)
|
2013-08-25 23:47:10 -04:00
|
|
|
})
|
2013-08-24 20:14:15 -04:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
2013-08-25 23:29:50 -04:00
|
|
|
|
|
|
|
return nil
|
2013-08-24 20:14:15 -04:00
|
|
|
}
|