2019-01-28 06:30:15 -05:00
|
|
|
package hyperone
|
|
|
|
|
|
|
|
import (
|
2019-04-03 11:14:55 -04:00
|
|
|
"context"
|
2019-01-28 06:30:15 -05:00
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
"strconv"
|
|
|
|
|
|
|
|
"github.com/hashicorp/packer/packer"
|
|
|
|
)
|
|
|
|
|
|
|
|
type CommandWrapper func(string) (string, error)
|
|
|
|
|
|
|
|
// ChrootCommunicator works as a wrapper on SSHCommunicator, modyfing paths in
|
|
|
|
// flight to be run in a chroot.
|
|
|
|
type ChrootCommunicator struct {
|
|
|
|
Chroot string
|
|
|
|
CmdWrapper CommandWrapper
|
|
|
|
Wrapped packer.Communicator
|
|
|
|
}
|
|
|
|
|
2019-04-03 11:14:55 -04:00
|
|
|
func (c *ChrootCommunicator) Start(ctx context.Context, cmd *packer.RemoteCmd) error {
|
2019-01-28 06:30:15 -05:00
|
|
|
command := strconv.Quote(cmd.Command)
|
|
|
|
chrootCommand, err := c.CmdWrapper(
|
|
|
|
fmt.Sprintf("sudo chroot %s /bin/sh -c %s", c.Chroot, command))
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
cmd.Command = chrootCommand
|
|
|
|
|
2019-04-03 11:14:55 -04:00
|
|
|
return c.Wrapped.Start(ctx, cmd)
|
2019-01-28 06:30:15 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
func (c *ChrootCommunicator) Upload(dst string, r io.Reader, fi *os.FileInfo) error {
|
|
|
|
dst = filepath.Join(c.Chroot, dst)
|
|
|
|
return c.Wrapped.Upload(dst, r, fi)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *ChrootCommunicator) UploadDir(dst string, src string, exclude []string) error {
|
|
|
|
dst = filepath.Join(c.Chroot, dst)
|
|
|
|
return c.Wrapped.UploadDir(dst, src, exclude)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *ChrootCommunicator) Download(src string, w io.Writer) error {
|
|
|
|
src = filepath.Join(c.Chroot, src)
|
|
|
|
return c.Wrapped.Download(src, w)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *ChrootCommunicator) DownloadDir(src string, dst string, exclude []string) error {
|
|
|
|
src = filepath.Join(c.Chroot, src)
|
|
|
|
return c.Wrapped.DownloadDir(src, dst, exclude)
|
|
|
|
}
|