Skip to content

Commit 17788ed

Browse files
authored
Merge pull request #12592 from ashley-cui/backports
[3.4] Backport podman machine ls
2 parents b8fde5c + 2b2ea4d commit 17788ed

File tree

4 files changed

+75
-2
lines changed

4 files changed

+75
-2
lines changed

cmd/podman/machine/list.go

Lines changed: 69 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,16 @@
33
package machine
44

55
import (
6+
"encoding/json"
67
"os"
78
"sort"
9+
"strconv"
810
"time"
911

1012
"github.com/containers/common/pkg/completion"
1113
"github.com/containers/common/pkg/config"
1214
"github.com/containers/common/pkg/report"
15+
"github.com/containers/podman/v3/cmd/podman/common"
1316
"github.com/containers/podman/v3/cmd/podman/registry"
1417
"github.com/containers/podman/v3/cmd/podman/validate"
1518
"github.com/containers/podman/v3/pkg/machine"
@@ -41,8 +44,11 @@ type listFlagType struct {
4144

4245
type machineReporter struct {
4346
Name string
47+
Default bool
4448
Created string
49+
Running bool
4550
LastUp string
51+
Stream string
4652
VMType string
4753
CPUs uint64
4854
Memory string
@@ -57,8 +63,8 @@ func init() {
5763

5864
flags := lsCmd.Flags()
5965
formatFlagName := "format"
60-
flags.StringVar(&listFlag.format, formatFlagName, "{{.Name}}\t{{.VMType}}\t{{.Created}}\t{{.LastUp}}\t{{.CPUs}}\t{{.Memory}}\t{{.DiskSize}}\n", "Format volume output using Go template")
61-
_ = lsCmd.RegisterFlagCompletionFunc(formatFlagName, completion.AutocompleteNone)
66+
flags.StringVar(&listFlag.format, formatFlagName, "{{.Name}}\t{{.VMType}}\t{{.Created}}\t{{.LastUp}}\t{{.CPUs}}\t{{.Memory}}\t{{.DiskSize}}\n", "Format volume output using JSON or a Go template")
67+
_ = lsCmd.RegisterFlagCompletionFunc(formatFlagName, common.AutocompleteFormat(machineReporter{}))
6268
flags.BoolVar(&listFlag.noHeading, "noheading", false, "Do not print headers")
6369
}
6470

@@ -78,6 +84,21 @@ func list(cmd *cobra.Command, args []string) error {
7884
sort.Slice(listResponse, func(i, j int) bool {
7985
return listResponse[i].Running
8086
})
87+
88+
if report.IsJSON(listFlag.format) {
89+
machineReporter, err := toMachineFormat(listResponse)
90+
if err != nil {
91+
return err
92+
}
93+
94+
b, err := json.Marshal(machineReporter)
95+
if err != nil {
96+
return err
97+
}
98+
os.Stdout.Write(b)
99+
return nil
100+
}
101+
81102
machineReporter, err := toHumanFormat(listResponse)
82103
if err != nil {
83104
return err
@@ -121,6 +142,50 @@ func outputTemplate(cmd *cobra.Command, responses []*machineReporter) error {
121142
return tmpl.Execute(w, responses)
122143
}
123144

145+
func strTime(t time.Time) string {
146+
iso, err := t.MarshalText()
147+
if err != nil {
148+
return ""
149+
}
150+
return string(iso)
151+
}
152+
153+
func strUint(u uint64) string {
154+
return strconv.FormatUint(u, 10)
155+
}
156+
157+
func streamName(imageStream string) string {
158+
if imageStream == "" {
159+
return "default"
160+
}
161+
return imageStream
162+
}
163+
164+
func toMachineFormat(vms []*machine.ListResponse) ([]*machineReporter, error) {
165+
cfg, err := config.ReadCustomConfig()
166+
if err != nil {
167+
return nil, err
168+
}
169+
170+
machineResponses := make([]*machineReporter, 0, len(vms))
171+
for _, vm := range vms {
172+
response := new(machineReporter)
173+
response.Default = vm.Name == cfg.Engine.ActiveService
174+
response.Name = vm.Name
175+
response.Running = vm.Running
176+
response.LastUp = strTime(vm.LastUp)
177+
response.Created = strTime(vm.CreatedAt)
178+
response.Stream = streamName(vm.Stream)
179+
response.VMType = vm.VMType
180+
response.CPUs = vm.CPUs
181+
response.Memory = strUint(vm.Memory * units.MiB)
182+
response.DiskSize = strUint(vm.DiskSize * units.GiB)
183+
184+
machineResponses = append(machineResponses, response)
185+
}
186+
return machineResponses, nil
187+
}
188+
124189
func toHumanFormat(vms []*machine.ListResponse) ([]*machineReporter, error) {
125190
cfg, err := config.ReadCustomConfig()
126191
if err != nil {
@@ -132,11 +197,13 @@ func toHumanFormat(vms []*machine.ListResponse) ([]*machineReporter, error) {
132197
response := new(machineReporter)
133198
if vm.Name == cfg.Engine.ActiveService {
134199
response.Name = vm.Name + "*"
200+
response.Default = true
135201
} else {
136202
response.Name = vm.Name
137203
}
138204
if vm.Running {
139205
response.LastUp = "Currently running"
206+
response.Running = true
140207
} else {
141208
response.LastUp = units.HumanDuration(time.Since(vm.LastUp)) + " ago"
142209
}

pkg/machine/config.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ type ListResponse struct {
5757
CreatedAt time.Time
5858
LastUp time.Time
5959
Running bool
60+
Stream string
6061
VMType string
6162
CPUs uint64
6263
Memory uint64

pkg/machine/qemu/config.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ type MachineVM struct {
1313
IdentityPath string
1414
// IgnitionFilePath is the fq path to the .ign file
1515
IgnitionFilePath string
16+
// ImageStream is the update stream for the image
17+
ImageStream string
1618
// ImagePath is the fq path to
1719
ImagePath string
1820
// Memory in megabytes assigned to the vm

pkg/machine/qemu/machine.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,7 @@ func (v *MachineVM) Init(opts machine.InitOptions) error {
143143
switch opts.ImagePath {
144144
case "testing", "next", "stable", "":
145145
// Get image as usual
146+
v.ImageStream = opts.ImagePath
146147
dd, err := machine.NewFcosDownloader(vmtype, v.Name, opts.ImagePath)
147148
if err != nil {
148149
return err
@@ -154,6 +155,7 @@ func (v *MachineVM) Init(opts machine.InitOptions) error {
154155
default:
155156
// The user has provided an alternate image which can be a file path
156157
// or URL.
158+
v.ImageStream = "custom"
157159
g, err := machine.NewGenericDownloader(vmtype, v.Name, opts.ImagePath)
158160
if err != nil {
159161
return err
@@ -590,6 +592,7 @@ func GetVMInfos() ([]*machine.ListResponse, error) {
590592
listEntry := new(machine.ListResponse)
591593

592594
listEntry.Name = vm.Name
595+
listEntry.Stream = vm.ImageStream
593596
listEntry.VMType = "qemu"
594597
listEntry.CPUs = vm.CPUs
595598
listEntry.Memory = vm.Memory

0 commit comments

Comments
 (0)