diff options
author | Jhon Honce <jhonce@redhat.com> | 2018-06-27 21:37:42 -0700 |
---|---|---|
committer | Jhon Honce <jhonce@redhat.com> | 2018-07-13 11:29:28 -0700 |
commit | 44b523c946c88e540b50d7ba59f441b5f8e0bad0 (patch) | |
tree | 744c09508b139c1aca3d7fc995fad7ff354e3667 /contrib/python/cmd/lib/actions/ps_action.py | |
parent | 14a6d51a8432fc0c3324fec02e8729d3032f2af2 (diff) | |
download | podman-44b523c946c88e540b50d7ba59f441b5f8e0bad0.tar.gz podman-44b523c946c88e540b50d7ba59f441b5f8e0bad0.tar.bz2 podman-44b523c946c88e540b50d7ba59f441b5f8e0bad0.zip |
remote python client for podman
* Use podman library for access
* Verbose error checking
* Planned windows and macosx ports
Signed-off-by: Jhon Honce <jhonce@redhat.com>
Diffstat (limited to 'contrib/python/cmd/lib/actions/ps_action.py')
-rw-r--r-- | contrib/python/cmd/lib/actions/ps_action.py | 76 |
1 files changed, 76 insertions, 0 deletions
diff --git a/contrib/python/cmd/lib/actions/ps_action.py b/contrib/python/cmd/lib/actions/ps_action.py new file mode 100644 index 000000000..9fc3a155b --- /dev/null +++ b/contrib/python/cmd/lib/actions/ps_action.py @@ -0,0 +1,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(klass=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) |