def list(self): """List containers.""" if self._args.all: ictnrs = self.client.containers.list() else: ictnrs = filter( lambda c: podman.FoldedString(c['status']) == 'running', self.client.containers.list()) # TODO: Verify sorting on dates and size ctnrs = sorted(ictnrs, key=operator.attrgetter(self._args.sort)) if not ctnrs: return 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)
def processes(self): """List pods.""" pods = sorted(self.client.pods.list(), key=operator.attrgetter(self._args.sort)) if not pods: return rows = list() for pod in pods: fields = dict(pod) if self._args.ctr_ids \ or self._args.ctr_names \ or self._args.ctr_status: keys = ('id', 'name', 'status', 'info') info = [] for ctnr in pod.containersinfo: ctnr_info = [] if self._args.ctr_ids: ctnr_info.append(ctnr['id']) if self._args.ctr_names: ctnr_info.append(ctnr['name']) if self._args.ctr_status: ctnr_info.append(ctnr['status']) info.append("[ {} ]".format(" ".join(ctnr_info))) fields.update({'info': " ".join(info)}) else: keys = ('id', 'name', 'status', 'numberofcontainers') rows.append(fields) with Report(self.columns, heading=self._args.heading) as report: report.layout(rows, keys, truncate=self._args.truncate) for row in rows: report.row(**row)
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 not ctnrs: return rows = list() for ctnr in ctnrs: fields = dict(ctnr) fields.update({ 'command': ' '.join(ctnr.command), 'createdat': humanize.naturaldate(podman.datetime_parse(ctnr.createdat)), }) if self._args.truncate: fields.update({'image': ctnr.image[-30:]}) 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)
def history(self): """Report image history.""" rows = list() for ident in self._args.image: for details in self.client.images.get(ident).history(): fields = dict(details._asdict()) if self._args.human: fields.update({ 'size': humanize.naturalsize(details.size), 'created': humanize.naturaldate( podman.datetime_parse(details.created)), }) del fields['tags'] rows.append(fields) if self._args.quiet: for row in rows: ident = row['id'][:12] if self._args.truncate else row['id'] print(ident) elif self._args.format == 'json': print(json.dumps(rows, indent=2), flush=True) else: 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)
def mount(self): """Retrieve mounts from containers.""" try: ctnrs = [] if not self._args.containers: ctnrs = self.client.containers.list() else: for ident in self._args.containers: try: ctnrs.append(self.client.containers.get(ident)) except podman.ContainerNotFound as e: sys.stdout.flush() print('Container "{}" not found'.format(e.name), file=sys.stderr, flush=True) except podman.ErrorOccurred as e: sys.stdout.flush() print('{}'.format(e.reason).capitalize(), file=sys.stderr, flush=True) return 1 if not ctnrs: print('Unable to find any containers.', file=sys.stderr, flush=True) return 1 rows = list() for ctnr in ctnrs: details = ctnr.inspect() rows.append({ 'id': ctnr.id, 'destination': details.graphdriver['data']['mergeddir'] }) 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)
def search(self): """Search registries for image.""" try: rows = list() for entry in self.client.images.search( self._args.term[0], limit=self._args.limit): if self._args.filter == 'is-official': if self._args.filter_value != entry.is_official: continue elif self._args.filter == 'is-automated': if self._args.filter_value != entry.is_automated: continue elif self._args.filter == 'stars': if self._args.filter_value > entry.star_count: continue fields = dict(entry._asdict()) status = '[OK]' if entry.is_official else '' fields['is_official'] = status status = '[OK]' if entry.is_automated else '' fields['is_automated'] = status if self._args.truncate: fields.update({'name': entry.name[-44:]}) 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) except podman.ErrorOccurred as e: sys.stdout.flush() print( '{}'.format(e.reason).capitalize(), file=sys.stderr, flush=True)
def list(self): """List images.""" images = sorted(self.client.images.list(), key=operator.attrgetter(self._args.sort)) if not images: return rows = list() for image in images: fields = dict(image) fields.update({ 'created': humanize.naturaldate(podman.datetime_parse(image.created)), 'size': humanize.naturalsize(int(image.size)), 'repoDigests': ' '.join(image.repoDigests), }) for r in image.repoTags: name, tag = r.split(':', 1) fields.update({ 'name': name, 'tag': tag, }) rows.append(fields) if not self._args.digests: del self.columns['repoDigests'] 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)