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
|
"""Remote client commands dealing with containers."""
import operator
from collections import OrderedDict
import humanize
import podman
from .. import AbstractActionBase, Report, ReportColumn
class Ps(AbstractActionBase):
"""Class for Container manipulation."""
@classmethod
def subparser(cls, parent):
"""Add Images command to parent parser."""
parser = parent.add_parser('ps', help='list containers')
super().subparser(parser)
parser.add_argument(
'--sort',
choices=[
'createdat', 'id', 'image', 'names', 'runningfor', 'size',
'status'
],
default='createdat',
type=str.lower,
help=('Change sort ordered of displayed containers.'
' (default: %(default)s)'))
parser.set_defaults(class_=cls, method='list')
def __init__(self, args):
"""Construct Ps class."""
super().__init__(args)
self.columns = OrderedDict({
'id':
ReportColumn('id', 'CONTAINER ID', 14),
'image':
ReportColumn('image', 'IMAGE', 30),
'command':
ReportColumn('column', 'COMMAND', 20),
'createdat':
ReportColumn('createdat', 'CREATED', 12),
'status':
ReportColumn('status', 'STATUS', 10),
'ports':
ReportColumn('ports', 'PORTS', 28),
'names':
ReportColumn('names', 'NAMES', 18)
})
def list(self):
"""List containers."""
# TODO: Verify sorting on dates and size
ctnrs = sorted(
self.client.containers.list(),
key=operator.attrgetter(self._args.sort))
if len(ctnrs) == 0:
return 0
rows = list()
for ctnr in ctnrs:
fields = dict(ctnr)
fields.update({
'command':
' '.join(ctnr.command),
'createdat':
humanize.naturaldate(podman.datetime_parse(ctnr.createdat)),
})
rows.append(fields)
with Report(self.columns, heading=self._args.heading) as report:
report.layout(
rows, self.columns.keys(), truncate=self._args.truncate)
for row in rows:
report.row(**row)
|