cmd/run: Add function to get the entry point PID of a container

For some reason, the State:Pid value in the JSON returned by
'podman inspect --format json --type container' is a float64 [1], even
though internally Podman represents it as an int.

[1] https://github.com/containers/libpod/issues/6105

https://github.com/containers/toolbox/pull/318
This commit is contained in:
Harry Míchal 2020-05-06 17:07:10 +02:00 committed by Debarshi Ray
parent eb0ce415b0
commit cf5c58ab00

View file

@ -20,7 +20,9 @@ import (
"fmt"
"os"
"github.com/containers/toolbox/pkg/podman"
"github.com/containers/toolbox/pkg/utils"
"github.com/sirupsen/logrus"
"github.com/spf13/cobra"
)
@ -81,3 +83,35 @@ func runHelp(cmd *cobra.Command, args []string) {
return
}
}
func getEntryPointAndPID(container string) (string, int, error) {
logrus.Debugf("Inspecting entry point of container %s", container)
info, err := podman.Inspect("container", container)
if err != nil {
return "", 0, fmt.Errorf("failed to inspect entry point of container %s", container)
}
config := info["Config"].(map[string]interface{})
entryPoint := config["Cmd"].([]interface{})[0].(string)
state := info["State"].(map[string]interface{})
entryPointPID := state["Pid"]
logrus.Debugf("Entry point PID is a %T", entryPointPID)
var entryPointPIDInt int
switch entryPointPID.(type) {
case float64:
entryPointPIDFloat := entryPointPID.(float64)
entryPointPIDInt = int(entryPointPIDFloat)
case int:
entryPointPIDInt = entryPointPID.(int)
default:
return "", 0, fmt.Errorf("failed to inspect entry point PID of container %s", container)
}
logrus.Debugf("Entry point of container %s is %s (PID=%d)", container, entryPoint, entryPointPIDInt)
return entryPoint, entryPointPIDInt, nil
}