WIP copying files.

This commit is contained in:
Matthew Hooker 2013-09-28 01:10:33 +00:00
parent 837e641802
commit a84c74318c
3 changed files with 42 additions and 32 deletions

View File

@ -49,7 +49,7 @@ func (c *Communicator) Start(cmd *packer.RemoteCmd) error {
} }
log.Printf( log.Printf(
"Chroot executation ended with '%d': '%s'", "Chroot executation exited with '%d': '%s'",
exitStatus, cmd.Command) exitStatus, cmd.Command)
cmd.SetExited(exitStatus) cmd.SetExited(exitStatus)
}() }()
@ -67,35 +67,39 @@ func (c *Communicator) Upload(dst string, r io.Reader) error {
defer os.Remove(tf.Name()) defer os.Remove(tf.Name())
io.Copy(tf, r) io.Copy(tf, r)
cpCmd := fmt.Sprintf("cp %s %s", tf.Name(), dst) 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 { func (c *Communicator) UploadDir(dst string, src string, exclude []string) error {
walkFn := func(fullPath string, info os.FileInfo, err error) error { /*
if err != nil { walkFn := func(fullPath string, info os.FileInfo, err error) error {
return err 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
} }
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) chrootDest := filepath.Join(c.Chroot, dst)
log.Printf("Uploading to chroot dir: %s", dst) log.Printf("Uploading directory '%s' to '%s'", src, chrootDest)
cpCmd := fmt.Sprintf("cp %s %s", fullPath, chrootDest) cpCmd := fmt.Sprintf("cp -R %s* %s", src, chrootDest)
return c.ChrootCmd(cpCmd).Run() return c.WrappedCommand(cpCmd).Run()
}
log.Printf("Uploading directory '%s' to '%s'", src, dst)
return filepath.Walk(src, walkFn)
} }
func (c *Communicator) Download(src string, w io.Writer) error { func (c *Communicator) Download(src string, w io.Writer) error {

View File

@ -7,12 +7,13 @@ import (
) )
func ChrootCommand(chroot string, command string) *exec.Cmd { func ChrootCommand(chroot string, command string) *exec.Cmd {
chrootCommand := fmt.Sprintf("chroot %s %s", chroot, command) cmd := fmt.Sprintf("sudo chroot %s", chroot)
return ShellCommand(chrootCommand) return ShellCommand(cmd, command)
} }
func ShellCommand(command string) *exec.Cmd { func ShellCommand(commands ...string) *exec.Cmd {
cmd := exec.Command("/bin/sh", "-c", command) cmds := append([]string{"-c"}, commands...)
log.Printf("ShellCommand(%s) -> #%v", command, cmd.Args) cmd := exec.Command("/bin/sh", cmds...)
log.Printf("ShellCommand: %s %v", cmd.Path, cmd.Args[1:])
return cmd return cmd
} }

View File

@ -1,6 +1,7 @@
package chroot package chroot
import ( import (
"bytes"
"fmt" "fmt"
"github.com/mitchellh/multistep" "github.com/mitchellh/multistep"
"github.com/mitchellh/packer/packer" "github.com/mitchellh/packer/packer"
@ -22,6 +23,7 @@ func (s *StepCopyFiles) Run(state multistep.StateBag) multistep.StepAction {
mountPath := state.Get("mount_path").(string) mountPath := state.Get("mount_path").(string)
ui := state.Get("ui").(packer.Ui) ui := state.Get("ui").(packer.Ui)
wrappedCommand := state.Get("wrappedCommand").(Command) wrappedCommand := state.Get("wrappedCommand").(Command)
stderr := new(bytes.Buffer)
s.files = make([]string, 0, len(config.CopyFiles)) s.files = make([]string, 0, len(config.CopyFiles))
if len(config.CopyFiles) > 0 { if len(config.CopyFiles) > 0 {
@ -31,9 +33,12 @@ func (s *StepCopyFiles) Run(state multistep.StateBag) multistep.StepAction {
chrootPath := filepath.Join(mountPath, path) chrootPath := filepath.Join(mountPath, path)
log.Printf("Copying '%s' to '%s'", path, chrootPath) log.Printf("Copying '%s' to '%s'", path, chrootPath)
cmd := fmt.Sprintf("cp %s %s", path, chrootPath) cmd := wrappedCommand(fmt.Sprintf("cp %s %s", path, chrootPath))
if err := wrappedCommand(cmd); err != nil { stderr.Reset()
err := fmt.Errorf("Error copying file: %s", err) 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) state.Put("error", err)
ui.Error(err.Error()) ui.Error(err.Error())
return multistep.ActionHalt return multistep.ActionHalt