1
0

Add a flag to set the container runtime binary

This commit is contained in:
dump_stack() 2023-04-07 18:57:18 +00:00
parent e35e030c54
commit 9e55ebd44e
Signed by: dump_stack
GPG Key ID: BE44DA8C062D87DC
3 changed files with 28 additions and 5 deletions

View File

@ -20,6 +20,8 @@ import (
"code.dumpstack.io/tools/out-of-tree/config"
)
var containerRuntime = "docker"
type ContainerCmd struct {
Filter string `help:"filter by name"`
@ -56,7 +58,8 @@ type ContainerCleanupCmd struct{}
func (cmd ContainerCleanupCmd) Run(containerCmd *ContainerCmd) (err error) {
var output []byte
for _, name := range containerCmd.Containers() {
output, err = exec.Command("docker", "image", "rm", name).CombinedOutput()
output, err = exec.Command(containerRuntime, "image", "rm", name).
CombinedOutput()
if err != nil {
log.Error().Err(err).Str("output", string(output)).Msg("")
return
@ -72,7 +75,7 @@ type containerImageInfo struct {
}
func listContainerImages() (diis []containerImageInfo, err error) {
cmd := exec.Command("docker", "images")
cmd := exec.Command(containerRuntime, "images")
log.Debug().Msgf("%v", cmd)
rawOutput, err := cmd.CombinedOutput()
@ -148,7 +151,7 @@ func (c container) Build(imagePath string) (output string, err error) {
args := []string{"build"}
args = append(args, "-t", c.name, imagePath)
cmd := exec.Command("docker", args...)
cmd := exec.Command(containerRuntime, args...)
flog := log.With().
Str("command", fmt.Sprintf("%v", cmd)).
@ -207,7 +210,7 @@ func (c container) Run(workdir string, command string) (output string, err error
args = append(args, command)
}
cmd := exec.Command("docker", args...)
cmd := exec.Command(containerRuntime, args...)
log.Debug().Msgf("%v", cmd)

View File

@ -262,7 +262,8 @@ func generateBaseDockerImage(registry string, commands []config.DockerCommand,
d := "# BASE\n"
cmd := exec.Command("docker", "images", "-q", sk.DockerName())
// TODO move as function to container.go
cmd := exec.Command(containerRuntime, "images", "-q", sk.DockerName())
log.Debug().Msgf("run %v", cmd)
rawOutput, err := cmd.CombinedOutput()

19
main.go
View File

@ -9,6 +9,7 @@ import (
"io"
"math/rand"
"os"
"os/exec"
"os/user"
"runtime/debug"
"strconv"
@ -44,6 +45,8 @@ type CLI struct {
Version VersionFlag `name:"version" help:"print version information and quit"`
LogLevel LogLevelFlag `enum:"trace,debug,info,warn,error" default:"info"`
ContainerRuntime string `enum:"podman,docker" default:"podman"`
}
type LogLevelFlag string
@ -153,6 +156,22 @@ func main() {
log.Debug().Msgf("%v", buildInfo.Settings)
}
_, err = exec.LookPath(cli.ContainerRuntime)
if err != nil {
if cli.ContainerRuntime == "podman" { // default value
log.Debug().Msgf("podman is not found in $PATH, " +
"fall back to docker")
cli.ContainerRuntime = "docker"
}
_, err = exec.LookPath(cli.ContainerRuntime)
if err != nil {
log.Fatal().Msgf("%v is not found in $PATH",
cli.ContainerRuntime)
}
}
containerRuntime = cli.ContainerRuntime
err = ctx.Run(&cli.Globals)
ctx.FatalIfErrorf(err)
}