2015-12-22 09:56:33 -05:00
|
|
|
package lxc
|
|
|
|
|
|
|
|
import (
|
2019-04-03 11:14:55 -04:00
|
|
|
"context"
|
2015-12-22 09:56:33 -05:00
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"io/ioutil"
|
|
|
|
"log"
|
|
|
|
"os"
|
|
|
|
"os/exec"
|
|
|
|
"path/filepath"
|
|
|
|
"strings"
|
2016-05-25 15:34:36 -04:00
|
|
|
"syscall"
|
2018-01-22 20:21:10 -05:00
|
|
|
|
2020-12-17 16:29:25 -05:00
|
|
|
packersdk "github.com/hashicorp/packer-plugin-sdk/packer"
|
|
|
|
"github.com/hashicorp/packer-plugin-sdk/tmp"
|
2015-12-22 09:56:33 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
type LxcAttachCommunicator struct {
|
|
|
|
RootFs string
|
|
|
|
ContainerName string
|
2017-10-30 21:48:43 -04:00
|
|
|
AttachOptions []string
|
2015-12-22 09:56:33 -05:00
|
|
|
CmdWrapper CommandWrapper
|
|
|
|
}
|
|
|
|
|
2020-11-19 18:10:00 -05:00
|
|
|
func (c *LxcAttachCommunicator) Start(ctx context.Context, cmd *packersdk.RemoteCmd) error {
|
2015-12-22 09:56:33 -05:00
|
|
|
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(
|
|
|
|
"lxc-attach execution exited with '%d': '%s'",
|
|
|
|
exitStatus, cmd.Command)
|
|
|
|
cmd.SetExited(exitStatus)
|
|
|
|
}()
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *LxcAttachCommunicator) Upload(dst string, r io.Reader, fi *os.FileInfo) error {
|
|
|
|
log.Printf("Uploading to rootfs: %s", dst)
|
2018-12-12 09:45:00 -05:00
|
|
|
tf, err := tmp.File("packer-lxc-attach")
|
2015-12-22 09:56:33 -05:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("Error uploading file to rootfs: %s", err)
|
|
|
|
}
|
|
|
|
defer os.Remove(tf.Name())
|
|
|
|
io.Copy(tf, r)
|
|
|
|
|
2018-07-29 08:57:49 -04:00
|
|
|
attachCommand := []string{"cat", "%s", " | ", "lxc-attach"}
|
|
|
|
attachCommand = append(attachCommand, c.AttachOptions...)
|
|
|
|
attachCommand = append(attachCommand, []string{"--name", "%s", "--", "/bin/sh -c \"/bin/cat > %s\""}...)
|
|
|
|
|
|
|
|
cpCmd, err := c.CmdWrapper(fmt.Sprintf(strings.Join(attachCommand, " "), tf.Name(), c.ContainerName, dst))
|
2015-12-22 09:56:33 -05:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2017-10-04 16:35:15 -04:00
|
|
|
if fi != nil {
|
|
|
|
tfDir := filepath.Dir(tf.Name())
|
|
|
|
// rename tempfile to match original file name. This makes sure that if file is being
|
|
|
|
// moved into a directory, the filename is preserved instead of a temp name.
|
|
|
|
adjustedTempName := filepath.Join(tfDir, (*fi).Name())
|
2018-07-29 08:57:49 -04:00
|
|
|
mvCmd, err := c.CmdWrapper(fmt.Sprintf("mv %s %s", tf.Name(), adjustedTempName))
|
2017-10-04 16:35:15 -04:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer os.Remove(adjustedTempName)
|
2017-10-04 18:23:36 -04:00
|
|
|
ShellCommand(mvCmd).Run()
|
2017-10-04 16:35:15 -04:00
|
|
|
// change cpCmd to use new file name as source
|
2018-07-29 08:57:49 -04:00
|
|
|
cpCmd, err = c.CmdWrapper(fmt.Sprintf(strings.Join(attachCommand, " "), adjustedTempName, c.ContainerName, dst))
|
2017-10-04 16:35:15 -04:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-12-22 09:56:33 -05:00
|
|
|
log.Printf("Running copy command: %s", dst)
|
|
|
|
|
|
|
|
return ShellCommand(cpCmd).Run()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *LxcAttachCommunicator) UploadDir(dst string, src string, exclude []string) error {
|
|
|
|
// TODO: remove any file copied if it appears in `exclude`
|
|
|
|
dest := filepath.Join(c.RootFs, dst)
|
|
|
|
log.Printf("Uploading directory '%s' to rootfs '%s'", src, dest)
|
2018-07-29 08:57:49 -04:00
|
|
|
cpCmd, err := c.CmdWrapper(fmt.Sprintf("cp -R %s/. %s", src, dest))
|
2015-12-22 09:56:33 -05:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return ShellCommand(cpCmd).Run()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *LxcAttachCommunicator) Download(src string, w io.Writer) error {
|
|
|
|
src = filepath.Join(c.RootFs, src)
|
|
|
|
log.Printf("Downloading from rootfs 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
|
|
|
|
}
|
|
|
|
|
2016-05-10 20:37:01 -04:00
|
|
|
func (c *LxcAttachCommunicator) DownloadDir(src string, dst string, exclude []string) error {
|
2016-05-25 15:34:36 -04:00
|
|
|
return fmt.Errorf("DownloadDir is not implemented for lxc")
|
2016-05-10 20:37:01 -04:00
|
|
|
}
|
|
|
|
|
2015-12-22 09:56:33 -05:00
|
|
|
func (c *LxcAttachCommunicator) Execute(commandString string) (*exec.Cmd, error) {
|
|
|
|
log.Printf("Executing with lxc-attach in container: %s %s %s", c.ContainerName, c.RootFs, commandString)
|
2017-10-30 21:48:43 -04:00
|
|
|
|
2018-07-29 08:57:49 -04:00
|
|
|
attachCommand := []string{"lxc-attach"}
|
2017-10-30 21:48:43 -04:00
|
|
|
attachCommand = append(attachCommand, c.AttachOptions...)
|
|
|
|
attachCommand = append(attachCommand, []string{"--name", "%s", "--", "/bin/sh -c \"%s\""}...)
|
|
|
|
|
2015-12-22 09:56:33 -05:00
|
|
|
command, err := c.CmdWrapper(
|
2017-10-30 21:48:43 -04:00
|
|
|
fmt.Sprintf(strings.Join(attachCommand, " "), c.ContainerName, commandString))
|
2015-12-22 09:56:33 -05:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
localCmd := ShellCommand(command)
|
|
|
|
log.Printf("Executing lxc-attach: %s %#v", localCmd.Path, localCmd.Args)
|
|
|
|
|
|
|
|
return localCmd, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *LxcAttachCommunicator) CheckInit() (string, error) {
|
|
|
|
log.Printf("Debug runlevel exec")
|
|
|
|
localCmd, err := c.Execute("/sbin/runlevel")
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
pr, _ := localCmd.StdoutPipe()
|
|
|
|
if err = localCmd.Start(); err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
output, err := ioutil.ReadAll(pr)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
err = localCmd.Wait()
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
return strings.TrimSpace(string(output)), nil
|
2016-05-10 20:37:01 -04:00
|
|
|
}
|