2013-07-30 17:44:03 -04:00
|
|
|
package chroot
|
|
|
|
|
|
|
|
import (
|
2013-09-26 04:32:53 -04:00
|
|
|
"fmt"
|
2013-07-30 17:44:03 -04:00
|
|
|
"github.com/mitchellh/packer/packer"
|
|
|
|
"io"
|
|
|
|
"log"
|
|
|
|
"os"
|
|
|
|
"os/exec"
|
|
|
|
"path/filepath"
|
|
|
|
"syscall"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Communicator is a special communicator that works by executing
|
|
|
|
// commands locally but within a chroot.
|
|
|
|
type Communicator struct {
|
2013-09-25 06:20:22 -04:00
|
|
|
Chroot string
|
|
|
|
ChrootCommand string
|
2013-09-26 03:58:25 -04:00
|
|
|
CopyCommand string
|
2013-07-30 17:44:03 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Communicator) Start(cmd *packer.RemoteCmd) error {
|
|
|
|
|
2013-09-25 06:20:22 -04:00
|
|
|
chrootCommand := fmt.Sprintf("%s %s %s", c.ChrootCommand, c.Chroot, cmd.Command)
|
2013-09-26 04:32:53 -04:00
|
|
|
localCmd := exec.Command("/bin/sh", "-c", chrootCommand)
|
2013-07-30 17:44:03 -04:00
|
|
|
localCmd.Stdin = cmd.Stdin
|
|
|
|
localCmd.Stdout = cmd.Stdout
|
|
|
|
localCmd.Stderr = cmd.Stderr
|
|
|
|
log.Printf("Executing: %s %#v", localCmd.Path, localCmd.Args)
|
|
|
|
if err := localCmd.Start(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
go func() {
|
|
|
|
exitStatus := 0
|
|
|
|
if err := localCmd.Wait(); err != nil {
|
|
|
|
if exitErr, ok := err.(*exec.ExitError); ok {
|
|
|
|
exitStatus = 1
|
|
|
|
|
|
|
|
// There is no process-independent way to get the REAL
|
|
|
|
// exit status so we just try to go deeper.
|
|
|
|
if status, ok := exitErr.Sys().(syscall.WaitStatus); ok {
|
|
|
|
exitStatus = status.ExitStatus()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-07-30 20:23:37 -04:00
|
|
|
log.Printf(
|
|
|
|
"Chroot executation ended with '%d': '%s'",
|
|
|
|
exitStatus, cmd.Command)
|
2013-07-30 17:44:03 -04:00
|
|
|
cmd.SetExited(exitStatus)
|
|
|
|
}()
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2013-09-26 04:16:51 -04:00
|
|
|
func (c *Communicator) Upload(dst string, r io.Reader) error {
|
|
|
|
dst = filepath.Join(c.Chroot, dst)
|
|
|
|
log.Printf("Uploading to chroot dir: %s", dst)
|
|
|
|
f, err := os.Create(dst)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer f.Close()
|
|
|
|
|
|
|
|
if _, err := io.Copy(f, r); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2013-08-23 22:31:33 -04:00
|
|
|
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
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-09-26 04:35:29 -04:00
|
|
|
chrootDest := filepath.Join(c.Chroot, dst, path)
|
2013-09-26 03:58:25 -04:00
|
|
|
log.Printf("Uploading to chroot dir: %s", dst)
|
2013-09-26 04:35:29 -04:00
|
|
|
return copySingle(chrootDest, fullPath, c.CopyCommand)
|
2013-08-23 22:31:33 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
log.Printf("Uploading directory '%s' to '%s'", src, dst)
|
|
|
|
return filepath.Walk(src, walkFn)
|
|
|
|
}
|
|
|
|
|
2013-07-30 17:44:03 -04:00
|
|
|
func (c *Communicator) Download(src string, w io.Writer) error {
|
|
|
|
src = filepath.Join(c.Chroot, src)
|
2013-07-30 18:14:21 -04:00
|
|
|
log.Printf("Downloading from chroot dir: %s", src)
|
2013-07-30 17:44:03 -04:00
|
|
|
f, err := os.Open(src)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer f.Close()
|
|
|
|
|
|
|
|
if _, err := io.Copy(w, f); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|