From a84c74318cc2868cad6d9f45d3da165a338f80ec Mon Sep 17 00:00:00 2001 From: Matthew Hooker Date: Sat, 28 Sep 2013 01:10:33 +0000 Subject: [PATCH] WIP copying files. --- builder/amazon/chroot/communicator.go | 52 +++++++++++++----------- builder/amazon/chroot/copy_files.go | 11 ++--- builder/amazon/chroot/step_copy_files.go | 11 +++-- 3 files changed, 42 insertions(+), 32 deletions(-) diff --git a/builder/amazon/chroot/communicator.go b/builder/amazon/chroot/communicator.go index 92ed0d25a..37a9bbdf8 100644 --- a/builder/amazon/chroot/communicator.go +++ b/builder/amazon/chroot/communicator.go @@ -49,7 +49,7 @@ func (c *Communicator) Start(cmd *packer.RemoteCmd) error { } log.Printf( - "Chroot executation ended with '%d': '%s'", + "Chroot executation exited with '%d': '%s'", exitStatus, cmd.Command) cmd.SetExited(exitStatus) }() @@ -67,35 +67,39 @@ func (c *Communicator) Upload(dst string, r io.Reader) error { defer os.Remove(tf.Name()) io.Copy(tf, r) cpCmd := fmt.Sprintf("cp %s %s", tf.Name(), dst) - return (*c.ChrootCmd(cpCmd)).Run() + return (c.WrappedCommand(cpCmd)).Run() } func (c *Communicator) UploadDir(dst string, src string, exclude []string) error { - walkFn := func(fullPath string, info os.FileInfo, err error) error { - if err != nil { - return err - } - - path, err := filepath.Rel(src, fullPath) - if err != nil { - return err - } - - for _, e := range exclude { - if e == path { - log.Printf("Skipping excluded file: %s", path) - return nil + /* + walkFn := func(fullPath string, info os.FileInfo, err error) error { + if err != nil { + return err } + + path, err := filepath.Rel(src, fullPath) + if err != nil { + return err + } + + for _, e := range exclude { + if e == path { + log.Printf("Skipping excluded file: %s", path) + return nil + } + } + + chrootDest := filepath.Join(c.Chroot, dst, path) + log.Printf("Uploading dir %s to chroot dir: %s", src, dst) + cpCmd := fmt.Sprintf("cp %s %s", fullPath, chrootDest) + return c.WrappedCommand(cpCmd).Run() } + */ - chrootDest := filepath.Join(c.Chroot, dst, path) - log.Printf("Uploading to chroot dir: %s", dst) - cpCmd := fmt.Sprintf("cp %s %s", fullPath, chrootDest) - return c.ChrootCmd(cpCmd).Run() - } - - log.Printf("Uploading directory '%s' to '%s'", src, dst) - return filepath.Walk(src, walkFn) + chrootDest := filepath.Join(c.Chroot, dst) + log.Printf("Uploading directory '%s' to '%s'", src, chrootDest) + cpCmd := fmt.Sprintf("cp -R %s* %s", src, chrootDest) + return c.WrappedCommand(cpCmd).Run() } func (c *Communicator) Download(src string, w io.Writer) error { diff --git a/builder/amazon/chroot/copy_files.go b/builder/amazon/chroot/copy_files.go index 5af58baa5..b8807d095 100644 --- a/builder/amazon/chroot/copy_files.go +++ b/builder/amazon/chroot/copy_files.go @@ -7,12 +7,13 @@ import ( ) func ChrootCommand(chroot string, command string) *exec.Cmd { - chrootCommand := fmt.Sprintf("chroot %s %s", chroot, command) - return ShellCommand(chrootCommand) + cmd := fmt.Sprintf("sudo chroot %s", chroot) + return ShellCommand(cmd, command) } -func ShellCommand(command string) *exec.Cmd { - cmd := exec.Command("/bin/sh", "-c", command) - log.Printf("ShellCommand(%s) -> #%v", command, cmd.Args) +func ShellCommand(commands ...string) *exec.Cmd { + cmds := append([]string{"-c"}, commands...) + cmd := exec.Command("/bin/sh", cmds...) + log.Printf("ShellCommand: %s %v", cmd.Path, cmd.Args[1:]) return cmd } diff --git a/builder/amazon/chroot/step_copy_files.go b/builder/amazon/chroot/step_copy_files.go index c7be3bb66..3cb096d10 100644 --- a/builder/amazon/chroot/step_copy_files.go +++ b/builder/amazon/chroot/step_copy_files.go @@ -1,6 +1,7 @@ package chroot import ( + "bytes" "fmt" "github.com/mitchellh/multistep" "github.com/mitchellh/packer/packer" @@ -22,6 +23,7 @@ func (s *StepCopyFiles) Run(state multistep.StateBag) multistep.StepAction { mountPath := state.Get("mount_path").(string) ui := state.Get("ui").(packer.Ui) wrappedCommand := state.Get("wrappedCommand").(Command) + stderr := new(bytes.Buffer) s.files = make([]string, 0, len(config.CopyFiles)) if len(config.CopyFiles) > 0 { @@ -31,9 +33,12 @@ func (s *StepCopyFiles) Run(state multistep.StateBag) multistep.StepAction { chrootPath := filepath.Join(mountPath, path) log.Printf("Copying '%s' to '%s'", path, chrootPath) - cmd := fmt.Sprintf("cp %s %s", path, chrootPath) - if err := wrappedCommand(cmd); err != nil { - err := fmt.Errorf("Error copying file: %s", err) + cmd := wrappedCommand(fmt.Sprintf("cp %s %s", path, chrootPath)) + stderr.Reset() + cmd.Stderr = stderr + if err := cmd.Run(); err != nil { + err := fmt.Errorf( + "Error copying file: %s\nnStderr: %s", err, stderr.String()) state.Put("error", err) ui.Error(err.Error()) return multistep.ActionHalt