mirror of
https://github.com/containers/podman.git
synced 2025-11-29 17:48:05 +08:00
For Podman 6, we still have providers and will continue to have a default provider for each platform. But where a platform has multiple providers, we want users to be able to cross provider boudnaries imposed in Podman 4/5. The key change is to look up virtual machines by name, as before, but to then also iterate all possible providers. As of this PR, init will still only create with the default provider, but a subsequent PR will introdouce an provider override. I also removed the "--all-providers" command line option on `podman machine ls` because it no longer makes sense. And I marked the all provider list test to be skipped. Signed-off-by: Brent Baude <bbaude@redhat.com>
132 lines
3.2 KiB
Go
132 lines
3.2 KiB
Go
//go:build amd64 || arm64
|
|
|
|
package machine
|
|
|
|
import (
|
|
"os"
|
|
|
|
"github.com/containers/podman/v6/cmd/podman/common"
|
|
"github.com/containers/podman/v6/cmd/podman/registry"
|
|
"github.com/containers/podman/v6/cmd/podman/utils"
|
|
"github.com/containers/podman/v6/pkg/machine"
|
|
"github.com/containers/podman/v6/pkg/machine/env"
|
|
"github.com/containers/podman/v6/pkg/machine/shim"
|
|
"github.com/spf13/cobra"
|
|
"go.podman.io/common/pkg/report"
|
|
)
|
|
|
|
var (
|
|
inspectCmd = &cobra.Command{
|
|
Use: "inspect [options] [MACHINE...]",
|
|
Short: "Inspect an existing machine",
|
|
Long: "Provide details on a managed virtual machine",
|
|
PersistentPreRunE: machinePreRunE,
|
|
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))
|
|
for _, name := range args {
|
|
mc, vmProvider, err := shim.VMExists(name)
|
|
if err != nil {
|
|
errs = append(errs, err)
|
|
continue
|
|
}
|
|
|
|
dirs, err := env.GetMachineDirs(provider.VMType())
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
state, err := vmProvider.State(mc, false)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
podmanSocket, podmanPipe, err := mc.ConnectionInfo(vmProvider.VMType())
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
rosetta, err := vmProvider.GetRosetta(mc)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
ii := machine.InspectInfo{
|
|
ConfigDir: *dirs.ConfigDir,
|
|
ConnectionInfo: machine.ConnectionConfig{
|
|
PodmanSocket: podmanSocket,
|
|
PodmanPipe: podmanPipe,
|
|
},
|
|
Created: mc.Created,
|
|
LastUp: mc.LastUp,
|
|
Name: mc.Name,
|
|
Resources: mc.Resources,
|
|
SSHConfig: mc.SSH,
|
|
State: state,
|
|
UserModeNetworking: vmProvider.UserModeNetworkEnabled(mc),
|
|
Rootful: mc.HostUser.Rootful,
|
|
Rosetta: rosetta,
|
|
}
|
|
|
|
vms = append(vms, ii)
|
|
}
|
|
|
|
switch {
|
|
case cmd.Flag("format").Changed:
|
|
rpt := report.New(os.Stdout, cmd.Name())
|
|
defer rpt.Flush()
|
|
|
|
rpt, err := rpt.Parse(report.OriginUser, inspectFlag.format)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
if err := rpt.Execute(vms); err != nil {
|
|
errs = append(errs, err)
|
|
}
|
|
default:
|
|
if err := printJSON(vms); err != nil {
|
|
errs = append(errs, 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)
|
|
}
|