From 08e8a80d4c04c0068554de49811249ece43eb587 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Harry=20M=C3=ADchal?= Date: Sun, 10 May 2020 13:25:33 +0200 Subject: [PATCH] pkg/podman: Add functions to check if a container/image is a Toolbox https://github.com/containers/toolbox/pull/318 --- src/pkg/podman/podman.go | 35 +++++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) diff --git a/src/pkg/podman/podman.go b/src/pkg/podman/podman.go index f96f0b9..afb3721 100644 --- a/src/pkg/podman/podman.go +++ b/src/pkg/podman/podman.go @@ -19,6 +19,7 @@ package podman import ( "bytes" "encoding/json" + "fmt" "github.com/HarryMichal/go-version" "github.com/containers/toolbox/pkg/shell" @@ -147,6 +148,40 @@ func Inspect(typearg string, target string) (map[string]interface{}, error) { return info[0], nil } +func IsToolboxContainer(container string) (bool, error) { + info, err := Inspect("container", container) + if err != nil { + return false, fmt.Errorf("failed to inspect container %s", container) + } + + labels, _ := info["Config"].(map[string]interface{})["Labels"].(map[string]interface{}) + if labels["com.redhat.component"] != "fedora-toolbox" && + labels["com.github.debarshiray.toolbox"] != "true" { + return false, fmt.Errorf("%s is not a toolbox container", container) + } + + return true, nil +} + +func IsToolboxImage(image string) (bool, error) { + info, err := Inspect("image", image) + if err != nil { + return false, fmt.Errorf("failed to inspect image %s", image) + } + + if info["Labels"] == nil { + return false, fmt.Errorf("%s is not a toolbox image", image) + } + + labels := info["Labels"].(map[string]interface{}) + if labels["com.redhat.component"] != "fedora-toolbox" && + labels["com.github.debarshiray.toolbox"] != "true" { + return false, fmt.Errorf("%s is not a toolbox image", image) + } + + return true, nil +} + func SetLogLevel(logLevel logrus.Level) { LogLevel = logLevel }