2013-07-30 17:44:03 -04:00
|
|
|
package chroot
|
|
|
|
|
|
|
|
import (
|
2013-11-08 13:53:54 -05:00
|
|
|
"bytes"
|
2013-09-26 04:32:53 -04:00
|
|
|
"fmt"
|
2013-07-30 17:44:03 -04:00
|
|
|
"io"
|
|
|
|
"log"
|
|
|
|
"os"
|
|
|
|
"os/exec"
|
|
|
|
"path/filepath"
|
2018-08-27 22:36:40 -04:00
|
|
|
"strconv"
|
2013-11-08 13:53:54 -05:00
|
|
|
"strings"
|
2013-07-30 17:44:03 -04:00
|
|
|
"syscall"
|
2015-11-02 06:22:52 -05:00
|
|
|
|
2017-04-04 16:39:01 -04:00
|
|
|
"github.com/hashicorp/packer/packer"
|
2018-12-12 09:45:00 -05:00
|
|
|
"github.com/hashicorp/packer/packer/tmp"
|
2013-07-30 17:44:03 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
// Communicator is a special communicator that works by executing
|
|
|
|
// commands locally but within a chroot.
|
|
|
|
type Communicator struct {
|
2013-09-30 12:33:57 -04:00
|
|
|
Chroot string
|
|
|
|
CmdWrapper CommandWrapper
|
2013-07-30 17:44:03 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Communicator) Start(cmd *packer.RemoteCmd) error {
|
2018-08-27 22:36:40 -04:00
|
|
|
// need extra escapes for the command since we're wrapping it in quotes
|
|
|
|
cmd.Command = strconv.Quote(cmd.Command)
|
2013-09-30 12:33:57 -04:00
|
|
|
command, err := c.CmdWrapper(
|
2018-08-27 22:36:40 -04:00
|
|
|
fmt.Sprintf("chroot %s /bin/sh -c %s", c.Chroot, cmd.Command))
|
2013-09-30 12:33:57 -04:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
localCmd := ShellCommand(command)
|
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(
|
2013-09-30 17:53:01 -04:00
|
|
|
"Chroot execution exited with '%d': '%s'",
|
2013-07-30 20:23:37 -04:00
|
|
|
exitStatus, cmd.Command)
|
2013-07-30 17:44:03 -04:00
|
|
|
cmd.SetExited(exitStatus)
|
|
|
|
}()
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2014-05-10 00:03:35 -04:00
|
|
|
func (c *Communicator) Upload(dst string, r io.Reader, fi *os.FileInfo) error {
|
2013-09-26 04:16:51 -04:00
|
|
|
dst = filepath.Join(c.Chroot, dst)
|
|
|
|
log.Printf("Uploading to chroot dir: %s", dst)
|
2018-12-12 09:45:00 -05:00
|
|
|
tf, err := tmp.File("packer-amazon-chroot")
|
2013-09-26 04:16:51 -04:00
|
|
|
if err != nil {
|
2013-09-26 22:11:28 -04:00
|
|
|
return fmt.Errorf("Error preparing shell script: %s", err)
|
2013-09-26 04:16:51 -04:00
|
|
|
}
|
2013-09-26 22:11:28 -04:00
|
|
|
defer os.Remove(tf.Name())
|
2017-03-28 23:35:22 -04:00
|
|
|
|
|
|
|
if _, err := io.Copy(tf, r); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2013-09-26 04:16:51 -04:00
|
|
|
|
2013-09-30 12:33:57 -04:00
|
|
|
cpCmd, err := c.CmdWrapper(fmt.Sprintf("cp %s %s", tf.Name(), dst))
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2013-08-23 22:31:33 -04:00
|
|
|
|
2013-09-30 12:33:57 -04:00
|
|
|
return ShellCommand(cpCmd).Run()
|
|
|
|
}
|
2013-08-23 22:31:33 -04:00
|
|
|
|
2013-09-30 12:33:57 -04:00
|
|
|
func (c *Communicator) UploadDir(dst string, src string, exclude []string) error {
|
2014-05-27 20:02:29 -04:00
|
|
|
// If src ends with a trailing "/", copy from "src/." so that
|
|
|
|
// directory contents (including hidden files) are copied, but the
|
|
|
|
// directory "src" is omitted. BSD does this automatically when
|
|
|
|
// the source contains a trailing slash, but linux does not.
|
|
|
|
if src[len(src)-1] == '/' {
|
|
|
|
src = src + "."
|
|
|
|
}
|
|
|
|
|
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)
|
2014-05-27 20:02:29 -04:00
|
|
|
|
2013-09-27 21:10:33 -04:00
|
|
|
log.Printf("Uploading directory '%s' to '%s'", src, chrootDest)
|
2014-05-02 12:18:56 -04:00
|
|
|
cpCmd, err := c.CmdWrapper(fmt.Sprintf("cp -R '%s' %s", src, chrootDest))
|
2013-09-30 12:33:57 -04:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2013-11-08 13:53:54 -05:00
|
|
|
var stderr bytes.Buffer
|
|
|
|
cmd := ShellCommand(cpCmd)
|
|
|
|
cmd.Env = append(cmd.Env, "LANG=C")
|
2014-05-08 11:11:19 -04:00
|
|
|
cmd.Env = append(cmd.Env, os.Environ()...)
|
2013-11-08 13:53:54 -05:00
|
|
|
cmd.Stderr = &stderr
|
2013-11-08 14:19:10 -05:00
|
|
|
err = cmd.Run()
|
2013-11-08 13:53:54 -05:00
|
|
|
if err == nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if strings.Contains(stderr.String(), "No such file") {
|
|
|
|
// This just means that the directory was empty. Just ignore it.
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return err
|
2013-08-23 22:31:33 -04:00
|
|
|
}
|
|
|
|
|
2015-11-02 06:22:52 -05:00
|
|
|
func (c *Communicator) DownloadDir(src string, dst string, exclude []string) error {
|
|
|
|
return fmt.Errorf("DownloadDir is not implemented for amazon-chroot")
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
}
|