WIP copying files.

This commit is contained in:
Matthew Hooker 2013-09-28 01:10:33 +00:00
parent 831d5caa50
commit a15f629f4f
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(
"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 {

View File

@ -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
}

View File

@ -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