communicator/ssh: UploadDir works properly

This commit is contained in:
Mitchell Hashimoto 2013-08-25 20:29:50 -07:00
parent a050d344eb
commit 095631107a
2 changed files with 42 additions and 75 deletions

View File

@ -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 {
for _, fi := range fs {
realPath := filepath.Join(root, fi.Name())
if !fi.IsDir() {
// 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 {
return err
}
@ -328,58 +330,44 @@ func scpUploadDir(root string, fs []os.FileInfo, w io.Writer, r *bufio.Reader) e
if err != nil {
return err
}
}
continue
}
return nil
}
func scpWalkFn(cur string, dst string, src string, w io.Writer, r *bufio.Reader) filepath.WalkFunc {
return func(path string, info os.FileInfo, err error) error {
if err != nil {
return err
}
if path == cur {
// 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 {
return err
}
// TODO(mitchellh): Check excludes
targetPath := filepath.Base(relPath)
if info.IsDir() {
log.Printf("SCP: starting directory upload: %s", targetPath)
fmt.Fprintln(w, "D0755 0", targetPath)
// It is a directory, recursively upload
log.Printf("SCP: starting directory upload: %s", fi.Name())
fmt.Fprintln(w, "D0755 0", fi.Name())
err := checkSCPStatus(r)
if err != nil {
return err
}
err = filepath.Walk(path, scpWalkFn(path, dst, src, w, r))
f, err := os.Open(realPath)
if err != nil {
return err
}
// Execute this in a function just so that we have easy "defer"
// available because laziness.
err = func() error {
defer f.Close()
entries, err := f.Readdir(-1)
if err != nil {
return err
}
return scpUploadDir(realPath, entries, w, r)
}()
if err != nil {
return err
}
fmt.Fprintln(w, "E")
return checkSCPStatus(r)
}
// Open the file for uploading
f, err := os.Open(path)
if err != nil {
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
}

View File

@ -2,7 +2,6 @@ package file
import (
"github.com/mitchellh/packer/packer"
"io"
"io/ioutil"
"os"
"strings"
@ -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 {
sayMessages string
}
@ -138,7 +117,7 @@ func TestProvisionerProvision_SendsFile(t *testing.T) {
}
ui := &stubUi{}
comm := &stubUploadCommunicator{}
comm := &packer.MockCommunicator{}
err = p.Provision(ui, comm)
if err != nil {
t.Fatalf("should successfully provision: %s", err)
@ -152,11 +131,11 @@ func TestProvisionerProvision_SendsFile(t *testing.T) {
t.Fatalf("should print destination filename")
}
if comm.dest != "something" {
if comm.UploadPath != "something" {
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")
}
}