2024-08-01 22:43:18 +00:00
|
|
|
// Podman driver
|
|
|
|
//
|
|
|
|
// Abstracts interactions with Podman commands via the ContainerDriver interface.
|
2024-08-23 03:11:00 +00:00
|
|
|
package driver
|
2024-08-01 22:43:18 +00:00
|
|
|
|
|
|
|
import (
|
2024-08-12 03:23:41 +00:00
|
|
|
logger "courgette/internal/logging"
|
|
|
|
"fmt"
|
2024-08-01 22:43:18 +00:00
|
|
|
"os"
|
|
|
|
"os/exec"
|
|
|
|
)
|
|
|
|
|
|
|
|
type PodmanDriver struct{}
|
|
|
|
|
|
|
|
func (d PodmanDriver) Pull(uri string) error {
|
|
|
|
cmd := exec.Command("podman", "pull", uri)
|
|
|
|
cmd.Stdout = os.Stdout
|
|
|
|
cmd.Stderr = os.Stderr
|
|
|
|
|
|
|
|
if err := cmd.Run(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2024-08-12 03:23:41 +00:00
|
|
|
// Sets up a new container with an attached workspace volume.
|
|
|
|
//
|
|
|
|
// The volume created is labeled with `owner=<containerName>` so it
|
|
|
|
// can be easily collected and cleaned up on stop.
|
2024-08-01 22:43:18 +00:00
|
|
|
func (d PodmanDriver) Start(uri string, containerName string) error {
|
2024-08-12 03:23:41 +00:00
|
|
|
volumeName := fmt.Sprintf("%s-workspace", containerName)
|
|
|
|
cmd := exec.Command("podman", "volume", "create", volumeName, "--label", fmt.Sprintf("owner=%s", containerName))
|
|
|
|
cmd.Stdout = os.Stdout
|
|
|
|
cmd.Stderr = os.Stderr
|
|
|
|
|
|
|
|
if err := cmd.Run(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
cmd = exec.Command("podman", "run", "-td", "--name", containerName, "-v", fmt.Sprintf("%s:/workspace", volumeName), uri)
|
2024-08-01 22:43:18 +00:00
|
|
|
cmd.Stdout = os.Stdout
|
|
|
|
cmd.Stderr = os.Stderr
|
|
|
|
|
|
|
|
if err := cmd.Run(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2024-08-12 03:23:41 +00:00
|
|
|
// Stops a container and removes any volumes labelled with its name
|
|
|
|
// as owner.
|
2024-08-01 22:43:18 +00:00
|
|
|
func (d PodmanDriver) Stop(containerName string) error {
|
2024-08-12 03:23:41 +00:00
|
|
|
cmd := exec.Command("podman", "rm", "-f", "-t", "2", containerName)
|
|
|
|
cmd.Stdout = os.Stdout
|
|
|
|
cmd.Stderr = os.Stderr
|
|
|
|
|
|
|
|
if err := cmd.Run(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
cmd = exec.Command("podman", "wait", containerName, "--ignore")
|
|
|
|
cmd.Stdout = os.Stdout
|
|
|
|
cmd.Stderr = os.Stderr
|
|
|
|
|
|
|
|
if err := cmd.Run(); err != nil {
|
|
|
|
logger.Error("%+v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
cmd = exec.Command("podman", "volume", "prune", "--filter", fmt.Sprintf("label=owner=%s", containerName), "-f")
|
2024-08-01 22:43:18 +00:00
|
|
|
cmd.Stdout = os.Stdout
|
|
|
|
cmd.Stderr = os.Stderr
|
|
|
|
|
|
|
|
if err := cmd.Run(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2024-08-21 17:46:57 +00:00
|
|
|
func (d PodmanDriver) Exec(containerId string, command string, options CommandOptions) CommandResult {
|
2024-08-21 03:35:49 +00:00
|
|
|
envArgs := []string{}
|
|
|
|
|
2024-08-21 17:46:57 +00:00
|
|
|
for key, value := range options.Env {
|
2024-08-21 03:35:49 +00:00
|
|
|
envArgs = append(envArgs, fmt.Sprintf("-e=%s=%s", key, value))
|
|
|
|
}
|
|
|
|
|
|
|
|
commandArgs := []string{
|
|
|
|
"exec",
|
|
|
|
"--workdir",
|
2024-08-21 17:46:57 +00:00
|
|
|
options.Cwd,
|
2024-08-21 03:35:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
commandArgs = append(commandArgs, envArgs...)
|
|
|
|
|
|
|
|
commandArgs = append(commandArgs, containerId,
|
|
|
|
"bash",
|
|
|
|
"-c",
|
|
|
|
command,
|
|
|
|
)
|
|
|
|
|
|
|
|
cmd := exec.Command("podman", commandArgs...)
|
2024-08-01 22:43:18 +00:00
|
|
|
cmd.Stdout = os.Stdout
|
|
|
|
cmd.Stderr = os.Stderr
|
2024-08-02 20:38:18 +00:00
|
|
|
err := cmd.Run()
|
|
|
|
|
|
|
|
return CommandResult{
|
|
|
|
Error: err,
|
|
|
|
ExitCode: cmd.ProcessState.ExitCode(),
|
|
|
|
}
|
2024-08-01 22:43:18 +00:00
|
|
|
}
|