File: chroot_communicator.go

package info (click to toggle)
packer 1.6.6%2Bds2-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 33,156 kB
  • sloc: sh: 1,154; python: 619; makefile: 251; ruby: 205; xml: 97
file content (55 lines) | stat: -rw-r--r-- 1,427 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
package hyperone

import (
	"context"
	"fmt"
	"io"
	"os"
	"path/filepath"
	"strconv"

	packersdk "github.com/hashicorp/packer/packer-plugin-sdk/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    packersdk.Communicator
}

func (c *ChrootCommunicator) Start(ctx context.Context, cmd *packersdk.RemoteCmd) error {
	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

	return c.Wrapped.Start(ctx, cmd)
}

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)
}