1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
|
package main
import (
"reflect"
"sort"
"strconv"
"strings"
"time"
"github.com/containers/libpod/cmd/podman/formats"
"github.com/containers/libpod/cmd/podman/libpodruntime"
"github.com/containers/libpod/cmd/podman/shared"
"github.com/containers/libpod/libpod"
"github.com/containers/libpod/pkg/util"
"github.com/docker/go-units"
"github.com/pkg/errors"
"github.com/urfave/cli"
)
const (
STOPPED = "Stopped"
RUNNING = "Running"
PAUSED = "Paused"
EXITED = "Exited"
ERROR = "Error"
CREATED = "Created"
NUM_CTR_INFO = 10
)
var (
bc_opts shared.PsOptions
)
type podPsCtrInfo struct {
Name string `"json:name,omitempty"`
Id string `"json:id,omitempty"`
Status string `"json:status,omitempty"`
}
type podPsOptions struct {
NoTrunc bool
Format string
Sort string
Quiet bool
NumberOfContainers bool
Cgroup bool
NamesOfContainers bool
IdsOfContainers bool
StatusOfContainers bool
}
type podPsTemplateParams struct {
Created string
ID string
Name string
NumberOfContainers int
Status string
Cgroup string
ContainerInfo string
InfraContainerID string
SharedNamespaces string
}
// podPsJSONParams is used as a base structure for the psParams
// If template output is requested, podPsJSONParams will be converted to
// podPsTemplateParams.
// podPsJSONParams will be populated by data from libpod.Container,
// the members of the struct are the sama data types as their sources.
type podPsJSONParams struct {
CreatedAt time.Time `json:"createdAt"`
ID string `json:"id"`
Name string `json:"name"`
NumberOfContainers int `json:"numberofcontainers"`
Status string `json:"status"`
CtrsInfo []podPsCtrInfo `json:"containerinfo,omitempty"`
Cgroup string `json:"cgroup,omitempty"`
InfraContainerID string `json:"infracontainerid,omitempty"`
SharedNamespaces []string `json:"sharednamespaces,omitempty"`
}
// Type declaration and functions for sorting the pod PS output
type podPsSorted []podPsJSONParams
func (a podPsSorted) Len() int { return len(a) }
func (a podPsSorted) Swap(i, j int) { a[i], a[j] = a[j], a[i] }
type podPsSortedCreated struct{ podPsSorted }
func (a podPsSortedCreated) Less(i, j int) bool {
return a.podPsSorted[i].CreatedAt.After(a.podPsSorted[j].CreatedAt)
}
type podPsSortedId struct{ podPsSorted }
func (a podPsSortedId) Less(i, j int) bool { return a.podPsSorted[i].ID < a.podPsSorted[j].ID }
type podPsSortedNumber struct{ podPsSorted }
func (a podPsSortedNumber) Less(i, j int) bool {
return len(a.podPsSorted[i].CtrsInfo) < len(a.podPsSorted[j].CtrsInfo)
}
type podPsSortedName struct{ podPsSorted }
func (a podPsSortedName) Less(i, j int) bool { return a.podPsSorted[i].Name < a.podPsSorted[j].Name }
type podPsSortedStatus struct{ podPsSorted }
func (a podPsSortedStatus) Less(i, j int) bool {
return a.podPsSorted[i].Status < a.podPsSorted[j].Status
}
var (
podPsFlags = []cli.Flag{
cli.BoolFlag{
Name: "ctr-names",
Usage: "Display the container names",
},
cli.BoolFlag{
Name: "ctr-ids",
Usage: "Display the container UUIDs. If no-trunc is not set they will be truncated",
},
cli.BoolFlag{
Name: "ctr-status",
Usage: "Display the container status",
},
cli.StringFlag{
Name: "filter, f",
Usage: "Filter output based on conditions given",
},
cli.StringFlag{
Name: "format",
Usage: "Pretty-print pods to JSON or using a Go template",
},
cli.BoolFlag{
Name: "latest, l",
Usage: "Show the latest pod created",
},
cli.BoolFlag{
Name: "namespace, ns",
Usage: "Display namespace information of the pod",
},
cli.BoolFlag{
Name: "no-trunc",
Usage: "Do not truncate pod and container IDs",
},
cli.BoolFlag{
Name: "quiet, q",
Usage: "Print the numeric IDs of the pods only",
},
cli.StringFlag{
Name: "sort",
Usage: "Sort output by created, id, name, or number",
Value: "created",
},
}
podPsDescription = "List all pods on system including their names, ids and current state."
podPsCommand = cli.Command{
Name: "ps",
Aliases: []string{"ls", "list"},
Usage: "List pods",
Description: podPsDescription,
Flags: podPsFlags,
Action: podPsCmd,
UseShortOptionHandling: true,
}
)
func podPsCmd(c *cli.Context) error {
if err := validateFlags(c, podPsFlags); err != nil {
return err
}
if err := podPsCheckFlagsPassed(c); err != nil {
return errors.Wrapf(err, "error with flags passed")
}
runtime, err := libpodruntime.GetRuntime(c)
if err != nil {
return errors.Wrapf(err, "error creating libpod runtime")
}
defer runtime.Shutdown(false)
if len(c.Args()) > 0 {
return errors.Errorf("too many arguments, ps takes no arguments")
}
opts := podPsOptions{
NoTrunc: c.Bool("no-trunc"),
Quiet: c.Bool("quiet"),
Sort: c.String("sort"),
IdsOfContainers: c.Bool("ctr-ids"),
NamesOfContainers: c.Bool("ctr-names"),
StatusOfContainers: c.Bool("ctr-status"),
}
opts.Format = genPodPsFormat(c)
var filterFuncs []libpod.PodFilter
if c.String("filter") != "" {
filters := strings.Split(c.String("filter"), ",")
for _, f := range filters {
filterSplit := strings.Split(f, "=")
if len(filterSplit) < 2 {
return errors.Errorf("filter input must be in the form of filter=value: %s is invalid", f)
}
generatedFunc, err := generatePodFilterFuncs(filterSplit[0], filterSplit[1], runtime)
if err != nil {
return errors.Wrapf(err, "invalid filter")
}
filterFuncs = append(filterFuncs, generatedFunc)
}
}
var pods []*libpod.Pod
if c.IsSet("latest") {
pod, err := runtime.GetLatestPod()
if err != nil {
return err
}
pods = append(pods, pod)
} else {
pods, err = runtime.GetAllPods()
if err != nil {
return err
}
}
podsFiltered := make([]*libpod.Pod, 0, len(pods))
for _, pod := range pods {
include := true
for _, filter := range filterFuncs {
include = include && filter(pod)
}
if include {
podsFiltered = append(podsFiltered, pod)
}
}
return generatePodPsOutput(podsFiltered, opts, runtime)
}
// podPsCheckFlagsPassed checks if mutually exclusive flags are passed together
func podPsCheckFlagsPassed(c *cli.Context) error {
// quiet, and format with Go template are mutually exclusive
flags := 0
if c.Bool("quiet") {
flags++
}
if c.IsSet("format") && c.String("format") != formats.JSONString {
flags++
}
if flags > 1 {
return errors.Errorf("quiet and format with Go template are mutually exclusive")
}
return nil
}
func generatePodFilterFuncs(filter, filterValue string, runtime *libpod.Runtime) (func(pod *libpod.Pod) bool, error) {
switch filter {
case "ctr-ids":
return func(p *libpod.Pod) bool {
ctrIds, err := p.AllContainersByID()
if err != nil {
return false
}
return util.StringInSlice(filterValue, ctrIds)
}, nil
case "ctr-names":
return func(p *libpod.Pod) bool {
ctrs, err := p.AllContainers()
if err != nil {
return false
}
for _, ctr := range ctrs {
if filterValue == ctr.Name() {
return true
}
}
return false
}, nil
case "ctr-number":
return func(p *libpod.Pod) bool {
ctrIds, err := p.AllContainersByID()
if err != nil {
return false
}
fVint, err2 := strconv.Atoi(filterValue)
if err2 != nil {
return false
}
return len(ctrIds) == fVint
}, nil
case "ctr-status":
if !util.StringInSlice(filterValue, []string{"created", "restarting", "running", "paused", "exited", "unknown"}) {
return nil, errors.Errorf("%s is not a valid status", filterValue)
}
return func(p *libpod.Pod) bool {
ctr_statuses, err := p.Status()
if err != nil {
return false
}
for _, ctr_status := range ctr_statuses {
state := ctr_status.String()
if ctr_status == libpod.ContainerStateConfigured {
state = "created"
}
if state == filterValue {
return true
}
}
return false
}, nil
case "id":
return func(p *libpod.Pod) bool {
return strings.Contains(p.ID(), filterValue)
}, nil
case "name":
return func(p *libpod.Pod) bool {
return strings.Contains(p.Name(), filterValue)
}, nil
case "status":
if !util.StringInSlice(filterValue, []string{"stopped", "running", "paused", "exited", "dead", "created"}) {
return nil, errors.Errorf("%s is not a valid pod status", filterValue)
}
return func(p *libpod.Pod) bool {
status, err := shared.GetPodStatus(p)
if err != nil {
return false
}
if strings.ToLower(status) == filterValue {
return true
}
return false
}, nil
}
return nil, errors.Errorf("%s is an invalid filter", filter)
}
// generate the template based on conditions given
func genPodPsFormat(c *cli.Context) string {
format := ""
if c.String("format") != "" {
// "\t" from the command line is not being recognized as a tab
// replacing the string "\t" to a tab character if the user passes in "\t"
format = strings.Replace(c.String("format"), `\t`, "\t", -1)
} else if c.Bool("quiet") {
format = formats.IDString
} else {
format = "table {{.ID}}\t{{.Name}}\t{{.Status}}\t{{.Created}}"
if c.Bool("namespace") {
format += "\t{{.Cgroup}}\t{{.SharedNamespaces}}"
}
if c.Bool("ctr-names") || c.Bool("ctr-ids") || c.Bool("ctr-status") {
format += "\t{{.ContainerInfo}}"
} else {
format += "\t{{.NumberOfContainers}}"
}
format += "\t{{.InfraContainerID}}"
}
return format
}
func podPsToGeneric(templParams []podPsTemplateParams, JSONParams []podPsJSONParams) (genericParams []interface{}) {
if len(templParams) > 0 {
for _, v := range templParams {
genericParams = append(genericParams, interface{}(v))
}
return
}
for _, v := range JSONParams {
genericParams = append(genericParams, interface{}(v))
}
return
}
// generate the accurate header based on template given
func (p *podPsTemplateParams) podHeaderMap() map[string]string {
v := reflect.Indirect(reflect.ValueOf(p))
values := make(map[string]string)
for i := 0; i < v.NumField(); i++ {
key := v.Type().Field(i).Name
value := key
if value == "ID" {
value = "Pod" + value
}
values[key] = strings.ToUpper(splitCamelCase(value))
}
return values
}
func sortPodPsOutput(sortBy string, psOutput podPsSorted) (podPsSorted, error) {
switch sortBy {
case "created":
sort.Sort(podPsSortedCreated{psOutput})
case "id":
sort.Sort(podPsSortedId{psOutput})
case "name":
sort.Sort(podPsSortedName{psOutput})
case "number":
sort.Sort(podPsSortedNumber{psOutput})
case "status":
sort.Sort(podPsSortedStatus{psOutput})
default:
return nil, errors.Errorf("invalid option for --sort, options are: id, names, or number")
}
return psOutput, nil
}
// getPodTemplateOutput returns the modified container information
func getPodTemplateOutput(psParams []podPsJSONParams, opts podPsOptions) ([]podPsTemplateParams, error) {
var (
psOutput []podPsTemplateParams
)
for _, psParam := range psParams {
podID := psParam.ID
infraID := psParam.InfraContainerID
var ctrStr string
truncated := ""
if !opts.NoTrunc {
podID = shortID(podID)
if len(psParam.CtrsInfo) > NUM_CTR_INFO {
psParam.CtrsInfo = psParam.CtrsInfo[:NUM_CTR_INFO]
truncated = "..."
}
infraID = shortID(infraID)
}
for _, ctrInfo := range psParam.CtrsInfo {
ctrStr += "[ "
if opts.IdsOfContainers {
if opts.NoTrunc {
ctrStr += ctrInfo.Id
} else {
ctrStr += shortID(ctrInfo.Id)
}
}
if opts.NamesOfContainers {
ctrStr += ctrInfo.Name + " "
}
if opts.StatusOfContainers {
ctrStr += ctrInfo.Status + " "
}
ctrStr += "] "
}
ctrStr += truncated
params := podPsTemplateParams{
Created: units.HumanDuration(time.Since(psParam.CreatedAt)) + " ago",
ID: podID,
Name: psParam.Name,
Status: psParam.Status,
NumberOfContainers: psParam.NumberOfContainers,
Cgroup: psParam.Cgroup,
ContainerInfo: ctrStr,
InfraContainerID: infraID,
SharedNamespaces: strings.Join(psParam.SharedNamespaces, ","),
}
psOutput = append(psOutput, params)
}
return psOutput, nil
}
func getSharedNamespaces(pod *libpod.Pod) []string {
var shared []string
if pod.SharesPID() {
shared = append(shared, "pid")
}
if pod.SharesNet() {
shared = append(shared, "net")
}
if pod.SharesMNT() {
shared = append(shared, "mnt")
}
if pod.SharesIPC() {
shared = append(shared, "ipc")
}
if pod.SharesUser() {
shared = append(shared, "user")
}
if pod.SharesCgroup() {
shared = append(shared, "cgroup")
}
if pod.SharesUTS() {
shared = append(shared, "uts")
}
return shared
}
// getAndSortPodJSONOutput returns the container info in its raw, sorted form
func getAndSortPodJSONParams(pods []*libpod.Pod, opts podPsOptions, runtime *libpod.Runtime) ([]podPsJSONParams, error) {
var (
psOutput []podPsJSONParams
)
for _, pod := range pods {
ctrs, err := pod.AllContainers()
ctrsInfo := make([]podPsCtrInfo, 0)
if err != nil {
return nil, err
}
ctrNum := len(ctrs)
status, err := shared.GetPodStatus(pod)
if err != nil {
return nil, err
}
infraContainerID, err := pod.InfraContainerID()
if err != nil {
return nil, err
}
for _, ctr := range ctrs {
batchInfo, err := shared.BatchContainerOp(ctr, bc_opts)
if err != nil {
return nil, err
}
var status string
switch batchInfo.ConState {
case libpod.ContainerStateStopped:
status = EXITED
case libpod.ContainerStateRunning:
status = RUNNING
case libpod.ContainerStatePaused:
status = PAUSED
case libpod.ContainerStateCreated, libpod.ContainerStateConfigured:
status = CREATED
default:
status = ERROR
}
ctrsInfo = append(ctrsInfo, podPsCtrInfo{
Name: batchInfo.ConConfig.Name,
Id: ctr.ID(),
Status: status,
})
}
params := podPsJSONParams{
CreatedAt: pod.CreatedTime(),
ID: pod.ID(),
Name: pod.Name(),
Status: status,
Cgroup: pod.CgroupParent(),
NumberOfContainers: ctrNum,
CtrsInfo: ctrsInfo,
SharedNamespaces: getSharedNamespaces(pod),
InfraContainerID: infraContainerID,
}
psOutput = append(psOutput, params)
}
return sortPodPsOutput(opts.Sort, psOutput)
}
func generatePodPsOutput(pods []*libpod.Pod, opts podPsOptions, runtime *libpod.Runtime) error {
if len(pods) == 0 && opts.Format != formats.JSONString {
return nil
}
psOutput, err := getAndSortPodJSONParams(pods, opts, runtime)
if err != nil {
return err
}
var out formats.Writer
switch opts.Format {
case formats.JSONString:
if err != nil {
return errors.Wrapf(err, "unable to create JSON for output")
}
out = formats.JSONStructArray{Output: podPsToGeneric([]podPsTemplateParams{}, psOutput)}
default:
psOutput, err := getPodTemplateOutput(psOutput, opts)
if err != nil {
return errors.Wrapf(err, "unable to create output")
}
out = formats.StdoutTemplateArray{Output: podPsToGeneric(psOutput, []podPsJSONParams{}), Template: opts.Format, Fields: psOutput[0].podHeaderMap()}
}
return formats.Writer(out).Out()
}
|