packer-cn/builder/amazon/chroot/communicator.go

121 lines
2.8 KiB
Go
Raw Normal View History

package chroot
// pf := func () { somefunc("a str", 1) }
import (
2013-09-26 04:32:53 -04:00
"fmt"
"github.com/mitchellh/packer/packer"
"io"
2013-09-26 22:11:28 -04:00
"io/ioutil"
"log"
"os"
"os/exec"
"path/filepath"
"syscall"
)
type Command func(string) *exec.Cmd
// Communicator is a special communicator that works by executing
// commands locally but within a chroot.
type Communicator struct {
Chroot string
ChrootCmd Command
2013-09-27 16:47:44 -04:00
WrappedCommand Command
}
func (c *Communicator) Start(cmd *packer.RemoteCmd) error {
localCmd := c.ChrootCmd(cmd.Command)
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()
}
}
}
log.Printf(
2013-09-27 21:10:33 -04:00
"Chroot executation exited with '%d': '%s'",
exitStatus, cmd.Command)
cmd.SetExited(exitStatus)
}()
return nil
}
func (c *Communicator) Upload(dst string, r io.Reader) error {
dst = filepath.Join(c.Chroot, dst)
log.Printf("Uploading to chroot dir: %s", dst)
2013-09-26 22:11:28 -04:00
tf, err := ioutil.TempFile("", "packer-amazon-chroot")
if err != nil {
2013-09-26 22:11:28 -04:00
return fmt.Errorf("Error preparing shell script: %s", err)
}
2013-09-26 22:11:28 -04:00
defer os.Remove(tf.Name())
io.Copy(tf, r)
2013-09-27 18:08:15 -04:00
cpCmd := fmt.Sprintf("cp %s %s", tf.Name(), dst)
2013-09-27 21:10:33 -04:00
return (c.WrappedCommand(cpCmd)).Run()
}
func (c *Communicator) UploadDir(dst string, src string, exclude []string) error {
2013-09-27 21:10:33 -04:00
/*
walkFn := func(fullPath string, info os.FileInfo, err error) error {
if err != nil {
return err
}
2013-09-27 21:10:33 -04:00
path, err := filepath.Rel(src, fullPath)
if err != nil {
return err
}
2013-09-27 21:10:33 -04:00
for _, e := range exclude {
if e == path {
log.Printf("Skipping excluded file: %s", path)
return nil
}
}
2013-09-27 21:10:33 -04:00
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()
}
*/
2013-09-29 04:04:57 -04:00
// TODO: remove any file copied if it appears in `exclude`
2013-09-27 21:10:33 -04:00
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 {
src = filepath.Join(c.Chroot, src)
log.Printf("Downloading from chroot dir: %s", src)
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
}