2016-05-26 23:21:55 -04:00
|
|
|
package lxd
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"log"
|
|
|
|
"os"
|
|
|
|
"os/exec"
|
|
|
|
"path/filepath"
|
|
|
|
"syscall"
|
2018-01-22 20:21:10 -05:00
|
|
|
|
|
|
|
"github.com/hashicorp/packer/packer"
|
2016-05-26 23:21:55 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
type Communicator struct {
|
|
|
|
ContainerName string
|
|
|
|
CmdWrapper CommandWrapper
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Communicator) Start(cmd *packer.RemoteCmd) error {
|
|
|
|
localCmd, err := c.Execute(cmd.Command)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
localCmd.Stdin = cmd.Stdin
|
|
|
|
localCmd.Stdout = cmd.Stdout
|
|
|
|
localCmd.Stderr = cmd.Stderr
|
|
|
|
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(
|
2016-05-28 21:24:46 -04:00
|
|
|
"lxc exec execution exited with '%d': '%s'",
|
2016-05-26 23:21:55 -04:00
|
|
|
exitStatus, cmd.Command)
|
|
|
|
cmd.SetExited(exitStatus)
|
|
|
|
}()
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Communicator) Upload(dst string, r io.Reader, fi *os.FileInfo) error {
|
2017-10-04 18:23:36 -04:00
|
|
|
fileDestination := filepath.Join(c.ContainerName, dst)
|
|
|
|
// find out if the place we are pushing to is a directory
|
|
|
|
testDirectoryCommand := fmt.Sprintf(`test -d "%s"`, dst)
|
|
|
|
cmd := &packer.RemoteCmd{Command: testDirectoryCommand}
|
|
|
|
err := c.Start(cmd)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
log.Printf("Unable to check whether remote path is a dir: %s", err)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
cmd.Wait()
|
|
|
|
|
|
|
|
if cmd.ExitStatus == 0 {
|
|
|
|
log.Printf("path is a directory; copying file into directory.")
|
|
|
|
fileDestination = filepath.Join(c.ContainerName, dst, (*fi).Name())
|
|
|
|
}
|
|
|
|
|
|
|
|
cpCmd, err := c.CmdWrapper(fmt.Sprintf("lxc file push - %s", fileDestination))
|
2016-05-26 23:21:55 -04:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2016-05-28 21:24:46 -04:00
|
|
|
log.Printf("Running copy command: %s", cpCmd)
|
|
|
|
command := ShellCommand(cpCmd)
|
|
|
|
command.Stdin = r
|
2016-05-26 23:21:55 -04:00
|
|
|
|
2016-05-28 21:24:46 -04:00
|
|
|
return command.Run()
|
2016-05-26 23:21:55 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Communicator) UploadDir(dst string, src string, exclude []string) error {
|
2016-05-30 19:13:59 -04:00
|
|
|
// NOTE:lxc file push doesn't yet support directory uploads.
|
|
|
|
// As a work around, we tar up the folder, upload it as a file, then extract it
|
2016-05-28 21:24:46 -04:00
|
|
|
|
2017-01-27 16:06:18 -05:00
|
|
|
// Don't use 'z' flag as compressing may take longer and the transfer is likely local.
|
2017-09-04 13:57:55 -04:00
|
|
|
// If this isn't the case, it is possible for the user to compress in another step then transfer.
|
2017-01-27 16:06:18 -05:00
|
|
|
// It wouldn't be possibe to disable compression, without exposing this option.
|
|
|
|
tar, err := c.CmdWrapper(fmt.Sprintf("tar -cf - -C %s .", src))
|
2016-05-26 23:21:55 -04:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2017-01-27 16:06:18 -05:00
|
|
|
cp, err := c.CmdWrapper(fmt.Sprintf("lxc exec %s -- tar -xf - -C %s", c.ContainerName, dst))
|
2016-05-30 19:13:59 -04:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
tarCmd := ShellCommand(tar)
|
|
|
|
cpCmd := ShellCommand(cp)
|
|
|
|
|
|
|
|
cpCmd.Stdin, _ = tarCmd.StdoutPipe()
|
|
|
|
log.Printf("Starting tar command: %s", tar)
|
|
|
|
err = tarCmd.Start()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
log.Printf("Running cp command: %s", cp)
|
|
|
|
err = cpCmd.Run()
|
|
|
|
if err != nil {
|
|
|
|
log.Printf("Error running cp command: %s", err)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
err = tarCmd.Wait()
|
|
|
|
if err != nil {
|
|
|
|
log.Printf("Error running tar command: %s", err)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
2016-05-26 23:21:55 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Communicator) Download(src string, w io.Writer) error {
|
2016-05-28 21:24:46 -04:00
|
|
|
cpCmd, err := c.CmdWrapper(fmt.Sprintf("lxc file pull %s -", filepath.Join(c.ContainerName, src)))
|
2016-05-26 23:21:55 -04:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2016-05-28 21:24:46 -04:00
|
|
|
log.Printf("Running copy command: %s", cpCmd)
|
|
|
|
command := ShellCommand(cpCmd)
|
|
|
|
command.Stdout = w
|
2016-05-26 23:21:55 -04:00
|
|
|
|
2016-05-28 21:24:46 -04:00
|
|
|
return command.Run()
|
2016-05-26 23:21:55 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Communicator) DownloadDir(src string, dst string, exclude []string) error {
|
2016-05-30 19:13:59 -04:00
|
|
|
// TODO This could probably be "lxc exec <container> -- cd <src> && tar -czf - | tar -xzf - -C <dst>"
|
2016-05-26 23:21:55 -04:00
|
|
|
return fmt.Errorf("DownloadDir is not implemented for lxc")
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Communicator) Execute(commandString string) (*exec.Cmd, error) {
|
2016-05-28 21:24:46 -04:00
|
|
|
log.Printf("Executing with lxc exec in container: %s %s", c.ContainerName, commandString)
|
2016-05-26 23:21:55 -04:00
|
|
|
command, err := c.CmdWrapper(
|
2016-08-06 23:25:57 -04:00
|
|
|
fmt.Sprintf("lxc exec %s -- /bin/sh -c \"%s\"", c.ContainerName, commandString))
|
2016-05-26 23:21:55 -04:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
localCmd := ShellCommand(command)
|
2016-05-28 21:24:46 -04:00
|
|
|
log.Printf("Executing lxc exec: %s %#v", localCmd.Path, localCmd.Args)
|
2016-05-26 23:21:55 -04:00
|
|
|
|
|
|
|
return localCmd, nil
|
|
|
|
}
|