Commit 09563110 authored by Mitchell Hashimoto's avatar Mitchell Hashimoto

communicator/ssh: UploadDir works properly

parent a050d344
...@@ -313,9 +313,11 @@ func scpUploadFile(dst string, src io.Reader, w io.Writer, r *bufio.Reader) erro ...@@ -313,9 +313,11 @@ func scpUploadFile(dst string, src io.Reader, w io.Writer, r *bufio.Reader) erro
func scpUploadDir(root string, fs []os.FileInfo, w io.Writer, r *bufio.Reader) error { func scpUploadDir(root string, fs []os.FileInfo, w io.Writer, r *bufio.Reader) error {
for _, fi := range fs { for _, fi := range fs {
realPath := filepath.Join(root, fi.Name())
if !fi.IsDir() { if !fi.IsDir() {
// It is a regular file, just upload it // It is a regular file, just upload it
f, err := os.Open(filepath.Join(root, fi.Name())) f, err := os.Open(realPath)
if err != nil { if err != nil {
return err return err
} }
...@@ -328,58 +330,44 @@ func scpUploadDir(root string, fs []os.FileInfo, w io.Writer, r *bufio.Reader) e ...@@ -328,58 +330,44 @@ func scpUploadDir(root string, fs []os.FileInfo, w io.Writer, r *bufio.Reader) e
if err != nil { if err != nil {
return err return err
} }
}
}
return nil continue
} }
func scpWalkFn(cur string, dst string, src string, w io.Writer, r *bufio.Reader) filepath.WalkFunc { // It is a directory, recursively upload
return func(path string, info os.FileInfo, err error) error { log.Printf("SCP: starting directory upload: %s", fi.Name())
fmt.Fprintln(w, "D0755 0", fi.Name())
err := checkSCPStatus(r)
if err != nil { if err != nil {
return err return err
} }
if path == cur { f, err := os.Open(realPath)
// Don't upload ourselves
return nil
}
// Get the relative path so that we can check excludes and also
// so that we can build the full destination path
relPath, err := filepath.Rel(src, path)
if err != nil { if err != nil {
return err return err
} }
// TODO(mitchellh): Check excludes // Execute this in a function just so that we have easy "defer"
targetPath := filepath.Base(relPath) // available because laziness.
if info.IsDir() { err = func() error {
log.Printf("SCP: starting directory upload: %s", targetPath) defer f.Close()
fmt.Fprintln(w, "D0755 0", targetPath)
err := checkSCPStatus(r)
if err != nil {
return err
}
err = filepath.Walk(path, scpWalkFn(path, dst, src, w, r)) entries, err := f.Readdir(-1)
if err != nil { if err != nil {
return err return err
} }
fmt.Fprintln(w, "E") return scpUploadDir(realPath, entries, w, r)
return checkSCPStatus(r) }()
if err != nil {
return err
} }
// Open the file for uploading fmt.Fprintln(w, "E")
f, err := os.Open(path)
if err != nil { if err != nil {
return err return err
} }
defer f.Close()
// Upload the file like any normal SCP operation
targetPath = filepath.Base(relPath)
return scpUploadFile(targetPath, f, w, r)
} }
return nil
} }
...@@ -2,7 +2,6 @@ package file ...@@ -2,7 +2,6 @@ package file
import ( import (
"github.com/mitchellh/packer/packer" "github.com/mitchellh/packer/packer"
"io"
"io/ioutil" "io/ioutil"
"os" "os"
"strings" "strings"
...@@ -75,26 +74,6 @@ func TestProvisionerPrepare_EmptyDestination(t *testing.T) { ...@@ -75,26 +74,6 @@ func TestProvisionerPrepare_EmptyDestination(t *testing.T) {
} }
} }
type stubUploadCommunicator struct {
dest string
data []byte
}
func (suc *stubUploadCommunicator) Download(src string, data io.Writer) error {
return nil
}
func (suc *stubUploadCommunicator) Upload(dest string, data io.Reader) error {
var err error
suc.dest = dest
suc.data, err = ioutil.ReadAll(data)
return err
}
func (suc *stubUploadCommunicator) Start(cmd *packer.RemoteCmd) error {
return nil
}
type stubUi struct { type stubUi struct {
sayMessages string sayMessages string
} }
...@@ -138,7 +117,7 @@ func TestProvisionerProvision_SendsFile(t *testing.T) { ...@@ -138,7 +117,7 @@ func TestProvisionerProvision_SendsFile(t *testing.T) {
} }
ui := &stubUi{} ui := &stubUi{}
comm := &stubUploadCommunicator{} comm := &packer.MockCommunicator{}
err = p.Provision(ui, comm) err = p.Provision(ui, comm)
if err != nil { if err != nil {
t.Fatalf("should successfully provision: %s", err) t.Fatalf("should successfully provision: %s", err)
...@@ -152,11 +131,11 @@ func TestProvisionerProvision_SendsFile(t *testing.T) { ...@@ -152,11 +131,11 @@ func TestProvisionerProvision_SendsFile(t *testing.T) {
t.Fatalf("should print destination filename") t.Fatalf("should print destination filename")
} }
if comm.dest != "something" { if comm.UploadPath != "something" {
t.Fatalf("should upload to configured destination") t.Fatalf("should upload to configured destination")
} }
if string(comm.data) != "hello" { if comm.UploadData != "hello" {
t.Fatalf("should upload with source file's data") t.Fatalf("should upload with source file's data")
} }
} }
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment