diff options
author | OpenShift Merge Robot <openshift-merge-robot@users.noreply.github.com> | 2021-03-30 22:36:25 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-03-30 22:36:25 +0200 |
commit | a373e2fdf38456b8ac584809e73cdb9f9521a796 (patch) | |
tree | da598460aa5b6a28d43e78d376729df843ec4e1a /cmd | |
parent | bd07179e9c608ff1075f052843aeb9b1c8b7c7e3 (diff) | |
parent | ef4e91a59eed957e56bf1536bc03df94cd096576 (diff) | |
download | podman-a373e2fdf38456b8ac584809e73cdb9f9521a796.tar.gz podman-a373e2fdf38456b8ac584809e73cdb9f9521a796.tar.bz2 podman-a373e2fdf38456b8ac584809e73cdb9f9521a796.zip |
Merge pull request #9885 from ashley-cui/machinels
Add podman machine ls
Diffstat (limited to 'cmd')
-rw-r--r-- | cmd/podman/machine/list.go | 143 |
1 files changed, 143 insertions, 0 deletions
diff --git a/cmd/podman/machine/list.go b/cmd/podman/machine/list.go new file mode 100644 index 000000000..3c8368c6b --- /dev/null +++ b/cmd/podman/machine/list.go @@ -0,0 +1,143 @@ +// +build amd64,linux arm64,linux amd64,darwin arm64,darwin + +package machine + +import ( + "os" + "sort" + "text/tabwriter" + "text/template" + "time" + + "github.com/containers/common/pkg/completion" + "github.com/containers/common/pkg/config" + "github.com/containers/common/pkg/report" + "github.com/containers/podman/v3/cmd/podman/parse" + "github.com/containers/podman/v3/cmd/podman/registry" + "github.com/containers/podman/v3/cmd/podman/validate" + "github.com/containers/podman/v3/pkg/domain/entities" + "github.com/containers/podman/v3/pkg/machine" + "github.com/containers/podman/v3/pkg/machine/qemu" + "github.com/docker/go-units" + "github.com/pkg/errors" + "github.com/spf13/cobra" +) + +var ( + lsCmd = &cobra.Command{ + Use: "list [options]", + Aliases: []string{"ls"}, + Short: "List machines", + Long: "List Podman managed virtual machines.", + RunE: list, + Args: validate.NoArgs, + Example: `podman machine list, + podman machine ls`, + ValidArgsFunction: completion.AutocompleteNone, + } + listFlag = listFlagType{} +) + +type listFlagType struct { + format string + noHeading bool +} + +type machineReporter struct { + Name string + Created string + LastUp string + VMType string +} + +func init() { + registry.Commands = append(registry.Commands, registry.CliCommand{ + Mode: []entities.EngineMode{entities.ABIMode, entities.TunnelMode}, + Command: lsCmd, + Parent: machineCmd, + }) + + flags := lsCmd.Flags() + formatFlagName := "format" + flags.StringVar(&listFlag.format, formatFlagName, "{{.Name}}\t{{.VMType}}\t{{.Created}}\t{{.LastUp}}\n", "Format volume output using Go template") + _ = lsCmd.RegisterFlagCompletionFunc(formatFlagName, completion.AutocompleteNone) +} + +func list(cmd *cobra.Command, args []string) error { + var opts machine.ListOptions + // We only have qemu VM's for now + listResponse, err := qemu.List(opts) + if err != nil { + return errors.Wrap(err, "error listing vms") + } + + // Sort by last run + sort.Slice(listResponse, func(i, j int) bool { + return listResponse[i].LastUp.After(listResponse[j].LastUp) + }) + // Bring currently running machines to top + sort.Slice(listResponse, func(i, j int) bool { + return listResponse[i].Running + }) + machineReporter, err := toHumanFormat(listResponse) + if err != nil { + return err + } + + return outputTemplate(cmd, machineReporter) +} + +func outputTemplate(cmd *cobra.Command, responses []*machineReporter) error { + headers := report.Headers(machineReporter{}, map[string]string{ + "LastUp": "LAST UP", + "VmType": "VM TYPE", + }) + + row := report.NormalizeFormat(listFlag.format) + format := parse.EnforceRange(row) + + tmpl, err := template.New("list machines").Parse(format) + if err != nil { + return err + } + w := tabwriter.NewWriter(os.Stdout, 12, 2, 2, ' ', 0) + defer w.Flush() + + if cmd.Flags().Changed("format") && !parse.HasTable(listFlag.format) { + listFlag.noHeading = true + } + + if !listFlag.noHeading { + if err := tmpl.Execute(w, headers); err != nil { + return errors.Wrapf(err, "failed to write report column headers") + } + } + return tmpl.Execute(w, responses) +} + +func toHumanFormat(vms []*machine.ListResponse) ([]*machineReporter, error) { + cfg, err := config.ReadCustomConfig() + if err != nil { + return nil, err + } + + humanResponses := make([]*machineReporter, 0, len(vms)) + for _, vm := range vms { + response := new(machineReporter) + if vm.Name == cfg.Engine.ActiveService { + response.Name = vm.Name + "*" + } else { + response.Name = vm.Name + } + if vm.Running { + response.LastUp = "Currently running" + } else { + response.LastUp = units.HumanDuration(time.Since(vm.LastUp)) + " ago" + } + response.Created = units.HumanDuration(time.Since(vm.CreatedAt)) + " ago" + response.VMType = vm.VMType + + humanResponses = append(humanResponses, response) + } + return humanResponses, nil +} |