refactor: tidy podman driver, remove redundant command setup
This commit is contained in:
parent
7466ad21a9
commit
23eef5de24
1 changed files with 17 additions and 35 deletions
|
@ -10,18 +10,21 @@ import (
|
|||
"os/exec"
|
||||
)
|
||||
|
||||
type PodmanDriver struct{}
|
||||
|
||||
func (d PodmanDriver) Pull(uri string) error {
|
||||
cmd := exec.Command("podman", "pull", uri)
|
||||
// Executes a Podman command and pipes output to stdout/stderr.
|
||||
//
|
||||
// The exec.Cmd pointer returned isn't ran.
|
||||
func podmanCommand(args ...string) *exec.Cmd {
|
||||
cmd := exec.Command("podman", args...)
|
||||
cmd.Stdout = os.Stdout
|
||||
cmd.Stderr = os.Stderr
|
||||
|
||||
if err := cmd.Run(); err != nil {
|
||||
return err
|
||||
return cmd
|
||||
}
|
||||
|
||||
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.
|
||||
|
@ -30,19 +33,12 @@ func (d PodmanDriver) Pull(uri string) error {
|
|||
// can be easily collected and cleaned up on stop.
|
||||
func (d PodmanDriver) Start(uri string, containerName string) error {
|
||||
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
|
||||
}
|
||||
|
||||
cmd = exec.Command("podman", "run", "-td", "--name", containerName, "-v", fmt.Sprintf("%s:/workspace", volumeName), uri)
|
||||
cmd.Stdout = os.Stdout
|
||||
cmd.Stderr = os.Stderr
|
||||
|
||||
if err := cmd.Run(); err != nil {
|
||||
if err := podmanCommand("run", "-td", "--name", containerName, "-v", fmt.Sprintf("%s:/workspace", volumeName), uri).Run(); err != nil {
|
||||
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
|
||||
// as owner.
|
||||
func (d PodmanDriver) Stop(containerName string) error {
|
||||
cmd := exec.Command("podman", "rm", "-f", "-t", "2", containerName)
|
||||
cmd.Stdout = os.Stdout
|
||||
cmd.Stderr = os.Stderr
|
||||
|
||||
if err := cmd.Run(); err != nil {
|
||||
if err := podmanCommand("rm", "-f", "-t", "2", containerName).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 {
|
||||
if err := podmanCommand("wait", containerName, "--ignore").Run(); err != nil {
|
||||
logger.Error("%+v", err)
|
||||
}
|
||||
|
||||
cmd = exec.Command("podman", "volume", "prune", "--filter", fmt.Sprintf("label=owner=%s", containerName), "-f")
|
||||
cmd.Stdout = os.Stdout
|
||||
cmd.Stderr = os.Stderr
|
||||
|
||||
if err := cmd.Run(); err != nil {
|
||||
if err := podmanCommand("volume", "prune", "--filter", fmt.Sprintf("label=owner=%s", containerName), "-f").Run(); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
|
@ -100,9 +84,7 @@ func (d PodmanDriver) Exec(containerId string, command string, options CommandOp
|
|||
command,
|
||||
)
|
||||
|
||||
cmd := exec.Command("podman", commandArgs...)
|
||||
cmd.Stdout = os.Stdout
|
||||
cmd.Stderr = os.Stderr
|
||||
cmd := podmanCommand(commandArgs...)
|
||||
err := cmd.Run()
|
||||
|
||||
return CommandResult{
|
||||
|
|
Loading…
Reference in a new issue