refactor: tidy podman driver, remove redundant command setup

This commit is contained in:
Marc 2024-09-02 00:46:21 -04:00
parent 7466ad21a9
commit 23eef5de24
Signed by: marc
GPG key ID: 048E042F22B5DC79

View file

@ -10,18 +10,21 @@ import (
"os/exec" "os/exec"
) )
type PodmanDriver struct{} // Executes a Podman command and pipes output to stdout/stderr.
//
func (d PodmanDriver) Pull(uri string) error { // The exec.Cmd pointer returned isn't ran.
cmd := exec.Command("podman", "pull", uri) func podmanCommand(args ...string) *exec.Cmd {
cmd := exec.Command("podman", args...)
cmd.Stdout = os.Stdout cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr cmd.Stderr = os.Stderr
if err := cmd.Run(); err != nil { return cmd
return err }
}
return nil type PodmanDriver struct{}
func (d PodmanDriver) Pull(uri string) error {
return podmanCommand("pull", uri).Run()
} }
// Sets up a new container with an attached workspace volume. // Sets up a new container with an attached workspace volume.
@ -30,19 +33,12 @@ func (d PodmanDriver) Pull(uri string) error {
// can be easily collected and cleaned up on stop. // can be easily collected and cleaned up on stop.
func (d PodmanDriver) Start(uri string, containerName string) error { func (d PodmanDriver) Start(uri string, containerName string) error {
volumeName := fmt.Sprintf("%s-workspace", containerName) 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 { if err := podmanCommand("volume", "create", volumeName, "--label", fmt.Sprintf("owner=%s", containerName)).Run(); err != nil {
return err return err
} }
cmd = exec.Command("podman", "run", "-td", "--name", containerName, "-v", fmt.Sprintf("%s:/workspace", volumeName), uri) if err := podmanCommand("run", "-td", "--name", containerName, "-v", fmt.Sprintf("%s:/workspace", volumeName), uri).Run(); err != nil {
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
if err := cmd.Run(); err != nil {
return err return err
} }
@ -52,27 +48,15 @@ func (d PodmanDriver) Start(uri string, containerName string) error {
// Stops a container and removes any volumes labelled with its name // Stops a container and removes any volumes labelled with its name
// as owner. // as owner.
func (d PodmanDriver) Stop(containerName string) error { func (d PodmanDriver) Stop(containerName string) error {
cmd := exec.Command("podman", "rm", "-f", "-t", "2", containerName) if err := podmanCommand("rm", "-f", "-t", "2", containerName).Run(); err != nil {
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
if err := cmd.Run(); err != nil {
return err return err
} }
cmd = exec.Command("podman", "wait", containerName, "--ignore") if err := podmanCommand("wait", containerName, "--ignore").Run(); err != nil {
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
if err := cmd.Run(); err != nil {
logger.Error("%+v", err) logger.Error("%+v", err)
} }
cmd = exec.Command("podman", "volume", "prune", "--filter", fmt.Sprintf("label=owner=%s", containerName), "-f") if err := podmanCommand("volume", "prune", "--filter", fmt.Sprintf("label=owner=%s", containerName), "-f").Run(); err != nil {
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
if err := cmd.Run(); err != nil {
return err return err
} }
@ -100,9 +84,7 @@ func (d PodmanDriver) Exec(containerId string, command string, options CommandOp
command, command,
) )
cmd := exec.Command("podman", commandArgs...) cmd := podmanCommand(commandArgs...)
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
err := cmd.Run() err := cmd.Run()
return CommandResult{ return CommandResult{