summaryrefslogtreecommitdiff
path: root/cmd/podman/machine
diff options
context:
space:
mode:
authorBrent Baude <bbaude@redhat.com>2022-04-01 15:31:13 -0500
committerBrent Baude <bbaude@redhat.com>2022-04-12 15:51:39 -0500
commit8710197e8592e9726d98d11d017d1c79ab07415b (patch)
treed3e1788912e4b1ac7d58e72d1a56cf0cbaf6a143 /cmd/podman/machine
parentdb7cd88c6781c3d42376f02b5b1547c466c45d3e (diff)
downloadpodman-8710197e8592e9726d98d11d017d1c79ab07415b.tar.gz
podman-8710197e8592e9726d98d11d017d1c79ab07415b.tar.bz2
podman-8710197e8592e9726d98d11d017d1c79ab07415b.zip
Introduce machine inspect
Allow users to inspect their podman virtual machines. This will be helpful for debug and development alike, because more details about the machine can be collected. Signed-off-by: Brent Baude <bbaude@redhat.com> [NO NEW TESTS NEEDED] Signed-off-by: Brent Baude <bbaude@redhat.com>
Diffstat (limited to 'cmd/podman/machine')
-rw-r--r--cmd/podman/machine/inspect.go90
1 files changed, 90 insertions, 0 deletions
diff --git a/cmd/podman/machine/inspect.go b/cmd/podman/machine/inspect.go
new file mode 100644
index 000000000..d43cabf6b
--- /dev/null
+++ b/cmd/podman/machine/inspect.go
@@ -0,0 +1,90 @@
+//go:build amd64 || arm64
+// +build amd64 arm64
+
+package machine
+
+import (
+ "encoding/json"
+ "os"
+
+ "github.com/containers/podman/v4/cmd/podman/common"
+ "github.com/containers/podman/v4/cmd/podman/registry"
+ "github.com/containers/podman/v4/cmd/podman/utils"
+ "github.com/containers/podman/v4/libpod/define"
+ "github.com/containers/podman/v4/pkg/machine"
+ "github.com/sirupsen/logrus"
+ "github.com/spf13/cobra"
+)
+
+var (
+ inspectCmd = &cobra.Command{
+ Use: "inspect [options] [MACHINE...]",
+ Short: "Inspect an existing machine",
+ Long: "Provide details on a managed virtual machine",
+ RunE: inspect,
+ Example: `podman machine inspect myvm`,
+ ValidArgsFunction: autocompleteMachine,
+ }
+ inspectFlag = inspectFlagType{}
+)
+
+type inspectFlagType struct {
+ format string
+}
+
+func init() {
+ registry.Commands = append(registry.Commands, registry.CliCommand{
+ Command: inspectCmd,
+ Parent: machineCmd,
+ })
+
+ flags := inspectCmd.Flags()
+ formatFlagName := "format"
+ flags.StringVar(&inspectFlag.format, formatFlagName, "", "Format volume output using JSON or a Go template")
+ _ = inspectCmd.RegisterFlagCompletionFunc(formatFlagName, common.AutocompleteFormat(machine.InspectInfo{}))
+}
+
+func inspect(cmd *cobra.Command, args []string) error {
+ var (
+ errs utils.OutputErrors
+ )
+ if len(args) < 1 {
+ args = append(args, defaultMachineName)
+ }
+ vms := make([]machine.InspectInfo, 0, len(args))
+ provider := getSystemDefaultProvider()
+ for _, vmName := range args {
+ vm, err := provider.LoadVMByName(vmName)
+ if err != nil {
+ errs = append(errs, err)
+ continue
+ }
+ state, err := vm.State()
+ if err != nil {
+ errs = append(errs, err)
+ continue
+ }
+ ii := machine.InspectInfo{
+ State: state,
+ VM: vm,
+ }
+ vms = append(vms, ii)
+ }
+ if len(inspectFlag.format) > 0 {
+ // need jhonce to work his template magic
+ return define.ErrNotImplemented
+ }
+ if err := printJSON(vms); err != nil {
+ logrus.Error(err)
+ }
+ return errs.PrintErrors()
+}
+
+func printJSON(data []machine.InspectInfo) error {
+ enc := json.NewEncoder(os.Stdout)
+ // by default, json marshallers will force utf=8 from
+ // a string. this breaks healthchecks that use <,>, &&.
+ enc.SetEscapeHTML(false)
+ enc.SetIndent("", " ")
+ return enc.Encode(data)
+}