From 74ccd9ce5f29a1df4ffe70b4d8bd00c29d5d9d15 Mon Sep 17 00:00:00 2001 From: Jhon Honce Date: Thu, 12 Jul 2018 19:26:14 -0700 Subject: Update python directories to better support setup.py Signed-off-by: Jhon Honce --- contrib/python/CHANGES.txt | 1 - contrib/python/LICENSE.txt | 13 -- contrib/python/MANIFEST.in | 2 - contrib/python/Makefile | 16 -- contrib/python/README.md | 43 ---- contrib/python/cmd/lib/__init__.py | 5 - contrib/python/cmd/lib/action_base.py | 80 ------- contrib/python/cmd/lib/actions/__init__.py | 7 - contrib/python/cmd/lib/actions/images_action.py | 88 -------- contrib/python/cmd/lib/actions/ps_action.py | 76 ------- contrib/python/cmd/lib/actions/rm_action.py | 51 ----- contrib/python/cmd/lib/actions/rmi_action.py | 50 ----- contrib/python/cmd/lib/future_abstract.py | 29 --- contrib/python/cmd/lib/report.py | 67 ------ contrib/python/cmd/pydman.py | 248 --------------------- contrib/python/examples/eg_attach.py | 18 -- contrib/python/examples/eg_containers_by_image.py | 16 -- contrib/python/examples/eg_image_list.py | 10 - contrib/python/examples/eg_inspect_fedora.py | 16 -- contrib/python/examples/eg_latest_containers.py | 19 -- contrib/python/examples/eg_new_image.py | 32 --- contrib/python/examples/run_example.sh | 43 ---- contrib/python/podman/CHANGES.txt | 1 + contrib/python/podman/LICENSE.txt | 13 ++ contrib/python/podman/MANIFEST.in | 2 + contrib/python/podman/Makefile | 21 ++ contrib/python/podman/README.md | 43 ++++ contrib/python/podman/__init__.py | 22 -- contrib/python/podman/client.py | 206 ----------------- contrib/python/podman/examples/eg_attach.py | 18 ++ .../podman/examples/eg_containers_by_image.py | 16 ++ contrib/python/podman/examples/eg_image_list.py | 10 + .../python/podman/examples/eg_inspect_fedora.py | 16 ++ .../python/podman/examples/eg_latest_containers.py | 19 ++ contrib/python/podman/examples/eg_new_image.py | 32 +++ contrib/python/podman/examples/run_example.sh | 43 ++++ contrib/python/podman/libs/__init__.py | 54 ----- contrib/python/podman/libs/_containers_attach.py | 75 ------- contrib/python/podman/libs/_containers_start.py | 82 ------- contrib/python/podman/libs/containers.py | 245 -------------------- contrib/python/podman/libs/errors.py | 65 ------ contrib/python/podman/libs/images.py | 172 -------------- contrib/python/podman/libs/system.py | 40 ---- contrib/python/podman/libs/tunnel.py | 138 ------------ contrib/python/podman/podman/__init__.py | 22 ++ contrib/python/podman/podman/client.py | 202 +++++++++++++++++ contrib/python/podman/podman/libs/__init__.py | 54 +++++ .../podman/podman/libs/_containers_attach.py | 75 +++++++ .../python/podman/podman/libs/_containers_start.py | 82 +++++++ contrib/python/podman/podman/libs/containers.py | 245 ++++++++++++++++++++ contrib/python/podman/podman/libs/errors.py | 65 ++++++ contrib/python/podman/podman/libs/images.py | 172 ++++++++++++++ contrib/python/podman/podman/libs/system.py | 40 ++++ contrib/python/podman/podman/libs/tunnel.py | 139 ++++++++++++ contrib/python/podman/requirements.txt | 3 + contrib/python/podman/setup.py | 38 ++++ contrib/python/podman/test/__init__.py | 0 contrib/python/podman/test/podman_testcase.py | 107 +++++++++ contrib/python/podman/test/test_client.py | 35 +++ contrib/python/podman/test/test_containers.py | 234 +++++++++++++++++++ contrib/python/podman/test/test_images.py | 172 ++++++++++++++ contrib/python/podman/test/test_libs.py | 53 +++++ contrib/python/podman/test/test_runner.sh | 141 ++++++++++++ contrib/python/podman/test/test_system.py | 62 ++++++ contrib/python/podman/test/test_tunnel.py | 79 +++++++ contrib/python/pypodman/MANIFEST.in | 1 + contrib/python/pypodman/Makefile | 21 ++ contrib/python/pypodman/README.md | 32 +++ contrib/python/pypodman/docs/pypodman.1.md | 82 +++++++ contrib/python/pypodman/lib/__init__.py | 11 + contrib/python/pypodman/lib/action_base.py | 84 +++++++ contrib/python/pypodman/lib/actions/__init__.py | 7 + .../python/pypodman/lib/actions/images_action.py | 88 ++++++++ contrib/python/pypodman/lib/actions/ps_action.py | 76 +++++++ contrib/python/pypodman/lib/actions/rm_action.py | 51 +++++ contrib/python/pypodman/lib/actions/rmi_action.py | 50 +++++ contrib/python/pypodman/lib/config.py | 212 ++++++++++++++++++ contrib/python/pypodman/lib/future_abstract.py | 29 +++ contrib/python/pypodman/lib/pypodman.py | 76 +++++++ contrib/python/pypodman/lib/report.py | 67 ++++++ contrib/python/pypodman/requirements.txt | 4 + contrib/python/pypodman/setup.py | 44 ++++ contrib/python/pypodman/test/test_report.py | 23 ++ contrib/python/requirements.txt | 3 - contrib/python/setup.py | 38 ---- contrib/python/test/__init__.py | 0 contrib/python/test/podman_testcase.py | 107 --------- contrib/python/test/test_client.py | 37 --- contrib/python/test/test_containers.py | 234 ------------------- contrib/python/test/test_images.py | 172 -------------- contrib/python/test/test_libs.py | 53 ----- contrib/python/test/test_runner.sh | 141 ------------ contrib/python/test/test_system.py | 62 ------ contrib/python/test/test_tunnel.py | 79 ------- contrib/spec/podman.spec.in | 30 ++- 95 files changed, 3240 insertions(+), 2957 deletions(-) delete mode 100644 contrib/python/CHANGES.txt delete mode 100644 contrib/python/LICENSE.txt delete mode 100644 contrib/python/MANIFEST.in delete mode 100644 contrib/python/Makefile delete mode 100644 contrib/python/README.md delete mode 100644 contrib/python/cmd/lib/__init__.py delete mode 100644 contrib/python/cmd/lib/action_base.py delete mode 100644 contrib/python/cmd/lib/actions/__init__.py delete mode 100644 contrib/python/cmd/lib/actions/images_action.py delete mode 100644 contrib/python/cmd/lib/actions/ps_action.py delete mode 100644 contrib/python/cmd/lib/actions/rm_action.py delete mode 100644 contrib/python/cmd/lib/actions/rmi_action.py delete mode 100644 contrib/python/cmd/lib/future_abstract.py delete mode 100644 contrib/python/cmd/lib/report.py delete mode 100755 contrib/python/cmd/pydman.py delete mode 100644 contrib/python/examples/eg_attach.py delete mode 100644 contrib/python/examples/eg_containers_by_image.py delete mode 100644 contrib/python/examples/eg_image_list.py delete mode 100644 contrib/python/examples/eg_inspect_fedora.py delete mode 100644 contrib/python/examples/eg_latest_containers.py delete mode 100644 contrib/python/examples/eg_new_image.py delete mode 100755 contrib/python/examples/run_example.sh create mode 100644 contrib/python/podman/CHANGES.txt create mode 100644 contrib/python/podman/LICENSE.txt create mode 100644 contrib/python/podman/MANIFEST.in create mode 100644 contrib/python/podman/Makefile create mode 100644 contrib/python/podman/README.md delete mode 100644 contrib/python/podman/__init__.py delete mode 100644 contrib/python/podman/client.py create mode 100644 contrib/python/podman/examples/eg_attach.py create mode 100644 contrib/python/podman/examples/eg_containers_by_image.py create mode 100644 contrib/python/podman/examples/eg_image_list.py create mode 100644 contrib/python/podman/examples/eg_inspect_fedora.py create mode 100644 contrib/python/podman/examples/eg_latest_containers.py create mode 100644 contrib/python/podman/examples/eg_new_image.py create mode 100755 contrib/python/podman/examples/run_example.sh delete mode 100644 contrib/python/podman/libs/__init__.py delete mode 100644 contrib/python/podman/libs/_containers_attach.py delete mode 100644 contrib/python/podman/libs/_containers_start.py delete mode 100644 contrib/python/podman/libs/containers.py delete mode 100644 contrib/python/podman/libs/errors.py delete mode 100644 contrib/python/podman/libs/images.py delete mode 100644 contrib/python/podman/libs/system.py delete mode 100644 contrib/python/podman/libs/tunnel.py create mode 100644 contrib/python/podman/podman/__init__.py create mode 100644 contrib/python/podman/podman/client.py create mode 100644 contrib/python/podman/podman/libs/__init__.py create mode 100644 contrib/python/podman/podman/libs/_containers_attach.py create mode 100644 contrib/python/podman/podman/libs/_containers_start.py create mode 100644 contrib/python/podman/podman/libs/containers.py create mode 100644 contrib/python/podman/podman/libs/errors.py create mode 100644 contrib/python/podman/podman/libs/images.py create mode 100644 contrib/python/podman/podman/libs/system.py create mode 100644 contrib/python/podman/podman/libs/tunnel.py create mode 100644 contrib/python/podman/requirements.txt create mode 100644 contrib/python/podman/setup.py create mode 100644 contrib/python/podman/test/__init__.py create mode 100644 contrib/python/podman/test/podman_testcase.py create mode 100644 contrib/python/podman/test/test_client.py create mode 100644 contrib/python/podman/test/test_containers.py create mode 100644 contrib/python/podman/test/test_images.py create mode 100644 contrib/python/podman/test/test_libs.py create mode 100755 contrib/python/podman/test/test_runner.sh create mode 100644 contrib/python/podman/test/test_system.py create mode 100644 contrib/python/podman/test/test_tunnel.py create mode 100644 contrib/python/pypodman/MANIFEST.in create mode 100644 contrib/python/pypodman/Makefile create mode 100644 contrib/python/pypodman/README.md create mode 100644 contrib/python/pypodman/docs/pypodman.1.md create mode 100644 contrib/python/pypodman/lib/__init__.py create mode 100644 contrib/python/pypodman/lib/action_base.py create mode 100644 contrib/python/pypodman/lib/actions/__init__.py create mode 100644 contrib/python/pypodman/lib/actions/images_action.py create mode 100644 contrib/python/pypodman/lib/actions/ps_action.py create mode 100644 contrib/python/pypodman/lib/actions/rm_action.py create mode 100644 contrib/python/pypodman/lib/actions/rmi_action.py create mode 100644 contrib/python/pypodman/lib/config.py create mode 100644 contrib/python/pypodman/lib/future_abstract.py create mode 100755 contrib/python/pypodman/lib/pypodman.py create mode 100644 contrib/python/pypodman/lib/report.py create mode 100644 contrib/python/pypodman/requirements.txt create mode 100644 contrib/python/pypodman/setup.py create mode 100644 contrib/python/pypodman/test/test_report.py delete mode 100644 contrib/python/requirements.txt delete mode 100644 contrib/python/setup.py delete mode 100644 contrib/python/test/__init__.py delete mode 100644 contrib/python/test/podman_testcase.py delete mode 100644 contrib/python/test/test_client.py delete mode 100644 contrib/python/test/test_containers.py delete mode 100644 contrib/python/test/test_images.py delete mode 100644 contrib/python/test/test_libs.py delete mode 100755 contrib/python/test/test_runner.sh delete mode 100644 contrib/python/test/test_system.py delete mode 100644 contrib/python/test/test_tunnel.py (limited to 'contrib') diff --git a/contrib/python/CHANGES.txt b/contrib/python/CHANGES.txt deleted file mode 100644 index 2bac1c867..000000000 --- a/contrib/python/CHANGES.txt +++ /dev/null @@ -1 +0,0 @@ -v0.1.0, 2018-05-11 -- Initial release. diff --git a/contrib/python/LICENSE.txt b/contrib/python/LICENSE.txt deleted file mode 100644 index decfce56d..000000000 --- a/contrib/python/LICENSE.txt +++ /dev/null @@ -1,13 +0,0 @@ -Copyright 2018 Red Hat, Inc - -Licensed under the Apache License, Version 2.0 (the "License"); -you may not use this file except in compliance with the License. -You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - -Unless required by applicable law or agreed to in writing, software -distributed under the License is distributed on an "AS IS" BASIS, -WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -See the License for the specific language governing permissions and -limitations under the License. diff --git a/contrib/python/MANIFEST.in b/contrib/python/MANIFEST.in deleted file mode 100644 index 72e638cb9..000000000 --- a/contrib/python/MANIFEST.in +++ /dev/null @@ -1,2 +0,0 @@ -prune test/ -include README.md diff --git a/contrib/python/Makefile b/contrib/python/Makefile deleted file mode 100644 index 6cb63c403..000000000 --- a/contrib/python/Makefile +++ /dev/null @@ -1,16 +0,0 @@ -PYTHON ?= /usr/bin/python3 - -.PHONY: python-podman -python-podman: - $(PYTHON) setup.py bdist - -.PHONY: integration -integration: - test/test_runner.sh - -.PHONY: clean -clean: - $(PYTHON) setup.py clean --all - rm -rf podman.egg-info dist - find . -depth -name __pycache__ -exec rm -rf {} \; - find . -depth -name \*.pyc -exec rm -f {} \; diff --git a/contrib/python/README.md b/contrib/python/README.md deleted file mode 100644 index dcf40a1a9..000000000 --- a/contrib/python/README.md +++ /dev/null @@ -1,43 +0,0 @@ -# podman - pythonic library for working with varlink interface to Podman - -## Status: Active Development - -See [libpod](https://github.com/projectatomic/libpod) - -## Releases - -To build the podman egg: - -```sh -cd ~/libpod/contrib/pypodman -python3 setup.py clean -a && python3 setup.py bdist -``` - -## Code snippets/examples: - -### Show images in storage - -```python -import podman - -with podman.Client() as client: - list(map(print, client.images.list())) -``` - -### Show containers created since midnight - -```python -from datetime import datetime, time, timezone - -import podman - -midnight = datetime.combine(datetime.today(), time.min, tzinfo=timezone.utc) - -with podman.Client() as client: - for c in client.containers.list(): - created_at = podman.datetime_parse(c.createdat) - - if created_at > midnight: - print('Container {}: image: {} created at: {}'.format( - c.id[:12], c.image[:32], podman.datetime_format(created_at))) -``` diff --git a/contrib/python/cmd/lib/__init__.py b/contrib/python/cmd/lib/__init__.py deleted file mode 100644 index db0f640b1..000000000 --- a/contrib/python/cmd/lib/__init__.py +++ /dev/null @@ -1,5 +0,0 @@ -"""Remote podman client support library.""" -from .action_base import AbstractActionBase -from .report import Report, ReportColumn - -__all__ = ['AbstractActionBase', 'Report', 'ReportColumn'] diff --git a/contrib/python/cmd/lib/action_base.py b/contrib/python/cmd/lib/action_base.py deleted file mode 100644 index bafddea03..000000000 --- a/contrib/python/cmd/lib/action_base.py +++ /dev/null @@ -1,80 +0,0 @@ -"""Base class for all actions of remote client.""" -import abc -from functools import lru_cache - -import podman - - -class AbstractActionBase(abc.ABC): - """Base class for all actions of remote client.""" - - @classmethod - @abc.abstractmethod - def subparser(cls, parser): - """Define parser for this action. Subclasses must implement. - - API: - Use set_defaults() to set attributes "klass" and "method". These will - be invoked as klass(parsed_args).method() - """ - parser.add_argument( - '--all', - action='store_true', - help=('list all items.' - ' (default: no-op, included for compatibility.)')) - parser.add_argument( - '--no-trunc', - '--notruncate', - action='store_false', - dest='truncate', - default=True, - help='Display extended information. (default: False)') - parser.add_argument( - '--noheading', - action='store_false', - dest='heading', - default=True, - help=('Omit the table headings from the output.' - ' (default: False)')) - parser.add_argument( - '--quiet', - action='store_true', - help='List only the IDs. (default: %(default)s)') - - def __init__(self, args): - """Construct class.""" - self._args = args - - @property - def remote_uri(self): - """URI for remote side of connection.""" - return self._args.remote_uri - - @property - def local_uri(self): - """URI for local side of connection.""" - return self._args.local_uri - - @property - def identity_file(self): - """Key for authenication.""" - return self._args.identity_file - - @property - @lru_cache(maxsize=1) - def client(self): - """Podman remote client for communicating.""" - return podman.Client( - uri=self.local_uri, - remote_uri=self.remote_uri, - identity_file=self.identity_file) - - def __repr__(self): - """Compute the “official” string representation of object.""" - return ("{}(local_uri='{}', remote_uri='{}'," - " identity_file='{}')").format( - self.__class__, - self.local_uri, - self.remote_uri, - self.identity_file, - ) diff --git a/contrib/python/cmd/lib/actions/__init__.py b/contrib/python/cmd/lib/actions/__init__.py deleted file mode 100644 index cdc58b6ab..000000000 --- a/contrib/python/cmd/lib/actions/__init__.py +++ /dev/null @@ -1,7 +0,0 @@ -"""Module to export all the podman subcommands.""" -from .images_action import Images -from .ps_action import Ps -from .rm_action import Rm -from .rmi_action import Rmi - -__all__ = ['Images', 'Ps', 'Rm', 'Rmi'] diff --git a/contrib/python/cmd/lib/actions/images_action.py b/contrib/python/cmd/lib/actions/images_action.py deleted file mode 100644 index 74c77edbb..000000000 --- a/contrib/python/cmd/lib/actions/images_action.py +++ /dev/null @@ -1,88 +0,0 @@ -"""Remote client commands dealing with images.""" -import operator -from collections import OrderedDict - -import humanize -import podman - -from .. import AbstractActionBase, Report, ReportColumn - - -class Images(AbstractActionBase): - """Class for Image manipulation.""" - - @classmethod - def subparser(cls, parent): - """Add Images commands to parent parser.""" - parser = parent.add_parser('images', help='list images') - super().subparser(parser) - parser.add_argument( - '--sort', - choices=['created', 'id', 'repository', 'size', 'tag'], - default='created', - type=str.lower, - help=('Change sort ordered of displayed images.' - ' (default: %(default)s)')) - - group = parser.add_mutually_exclusive_group() - group.add_argument( - '--digests', - action='store_true', - help='Include digests with images. (default: %(default)s)') - parser.set_defaults(klass=cls, method='list') - - def __init__(self, args): - """Construct Images class.""" - super().__init__(args) - - self.columns = OrderedDict({ - 'name': - ReportColumn('name', 'REPOSITORY', 40), - 'tag': - ReportColumn('tag', 'TAG', 10), - 'id': - ReportColumn('id', 'IMAGE ID', 12), - 'created': - ReportColumn('created', 'CREATED', 12), - 'size': - ReportColumn('size', 'SIZE', 8), - 'repoDigests': - ReportColumn('repoDigests', 'DIGESTS', 35), - }) - - def list(self): - """List images.""" - images = sorted( - self.client.images.list(), - key=operator.attrgetter(self._args.sort)) - if len(images) == 0: - return 0 - - 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) diff --git a/contrib/python/cmd/lib/actions/ps_action.py b/contrib/python/cmd/lib/actions/ps_action.py deleted file mode 100644 index 9fc3a155b..000000000 --- a/contrib/python/cmd/lib/actions/ps_action.py +++ /dev/null @@ -1,76 +0,0 @@ -"""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) diff --git a/contrib/python/cmd/lib/actions/rm_action.py b/contrib/python/cmd/lib/actions/rm_action.py deleted file mode 100644 index 7595fee6a..000000000 --- a/contrib/python/cmd/lib/actions/rm_action.py +++ /dev/null @@ -1,51 +0,0 @@ -"""Remote client command for deleting containers.""" -import sys - -import podman - -from .. import AbstractActionBase - - -class Rm(AbstractActionBase): - """Class for removing containers from storage.""" - - @classmethod - def subparser(cls, parent): - """Add Rm command to parent parser.""" - parser = parent.add_parser('rm', help='delete container(s)') - parser.add_argument( - '-f', - '--force', - action='store_true', - help=('force delete of running container(s).' - ' (default: %(default)s)')) - parser.add_argument( - 'targets', nargs='*', help='container id(s) to delete') - parser.set_defaults(klass=cls, method='remove') - - def __init__(self, args): - """Construct Rm class.""" - super().__init__(args) - if len(args.targets) < 1: - raise ValueError('You must supply at least one container id' - ' or name to be deleted.') - - def remove(self): - """Remove container(s).""" - for id in self._args.targets: - try: - ctnr = self.client.containers.get(id) - ctnr.remove(self._args.force) - print(id) - 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) diff --git a/contrib/python/cmd/lib/actions/rmi_action.py b/contrib/python/cmd/lib/actions/rmi_action.py deleted file mode 100644 index db59fe030..000000000 --- a/contrib/python/cmd/lib/actions/rmi_action.py +++ /dev/null @@ -1,50 +0,0 @@ -"""Remote client command for deleting images.""" -import sys - -import podman - -from .. import AbstractActionBase - - -class Rmi(AbstractActionBase): - """Clas for removing images from storage.""" - - @classmethod - def subparser(cls, parent): - """Add Rmi command to parent parser.""" - parser = parent.add_parser('rmi', help='delete image(s)') - parser.add_argument( - '-f', - '--force', - action='store_true', - help=('force delete of image(s) and associated containers.' - ' (default: %(default)s)')) - parser.add_argument('targets', nargs='*', help='image id(s) to delete') - parser.set_defaults(klass=cls, method='remove') - - def __init__(self, args): - """Construct Rmi class.""" - super().__init__(args) - if len(args.targets) < 1: - raise ValueError('You must supply at least one image id' - ' or name to be deleted.') - - def remove(self): - """Remove image(s).""" - for id in self._args.targets: - try: - img = self.client.images.get(id) - img.remove(self._args.force) - print(id) - except podman.ImageNotFound as e: - sys.stdout.flush() - print( - 'Image {} 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) diff --git a/contrib/python/cmd/lib/future_abstract.py b/contrib/python/cmd/lib/future_abstract.py deleted file mode 100644 index 75a1d42db..000000000 --- a/contrib/python/cmd/lib/future_abstract.py +++ /dev/null @@ -1,29 +0,0 @@ -"""Utilities for with-statement contexts. See PEP 343.""" - -import abc - -import _collections_abc - -try: - from contextlib import AbstractContextManager -except ImportError: - # Copied from python3.7 library as "backport" - class AbstractContextManager(abc.ABC): - """An abstract base class for context managers.""" - - def __enter__(self): - """Return `self` upon entering the runtime context.""" - return self - - @abc.abstractmethod - def __exit__(self, exc_type, exc_value, traceback): - """Raise any exception triggered within the runtime context.""" - return None - - @classmethod - def __subclasshook__(cls, C): - """Check whether subclass is considered a subclass of this ABC.""" - if cls is AbstractContextManager: - return _collections_abc._check_methods(C, "__enter__", - "__exit__") - return NotImplemented diff --git a/contrib/python/cmd/lib/report.py b/contrib/python/cmd/lib/report.py deleted file mode 100644 index 25fe2ae0d..000000000 --- a/contrib/python/cmd/lib/report.py +++ /dev/null @@ -1,67 +0,0 @@ -"""Report Manager.""" -import sys -from collections import namedtuple - -from .future_abstract import AbstractContextManager - - -class ReportColumn(namedtuple('ReportColumn', 'key display width default')): - """Hold attributes of output column.""" - - __slots__ = () - - def __new__(cls, key, display, width, default=None): - """Add defaults for attributes.""" - return super(ReportColumn, cls).__new__(cls, key, display, width, - default) - - -class Report(AbstractContextManager): - """Report Manager.""" - - def __init__(self, columns, heading=True, epilog=None, file=sys.stdout): - """Construct Report. - - columns is a mapping for named fields to column headings. - headers True prints headers on table. - epilog will be printed when the report context is closed. - """ - self._columns = columns - self._file = file - self._heading = heading - self.epilog = epilog - self._format = None - - def row(self, **fields): - """Print row for report.""" - if self._heading: - hdrs = {k: v.display for (k, v) in self._columns.items()} - print(self._format.format(**hdrs), flush=True, file=self._file) - self._heading = False - fields = {k: str(v) for k, v in fields.items()} - print(self._format.format(**fields)) - - def __exit__(self, exc_type, exc_value, traceback): - """Leave Report context and print epilog if provided.""" - if self.epilog: - print(self.epilog, flush=True, file=self._file) - - def layout(self, iterable, keys, truncate=True): - """Use data and headings build format for table to fit.""" - format = [] - - for key in keys: - value = max(map(lambda x: len(str(x.get(key, ''))), iterable)) - # print('key', key, 'value', value) - - if truncate: - row = self._columns.get( - key, ReportColumn(key, key.upper(), len(key))) - if value < row.width: - step = row.width if value == 0 else value - value = max(len(key), step) - elif value > row.width: - value = row.width if row.width != 0 else value - - format.append('{{{0}:{1}.{1}}}'.format(key, value)) - self._format = ' '.join(format) diff --git a/contrib/python/cmd/pydman.py b/contrib/python/cmd/pydman.py deleted file mode 100755 index 5008c706d..000000000 --- a/contrib/python/cmd/pydman.py +++ /dev/null @@ -1,248 +0,0 @@ -#!/usr/bin/env python3 -"""Remote podman client.""" - -import argparse -import curses -import getpass -import inspect -import logging -import os -import sys - -import pkg_resources - -import lib.actions -import pytoml - -assert lib.actions # silence pyflakes - -# TODO: setup.py and obtain __version__ from rpm.spec -try: - __version__ = pkg_resources.get_distribution('pydman').version -except Exception: - __version__ = '0.0.0' - - -class HelpFormatter(argparse.RawDescriptionHelpFormatter): - """Set help width to screen size.""" - - def __init__(self, *args, **kwargs): - """Construct HelpFormatter using screen width.""" - if 'width' not in kwargs: - kwargs['width'] = 80 - try: - height, width = curses.initscr().getmaxyx() - kwargs['width'] = width - finally: - curses.endwin() - super().__init__(*args, **kwargs) - - -class PodmanArgumentParser(argparse.ArgumentParser): - """Default remote podman configuration.""" - - def __init__(self, **kwargs): - """Construct the parser.""" - kwargs['add_help'] = True - kwargs['allow_abbrev'] = True - kwargs['description'] = __doc__ - kwargs['formatter_class'] = HelpFormatter - - super().__init__(**kwargs) - - def initialize_parser(self): - """Initialize parser without causing recursion meltdown.""" - self.add_argument( - '--version', - action='version', - version='%(prog)s v. ' + __version__) - self.add_argument( - '--log-level', - choices=['DEBUG', 'INFO', 'WARNING', 'ERROR', 'CRITICAL'], - default='INFO', - type=str.upper, - help='set logging level for events. (default: %(default)s)', - ) - self.add_argument( - '--run-dir', - help=('directory to place local socket bindings.' - ' (default: XDG_RUNTIME_DIR)')) - self.add_argument( - '--user', - help=('Authenicating user on remote host.' - ' (default: {})').format(getpass.getuser())) - self.add_argument( - '--host', help='name of remote host. (default: None)') - self.add_argument( - '--remote-socket-path', - help=('path of podman socket on remote host' - ' (default: /run/podman/io.projectatomic.podman)')) - self.add_argument( - '--identity-file', - help=('path to ssh identity file. (default: ~/.ssh/id_rsa)')) - self.add_argument( - '--config', - default='/etc/containers/podman_client.conf', - dest='config_file', - help='path of configuration file. (default: %(default)s)') - - actions_parser = self.add_subparsers( - dest='subparser_name', help='actions') - - for name, obj in inspect.getmembers( - sys.modules['lib.actions'], - lambda member: inspect.isclass(member)): - if hasattr(obj, 'subparser'): - try: - obj.subparser(actions_parser) - except NameError as e: - logging.critical(e) - logging.warning( - 'See subparser configuration for Class "{}"'.format( - name)) - sys.exit(3) - - def parse_args(self, args=None, namespace=None): - """Parse command line arguments, backed by env var and config_file.""" - self.initialize_parser() - cooked = super().parse_args(args, namespace) - return self.resolve_configuration(cooked) - - def resolve_configuration(self, args): - """Find and fill in any arguments not passed on command line.""" - try: - # Configuration file optionall, arguments may be provided elsewhere - with open(args.config_file, 'r') as stream: - config = pytoml.load(stream) - except OSError: - logging.info( - 'Failed to read: {}'.format(args.config_file), - exc_info=args.log_level == logging.DEBUG) - config = {'default': {}} - else: - if 'default' not in config: - config['default'] = {} - - def resolve(name, value): - if value: - setattr(args, name, value) - return value - self.error('Required argument "%s" is not configured.' % name) - - xdg = os.path.join(os.environ['XDG_RUNTIME_DIR'], 'podman') \ - if os.environ.get('XDG_RUNTIME_DIR') else None - - resolve( - 'run_dir', - getattr(args, 'run_dir') - or os.environ.get('RUN_DIR') - or config['default'].get('run_dir') - or xdg - or '/tmp/podman' if os.path.isdir('/tmp') else None - ) # yapf: disable - - args.local_socket_path = os.path.join(args.run_dir, "podman.socket") - - resolve( - 'host', - getattr(args, 'host') - or os.environ.get('HOST') - or config['default'].get('host') - ) # yapf:disable - - resolve( - 'user', - getattr(args, 'user') - or os.environ.get('USER') - or config['default'].get('user') - or getpass.getuser() - ) # yapf:disable - - resolve( - 'remote_socket_path', - getattr(args, 'remote_socket_path') - or os.environ.get('REMOTE_SOCKET_PATH') - or config['default'].get('remote_socket_path') - or '/run/podman/io.projectatomic.podman' - ) # yapf:disable - - resolve( - 'identity_file', - getattr(args, 'identity_file') - or os.environ.get('IDENTITY_FILE') - or config['default'].get('identity_file') - or os.path.expanduser('~{}/.ssh/id_rsa'.format(args.user)) - ) # yapf:disable - - args.local_uri = "unix:{}".format(args.local_socket_path) - args.remote_uri = "ssh://{}@{}{}".format(args.user, args.host, - args.remote_socket_path) - return args - - def exit(self, status=0, message=None): - """Capture message and route to logger.""" - if message: - log = logging.info if status == 0 else logging.error - log(message) - super().exit(status) - - def error(self, message): - """Capture message and route to logger.""" - logging.error('{}: {}'.format(self.prog, message)) - logging.error("Try '{} --help' for more information.".format( - self.prog)) - super().exit(2) - - -if __name__ == '__main__': - # Setup logging so we use stderr and can change logging level later - # Do it now before there is any chance of a default setup. - log = logging.getLogger() - fmt = logging.Formatter('%(asctime)s | %(levelname)-8s | %(message)s', - '%Y-%m-%d %H:%M:%S %Z') - stderr = logging.StreamHandler(stream=sys.stderr) - stderr.setFormatter(fmt) - log.addHandler(stderr) - log.setLevel(logging.INFO) - - parser = PodmanArgumentParser() - args = parser.parse_args() - - log.setLevel(args.log_level) - logging.debug('Logging initialized at level {}'.format( - logging.getLevelName(logging.getLogger().getEffectiveLevel()))) - - def istraceback(): - """Add traceback when logging events.""" - return log.getEffectiveLevel() == logging.DEBUG - - try: - if not os.path.exists(args.run_dir): - os.makedirs(args.run_dir) - except PermissionError as e: - logging.critical(e, exc_info=istraceback()) - sys.exit(6) - - # Klass(args).method() are setup by the sub-command's parser - returncode = None - try: - obj = args.klass(args) - except Exception as e: - logging.critical(repr(e), exc_info=istraceback()) - logging.warning('See subparser "{}" configuration.'.format( - args.subparser_name)) - sys.exit(5) - - try: - returncode = getattr(obj, args.method)() - except AttributeError as e: - logging.critical(e, exc_info=istraceback()) - logging.warning('See subparser "{}" configuration.'.format( - args.subparser_name)) - returncode = 3 - except (ConnectionResetError, TimeoutError) as e: - logging.critical(e, exc_info=istraceback()) - logging.info('Review connection arguments for correctness.') - returncode = 4 - - sys.exit(0 if returncode is None else returncode) diff --git a/contrib/python/examples/eg_attach.py b/contrib/python/examples/eg_attach.py deleted file mode 100644 index f5070dc53..000000000 --- a/contrib/python/examples/eg_attach.py +++ /dev/null @@ -1,18 +0,0 @@ -#!/usr/bin/env python3 -"""Example: Run top on Alpine container.""" - -import podman - -print('{}\n'.format(__doc__)) - -with podman.Client() as client: - id = client.images.pull('alpine:latest') - img = client.images.get(id) - cntr = img.create(detach=True, tty=True, command=['/usr/bin/top']) - cntr.attach(eot=4) - - try: - cntr.start() - print() - except (BrokenPipeError, KeyboardInterrupt): - print('\nContainer disconnected.') diff --git a/contrib/python/examples/eg_containers_by_image.py b/contrib/python/examples/eg_containers_by_image.py deleted file mode 100644 index bf4fdebf1..000000000 --- a/contrib/python/examples/eg_containers_by_image.py +++ /dev/null @@ -1,16 +0,0 @@ -#!/usr/bin/env python3 -"""Example: Show containers grouped by image id.""" - -from itertools import groupby - -import podman - -print('{}\n'.format(__doc__)) - -with podman.Client() as client: - ctnrs = sorted(client.containers.list(), key=lambda k: k.imageid) - for key, grp in groupby(ctnrs, key=lambda k: k.imageid): - print('Image: {}'.format(key)) - for c in grp: - print(' : container: {} created at: {}'.format( - c.id[:12], podman.datetime_format(c.createdat))) diff --git a/contrib/python/examples/eg_image_list.py b/contrib/python/examples/eg_image_list.py deleted file mode 100644 index ef31fd708..000000000 --- a/contrib/python/examples/eg_image_list.py +++ /dev/null @@ -1,10 +0,0 @@ -#!/usr/bin/env python3 -"""Example: Show all images on system.""" - -import podman - -print('{}\n'.format(__doc__)) - -with podman.Client() as client: - for img in client.images.list(): - print(img) diff --git a/contrib/python/examples/eg_inspect_fedora.py b/contrib/python/examples/eg_inspect_fedora.py deleted file mode 100644 index b5bbba46d..000000000 --- a/contrib/python/examples/eg_inspect_fedora.py +++ /dev/null @@ -1,16 +0,0 @@ -#!/usr/bin/env python3 -"""Example: Pull Fedora and inspect image and container.""" - -import podman - -print('{}\n'.format(__doc__)) - -with podman.Client() as client: - id = client.images.pull('registry.fedoraproject.org/fedora:28') - img = client.images.get(id) - print(img.inspect()) - - cntr = img.create() - print(cntr.inspect()) - - cntr.remove() diff --git a/contrib/python/examples/eg_latest_containers.py b/contrib/python/examples/eg_latest_containers.py deleted file mode 100644 index 446f670dd..000000000 --- a/contrib/python/examples/eg_latest_containers.py +++ /dev/null @@ -1,19 +0,0 @@ -#!/usr/bin/env python3 -"""Example: Show all containers created since midnight.""" - -from datetime import datetime, time, timezone - -import podman - -print('{}\n'.format(__doc__)) - - -midnight = datetime.combine(datetime.today(), time.min, tzinfo=timezone.utc) - -with podman.Client() as client: - for c in client.containers.list(): - created_at = podman.datetime_parse(c.createdat) - - if created_at > midnight: - print('{}: image: {} createdAt: {}'.format( - c.id[:12], c.image[:32], podman.datetime_format(created_at))) diff --git a/contrib/python/examples/eg_new_image.py b/contrib/python/examples/eg_new_image.py deleted file mode 100644 index 21e076dcb..000000000 --- a/contrib/python/examples/eg_new_image.py +++ /dev/null @@ -1,32 +0,0 @@ -#!/usr/bin/env python3 -"""Example: Create new image from container.""" - -import sys - -import podman - - -def print_history(details): - """Format history data from an image, in a table.""" - for i, r in enumerate(details): - print( - '{}: {} {} {}'.format(i, r.id[:12], - podman.datetime_format(r.created), r.tags), - sep='\n') - print("-" * 25) - - -print('{}\n'.format(__doc__)) - -with podman.Client() as client: - ctnr = next( - (c for c in client.containers.list() if 'alpine' in c['image']), None) - - if ctnr: - print_history(client.images.get(ctnr.imageid).history()) - - # Make changes as we save the container to a new image - id = ctnr.commit('alpine-ash', changes=['CMD=/bin/ash']) - print_history(client.images.get(id).history()) - else: - print('Unable to find "alpine" container.', file=sys.stderr) diff --git a/contrib/python/examples/run_example.sh b/contrib/python/examples/run_example.sh deleted file mode 100755 index 0f6575073..000000000 --- a/contrib/python/examples/run_example.sh +++ /dev/null @@ -1,43 +0,0 @@ -#!/bin/bash - -export PYTHONPATH=.. - -function examples { - for file in $@; do - python3 -c "import ast; f=open('"${file}"'); t=ast.parse(f.read()); print(ast.get_docstring(t) + ' -- "${file}"')" - done -} - -while getopts "lh" arg; do - case $arg in - l ) examples $(ls eg_*.py); exit 0 ;; - h ) echo 1>&2 $0 [-l] [-h] filename ; exit 2 ;; - esac -done -shift $((OPTIND-1)) - -# podman needs to play some games with resources -if [[ $(id -u) != 0 ]]; then - echo 1>&2 $0 must be run as root. - exit 2 -fi - -if ! systemctl --quiet is-active io.projectatomic.podman.socket; then - echo 1>&2 'podman is not running. systemctl enable --now io.projectatomic.podman.socket' - exit 1 -fi - -function cleanup { - podman rm $1 >/dev/null 2>&1 -} - -# Setup storage with an image and container -podman pull alpine:latest >/tmp/podman.output 2>&1 -CTNR=$(podman create alpine) -trap "cleanup $CTNR" EXIT - -if [[ -f $1 ]]; then - python3 $1 -else - python3 $1.py -fi diff --git a/contrib/python/podman/CHANGES.txt b/contrib/python/podman/CHANGES.txt new file mode 100644 index 000000000..2bac1c867 --- /dev/null +++ b/contrib/python/podman/CHANGES.txt @@ -0,0 +1 @@ +v0.1.0, 2018-05-11 -- Initial release. diff --git a/contrib/python/podman/LICENSE.txt b/contrib/python/podman/LICENSE.txt new file mode 100644 index 000000000..decfce56d --- /dev/null +++ b/contrib/python/podman/LICENSE.txt @@ -0,0 +1,13 @@ +Copyright 2018 Red Hat, Inc + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. diff --git a/contrib/python/podman/MANIFEST.in b/contrib/python/podman/MANIFEST.in new file mode 100644 index 000000000..72e638cb9 --- /dev/null +++ b/contrib/python/podman/MANIFEST.in @@ -0,0 +1,2 @@ +prune test/ +include README.md diff --git a/contrib/python/podman/Makefile b/contrib/python/podman/Makefile new file mode 100644 index 000000000..ea40cccac --- /dev/null +++ b/contrib/python/podman/Makefile @@ -0,0 +1,21 @@ +PYTHON ?= /usr/bin/python3 + +.PHONY: python-podman +python-podman: + $(PYTHON) setup.py bdist + +.PHONY: integration +integration: + test/test_runner.sh + +.PHONY: install +install: + $(PYTHON) setup.py install --user + +.PHONY: clean +clean: + $(PYTHON) setup.py clean --all + pip3 uninstall podman ||: + rm -rf podman.egg-info dist + find . -depth -name __pycache__ -exec rm -rf {} \; + find . -depth -name \*.pyc -exec rm -f {} \; diff --git a/contrib/python/podman/README.md b/contrib/python/podman/README.md new file mode 100644 index 000000000..fad03fd27 --- /dev/null +++ b/contrib/python/podman/README.md @@ -0,0 +1,43 @@ +# podman - pythonic library for working with varlink interface to Podman + +## Status: Active Development + +See [libpod](https://github.com/projectatomic/libpod) + +## Releases + +To build the podman egg: + +```sh +cd ~/libpod/contrib/python +python3 setup.py clean -a && python3 setup.py bdist +``` + +## Code snippets/examples: + +### Show images in storage + +```python +import podman + +with podman.Client() as client: + list(map(print, client.images.list())) +``` + +### Show containers created since midnight + +```python +from datetime import datetime, time, timezone + +import podman + +midnight = datetime.combine(datetime.today(), time.min, tzinfo=timezone.utc) + +with podman.Client() as client: + for c in client.containers.list(): + created_at = podman.datetime_parse(c.createdat) + + if created_at > midnight: + print('Container {}: image: {} created at: {}'.format( + c.id[:12], c.image[:32], podman.datetime_format(created_at))) +``` diff --git a/contrib/python/podman/__init__.py b/contrib/python/podman/__init__.py deleted file mode 100644 index 5a0356311..000000000 --- a/contrib/python/podman/__init__.py +++ /dev/null @@ -1,22 +0,0 @@ -"""A client for communicating with a Podman server.""" -import pkg_resources - -from .client import Client -from .libs import datetime_format, datetime_parse -from .libs.errors import (ContainerNotFound, ErrorOccurred, ImageNotFound, - RuntimeError) - -try: - __version__ = pkg_resources.get_distribution('podman').version -except Exception: - __version__ = '0.0.0' - -__all__ = [ - 'Client', - 'ContainerNotFound', - 'datetime_format', - 'datetime_parse', - 'ErrorOccurred', - 'ImageNotFound', - 'RuntimeError', -] diff --git a/contrib/python/podman/client.py b/contrib/python/podman/client.py deleted file mode 100644 index ad166eb06..000000000 --- a/contrib/python/podman/client.py +++ /dev/null @@ -1,206 +0,0 @@ -"""A client for communicating with a Podman varlink service.""" -import errno -import os -from urllib.parse import urlparse - -from varlink import Client as VarlinkClient -from varlink import VarlinkError - -from .libs import cached_property -from .libs.containers import Containers -from .libs.errors import error_factory -from .libs.images import Images -from .libs.system import System -from .libs.tunnel import Context, Portal, Tunnel - - -class BaseClient(object): - """Context manager for API workers to access varlink.""" - - def __call__(self): - """Support being called for old API.""" - return self - - @classmethod - def factory(cls, - uri=None, - interface='io.projectatomic.podman', - *args, - **kwargs): - """Construct a Client based on input.""" - if uri is None: - raise ValueError('uri is required and cannot be None') - if interface is None: - raise ValueError('interface is required and cannot be None') - - unsupported = set(kwargs.keys()).difference( - ('uri', 'interface', 'remote_uri', 'identity_file')) - if unsupported: - raise ValueError('Unknown keyword arguments: {}'.format( - ', '.join(unsupported))) - - local_path = urlparse(uri).path - if local_path == '': - raise ValueError('path is required for uri,' - ' expected format "unix://path_to_socket"') - - if kwargs.get('remote_uri') or kwargs.get('identity_file'): - # Remote access requires the full tuple of information - if kwargs.get('remote_uri') is None: - raise ValueError( - 'remote is required,' - ' expected format "ssh://user@hostname/path_to_socket".') - remote = urlparse(kwargs['remote_uri']) - if remote.username is None: - raise ValueError( - 'username is required for remote_uri,' - ' expected format "ssh://user@hostname/path_to_socket".') - if remote.path == '': - raise ValueError( - 'path is required for remote_uri,' - ' expected format "ssh://user@hostname/path_to_socket".') - if remote.hostname is None: - raise ValueError( - 'hostname is required for remote_uri,' - ' expected format "ssh://user@hostname/path_to_socket".') - - if kwargs.get('identity_file') is None: - raise ValueError('identity_file is required.') - - if not os.path.isfile(kwargs['identity_file']): - raise FileNotFoundError( - errno.ENOENT, - os.strerror(errno.ENOENT), - kwargs['identity_file'], - ) - - return RemoteClient( - Context(uri, interface, local_path, remote.path, - remote.username, remote.hostname, - kwargs['identity_file'])) - else: - return LocalClient( - Context(uri, interface, None, None, None, None, None)) - - -class LocalClient(BaseClient): - """Context manager for API workers to access varlink.""" - - def __init__(self, context): - """Construct LocalClient.""" - self._context = context - - def __enter__(self): - """Enter context for LocalClient.""" - self._client = VarlinkClient(address=self._context.uri) - self._iface = self._client.open(self._context.interface) - return self._iface - - def __exit__(self, e_type, e, e_traceback): - """Cleanup context for LocalClient.""" - if hasattr(self._client, 'close'): - self._client.close() - self._iface.close() - - if isinstance(e, VarlinkError): - raise error_factory(e) - - -class RemoteClient(BaseClient): - """Context manager for API workers to access remote varlink.""" - - def __init__(self, context): - """Construct RemoteCLient.""" - self._context = context - self._portal = Portal() - - def __enter__(self): - """Context manager for API workers to access varlink.""" - tunnel = self._portal.get(self._context.uri) - if tunnel is None: - tunnel = Tunnel(self._context).bore(self._context.uri) - self._portal[self._context.uri] = tunnel - - try: - self._client = VarlinkClient(address=self._context.uri) - self._iface = self._client.open(self._context.interface) - return self._iface - except Exception: - tunnel.close(self._context.uri) - raise - - def __exit__(self, e_type, e, e_traceback): - """Cleanup context for RemoteClient.""" - if hasattr(self._client, 'close'): - self._client.close() - self._iface.close() - - # set timer to shutdown ssh tunnel - if isinstance(e, VarlinkError): - raise error_factory(e) - - -class Client(object): - """A client for communicating with a Podman varlink service. - - Example: - - >>> import podman - >>> c = podman.Client() - >>> c.system.versions - - Example remote podman: - - >>> import podman - >>> c = podman.Client(uri='unix:/tmp/podman.sock', - remote_uri='ssh://user@host/run/podman/io.projectatomic.podman', - identity_file='~/.ssh/id_rsa') - """ - - def __init__(self, - uri='unix:/run/podman/io.projectatomic.podman', - interface='io.projectatomic.podman', - **kwargs): - """Construct a podman varlink Client. - - uri from default systemd unit file. - interface from io.projectatomic.podman.varlink, do not change unless - you are a varlink guru. - """ - self._client = BaseClient.factory(uri, interface, **kwargs) - - address = "{}-{}".format(uri, interface) - # Quick validation of connection data provided - try: - if not System(self._client).ping(): - raise ConnectionRefusedError( - errno.ECONNREFUSED, - 'Failed varlink connection "{}"'.format(address), address) - except FileNotFoundError: - raise ConnectionError( - errno.ECONNREFUSED, - ('Failed varlink connection "{}".' - ' Is podman service running?').format(address), address) - - def __enter__(self): - """Return `self` upon entering the runtime context.""" - return self - - def __exit__(self, exc_type, exc_value, traceback): - """Raise any exception triggered within the runtime context.""" - return None - - @cached_property - def system(self): - """Manage system model for podman.""" - return System(self._client) - - @cached_property - def images(self): - """Manage images model for libpod.""" - return Images(self._client) - - @cached_property - def containers(self): - """Manage containers model for libpod.""" - return Containers(self._client) diff --git a/contrib/python/podman/examples/eg_attach.py b/contrib/python/podman/examples/eg_attach.py new file mode 100644 index 000000000..f5070dc53 --- /dev/null +++ b/contrib/python/podman/examples/eg_attach.py @@ -0,0 +1,18 @@ +#!/usr/bin/env python3 +"""Example: Run top on Alpine container.""" + +import podman + +print('{}\n'.format(__doc__)) + +with podman.Client() as client: + id = client.images.pull('alpine:latest') + img = client.images.get(id) + cntr = img.create(detach=True, tty=True, command=['/usr/bin/top']) + cntr.attach(eot=4) + + try: + cntr.start() + print() + except (BrokenPipeError, KeyboardInterrupt): + print('\nContainer disconnected.') diff --git a/contrib/python/podman/examples/eg_containers_by_image.py b/contrib/python/podman/examples/eg_containers_by_image.py new file mode 100644 index 000000000..bf4fdebf1 --- /dev/null +++ b/contrib/python/podman/examples/eg_containers_by_image.py @@ -0,0 +1,16 @@ +#!/usr/bin/env python3 +"""Example: Show containers grouped by image id.""" + +from itertools import groupby + +import podman + +print('{}\n'.format(__doc__)) + +with podman.Client() as client: + ctnrs = sorted(client.containers.list(), key=lambda k: k.imageid) + for key, grp in groupby(ctnrs, key=lambda k: k.imageid): + print('Image: {}'.format(key)) + for c in grp: + print(' : container: {} created at: {}'.format( + c.id[:12], podman.datetime_format(c.createdat))) diff --git a/contrib/python/podman/examples/eg_image_list.py b/contrib/python/podman/examples/eg_image_list.py new file mode 100644 index 000000000..ef31fd708 --- /dev/null +++ b/contrib/python/podman/examples/eg_image_list.py @@ -0,0 +1,10 @@ +#!/usr/bin/env python3 +"""Example: Show all images on system.""" + +import podman + +print('{}\n'.format(__doc__)) + +with podman.Client() as client: + for img in client.images.list(): + print(img) diff --git a/contrib/python/podman/examples/eg_inspect_fedora.py b/contrib/python/podman/examples/eg_inspect_fedora.py new file mode 100644 index 000000000..b5bbba46d --- /dev/null +++ b/contrib/python/podman/examples/eg_inspect_fedora.py @@ -0,0 +1,16 @@ +#!/usr/bin/env python3 +"""Example: Pull Fedora and inspect image and container.""" + +import podman + +print('{}\n'.format(__doc__)) + +with podman.Client() as client: + id = client.images.pull('registry.fedoraproject.org/fedora:28') + img = client.images.get(id) + print(img.inspect()) + + cntr = img.create() + print(cntr.inspect()) + + cntr.remove() diff --git a/contrib/python/podman/examples/eg_latest_containers.py b/contrib/python/podman/examples/eg_latest_containers.py new file mode 100644 index 000000000..446f670dd --- /dev/null +++ b/contrib/python/podman/examples/eg_latest_containers.py @@ -0,0 +1,19 @@ +#!/usr/bin/env python3 +"""Example: Show all containers created since midnight.""" + +from datetime import datetime, time, timezone + +import podman + +print('{}\n'.format(__doc__)) + + +midnight = datetime.combine(datetime.today(), time.min, tzinfo=timezone.utc) + +with podman.Client() as client: + for c in client.containers.list(): + created_at = podman.datetime_parse(c.createdat) + + if created_at > midnight: + print('{}: image: {} createdAt: {}'.format( + c.id[:12], c.image[:32], podman.datetime_format(created_at))) diff --git a/contrib/python/podman/examples/eg_new_image.py b/contrib/python/podman/examples/eg_new_image.py new file mode 100644 index 000000000..21e076dcb --- /dev/null +++ b/contrib/python/podman/examples/eg_new_image.py @@ -0,0 +1,32 @@ +#!/usr/bin/env python3 +"""Example: Create new image from container.""" + +import sys + +import podman + + +def print_history(details): + """Format history data from an image, in a table.""" + for i, r in enumerate(details): + print( + '{}: {} {} {}'.format(i, r.id[:12], + podman.datetime_format(r.created), r.tags), + sep='\n') + print("-" * 25) + + +print('{}\n'.format(__doc__)) + +with podman.Client() as client: + ctnr = next( + (c for c in client.containers.list() if 'alpine' in c['image']), None) + + if ctnr: + print_history(client.images.get(ctnr.imageid).history()) + + # Make changes as we save the container to a new image + id = ctnr.commit('alpine-ash', changes=['CMD=/bin/ash']) + print_history(client.images.get(id).history()) + else: + print('Unable to find "alpine" container.', file=sys.stderr) diff --git a/contrib/python/podman/examples/run_example.sh b/contrib/python/podman/examples/run_example.sh new file mode 100755 index 000000000..0f6575073 --- /dev/null +++ b/contrib/python/podman/examples/run_example.sh @@ -0,0 +1,43 @@ +#!/bin/bash + +export PYTHONPATH=.. + +function examples { + for file in $@; do + python3 -c "import ast; f=open('"${file}"'); t=ast.parse(f.read()); print(ast.get_docstring(t) + ' -- "${file}"')" + done +} + +while getopts "lh" arg; do + case $arg in + l ) examples $(ls eg_*.py); exit 0 ;; + h ) echo 1>&2 $0 [-l] [-h] filename ; exit 2 ;; + esac +done +shift $((OPTIND-1)) + +# podman needs to play some games with resources +if [[ $(id -u) != 0 ]]; then + echo 1>&2 $0 must be run as root. + exit 2 +fi + +if ! systemctl --quiet is-active io.projectatomic.podman.socket; then + echo 1>&2 'podman is not running. systemctl enable --now io.projectatomic.podman.socket' + exit 1 +fi + +function cleanup { + podman rm $1 >/dev/null 2>&1 +} + +# Setup storage with an image and container +podman pull alpine:latest >/tmp/podman.output 2>&1 +CTNR=$(podman create alpine) +trap "cleanup $CTNR" EXIT + +if [[ -f $1 ]]; then + python3 $1 +else + python3 $1.py +fi diff --git a/contrib/python/podman/libs/__init__.py b/contrib/python/podman/libs/__init__.py deleted file mode 100644 index 3a8a35021..000000000 --- a/contrib/python/podman/libs/__init__.py +++ /dev/null @@ -1,54 +0,0 @@ -"""Support files for podman API implementation.""" -import collections -import datetime -import functools - -from dateutil.parser import parse as dateutil_parse - -__all__ = [ - 'cached_property', - 'datetime_parse', - 'datetime_format', -] - - -def cached_property(fn): - """Decorate property to cache return value.""" - return property(functools.lru_cache(maxsize=8)(fn)) - - -class Config(collections.UserDict): - """Silently ignore None values, only take key once.""" - - def __init__(self, **kwargs): - """Construct dictionary.""" - super(Config, self).__init__(kwargs) - - def __setitem__(self, key, value): - """Store unique, not None values.""" - if value is None: - return - - if super().__contains__(key): - return - - super().__setitem__(key, value) - - -def datetime_parse(string): - """Convert timestamps to datetime. - - tzinfo aware, if provided. - """ - return dateutil_parse(string.upper(), fuzzy=True) - - -def datetime_format(dt): - """Format datetime in consistent style.""" - if isinstance(dt, str): - return datetime_parse(dt).isoformat() - elif isinstance(dt, datetime.datetime): - return dt.isoformat() - else: - raise ValueError('Unable to format {}. Type {} not supported.'.format( - dt, type(dt))) diff --git a/contrib/python/podman/libs/_containers_attach.py b/contrib/python/podman/libs/_containers_attach.py deleted file mode 100644 index df12fa998..000000000 --- a/contrib/python/podman/libs/_containers_attach.py +++ /dev/null @@ -1,75 +0,0 @@ -"""Exported method Container.attach().""" - -import collections -import fcntl -import logging -import struct -import sys -import termios - - -class Mixin: - """Publish attach() for inclusion in Container class.""" - - def attach(self, eot=4, stdin=None, stdout=None): - """Attach to container's PID1 stdin and stdout. - - stderr is ignored. - PseudoTTY work is done in start(). - """ - if stdin is None: - stdin = sys.stdin.fileno() - - if stdout is None: - stdout = sys.stdout.fileno() - - with self._client() as podman: - attach = podman.GetAttachSockets(self._id) - - # This is the UDS where all the IO goes - io_socket = attach['sockets']['io_socket'] - assert len(io_socket) <= 107,\ - 'Path length for sockets too long. {} > 107'.format( - len(io_socket) - ) - - # This is the control socket where resizing events are sent to conmon - # attach['sockets']['control_socket'] - self.pseudo_tty = collections.namedtuple( - 'PseudoTTY', - ['stdin', 'stdout', 'io_socket', 'control_socket', 'eot'])( - stdin, - stdout, - attach['sockets']['io_socket'], - attach['sockets']['control_socket'], - eot, - ) - - @property - def resize_handler(self): - """Send the new window size to conmon.""" - - def wrapped(signum, frame): - packed = fcntl.ioctl(self.pseudo_tty.stdout, termios.TIOCGWINSZ, - struct.pack('HHHH', 0, 0, 0, 0)) - rows, cols, _, _ = struct.unpack('HHHH', packed) - logging.debug('Resize window({}x{}) using {}'.format( - rows, cols, self.pseudo_tty.control_socket)) - - # TODO: Need some kind of timeout in case pipe is blocked - with open(self.pseudo_tty.control_socket, 'w') as skt: - # send conmon window resize message - skt.write('1 {} {}\n'.format(rows, cols)) - - return wrapped - - @property - def log_handler(self): - """Send command to reopen log to conmon.""" - - def wrapped(signum, frame): - with open(self.pseudo_tty.control_socket, 'w') as skt: - # send conmon reopen log message - skt.write('2\n') - - return wrapped diff --git a/contrib/python/podman/libs/_containers_start.py b/contrib/python/podman/libs/_containers_start.py deleted file mode 100644 index ad9f32eab..000000000 --- a/contrib/python/podman/libs/_containers_start.py +++ /dev/null @@ -1,82 +0,0 @@ -"""Exported method Container.start().""" -import logging -import os -import select -import signal -import socket -import sys -import termios -import tty - -CONMON_BUFSZ = 8192 - - -class Mixin: - """Publish start() for inclusion in Container class.""" - - def start(self): - """Start container, return container on success. - - Will block if container has been detached. - """ - with self._client() as podman: - results = podman.StartContainer(self.id) - logging.debug('Started Container "{}"'.format( - results['container'])) - - if not hasattr(self, 'pseudo_tty') or self.pseudo_tty is None: - return self._refresh(podman) - - logging.debug('Setting up PseudoTTY for Container "{}"'.format( - results['container'])) - - try: - # save off the old settings for terminal - tcoldattr = termios.tcgetattr(self.pseudo_tty.stdin) - tty.setraw(self.pseudo_tty.stdin) - - # initialize container's window size - self.resize_handler(None, sys._getframe(0)) - - # catch any resizing events and send the resize info - # to the control fifo "socket" - signal.signal(signal.SIGWINCH, self.resize_handler) - - except termios.error: - tcoldattr = None - - try: - # TODO: Is socket.SOCK_SEQPACKET supported in Windows? - with socket.socket(socket.AF_UNIX, - socket.SOCK_SEQPACKET) as skt: - # Prepare socket for use with conmon/container - skt.connect(self.pseudo_tty.io_socket) - - sources = [skt, self.pseudo_tty.stdin] - while sources: - logging.debug('Waiting on sources: {}'.format(sources)) - readable, _, _ = select.select(sources, [], []) - - if skt in readable: - data = skt.recv(CONMON_BUFSZ) - if data: - # Remove source marker when writing - os.write(self.pseudo_tty.stdout, data[1:]) - else: - sources.remove(skt) - - if self.pseudo_tty.stdin in readable: - data = os.read(self.pseudo_tty.stdin, CONMON_BUFSZ) - if data: - skt.sendall(data) - - if self.pseudo_tty.eot in data: - sources.clear() - else: - sources.remove(self.pseudo_tty.stdin) - finally: - if tcoldattr: - termios.tcsetattr(self.pseudo_tty.stdin, termios.TCSADRAIN, - tcoldattr) - signal.signal(signal.SIGWINCH, signal.SIG_DFL) - return self._refresh(podman) diff --git a/contrib/python/podman/libs/containers.py b/contrib/python/podman/libs/containers.py deleted file mode 100644 index 6dc2c141e..000000000 --- a/contrib/python/podman/libs/containers.py +++ /dev/null @@ -1,245 +0,0 @@ -"""Models for manipulating containers and storage.""" -import collections -import functools -import getpass -import json -import signal -import time - -from ._containers_attach import Mixin as AttachMixin -from ._containers_start import Mixin as StartMixin - - -class Container(AttachMixin, StartMixin, collections.UserDict): - """Model for a container.""" - - def __init__(self, client, id, data): - """Construct Container Model.""" - super(Container, self).__init__(data) - - self._client = client - self._id = id - - with client() as podman: - self._refresh(podman) - - assert self._id == self.data['id'],\ - 'Requested container id({}) does not match store id({})'.format( - self._id, self.id - ) - - def __getitem__(self, key): - """Get items from parent dict.""" - return super().__getitem__(key) - - def _refresh(self, podman): - ctnr = podman.GetContainer(self._id) - super().update(ctnr['container']) - - for k, v in self.data.items(): - setattr(self, k, v) - if 'containerrunning' in self.data: - setattr(self, 'running', self.data['containerrunning']) - self.data['running'] = self.data['containerrunning'] - - return self - - def refresh(self): - """Refresh status fields for this container.""" - with self._client() as podman: - return self._refresh(podman) - - def processes(self): - """Show processes running in container.""" - with self._client() as podman: - results = podman.ListContainerProcesses(self.id) - yield from results['container'] - - def changes(self): - """Retrieve container changes.""" - with self._client() as podman: - results = podman.ListContainerChanges(self.id) - return results['container'] - - def kill(self, signal=signal.SIGTERM, wait=25): - """Send signal to container. - - default signal is signal.SIGTERM. - wait n of seconds, 0 waits forever. - """ - with self._client() as podman: - podman.KillContainer(self.id, signal) - timeout = time.time() + wait - while True: - self._refresh(podman) - if self.status != 'running': - return self - - if wait and timeout < time.time(): - raise TimeoutError() - - time.sleep(0.5) - - def _lower_hook(self): - """Convert all keys to lowercase.""" - - @functools.wraps(self._lower_hook) - def wrapped(input): - return {k.lower(): v for (k, v) in input.items()} - - return wrapped - - def inspect(self): - """Retrieve details about containers.""" - with self._client() as podman: - results = podman.InspectContainer(self.id) - obj = json.loads(results['container'], object_hook=self._lower_hook()) - return collections.namedtuple('ContainerInspect', obj.keys())(**obj) - - def export(self, target): - """Export container from store to tarball. - - TODO: should there be a compress option, like images? - """ - with self._client() as podman: - results = podman.ExportContainer(self.id, target) - return results['tarfile'] - - def commit(self, - image_name, - *args, - changes=[], - message='', - pause=True, - **kwargs): - """Create image from container. - - All changes overwrite existing values. - See inspect() to obtain current settings. - - Changes: - CMD=/usr/bin/zsh - ENTRYPOINT=/bin/sh date - ENV=TEST=test_containers.TestContainers.test_commit - EXPOSE=8888/tcp - LABEL=unittest=test_commit - USER=bozo:circus - VOLUME=/data - WORKDIR=/data/application - """ - # TODO: Clean up *args, **kwargs after Commit() is complete - try: - author = kwargs.get('author', getpass.getuser()) - except Exception: - author = '' - - for c in changes: - if c.startswith('LABEL=') and c.count('=') < 2: - raise ValueError( - 'LABEL should have the format: LABEL=label=value, not {}'. - format(c)) - - with self._client() as podman: - results = podman.Commit(self.id, image_name, changes, author, - message, pause) - return results['image'] - - def stop(self, timeout=25): - """Stop container, return id on success.""" - with self._client() as podman: - podman.StopContainer(self.id, timeout) - return self._refresh(podman) - - def remove(self, force=False): - """Remove container, return id on success. - - force=True, stop running container. - """ - with self._client() as podman: - results = podman.RemoveContainer(self.id, force) - return results['container'] - - def restart(self, timeout=25): - """Restart container with timeout, return id on success.""" - with self._client() as podman: - podman.RestartContainer(self.id, timeout) - return self._refresh(podman) - - def rename(self, target): - """Rename container, return id on success.""" - with self._client() as podman: - # TODO: Need arguments - results = podman.RenameContainer() - # TODO: fixup objects cached information - return results['container'] - - def resize_tty(self, width, height): - """Resize container tty.""" - with self._client() as podman: - # TODO: magic re: attach(), arguments - podman.ResizeContainerTty() - - def pause(self): - """Pause container, return id on success.""" - with self._client() as podman: - podman.PauseContainer(self.id) - return self._refresh(podman) - - def unpause(self): - """Unpause container, return id on success.""" - with self._client() as podman: - podman.UnpauseContainer(self.id) - return self._refresh(podman) - - def update_container(self, *args, **kwargs): - """TODO: Update container..., return id on success.""" - with self._client() as podman: - podman.UpdateContainer() - return self._refresh(podman) - - def wait(self): - """Wait for container to finish, return 'returncode'.""" - with self._client() as podman: - results = podman.WaitContainer(self.id) - return int(results['exitcode']) - - def stats(self): - """Retrieve resource stats from the container.""" - with self._client() as podman: - results = podman.GetContainerStats(self.id) - obj = results['container'] - return collections.namedtuple('StatDetail', obj.keys())(**obj) - - def logs(self, *args, **kwargs): - """Retrieve container logs.""" - with self._client() as podman: - results = podman.GetContainerLogs(self.id) - yield from results - - -class Containers(object): - """Model for Containers collection.""" - - def __init__(self, client): - """Construct model for Containers collection.""" - self._client = client - - def list(self): - """List of containers in the container store.""" - with self._client() as podman: - results = podman.ListContainers() - for cntr in results['containers']: - yield Container(self._client, cntr['id'], cntr) - - def delete_stopped(self): - """Delete all stopped containers.""" - with self._client() as podman: - results = podman.DeleteStoppedContainers() - return results['containers'] - - def get(self, id): - """Retrieve container details from store.""" - with self._client() as podman: - cntr = podman.GetContainer(id) - return Container(self._client, cntr['container']['id'], - cntr['container']) diff --git a/contrib/python/podman/libs/errors.py b/contrib/python/podman/libs/errors.py deleted file mode 100644 index b98210481..000000000 --- a/contrib/python/podman/libs/errors.py +++ /dev/null @@ -1,65 +0,0 @@ -"""Error classes and wrappers for VarlinkError.""" -from varlink import VarlinkError - - -class VarlinkErrorProxy(VarlinkError): - """Class to Proxy VarlinkError methods.""" - - def __init__(self, message, namespaced=False): - """Construct proxy from Exception.""" - super().__init__(message.as_dict(), namespaced) - self._message = message - self.__module__ = 'libpod' - - def __getattr__(self, method): - """Return attribute from proxied Exception.""" - if hasattr(self._message, method): - return getattr(self._message, method) - - try: - return self._message.parameters()[method] - except KeyError: - raise AttributeError('No such attribute: {}'.format(method)) - - -class ContainerNotFound(VarlinkErrorProxy): - """Raised when Client can not find requested container.""" - - pass - - -class ImageNotFound(VarlinkErrorProxy): - """Raised when Client can not find requested image.""" - - pass - - -class ErrorOccurred(VarlinkErrorProxy): - """Raised when an error occurs during the execution. - - See error() to see actual error text. - """ - - pass - - -class RuntimeError(VarlinkErrorProxy): - """Raised when Client fails to connect to runtime.""" - - pass - - -error_map = { - 'io.projectatomic.podman.ContainerNotFound': ContainerNotFound, - 'io.projectatomic.podman.ErrorOccurred': ErrorOccurred, - 'io.projectatomic.podman.ImageNotFound': ImageNotFound, - 'io.projectatomic.podman.RuntimeError': RuntimeError, -} - - -def error_factory(exception): - """Map Exceptions to a discrete type.""" - try: - return error_map[exception.error()](exception) - except KeyError: - return exception diff --git a/contrib/python/podman/libs/images.py b/contrib/python/podman/libs/images.py deleted file mode 100644 index 334ff873c..000000000 --- a/contrib/python/podman/libs/images.py +++ /dev/null @@ -1,172 +0,0 @@ -"""Models for manipulating images in/to/from storage.""" -import collections -import copy -import functools -import json -import logging - -from . import Config -from .containers import Container - - -class Image(collections.UserDict): - """Model for an Image.""" - - def __init__(self, client, id, data): - """Construct Image Model.""" - super(Image, self).__init__(data) - for k, v in data.items(): - setattr(self, k, v) - - self._id = id - self._client = client - - assert self._id == self.id,\ - 'Requested image id({}) does not match store id({})'.format( - self._id, self.id - ) - - def __getitem__(self, key): - """Get items from parent dict.""" - return super().__getitem__(key) - - def _split_token(self, values=None, sep='='): - return dict([v.split(sep, 1) for v in values if values]) - - def create(self, *args, **kwargs): - """Create container from image. - - Pulls defaults from image.inspect() - """ - details = self.inspect() - - config = Config(image_id=self.id, **kwargs) - config['command'] = details.containerconfig['cmd'] - config['env'] = self._split_token(details.containerconfig['env']) - config['image'] = copy.deepcopy(details.repotags[0]) - config['labels'] = copy.deepcopy(details.labels) - config['net_mode'] = 'bridge' - config['network'] = 'bridge' - - logging.debug('Image {}: create config: {}'.format(self.id, config)) - with self._client() as podman: - id = podman.CreateContainer(config)['container'] - cntr = podman.GetContainer(id) - return Container(self._client, id, cntr['container']) - - container = create - - def export(self, dest, compressed=False): - """Write image to dest, return id on success.""" - with self._client() as podman: - results = podman.ExportImage(self.id, dest, compressed) - return results['image'] - - def history(self): - """Retrieve image history.""" - with self._client() as podman: - for r in podman.HistoryImage(self.id)['history']: - yield collections.namedtuple('HistoryDetail', r.keys())(**r) - - # Convert all keys to lowercase. - def _lower_hook(self): - @functools.wraps(self._lower_hook) - def wrapped(input): - return {k.lower(): v for (k, v) in input.items()} - - return wrapped - - def inspect(self): - """Retrieve details about image.""" - with self._client() as podman: - results = podman.InspectImage(self.id) - obj = json.loads(results['image'], object_hook=self._lower_hook()) - return collections.namedtuple('ImageInspect', obj.keys())(**obj) - - def push(self, target, tlsverify=False): - """Copy image to target, return id on success.""" - with self._client() as podman: - results = podman.PushImage(self.id, target, tlsverify) - return results['image'] - - def remove(self, force=False): - """Delete image, return id on success. - - force=True, stop any running containers using image. - """ - with self._client() as podman: - results = podman.RemoveImage(self.id, force) - return results['image'] - - def tag(self, tag): - """Tag image.""" - with self._client() as podman: - results = podman.TagImage(self.id, tag) - return results['image'] - - -class Images(object): - """Model for Images collection.""" - - def __init__(self, client): - """Construct model for Images collection.""" - self._client = client - - def list(self): - """List all images in the libpod image store.""" - with self._client() as podman: - results = podman.ListImages() - for img in results['images']: - yield Image(self._client, img['id'], img) - - def build(self, dockerfile=None, tags=None, **kwargs): - """Build container from image. - - See podman-build.1.md for kwargs details. - """ - if dockerfile is None: - raise ValueError('"dockerfile" is a required argument.') - elif not hasattr(dockerfile, '__iter__'): - raise ValueError('"dockerfile" is required to be an iter.') - - if tags is None: - raise ValueError('"tags" is a required argument.') - elif not hasattr(tags, '__iter__'): - raise ValueError('"tags" is required to be an iter.') - - config = Config(dockerfile=dockerfile, tags=tags, **kwargs) - with self._client() as podman: - result = podman.BuildImage(config) - return self.get(result['image']['id']), \ - (line for line in result['image']['logs']) - - def delete_unused(self): - """Delete Images not associated with a container.""" - with self._client() as podman: - results = podman.DeleteUnusedImages() - return results['images'] - - def import_image(self, source, reference, message=None, changes=None): - """Read image tarball from source and save in image store.""" - with self._client() as podman: - results = podman.ImportImage(source, reference, message, changes) - return results['image'] - - def pull(self, source): - """Copy image from registry to image store.""" - with self._client() as podman: - results = podman.PullImage(source) - return results['id'] - - def search(self, id, limit=25): - """Search registries for id.""" - with self._client() as podman: - results = podman.SearchImage(id, limit) - for img in results['images']: - yield collections.namedtuple('ImageSearch', img.keys())(**img) - - def get(self, id): - """Get Image from id.""" - with self._client() as podman: - result = podman.GetImage(id) - return Image(self._client, result['image']['id'], result['image']) diff --git a/contrib/python/podman/libs/system.py b/contrib/python/podman/libs/system.py deleted file mode 100644 index c59867760..000000000 --- a/contrib/python/podman/libs/system.py +++ /dev/null @@ -1,40 +0,0 @@ -"""Models for accessing details from varlink server.""" -import collections - -import pkg_resources - -from . import cached_property - - -class System(object): - """Model for accessing system resources.""" - - def __init__(self, client): - """Construct system model.""" - self._client = client - - @cached_property - def versions(self): - """Access versions.""" - with self._client() as podman: - vers = podman.GetVersion()['version'] - - client = '0.0.0' - try: - client = pkg_resources.get_distribution('podman').version - except Exception: - pass - vers['client_version'] = client - return collections.namedtuple('Version', vers.keys())(**vers) - - def info(self): - """Return podman info.""" - with self._client() as podman: - info = podman.GetInfo()['info'] - return collections.namedtuple('Info', info.keys())(**info) - - def ping(self): - """Return True if server awake.""" - with self._client() as podman: - response = podman.Ping() - return 'OK' == response['ping']['message'] diff --git a/contrib/python/podman/libs/tunnel.py b/contrib/python/podman/libs/tunnel.py deleted file mode 100644 index 9effdff6c..000000000 --- a/contrib/python/podman/libs/tunnel.py +++ /dev/null @@ -1,138 +0,0 @@ -"""Cache for SSH tunnels.""" -import collections -import logging -import os -import subprocess -import threading -import time -import weakref - -Context = collections.namedtuple('Context', ( - 'uri', - 'interface', - 'local_socket', - 'remote_socket', - 'username', - 'hostname', - 'identity_file', -)) - - -class Portal(collections.MutableMapping): - """Expiring container for tunnels.""" - - def __init__(self, sweap=25): - """Construct portal, reap tunnels every sweap seconds.""" - self.data = collections.OrderedDict() - self.sweap = sweap - self.ttl = sweap * 2 - self.lock = threading.RLock() - self._schedule_reaper() - - def __getitem__(self, key): - """Given uri return tunnel and update TTL.""" - with self.lock: - value, _ = self.data[key] - self.data[key] = (value, time.time() + self.ttl) - self.data.move_to_end(key) - return value - - def __setitem__(self, key, value): - """Store given tunnel keyed with uri.""" - if not isinstance(value, Tunnel): - raise ValueError('Portals only support Tunnels.') - - with self.lock: - self.data[key] = (value, time.time() + self.ttl) - self.data.move_to_end(key) - - def __delitem__(self, key): - """Remove and close tunnel from portal.""" - with self.lock: - value, _ = self.data[key] - del self.data[key] - value.close(key) - del value - - def __iter__(self): - """Iterate tunnels.""" - with self.lock: - values = self.data.values() - - for tunnel, _ in values: - yield tunnel - - def __len__(self): - """Return number of tunnels in portal.""" - with self.lock: - return len(self.data) - - def _schedule_reaper(self): - timer = threading.Timer(interval=self.sweap, function=self.reap) - timer.setName('PortalReaper') - timer.setDaemon(True) - timer.start() - - def reap(self): - """Remove tunnels who's TTL has expired.""" - now = time.time() - with self.lock: - reaped_data = self.data.copy() - for entry in reaped_data.items(): - if entry[1][1] < now: - del self.data[entry[0]] - else: - # StopIteration as soon as possible - break - self._schedule_reaper() - - -class Tunnel(object): - """SSH tunnel.""" - - def __init__(self, context): - """Construct Tunnel.""" - self.context = context - self._tunnel = None - - def bore(self, id): - """Create SSH tunnel from given context.""" - ssh_opts = '-nNT' - if logging.getLogger().getEffectiveLevel() == logging.DEBUG: - ssh_opts += 'v' - else: - ssh_opts += 'q' - - cmd = [ - 'ssh', - ssh_opts, - '-L', - '{}:{}'.format(self.context.local_socket, - self.context.remote_socket), - '-i', - self.context.identity_file, - 'ssh://{}@{}'.format(self.context.username, self.context.hostname), - ] - logging.debug('Tunnel cmd "{}"'.format(' '.join(cmd))) - - self._tunnel = subprocess.Popen(cmd, close_fds=True) - for i in range(10): - # TODO: Make timeout configurable - if os.path.exists(self.context.local_socket): - break - time.sleep(0.5) - else: - raise TimeoutError('Failed to create tunnel using: {}'.format( - ' '.join(cmd))) - weakref.finalize(self, self.close, id) - return self - - def close(self, id): - """Close SSH tunnel.""" - if self._tunnel is None: - return - - self._tunnel.kill() - self._tunnel.wait(300) - os.remove(self.context.local_socket) - self._tunnel = None diff --git a/contrib/python/podman/podman/__init__.py b/contrib/python/podman/podman/__init__.py new file mode 100644 index 000000000..5a0356311 --- /dev/null +++ b/contrib/python/podman/podman/__init__.py @@ -0,0 +1,22 @@ +"""A client for communicating with a Podman server.""" +import pkg_resources + +from .client import Client +from .libs import datetime_format, datetime_parse +from .libs.errors import (ContainerNotFound, ErrorOccurred, ImageNotFound, + RuntimeError) + +try: + __version__ = pkg_resources.get_distribution('podman').version +except Exception: + __version__ = '0.0.0' + +__all__ = [ + 'Client', + 'ContainerNotFound', + 'datetime_format', + 'datetime_parse', + 'ErrorOccurred', + 'ImageNotFound', + 'RuntimeError', +] diff --git a/contrib/python/podman/podman/client.py b/contrib/python/podman/podman/client.py new file mode 100644 index 000000000..404b7d117 --- /dev/null +++ b/contrib/python/podman/podman/client.py @@ -0,0 +1,202 @@ +"""A client for communicating with a Podman varlink service.""" +import errno +import os +from urllib.parse import urlparse + +from varlink import Client as VarlinkClient +from varlink import VarlinkError + +from .libs import cached_property +from .libs.containers import Containers +from .libs.errors import error_factory +from .libs.images import Images +from .libs.system import System +from .libs.tunnel import Context, Portal, Tunnel + + +class BaseClient(object): + """Context manager for API workers to access varlink.""" + + def __call__(self): + """Support being called for old API.""" + return self + + @classmethod + def factory(cls, + uri=None, + interface='io.projectatomic.podman', + *args, + **kwargs): + """Construct a Client based on input.""" + if uri is None: + raise ValueError('uri is required and cannot be None') + if interface is None: + raise ValueError('interface is required and cannot be None') + + unsupported = set(kwargs.keys()).difference( + ('uri', 'interface', 'remote_uri', 'identity_file')) + if unsupported: + raise ValueError('Unknown keyword arguments: {}'.format( + ', '.join(unsupported))) + + local_path = urlparse(uri).path + if local_path == '': + raise ValueError('path is required for uri,' + ' expected format "unix://path_to_socket"') + + if kwargs.get('remote_uri'): + # Remote access requires the full tuple of information + if kwargs.get('remote_uri') is None: + raise ValueError( + 'remote_uri is required,' + ' expected format "ssh://user@hostname/path_to_socket".') + remote = urlparse(kwargs['remote_uri']) + if remote.username is None: + raise ValueError( + 'username is required for remote_uri,' + ' expected format "ssh://user@hostname/path_to_socket".') + if remote.path == '': + raise ValueError( + 'path is required for remote_uri,' + ' expected format "ssh://user@hostname/path_to_socket".') + if remote.hostname is None: + raise ValueError( + 'hostname is required for remote_uri,' + ' expected format "ssh://user@hostname/path_to_socket".') + + return RemoteClient( + Context( + uri, + interface, + local_path, + remote.path, + remote.username, + remote.hostname, + kwargs.get('identity_file'), + )) + else: + return LocalClient( + Context(uri, interface, None, None, None, None, None)) + + +class LocalClient(BaseClient): + """Context manager for API workers to access varlink.""" + + def __init__(self, context): + """Construct LocalClient.""" + self._context = context + + def __enter__(self): + """Enter context for LocalClient.""" + self._client = VarlinkClient(address=self._context.uri) + self._iface = self._client.open(self._context.interface) + return self._iface + + def __exit__(self, e_type, e, e_traceback): + """Cleanup context for LocalClient.""" + if hasattr(self._client, 'close'): + self._client.close() + self._iface.close() + + if isinstance(e, VarlinkError): + raise error_factory(e) + + +class RemoteClient(BaseClient): + """Context manager for API workers to access remote varlink.""" + + def __init__(self, context): + """Construct RemoteCLient.""" + self._context = context + self._portal = Portal() + + def __enter__(self): + """Context manager for API workers to access varlink.""" + tunnel = self._portal.get(self._context.uri) + if tunnel is None: + tunnel = Tunnel(self._context).bore(self._context.uri) + self._portal[self._context.uri] = tunnel + + try: + self._client = VarlinkClient(address=self._context.uri) + self._iface = self._client.open(self._context.interface) + return self._iface + except Exception: + tunnel.close(self._context.uri) + raise + + def __exit__(self, e_type, e, e_traceback): + """Cleanup context for RemoteClient.""" + if hasattr(self._client, 'close'): + self._client.close() + self._iface.close() + + # set timer to shutdown ssh tunnel + if isinstance(e, VarlinkError): + raise error_factory(e) + + +class Client(object): + """A client for communicating with a Podman varlink service. + + Example: + + >>> import podman + >>> c = podman.Client() + >>> c.system.versions + + Example remote podman: + + >>> import podman + >>> c = podman.Client(uri='unix:/tmp/podman.sock', + remote_uri='ssh://user@host/run/podman/io.projectatomic.podman', + identity_file='~/.ssh/id_rsa') + """ + + def __init__(self, + uri='unix:/run/podman/io.projectatomic.podman', + interface='io.projectatomic.podman', + **kwargs): + """Construct a podman varlink Client. + + uri from default systemd unit file. + interface from io.projectatomic.podman.varlink, do not change unless + you are a varlink guru. + """ + self._client = BaseClient.factory(uri, interface, **kwargs) + + address = "{}-{}".format(uri, interface) + # Quick validation of connection data provided + try: + if not System(self._client).ping(): + raise ConnectionRefusedError( + errno.ECONNREFUSED, + 'Failed varlink connection "{}"'.format(address), address) + except FileNotFoundError: + raise ConnectionError( + errno.ECONNREFUSED, + ('Failed varlink connection "{}".' + ' Is podman service running?').format(address), address) + + def __enter__(self): + """Return `self` upon entering the runtime context.""" + return self + + def __exit__(self, exc_type, exc_value, traceback): + """Raise any exception triggered within the runtime context.""" + return None + + @cached_property + def system(self): + """Manage system model for podman.""" + return System(self._client) + + @cached_property + def images(self): + """Manage images model for libpod.""" + return Images(self._client) + + @cached_property + def containers(self): + """Manage containers model for libpod.""" + return Containers(self._client) diff --git a/contrib/python/podman/podman/libs/__init__.py b/contrib/python/podman/podman/libs/__init__.py new file mode 100644 index 000000000..3a8a35021 --- /dev/null +++ b/contrib/python/podman/podman/libs/__init__.py @@ -0,0 +1,54 @@ +"""Support files for podman API implementation.""" +import collections +import datetime +import functools + +from dateutil.parser import parse as dateutil_parse + +__all__ = [ + 'cached_property', + 'datetime_parse', + 'datetime_format', +] + + +def cached_property(fn): + """Decorate property to cache return value.""" + return property(functools.lru_cache(maxsize=8)(fn)) + + +class Config(collections.UserDict): + """Silently ignore None values, only take key once.""" + + def __init__(self, **kwargs): + """Construct dictionary.""" + super(Config, self).__init__(kwargs) + + def __setitem__(self, key, value): + """Store unique, not None values.""" + if value is None: + return + + if super().__contains__(key): + return + + super().__setitem__(key, value) + + +def datetime_parse(string): + """Convert timestamps to datetime. + + tzinfo aware, if provided. + """ + return dateutil_parse(string.upper(), fuzzy=True) + + +def datetime_format(dt): + """Format datetime in consistent style.""" + if isinstance(dt, str): + return datetime_parse(dt).isoformat() + elif isinstance(dt, datetime.datetime): + return dt.isoformat() + else: + raise ValueError('Unable to format {}. Type {} not supported.'.format( + dt, type(dt))) diff --git a/contrib/python/podman/podman/libs/_containers_attach.py b/contrib/python/podman/podman/libs/_containers_attach.py new file mode 100644 index 000000000..df12fa998 --- /dev/null +++ b/contrib/python/podman/podman/libs/_containers_attach.py @@ -0,0 +1,75 @@ +"""Exported method Container.attach().""" + +import collections +import fcntl +import logging +import struct +import sys +import termios + + +class Mixin: + """Publish attach() for inclusion in Container class.""" + + def attach(self, eot=4, stdin=None, stdout=None): + """Attach to container's PID1 stdin and stdout. + + stderr is ignored. + PseudoTTY work is done in start(). + """ + if stdin is None: + stdin = sys.stdin.fileno() + + if stdout is None: + stdout = sys.stdout.fileno() + + with self._client() as podman: + attach = podman.GetAttachSockets(self._id) + + # This is the UDS where all the IO goes + io_socket = attach['sockets']['io_socket'] + assert len(io_socket) <= 107,\ + 'Path length for sockets too long. {} > 107'.format( + len(io_socket) + ) + + # This is the control socket where resizing events are sent to conmon + # attach['sockets']['control_socket'] + self.pseudo_tty = collections.namedtuple( + 'PseudoTTY', + ['stdin', 'stdout', 'io_socket', 'control_socket', 'eot'])( + stdin, + stdout, + attach['sockets']['io_socket'], + attach['sockets']['control_socket'], + eot, + ) + + @property + def resize_handler(self): + """Send the new window size to conmon.""" + + def wrapped(signum, frame): + packed = fcntl.ioctl(self.pseudo_tty.stdout, termios.TIOCGWINSZ, + struct.pack('HHHH', 0, 0, 0, 0)) + rows, cols, _, _ = struct.unpack('HHHH', packed) + logging.debug('Resize window({}x{}) using {}'.format( + rows, cols, self.pseudo_tty.control_socket)) + + # TODO: Need some kind of timeout in case pipe is blocked + with open(self.pseudo_tty.control_socket, 'w') as skt: + # send conmon window resize message + skt.write('1 {} {}\n'.format(rows, cols)) + + return wrapped + + @property + def log_handler(self): + """Send command to reopen log to conmon.""" + + def wrapped(signum, frame): + with open(self.pseudo_tty.control_socket, 'w') as skt: + # send conmon reopen log message + skt.write('2\n') + + return wrapped diff --git a/contrib/python/podman/podman/libs/_containers_start.py b/contrib/python/podman/podman/libs/_containers_start.py new file mode 100644 index 000000000..ad9f32eab --- /dev/null +++ b/contrib/python/podman/podman/libs/_containers_start.py @@ -0,0 +1,82 @@ +"""Exported method Container.start().""" +import logging +import os +import select +import signal +import socket +import sys +import termios +import tty + +CONMON_BUFSZ = 8192 + + +class Mixin: + """Publish start() for inclusion in Container class.""" + + def start(self): + """Start container, return container on success. + + Will block if container has been detached. + """ + with self._client() as podman: + results = podman.StartContainer(self.id) + logging.debug('Started Container "{}"'.format( + results['container'])) + + if not hasattr(self, 'pseudo_tty') or self.pseudo_tty is None: + return self._refresh(podman) + + logging.debug('Setting up PseudoTTY for Container "{}"'.format( + results['container'])) + + try: + # save off the old settings for terminal + tcoldattr = termios.tcgetattr(self.pseudo_tty.stdin) + tty.setraw(self.pseudo_tty.stdin) + + # initialize container's window size + self.resize_handler(None, sys._getframe(0)) + + # catch any resizing events and send the resize info + # to the control fifo "socket" + signal.signal(signal.SIGWINCH, self.resize_handler) + + except termios.error: + tcoldattr = None + + try: + # TODO: Is socket.SOCK_SEQPACKET supported in Windows? + with socket.socket(socket.AF_UNIX, + socket.SOCK_SEQPACKET) as skt: + # Prepare socket for use with conmon/container + skt.connect(self.pseudo_tty.io_socket) + + sources = [skt, self.pseudo_tty.stdin] + while sources: + logging.debug('Waiting on sources: {}'.format(sources)) + readable, _, _ = select.select(sources, [], []) + + if skt in readable: + data = skt.recv(CONMON_BUFSZ) + if data: + # Remove source marker when writing + os.write(self.pseudo_tty.stdout, data[1:]) + else: + sources.remove(skt) + + if self.pseudo_tty.stdin in readable: + data = os.read(self.pseudo_tty.stdin, CONMON_BUFSZ) + if data: + skt.sendall(data) + + if self.pseudo_tty.eot in data: + sources.clear() + else: + sources.remove(self.pseudo_tty.stdin) + finally: + if tcoldattr: + termios.tcsetattr(self.pseudo_tty.stdin, termios.TCSADRAIN, + tcoldattr) + signal.signal(signal.SIGWINCH, signal.SIG_DFL) + return self._refresh(podman) diff --git a/contrib/python/podman/podman/libs/containers.py b/contrib/python/podman/podman/libs/containers.py new file mode 100644 index 000000000..6dc2c141e --- /dev/null +++ b/contrib/python/podman/podman/libs/containers.py @@ -0,0 +1,245 @@ +"""Models for manipulating containers and storage.""" +import collections +import functools +import getpass +import json +import signal +import time + +from ._containers_attach import Mixin as AttachMixin +from ._containers_start import Mixin as StartMixin + + +class Container(AttachMixin, StartMixin, collections.UserDict): + """Model for a container.""" + + def __init__(self, client, id, data): + """Construct Container Model.""" + super(Container, self).__init__(data) + + self._client = client + self._id = id + + with client() as podman: + self._refresh(podman) + + assert self._id == self.data['id'],\ + 'Requested container id({}) does not match store id({})'.format( + self._id, self.id + ) + + def __getitem__(self, key): + """Get items from parent dict.""" + return super().__getitem__(key) + + def _refresh(self, podman): + ctnr = podman.GetContainer(self._id) + super().update(ctnr['container']) + + for k, v in self.data.items(): + setattr(self, k, v) + if 'containerrunning' in self.data: + setattr(self, 'running', self.data['containerrunning']) + self.data['running'] = self.data['containerrunning'] + + return self + + def refresh(self): + """Refresh status fields for this container.""" + with self._client() as podman: + return self._refresh(podman) + + def processes(self): + """Show processes running in container.""" + with self._client() as podman: + results = podman.ListContainerProcesses(self.id) + yield from results['container'] + + def changes(self): + """Retrieve container changes.""" + with self._client() as podman: + results = podman.ListContainerChanges(self.id) + return results['container'] + + def kill(self, signal=signal.SIGTERM, wait=25): + """Send signal to container. + + default signal is signal.SIGTERM. + wait n of seconds, 0 waits forever. + """ + with self._client() as podman: + podman.KillContainer(self.id, signal) + timeout = time.time() + wait + while True: + self._refresh(podman) + if self.status != 'running': + return self + + if wait and timeout < time.time(): + raise TimeoutError() + + time.sleep(0.5) + + def _lower_hook(self): + """Convert all keys to lowercase.""" + + @functools.wraps(self._lower_hook) + def wrapped(input): + return {k.lower(): v for (k, v) in input.items()} + + return wrapped + + def inspect(self): + """Retrieve details about containers.""" + with self._client() as podman: + results = podman.InspectContainer(self.id) + obj = json.loads(results['container'], object_hook=self._lower_hook()) + return collections.namedtuple('ContainerInspect', obj.keys())(**obj) + + def export(self, target): + """Export container from store to tarball. + + TODO: should there be a compress option, like images? + """ + with self._client() as podman: + results = podman.ExportContainer(self.id, target) + return results['tarfile'] + + def commit(self, + image_name, + *args, + changes=[], + message='', + pause=True, + **kwargs): + """Create image from container. + + All changes overwrite existing values. + See inspect() to obtain current settings. + + Changes: + CMD=/usr/bin/zsh + ENTRYPOINT=/bin/sh date + ENV=TEST=test_containers.TestContainers.test_commit + EXPOSE=8888/tcp + LABEL=unittest=test_commit + USER=bozo:circus + VOLUME=/data + WORKDIR=/data/application + """ + # TODO: Clean up *args, **kwargs after Commit() is complete + try: + author = kwargs.get('author', getpass.getuser()) + except Exception: + author = '' + + for c in changes: + if c.startswith('LABEL=') and c.count('=') < 2: + raise ValueError( + 'LABEL should have the format: LABEL=label=value, not {}'. + format(c)) + + with self._client() as podman: + results = podman.Commit(self.id, image_name, changes, author, + message, pause) + return results['image'] + + def stop(self, timeout=25): + """Stop container, return id on success.""" + with self._client() as podman: + podman.StopContainer(self.id, timeout) + return self._refresh(podman) + + def remove(self, force=False): + """Remove container, return id on success. + + force=True, stop running container. + """ + with self._client() as podman: + results = podman.RemoveContainer(self.id, force) + return results['container'] + + def restart(self, timeout=25): + """Restart container with timeout, return id on success.""" + with self._client() as podman: + podman.RestartContainer(self.id, timeout) + return self._refresh(podman) + + def rename(self, target): + """Rename container, return id on success.""" + with self._client() as podman: + # TODO: Need arguments + results = podman.RenameContainer() + # TODO: fixup objects cached information + return results['container'] + + def resize_tty(self, width, height): + """Resize container tty.""" + with self._client() as podman: + # TODO: magic re: attach(), arguments + podman.ResizeContainerTty() + + def pause(self): + """Pause container, return id on success.""" + with self._client() as podman: + podman.PauseContainer(self.id) + return self._refresh(podman) + + def unpause(self): + """Unpause container, return id on success.""" + with self._client() as podman: + podman.UnpauseContainer(self.id) + return self._refresh(podman) + + def update_container(self, *args, **kwargs): + """TODO: Update container..., return id on success.""" + with self._client() as podman: + podman.UpdateContainer() + return self._refresh(podman) + + def wait(self): + """Wait for container to finish, return 'returncode'.""" + with self._client() as podman: + results = podman.WaitContainer(self.id) + return int(results['exitcode']) + + def stats(self): + """Retrieve resource stats from the container.""" + with self._client() as podman: + results = podman.GetContainerStats(self.id) + obj = results['container'] + return collections.namedtuple('StatDetail', obj.keys())(**obj) + + def logs(self, *args, **kwargs): + """Retrieve container logs.""" + with self._client() as podman: + results = podman.GetContainerLogs(self.id) + yield from results + + +class Containers(object): + """Model for Containers collection.""" + + def __init__(self, client): + """Construct model for Containers collection.""" + self._client = client + + def list(self): + """List of containers in the container store.""" + with self._client() as podman: + results = podman.ListContainers() + for cntr in results['containers']: + yield Container(self._client, cntr['id'], cntr) + + def delete_stopped(self): + """Delete all stopped containers.""" + with self._client() as podman: + results = podman.DeleteStoppedContainers() + return results['containers'] + + def get(self, id): + """Retrieve container details from store.""" + with self._client() as podman: + cntr = podman.GetContainer(id) + return Container(self._client, cntr['container']['id'], + cntr['container']) diff --git a/contrib/python/podman/podman/libs/errors.py b/contrib/python/podman/podman/libs/errors.py new file mode 100644 index 000000000..b98210481 --- /dev/null +++ b/contrib/python/podman/podman/libs/errors.py @@ -0,0 +1,65 @@ +"""Error classes and wrappers for VarlinkError.""" +from varlink import VarlinkError + + +class VarlinkErrorProxy(VarlinkError): + """Class to Proxy VarlinkError methods.""" + + def __init__(self, message, namespaced=False): + """Construct proxy from Exception.""" + super().__init__(message.as_dict(), namespaced) + self._message = message + self.__module__ = 'libpod' + + def __getattr__(self, method): + """Return attribute from proxied Exception.""" + if hasattr(self._message, method): + return getattr(self._message, method) + + try: + return self._message.parameters()[method] + except KeyError: + raise AttributeError('No such attribute: {}'.format(method)) + + +class ContainerNotFound(VarlinkErrorProxy): + """Raised when Client can not find requested container.""" + + pass + + +class ImageNotFound(VarlinkErrorProxy): + """Raised when Client can not find requested image.""" + + pass + + +class ErrorOccurred(VarlinkErrorProxy): + """Raised when an error occurs during the execution. + + See error() to see actual error text. + """ + + pass + + +class RuntimeError(VarlinkErrorProxy): + """Raised when Client fails to connect to runtime.""" + + pass + + +error_map = { + 'io.projectatomic.podman.ContainerNotFound': ContainerNotFound, + 'io.projectatomic.podman.ErrorOccurred': ErrorOccurred, + 'io.projectatomic.podman.ImageNotFound': ImageNotFound, + 'io.projectatomic.podman.RuntimeError': RuntimeError, +} + + +def error_factory(exception): + """Map Exceptions to a discrete type.""" + try: + return error_map[exception.error()](exception) + except KeyError: + return exception diff --git a/contrib/python/podman/podman/libs/images.py b/contrib/python/podman/podman/libs/images.py new file mode 100644 index 000000000..334ff873c --- /dev/null +++ b/contrib/python/podman/podman/libs/images.py @@ -0,0 +1,172 @@ +"""Models for manipulating images in/to/from storage.""" +import collections +import copy +import functools +import json +import logging + +from . import Config +from .containers import Container + + +class Image(collections.UserDict): + """Model for an Image.""" + + def __init__(self, client, id, data): + """Construct Image Model.""" + super(Image, self).__init__(data) + for k, v in data.items(): + setattr(self, k, v) + + self._id = id + self._client = client + + assert self._id == self.id,\ + 'Requested image id({}) does not match store id({})'.format( + self._id, self.id + ) + + def __getitem__(self, key): + """Get items from parent dict.""" + return super().__getitem__(key) + + def _split_token(self, values=None, sep='='): + return dict([v.split(sep, 1) for v in values if values]) + + def create(self, *args, **kwargs): + """Create container from image. + + Pulls defaults from image.inspect() + """ + details = self.inspect() + + config = Config(image_id=self.id, **kwargs) + config['command'] = details.containerconfig['cmd'] + config['env'] = self._split_token(details.containerconfig['env']) + config['image'] = copy.deepcopy(details.repotags[0]) + config['labels'] = copy.deepcopy(details.labels) + config['net_mode'] = 'bridge' + config['network'] = 'bridge' + + logging.debug('Image {}: create config: {}'.format(self.id, config)) + with self._client() as podman: + id = podman.CreateContainer(config)['container'] + cntr = podman.GetContainer(id) + return Container(self._client, id, cntr['container']) + + container = create + + def export(self, dest, compressed=False): + """Write image to dest, return id on success.""" + with self._client() as podman: + results = podman.ExportImage(self.id, dest, compressed) + return results['image'] + + def history(self): + """Retrieve image history.""" + with self._client() as podman: + for r in podman.HistoryImage(self.id)['history']: + yield collections.namedtuple('HistoryDetail', r.keys())(**r) + + # Convert all keys to lowercase. + def _lower_hook(self): + @functools.wraps(self._lower_hook) + def wrapped(input): + return {k.lower(): v for (k, v) in input.items()} + + return wrapped + + def inspect(self): + """Retrieve details about image.""" + with self._client() as podman: + results = podman.InspectImage(self.id) + obj = json.loads(results['image'], object_hook=self._lower_hook()) + return collections.namedtuple('ImageInspect', obj.keys())(**obj) + + def push(self, target, tlsverify=False): + """Copy image to target, return id on success.""" + with self._client() as podman: + results = podman.PushImage(self.id, target, tlsverify) + return results['image'] + + def remove(self, force=False): + """Delete image, return id on success. + + force=True, stop any running containers using image. + """ + with self._client() as podman: + results = podman.RemoveImage(self.id, force) + return results['image'] + + def tag(self, tag): + """Tag image.""" + with self._client() as podman: + results = podman.TagImage(self.id, tag) + return results['image'] + + +class Images(object): + """Model for Images collection.""" + + def __init__(self, client): + """Construct model for Images collection.""" + self._client = client + + def list(self): + """List all images in the libpod image store.""" + with self._client() as podman: + results = podman.ListImages() + for img in results['images']: + yield Image(self._client, img['id'], img) + + def build(self, dockerfile=None, tags=None, **kwargs): + """Build container from image. + + See podman-build.1.md for kwargs details. + """ + if dockerfile is None: + raise ValueError('"dockerfile" is a required argument.') + elif not hasattr(dockerfile, '__iter__'): + raise ValueError('"dockerfile" is required to be an iter.') + + if tags is None: + raise ValueError('"tags" is a required argument.') + elif not hasattr(tags, '__iter__'): + raise ValueError('"tags" is required to be an iter.') + + config = Config(dockerfile=dockerfile, tags=tags, **kwargs) + with self._client() as podman: + result = podman.BuildImage(config) + return self.get(result['image']['id']), \ + (line for line in result['image']['logs']) + + def delete_unused(self): + """Delete Images not associated with a container.""" + with self._client() as podman: + results = podman.DeleteUnusedImages() + return results['images'] + + def import_image(self, source, reference, message=None, changes=None): + """Read image tarball from source and save in image store.""" + with self._client() as podman: + results = podman.ImportImage(source, reference, message, changes) + return results['image'] + + def pull(self, source): + """Copy image from registry to image store.""" + with self._client() as podman: + results = podman.PullImage(source) + return results['id'] + + def search(self, id, limit=25): + """Search registries for id.""" + with self._client() as podman: + results = podman.SearchImage(id, limit) + for img in results['images']: + yield collections.namedtuple('ImageSearch', img.keys())(**img) + + def get(self, id): + """Get Image from id.""" + with self._client() as podman: + result = podman.GetImage(id) + return Image(self._client, result['image']['id'], result['image']) diff --git a/contrib/python/podman/podman/libs/system.py b/contrib/python/podman/podman/libs/system.py new file mode 100644 index 000000000..c59867760 --- /dev/null +++ b/contrib/python/podman/podman/libs/system.py @@ -0,0 +1,40 @@ +"""Models for accessing details from varlink server.""" +import collections + +import pkg_resources + +from . import cached_property + + +class System(object): + """Model for accessing system resources.""" + + def __init__(self, client): + """Construct system model.""" + self._client = client + + @cached_property + def versions(self): + """Access versions.""" + with self._client() as podman: + vers = podman.GetVersion()['version'] + + client = '0.0.0' + try: + client = pkg_resources.get_distribution('podman').version + except Exception: + pass + vers['client_version'] = client + return collections.namedtuple('Version', vers.keys())(**vers) + + def info(self): + """Return podman info.""" + with self._client() as podman: + info = podman.GetInfo()['info'] + return collections.namedtuple('Info', info.keys())(**info) + + def ping(self): + """Return True if server awake.""" + with self._client() as podman: + response = podman.Ping() + return 'OK' == response['ping']['message'] diff --git a/contrib/python/podman/podman/libs/tunnel.py b/contrib/python/podman/podman/libs/tunnel.py new file mode 100644 index 000000000..440eb3951 --- /dev/null +++ b/contrib/python/podman/podman/libs/tunnel.py @@ -0,0 +1,139 @@ +"""Cache for SSH tunnels.""" +import collections +import logging +import os +import subprocess +import threading +import time +import weakref + +Context = collections.namedtuple('Context', ( + 'uri', + 'interface', + 'local_socket', + 'remote_socket', + 'username', + 'hostname', + 'identity_file', +)) + + +class Portal(collections.MutableMapping): + """Expiring container for tunnels.""" + + def __init__(self, sweap=25): + """Construct portal, reap tunnels every sweap seconds.""" + self.data = collections.OrderedDict() + self.sweap = sweap + self.ttl = sweap * 2 + self.lock = threading.RLock() + self._schedule_reaper() + + def __getitem__(self, key): + """Given uri return tunnel and update TTL.""" + with self.lock: + value, _ = self.data[key] + self.data[key] = (value, time.time() + self.ttl) + self.data.move_to_end(key) + return value + + def __setitem__(self, key, value): + """Store given tunnel keyed with uri.""" + if not isinstance(value, Tunnel): + raise ValueError('Portals only support Tunnels.') + + with self.lock: + self.data[key] = (value, time.time() + self.ttl) + self.data.move_to_end(key) + + def __delitem__(self, key): + """Remove and close tunnel from portal.""" + with self.lock: + value, _ = self.data[key] + del self.data[key] + value.close(key) + del value + + def __iter__(self): + """Iterate tunnels.""" + with self.lock: + values = self.data.values() + + for tunnel, _ in values: + yield tunnel + + def __len__(self): + """Return number of tunnels in portal.""" + with self.lock: + return len(self.data) + + def _schedule_reaper(self): + timer = threading.Timer(interval=self.sweap, function=self.reap) + timer.setName('PortalReaper') + timer.setDaemon(True) + timer.start() + + def reap(self): + """Remove tunnels who's TTL has expired.""" + now = time.time() + with self.lock: + reaped_data = self.data.copy() + for entry in reaped_data.items(): + if entry[1][1] < now: + del self.data[entry[0]] + else: + # StopIteration as soon as possible + break + self._schedule_reaper() + + +class Tunnel(object): + """SSH tunnel.""" + + def __init__(self, context): + """Construct Tunnel.""" + self.context = context + self._tunnel = None + + def bore(self, id): + """Create SSH tunnel from given context.""" + cmd = ['ssh'] + + ssh_opts = '-fNT' + if logging.getLogger().getEffectiveLevel() == logging.DEBUG: + ssh_opts += 'v' + else: + ssh_opts += 'q' + cmd.append(ssh_opts) + + cmd.extend(('-L', '{}:{}'.format(self.context.local_socket, + self.context.remote_socket))) + if self.context.identity_file: + cmd.extend(('-i', self.context.identity_file)) + + cmd.append('ssh://{}@{}'.format(self.context.username, + self.context.hostname)) + + logging.debug('Tunnel cmd "{}"'.format(' '.join(cmd))) + + self._tunnel = subprocess.Popen(cmd, close_fds=True) + for i in range(300): + # TODO: Make timeout configurable + if os.path.exists(self.context.local_socket): + break + time.sleep(0.5) + else: + raise TimeoutError('Failed to create tunnel using: {}'.format( + ' '.join(cmd))) + weakref.finalize(self, self.close, id) + return self + + def close(self, id): + """Close SSH tunnel.""" + if self._tunnel is None: + return + + self._tunnel.kill() + self._tunnel.wait(300) + os.remove(self.context.local_socket) + self._tunnel = None diff --git a/contrib/python/podman/requirements.txt b/contrib/python/podman/requirements.txt new file mode 100644 index 000000000..d294af3c7 --- /dev/null +++ b/contrib/python/podman/requirements.txt @@ -0,0 +1,3 @@ +varlink>=26.1.0 +setuptools>=39.2.0 +python-dateutil>=2.7.3 diff --git a/contrib/python/podman/setup.py b/contrib/python/podman/setup.py new file mode 100644 index 000000000..c9db30199 --- /dev/null +++ b/contrib/python/podman/setup.py @@ -0,0 +1,38 @@ +import os + +from setuptools import find_packages, setup + + +root = os.path.abspath(os.path.dirname(__file__)) + +with open(os.path.join(root, 'README.md')) as me: + readme = me.read() + +with open(os.path.join(root, 'requirements.txt')) as r: + requirements = r.read().splitlines() + + +setup( + name='podman', + version=os.environ.get('PODMAN_VERSION', '0.0.0'), + description='A client for communicating with a Podman server', + long_description=readme, + author='Jhon Honce', + author_email='jhonce@redhat.com', + url='http://github.com/projectatomic/libpod', + license='Apache Software License', + python_requires='>=3', + include_package_data=True, + install_requires=requirements, + packages=find_packages(exclude=['test']), + zip_safe=True, + keywords='varlink libpod podman', + classifiers=[ + 'Development Status :: 3 - Alpha', + 'Intended Audience :: Developers', + 'Topic :: Software Development', + 'License :: OSI Approved :: Apache Software License', + 'Programming Language :: Python :: 3.6', + ]) +# Not supported +# long_description_content_type='text/markdown', diff --git a/contrib/python/podman/test/__init__.py b/contrib/python/podman/test/__init__.py new file mode 100644 index 000000000..e69de29bb diff --git a/contrib/python/podman/test/podman_testcase.py b/contrib/python/podman/test/podman_testcase.py new file mode 100644 index 000000000..f96a3a013 --- /dev/null +++ b/contrib/python/podman/test/podman_testcase.py @@ -0,0 +1,107 @@ +import contextlib +import functools +import itertools +import os +import subprocess +import time +import unittest + +from varlink import VarlinkError + +MethodNotImplemented = 'org.varlink.service.MethodNotImplemented' + + +class PodmanTestCase(unittest.TestCase): + """Hide the sausage making of initializing storage.""" + + @classmethod + def setUpClass(cls): + if hasattr(PodmanTestCase, 'alpine_process'): + PodmanTestCase.tearDownClass() + + def run_cmd(*args): + cmd = list(itertools.chain(*args)) + try: + pid = subprocess.Popen( + cmd, + close_fds=True, + stdout=subprocess.PIPE, + stderr=subprocess.PIPE) + out, err = pid.communicate() + except OSError as e: + print('{}: {}({})'.format(cmd, e.strerror, e.returncode)) + except ValueError as e: + print('{}: {}'.format(cmd, e.message)) + raise + else: + return out.strip() + + tmpdir = os.environ.get('TMPDIR', '/tmp') + podman_args = [ + '--storage-driver=vfs', + '--root={}/crio'.format(tmpdir), + '--runroot={}/crio-run'.format(tmpdir), + '--cni-config-dir={}/cni/net.d'.format(tmpdir), + ] + + run_podman = functools.partial(run_cmd, ['podman'], podman_args) + + id = run_podman(['pull', 'alpine']) + setattr(PodmanTestCase, 'alpine_id', id) + + run_podman(['pull', 'busybox']) + run_podman(['images']) + + run_cmd(['rm', '-f', '{}/alpine_gold.tar'.format(tmpdir)]) + run_podman([ + 'save', '--output', '{}/alpine_gold.tar'.format(tmpdir), 'alpine' + ]) + + PodmanTestCase.alpine_log = open( + os.path.join('/tmp/', 'alpine.log'), 'w') + + cmd = ['podman'] + cmd.extend(podman_args) + # cmd.extend(['run', '-d', 'alpine', 'sleep', '500']) + cmd.extend(['run', '-dt', 'alpine', '/bin/sh']) + PodmanTestCase.alpine_process = subprocess.Popen( + cmd, + stdout=PodmanTestCase.alpine_log, + stderr=subprocess.STDOUT, + ) + + PodmanTestCase.busybox_log = open( + os.path.join('/tmp/', 'busybox.log'), 'w') + + cmd = ['podman'] + cmd.extend(podman_args) + cmd.extend(['create', 'busybox']) + PodmanTestCase.busybox_process = subprocess.Popen( + cmd, + stdout=PodmanTestCase.busybox_log, + stderr=subprocess.STDOUT, + ) + # give podman time to start ctnr + time.sleep(2) + + # Close our handle of file + PodmanTestCase.alpine_log.close() + PodmanTestCase.busybox_log.close() + + @classmethod + def tearDownClass(cls): + try: + PodmanTestCase.alpine_process.kill() + assert 0 == PodmanTestCase.alpine_process.wait(500) + delattr(PodmanTestCase, 'alpine_process') + + PodmanTestCase.busybox_process.kill() + assert 0 == PodmanTestCase.busybox_process.wait(500) + except Exception as e: + print('Exception: {}'.format(e)) + raise + + @contextlib.contextmanager + def assertRaisesNotImplemented(self): + with self.assertRaisesRegex(VarlinkError, MethodNotImplemented): + yield diff --git a/contrib/python/podman/test/test_client.py b/contrib/python/podman/test/test_client.py new file mode 100644 index 000000000..2abc60a24 --- /dev/null +++ b/contrib/python/podman/test/test_client.py @@ -0,0 +1,35 @@ +from __future__ import absolute_import + +import unittest +from unittest.mock import patch + +import podman +from podman.client import BaseClient, Client, LocalClient, RemoteClient + + +class TestClient(unittest.TestCase): + def setUp(self): + pass + + @patch('podman.libs.system.System.ping', return_value=True) + def test_local(self, mock_ping): + p = Client( + uri='unix:/run/podman', + interface='io.projectatomic.podman', + ) + + self.assertIsInstance(p._client, LocalClient) + self.assertIsInstance(p._client, BaseClient) + + mock_ping.assert_called_once_with() + + @patch('podman.libs.system.System.ping', return_value=True) + def test_remote(self, mock_ping): + p = Client( + uri='unix:/run/podman', + interface='io.projectatomic.podman', + remote_uri='ssh://user@hostname/run/podmain/podman', + identity_file='~/.ssh/id_rsa') + + self.assertIsInstance(p._client, BaseClient) + mock_ping.assert_called_once_with() diff --git a/contrib/python/podman/test/test_containers.py b/contrib/python/podman/test/test_containers.py new file mode 100644 index 000000000..ec2dcde03 --- /dev/null +++ b/contrib/python/podman/test/test_containers.py @@ -0,0 +1,234 @@ +import os +import signal +import unittest +from test.podman_testcase import PodmanTestCase + +import podman + + +class TestContainers(PodmanTestCase): + @classmethod + def setUpClass(cls): + super().setUpClass() + + @classmethod + def tearDownClass(cls): + super().tearDownClass() + + def setUp(self): + self.tmpdir = os.environ['TMPDIR'] + self.host = os.environ['PODMAN_HOST'] + + self.pclient = podman.Client(self.host) + self.loadCache() + + def tearDown(self): + pass + + def loadCache(self): + self.containers = list(self.pclient.containers.list()) + + self.alpine_ctnr = next( + iter([c for c in self.containers if 'alpine' in c['image']] or []), + None) + + if self.alpine_ctnr and self.alpine_ctnr.status != 'running': + self.alpine_ctnr.start() + + def test_list(self): + self.assertGreaterEqual(len(self.containers), 2) + self.assertIsNotNone(self.alpine_ctnr) + self.assertIn('alpine', self.alpine_ctnr.image) + + def test_delete_stopped(self): + before = len(self.containers) + + self.alpine_ctnr.stop() + target = self.alpine_ctnr.id + actual = self.pclient.containers.delete_stopped() + self.assertIn(target, actual) + + self.loadCache() + after = len(self.containers) + + self.assertLess(after, before) + TestContainers.setUpClass() + + def test_get(self): + actual = self.pclient.containers.get(self.alpine_ctnr.id) + for k in ['id', 'status', 'ports']: + self.assertEqual(actual[k], self.alpine_ctnr[k]) + + with self.assertRaises(podman.ContainerNotFound): + self.pclient.containers.get("bozo") + + def test_attach(self): + # StringIO does not support fileno() so we had to go old school + input = os.path.join(self.tmpdir, 'test_attach.stdin') + output = os.path.join(self.tmpdir, 'test_attach.stdout') + + with open(input, 'w+') as mock_in, open(output, 'w+') as mock_out: + # double quote is indeed in the expected place + mock_in.write('echo H"ello, World"; exit\n') + mock_in.seek(0, 0) + + ctnr = self.pclient.images.get(self.alpine_ctnr.image).container( + detach=True, tty=True) + ctnr.attach(stdin=mock_in.fileno(), stdout=mock_out.fileno()) + ctnr.start() + + mock_out.flush() + mock_out.seek(0, 0) + output = mock_out.read() + self.assertIn('Hello', output) + + ctnr.remove(force=True) + + def test_processes(self): + actual = list(self.alpine_ctnr.processes()) + self.assertGreaterEqual(len(actual), 2) + + def test_start_stop_wait(self): + ctnr = self.alpine_ctnr.stop() + self.assertFalse(ctnr['running']) + + ctnr.start() + self.assertTrue(ctnr.running) + + ctnr.stop() + self.assertFalse(ctnr['containerrunning']) + + actual = ctnr.wait() + self.assertGreaterEqual(actual, 0) + + def test_changes(self): + actual = self.alpine_ctnr.changes() + + self.assertListEqual( + sorted(['changed', 'added', 'deleted']), sorted( + list(actual.keys()))) + + # TODO: brittle, depends on knowing history of ctnr + self.assertGreaterEqual(len(actual['changed']), 2) + self.assertGreaterEqual(len(actual['added']), 3) + self.assertEqual(len(actual['deleted']), 0) + + def test_kill(self): + self.assertTrue(self.alpine_ctnr.running) + ctnr = self.alpine_ctnr.kill(signal.SIGKILL) + self.assertFalse(ctnr.running) + + def test_inspect(self): + actual = self.alpine_ctnr.inspect() + self.assertEqual(actual.id, self.alpine_ctnr.id) + # TODO: Datetime values from inspect missing offset in CI instance + # self.assertEqual( + # datetime_parse(actual.created), + # datetime_parse(self.alpine_ctnr.createdat)) + + def test_export(self): + target = os.path.join(self.tmpdir, 'alpine_export_ctnr.tar') + + actual = self.alpine_ctnr.export(target) + self.assertEqual(actual, target) + self.assertTrue(os.path.isfile(target)) + self.assertGreater(os.path.getsize(target), 0) + + def test_commit(self): + # TODO: Test for STOPSIGNAL when supported by OCI + # TODO: Test for message when supported by OCI + details = self.pclient.images.get(self.alpine_ctnr.image).inspect() + changes = ['ENV=' + i for i in details.containerconfig['env']] + changes.append('CMD=/usr/bin/zsh') + changes.append('ENTRYPOINT=/bin/sh date') + changes.append('ENV=TEST=test_containers.TestContainers.test_commit') + changes.append('EXPOSE=80') + changes.append('EXPOSE=8888') + changes.append('LABEL=unittest=test_commit') + changes.append('USER=bozo:circus') + changes.append('VOLUME=/data') + changes.append('WORKDIR=/data/application') + + id = self.alpine_ctnr.commit( + 'alpine3', author='Bozo the clown', changes=changes, pause=True) + img = self.pclient.images.get(id) + self.assertIsNotNone(img) + + details = img.inspect() + self.assertEqual(details.author, 'Bozo the clown') + self.assertListEqual(['/usr/bin/zsh'], details.containerconfig['cmd']) + self.assertListEqual(['/bin/sh date'], + details.containerconfig['entrypoint']) + self.assertIn('TEST=test_containers.TestContainers.test_commit', + details.containerconfig['env']) + self.assertTrue( + [e for e in details.containerconfig['env'] if 'PATH=' in e]) + self.assertDictEqual({ + '80': {}, + '8888': {}, + }, details.containerconfig['exposedports']) + self.assertDictEqual({'unittest': 'test_commit'}, details.labels) + self.assertEqual('bozo:circus', details.containerconfig['user']) + self.assertEqual({'/data': {}}, details.containerconfig['volumes']) + self.assertEqual('/data/application', + details.containerconfig['workingdir']) + + def test_remove(self): + before = len(self.containers) + + with self.assertRaises(podman.ErrorOccurred): + self.alpine_ctnr.remove() + + self.assertEqual( + self.alpine_ctnr.id, self.alpine_ctnr.remove(force=True)) + self.loadCache() + after = len(self.containers) + + self.assertLess(after, before) + TestContainers.setUpClass() + + def test_restart(self): + self.assertTrue(self.alpine_ctnr.running) + before = self.alpine_ctnr.runningfor + + ctnr = self.alpine_ctnr.restart() + self.assertTrue(ctnr.running) + + after = self.alpine_ctnr.runningfor + + # TODO: restore check when restart zeros counter + # self.assertLess(after, before) + + def test_rename(self): + with self.assertRaisesNotImplemented(): + self.alpine_ctnr.rename('new_alpine') + + def test_resize_tty(self): + with self.assertRaisesNotImplemented(): + self.alpine_ctnr.resize_tty(132, 43) + + def test_pause_unpause(self): + self.assertTrue(self.alpine_ctnr.running) + + ctnr = self.alpine_ctnr.pause() + self.assertEqual(ctnr.status, 'paused') + + ctnr = self.alpine_ctnr.unpause() + self.assertTrue(ctnr.running) + self.assertTrue(ctnr.status, 'running') + + def test_stats(self): + self.assertTrue(self.alpine_ctnr.running) + + actual = self.alpine_ctnr.stats() + self.assertEqual(self.alpine_ctnr.id, actual.id) + self.assertEqual(self.alpine_ctnr.names, actual.name) + + def test_logs(self): + self.assertTrue(self.alpine_ctnr.running) + actual = list(self.alpine_ctnr.logs()) + self.assertIsNotNone(actual) + + +if __name__ == '__main__': + unittest.main() diff --git a/contrib/python/podman/test/test_images.py b/contrib/python/podman/test/test_images.py new file mode 100644 index 000000000..14bf90992 --- /dev/null +++ b/contrib/python/podman/test/test_images.py @@ -0,0 +1,172 @@ +import itertools +import os +import unittest +from collections import Counter +from datetime import datetime, timezone +from test.podman_testcase import PodmanTestCase + +import podman + + +class TestImages(PodmanTestCase): + @classmethod + def setUpClass(cls): + super().setUpClass() + + @classmethod + def tearDownClass(cls): + super().tearDownClass() + + def setUp(self): + self.tmpdir = os.environ['TMPDIR'] + self.host = os.environ['PODMAN_HOST'] + + self.pclient = podman.Client(self.host) + self.images = self.loadCache() + + def tearDown(self): + pass + + def loadCache(self): + with podman.Client(self.host) as pclient: + self.images = list(pclient.images.list()) + + self.alpine_image = next( + iter([ + i for i in self.images + if 'docker.io/library/alpine:latest' in i['repoTags'] + ] or []), None) + + return self.images + + def test_list(self): + actual = self.loadCache() + self.assertGreaterEqual(len(actual), 2) + self.assertIsNotNone(self.alpine_image) + + def test_build(self): + path = os.path.join(self.tmpdir, 'ctnr', 'Dockerfile') + img, logs = self.pclient.images.build( + dockerfile=[path], + tags=['alpine-unittest'], + ) + self.assertIsNotNone(img) + self.assertIn('localhost/alpine-unittest:latest', img.repoTags) + self.assertLess( + podman.datetime_parse(img.created), datetime.now(timezone.utc)) + self.assertTrue(logs) + + def test_create(self): + img_details = self.alpine_image.inspect() + + actual = self.alpine_image.container() + self.assertIsNotNone(actual) + self.assertEqual(actual.status, 'configured') + + ctnr = actual.start() + self.assertIn(ctnr.status, ['running', 'exited']) + + ctnr_details = ctnr.inspect() + for e in img_details.containerconfig['env']: + self.assertIn(e, ctnr_details.config['env']) + + def test_export(self): + path = os.path.join(self.tmpdir, 'alpine_export.tar') + target = 'oci-archive:{}:latest'.format(path) + + actual = self.alpine_image.export(target, False) + self.assertTrue(actual) + self.assertTrue(os.path.isfile(path)) + + def test_get(self): + actual = self.pclient.images.get(self.alpine_image.id) + self.assertEqual(actual, self.alpine_image) + + def test_history(self): + records = [] + bucket = Counter() + for record in self.alpine_image.history(): + self.assertIn(record.id, (self.alpine_image.id, '')) + bucket[record.id] += 1 + records.append(record) + + self.assertGreater(bucket[self.alpine_image.id], 0) + self.assertEqual(sum(bucket.values()), len(records)) + + def test_inspect(self): + actual = self.alpine_image.inspect() + self.assertEqual(actual.id, self.alpine_image.id) + + def test_push(self): + path = '{}/alpine_push'.format(self.tmpdir) + target = 'dir:{}'.format(path) + self.alpine_image.push(target) + + self.assertTrue(os.path.isfile(os.path.join(path, 'manifest.json'))) + self.assertTrue(os.path.isfile(os.path.join(path, 'version'))) + + def test_tag(self): + self.assertEqual(self.alpine_image.id, + self.alpine_image.tag('alpine:fubar')) + self.loadCache() + self.assertIn('alpine:fubar', self.alpine_image.repoTags) + + def test_remove(self): + before = self.loadCache() + + # assertRaises doesn't follow the import name :( + with self.assertRaises(podman.ErrorOccurred): + self.alpine_image.remove() + + actual = self.alpine_image.remove(force=True) + self.assertEqual(self.alpine_image.id, actual) + after = self.loadCache() + + self.assertLess(len(after), len(before)) + TestImages.setUpClass() + self.loadCache() + + def test_import_delete_unused(self): + before = self.loadCache() + # create unused image, so we have something to delete + source = os.path.join(self.tmpdir, 'alpine_gold.tar') + new_img = self.pclient.images.import_image( + source, + 'alpine2:latest', + 'unittest.test_import', + ) + after = self.loadCache() + + self.assertEqual(len(before) + 1, len(after)) + self.assertIsNotNone( + next(iter([i for i in after if new_img in i['id']] or []), None)) + + actual = self.pclient.images.delete_unused() + self.assertIn(new_img, actual) + + after = self.loadCache() + self.assertGreaterEqual(len(before), len(after)) + + TestImages.setUpClass() + self.loadCache() + + def test_pull(self): + before = self.loadCache() + actual = self.pclient.images.pull('prom/busybox:latest') + after = self.loadCache() + + self.assertEqual(len(before) + 1, len(after)) + self.assertIsNotNone( + next(iter([i for i in after if actual in i['id']] or []), None)) + + def test_search(self): + actual = self.pclient.images.search('alpine', 25) + names, length = itertools.tee(actual) + + for img in names: + self.assertIn('alpine', img.name) + self.assertTrue(0 < len(list(length)) <= 25) + + +if __name__ == '__main__': + unittest.main() diff --git a/contrib/python/podman/test/test_libs.py b/contrib/python/podman/test/test_libs.py new file mode 100644 index 000000000..202bed1d8 --- /dev/null +++ b/contrib/python/podman/test/test_libs.py @@ -0,0 +1,53 @@ +import datetime +import unittest + +import podman + + +class TestLibs(unittest.TestCase): + def setUp(self): + pass + + def tearDown(self): + pass + + def test_parse(self): + expected = datetime.datetime.strptime( + '2018-05-08T14:12:53.797795-0700', '%Y-%m-%dT%H:%M:%S.%f%z') + for v in [ + '2018-05-08T14:12:53.797795191-07:00', + '2018-05-08T14:12:53.797795-07:00', + '2018-05-08T14:12:53.797795-0700', + '2018-05-08 14:12:53.797795191 -0700 MST', + ]: + actual = podman.datetime_parse(v) + self.assertEqual(actual, expected) + + expected = datetime.datetime.strptime( + '2018-05-08T14:12:53.797795-0000', '%Y-%m-%dT%H:%M:%S.%f%z') + for v in [ + '2018-05-08T14:12:53.797795191Z', + '2018-05-08T14:12:53.797795191z', + ]: + actual = podman.datetime_parse(v) + self.assertEqual(actual, expected) + + actual = podman.datetime_parse(datetime.datetime.now().isoformat()) + self.assertIsNotNone(actual) + + def test_parse_fail(self): + for v in [ + 'There is no time here.', + ]: + with self.assertRaises(ValueError): + podman.datetime_parse(v) + + def test_format(self): + expected = '2018-05-08T18:24:52.753227-07:00' + dt = podman.datetime_parse(expected) + actual = podman.datetime_format(dt) + self.assertEqual(actual, expected) + + +if __name__ == '__main__': + unittest.main() diff --git a/contrib/python/podman/test/test_runner.sh b/contrib/python/podman/test/test_runner.sh new file mode 100755 index 000000000..b3d2ba15b --- /dev/null +++ b/contrib/python/podman/test/test_runner.sh @@ -0,0 +1,141 @@ +#!/bin/bash + +# podman needs to play some games with resources +if [[ $(id -u) != 0 ]]; then + echo >&2 $0 must be run as root. + exit 2 +fi + +# setup path to find new binaries _NOT_ system binaries +if [[ ! -x ../../../bin/podman ]]; then + echo 1>&2 Cannot find podman binary from libpod root directory. Run \"make binaries\" + exit 1 +fi +export PATH=../../../bin:$PATH + +function usage { + echo 1>&2 $0 [-v] [-h] [test.TestCase|test.TestCase.step] +} + +while getopts "vh" arg; do + case $arg in + v ) VERBOSE='-v' ;; + h ) usage ; exit 0;; + \? ) usage ; exit 2;; + esac +done +shift $((OPTIND -1)) + +function cleanup { + # aggressive cleanup as tests may crash leaving crap around + umount '^(shm|nsfs)' + umount '\/run\/netns' + rm -r "$1" +} + +# Create temporary directory for storage +export TMPDIR=`mktemp -d /tmp/podman.XXXXXXXXXX` +trap "cleanup $TMPDIR" EXIT + +function umount { + # xargs -r always ran once, so write any mount points to file first + mount |awk "/$1/"' { print $3 }' >${TMPDIR}/mounts + if [[ -s ${TMPDIR}/mounts ]]; then + xargs <${TMPDIR}/mounts -t umount + fi +} + +function showlog { + [[ -s $1 ]] && cat <<-EOT +$1 ===== +$(cat "$1") + +EOT +} + +# Need locations to store stuff +mkdir -p ${TMPDIR}/{podman,crio,crio-run,cni/net.d,ctnr,tunnel} + +# Cannot be done in python unittest fixtures. EnvVar not picked up. +export REGISTRIES_CONFIG_PATH=${TMPDIR}/registry.conf +cat >$REGISTRIES_CONFIG_PATH <<-EOT + [registries.search] + registries = ['docker.io'] + [registries.insecure] + registries = [] + [registries.block] + registries = [] +EOT + +export CNI_CONFIG_PATH=${TMPDIR}/cni/net.d +cat >$CNI_CONFIG_PATH/87-podman-bridge.conflist <<-EOT +{ + "cniVersion": "0.3.0", + "name": "podman", + "plugins": [{ + "type": "bridge", + "bridge": "cni0", + "isGateway": true, + "ipMasq": true, + "ipam": { + "type": "host-local", + "subnet": "10.88.0.0/16", + "routes": [{ + "dst": "0.0.0.0/0" + }] + } + }, + { + "type": "portmap", + "capabilities": { + "portMappings": true + } + } + ] +} +EOT + +cat >$TMPDIR/ctnr/hello.sh <<-EOT +echo 'Hello, World' +EOT + +cat >$TMPDIR/ctnr/Dockerfile <<-EOT +FROM alpine:latest +COPY ./hello.sh /tmp/hello.sh +RUN chmod 755 /tmp/hello.sh +ENTRYPOINT ["/tmp/hello.sh"] +EOT + +export PODMAN_HOST="unix:${TMPDIR}/podman/io.projectatomic.podman" +PODMAN_ARGS="--storage-driver=vfs \ + --root=${TMPDIR}/crio \ + --runroot=${TMPDIR}/crio-run \ + --cni-config-dir=$CNI_CONFIG_PATH \ + " +if [[ -n $VERBOSE ]]; then + PODMAN_ARGS="$PODMAN_ARGS --log-level=debug" +fi +PODMAN="podman $PODMAN_ARGS" + +# document what we're about to do... +$PODMAN --version + +set -x +# Run podman in background without systemd for test purposes +$PODMAN varlink --timeout=0 ${PODMAN_HOST} >/tmp/test_runner.output 2>&1 & + +if [[ -z $1 ]]; then + export PYTHONPATH=. + python3 -m unittest discover -s . $VERBOSE +else + export PYTHONPATH=.:./test + python3 -m unittest $1 $VERBOSE +fi + +set +x +pkill -9 podman +pkill -9 conmon + +showlog /tmp/test_runner.output +showlog /tmp/alpine.log +showlog /tmp/busybox.log diff --git a/contrib/python/podman/test/test_system.py b/contrib/python/podman/test/test_system.py new file mode 100644 index 000000000..3f6ca57a2 --- /dev/null +++ b/contrib/python/podman/test/test_system.py @@ -0,0 +1,62 @@ +import os +import unittest +from urllib.parse import urlparse + +import podman +import varlink + + +class TestSystem(unittest.TestCase): + def setUp(self): + self.host = os.environ['PODMAN_HOST'] + self.tmpdir = os.environ['TMPDIR'] + + def tearDown(self): + pass + + def test_bad_address(self): + with self.assertRaisesRegex(varlink.client.ConnectionError, + "Invalid address 'bad address'"): + podman.Client('bad address') + + def test_ping(self): + with podman.Client(self.host) as pclient: + self.assertTrue(pclient.system.ping()) + + def test_remote_ping(self): + host = urlparse(self.host) + remote_uri = 'ssh://root@localhost/{}'.format(host.path) + + local_uri = 'unix:{}/tunnel/podman.sock'.format(self.tmpdir) + with podman.Client( + uri=local_uri, + remote_uri=remote_uri, + identity_file=os.path.expanduser('~/.ssh/id_rsa'), + ) as remote_client: + remote_client.system.ping() + + def test_versions(self): + with podman.Client(self.host) as pclient: + # Values change with each build so we cannot test too much + self.assertListEqual( + sorted([ + 'built', 'client_version', 'git_commit', 'go_version', + 'os_arch', 'version' + ]), sorted(list(pclient.system.versions._fields))) + pclient.system.versions + self.assertIsNot(podman.__version__, '0.0.0') + + def test_info(self): + with podman.Client(self.host) as pclient: + actual = pclient.system.info() + # Values change too much to do exhaustive testing + self.assertIsNotNone(actual.podman['go_version']) + self.assertListEqual( + sorted([ + 'host', 'insecure_registries', 'podman', 'registries', + 'store' + ]), sorted(list(actual._fields))) + + +if __name__ == '__main__': + unittest.main() diff --git a/contrib/python/podman/test/test_tunnel.py b/contrib/python/podman/test/test_tunnel.py new file mode 100644 index 000000000..719a2f9a4 --- /dev/null +++ b/contrib/python/podman/test/test_tunnel.py @@ -0,0 +1,79 @@ +from __future__ import absolute_import + +import time +import unittest +from unittest.mock import MagicMock, patch + +import podman +from podman.libs.tunnel import Context, Portal, Tunnel + + +class TestTunnel(unittest.TestCase): + def setUp(self): + self.tunnel_01 = MagicMock(spec=Tunnel) + self.tunnel_02 = MagicMock(spec=Tunnel) + + def test_portal_ops(self): + portal = Portal(sweap=500) + portal['unix:/01'] = self.tunnel_01 + portal['unix:/02'] = self.tunnel_02 + + self.assertEqual(portal.get('unix:/01'), self.tunnel_01) + self.assertEqual(portal.get('unix:/02'), self.tunnel_02) + + del portal['unix:/02'] + with self.assertRaises(KeyError): + portal['unix:/02'] + self.assertEqual(len(portal), 1) + + def test_portal_reaping(self): + portal = Portal(sweap=0.5) + portal['unix:/01'] = self.tunnel_01 + portal['unix:/02'] = self.tunnel_02 + + self.assertEqual(len(portal), 2) + for entry in portal: + self.assertIn(entry, (self.tunnel_01, self.tunnel_02)) + + time.sleep(1) + portal.reap() + self.assertEqual(len(portal), 0) + + def test_portal_no_reaping(self): + portal = Portal(sweap=500) + portal['unix:/01'] = self.tunnel_01 + portal['unix:/02'] = self.tunnel_02 + + portal.reap() + self.assertEqual(len(portal), 2) + for entry in portal: + self.assertIn(entry, (self.tunnel_01, self.tunnel_02)) + + @patch('subprocess.Popen') + @patch('os.path.exists', return_value=True) + @patch('weakref.finalize') + def test_tunnel(self, mock_finalize, mock_exists, mock_Popen): + context = Context( + 'unix:/01', + 'io.projectatomic.podman', + '/tmp/user/socket', + '/run/podman/socket', + 'user', + 'hostname', + '~/.ssh/id_rsa', + ) + tunnel = Tunnel(context).bore('unix:/01') + + cmd = [ + 'ssh', + '-fNTq', + '-L', + '{}:{}'.format(context.local_socket, context.remote_socket), + '-i', + context.identity_file, + 'ssh://{}@{}'.format(context.username, context.hostname), + ] + + mock_finalize.assert_called_once_with(tunnel, tunnel.close, 'unix:/01') + mock_exists.assert_called_once_with(context.local_socket) + mock_Popen.assert_called_once_with(cmd, close_fds=True) diff --git a/contrib/python/pypodman/MANIFEST.in b/contrib/python/pypodman/MANIFEST.in new file mode 100644 index 000000000..bb3ec5f0d --- /dev/null +++ b/contrib/python/pypodman/MANIFEST.in @@ -0,0 +1 @@ +include README.md diff --git a/contrib/python/pypodman/Makefile b/contrib/python/pypodman/Makefile new file mode 100644 index 000000000..4d76b1a1e --- /dev/null +++ b/contrib/python/pypodman/Makefile @@ -0,0 +1,21 @@ +PYTHON ?= /usr/bin/python3 + +.PHONY: python-pypodman +python-pypodman: + $(PYTHON) setup.py bdist + +.PHONY: integration +integration: + true + +.PHONY: install +install: + $(PYTHON) setup.py install --user + +.PHONY: clean +clean: + $(PYTHON) setup.py clean --all + pip3 uninstall pypodman ||: + rm -rf pypodman.egg-info dist + find . -depth -name __pycache__ -exec rm -rf {} \; + find . -depth -name \*.pyc -exec rm -f {} \; diff --git a/contrib/python/pypodman/README.md b/contrib/python/pypodman/README.md new file mode 100644 index 000000000..8a1c293f1 --- /dev/null +++ b/contrib/python/pypodman/README.md @@ -0,0 +1,32 @@ +# pypodman - CLI interface for podman written in python + +## Status: Active Development + +See [libpod](https://github.com/projectatomic/libpod/contrib/python/cmd) + +## Releases + +To build the pypodman egg: + +```sh +cd ~/libpod/contrib/python/cmd +python3 setup.py clean -a && python3 setup.py bdist +``` + +## Running command: + +### Against local podman service +```sh +$ pypodman images +``` +### Against remote podman service +```sh +$ pypodman --host node001.example.org images +``` +### Full help system available +```sh +$ pypodman -h +``` +```sh +$ pypodman images -h +``` diff --git a/contrib/python/pypodman/docs/pypodman.1.md b/contrib/python/pypodman/docs/pypodman.1.md new file mode 100644 index 000000000..1a6be994d --- /dev/null +++ b/contrib/python/pypodman/docs/pypodman.1.md @@ -0,0 +1,82 @@ +% pypodman "1" + +## NAME + +pypodman - Simple management tool for containers and images + +## SYNOPSIS + +**pypodman** [*global options*] _command_ [*options*] + +## DESCRIPTION + +pypodman is a simple client only tool to help with debugging issues when daemons +such as CRI runtime and the kubelet are not responding or failing. pypodman uses +a VarLink API to commicate with a podman service running on either the local or +remote machine. pypodman uses ssh to create secure tunnels when communicating +with a remote service. + +## GLOBAL OPTIONS + +**--help, -h** + +Print usage statement. + +**--version** + +Print program version number and exit. + +**--config-home** + +Directory that will be namespaced with `pypodman` to hold `pypodman.conf`. See FILES below for more details. + +**--log-level** + +Log events above specified level: DEBUG, INFO, WARNING (default), ERROR, or CRITICAL. + +**--run-dir** + +Directory that will be namespaced with `pypodman` to hold local socket bindings. The default is ``$XDG_RUNTIME_DIR\`. + +**--user** + +Authenicating user on remote host. `pypodman` defaults to the logged in user. + +**--host** + +Name of remote host. There is no default, if not given `pypodman` attempts to connect to `--remote-socket-path` on local host. + +**--remote-socket-path** + +Path on remote host for podman service's `AF_UNIX` socket. The default is `/run/podman/io.projectatomic.podman`. + +**--identity-file** + +The optional `ssh` identity file to authenicate when tunnelling to remote host. Default is None and will allow `ssh` to follow it's default methods for resolving the identity and private key using the logged in user. + +## COMMANDS + +See [podman(1)](podman.1.md) + +## FILES + +**pypodman/pypodman.conf** (`Any element of XDG_CONFIG_DIRS` and/or `XDG_CONFIG_HOME` and/or **--config-home**) + +pypodman.conf is one or more configuration files for running the pypodman command. pypodman.conf is a TOML file with the stanza `[default]`, with a map of option: value. + +pypodman follows the XDG (freedesktop.org) conventions for resolving it's configuration. The list below are read from top to bottom with later items overwriting earlier. Any missing items are ignored. + +- `pypodman/pypodman.conf` from any path element in `XDG_CONFIG_DIRS` or `\etc\xdg` +- `XDG_CONFIG_HOME` or $HOME/.config + `pypodman/pypodman.conf` +- From `--config-home` command line option + `pypodman/pypodman.conf` +- From environment variable, for example: RUN_DIR +- From command line option, for example: --run-dir + +This should provide Operators the ability to setup basic configurations and allow users to customize them. + +**XDG_RUNTIME_DIR** (`XDG_RUNTIME_DIR/io.projectatomic.podman`) + +Directory where pypodman stores non-essential runtime files and other file objects (such as sockets, named pipes, ...). + +## SEE ALSO +`podman(1)`, `libpod(8)` diff --git a/contrib/python/pypodman/lib/__init__.py b/contrib/python/pypodman/lib/__init__.py new file mode 100644 index 000000000..5a8303668 --- /dev/null +++ b/contrib/python/pypodman/lib/__init__.py @@ -0,0 +1,11 @@ +"""Remote podman client support library.""" +from .action_base import AbstractActionBase +from .config import PodmanArgumentParser +from .report import Report, ReportColumn + +__all__ = [ + 'AbstractActionBase', + 'PodmanArgumentParser', + 'Report', + 'ReportColumn', +] diff --git a/contrib/python/pypodman/lib/action_base.py b/contrib/python/pypodman/lib/action_base.py new file mode 100644 index 000000000..ff2922262 --- /dev/null +++ b/contrib/python/pypodman/lib/action_base.py @@ -0,0 +1,84 @@ +"""Base class for all actions of remote client.""" +import abc +from functools import lru_cache + +import podman + + +class AbstractActionBase(abc.ABC): + """Base class for all actions of remote client.""" + + @classmethod + @abc.abstractmethod + def subparser(cls, parser): + """Define parser for this action. Subclasses must implement. + + API: + Use set_defaults() to set attributes "class_" and "method". These will + be invoked as class_(parsed_args).method() + """ + parser.add_argument( + '--all', + action='store_true', + help=('list all items.' + ' (default: no-op, included for compatibility.)')) + parser.add_argument( + '--no-trunc', + '--notruncate', + action='store_false', + dest='truncate', + default=True, + help='Display extended information. (default: False)') + parser.add_argument( + '--noheading', + action='store_false', + dest='heading', + default=True, + help=('Omit the table headings from the output.' + ' (default: False)')) + parser.add_argument( + '--quiet', + action='store_true', + help='List only the IDs. (default: %(default)s)') + + def __init__(self, args): + """Construct class.""" + self._args = args + + @property + def remote_uri(self): + """URI for remote side of connection.""" + return self._args.remote_uri + + @property + def local_uri(self): + """URI for local side of connection.""" + return self._args.local_uri + + @property + def identity_file(self): + """Key for authenication.""" + return self._args.identity_file + + @property + @lru_cache(maxsize=1) + def client(self): + """Podman remote client for communicating.""" + if self._args.host is None: + return podman.Client( + uri=self.local_uri) + else: + return podman.Client( + uri=self.local_uri, + remote_uri=self.remote_uri, + identity_file=self.identity_file) + + def __repr__(self): + """Compute the “official” string representation of object.""" + return ("{}(local_uri='{}', remote_uri='{}'," + " identity_file='{}')").format( + self.__class__, + self.local_uri, + self.remote_uri, + self.identity_file, + ) diff --git a/contrib/python/pypodman/lib/actions/__init__.py b/contrib/python/pypodman/lib/actions/__init__.py new file mode 100644 index 000000000..cdc58b6ab --- /dev/null +++ b/contrib/python/pypodman/lib/actions/__init__.py @@ -0,0 +1,7 @@ +"""Module to export all the podman subcommands.""" +from .images_action import Images +from .ps_action import Ps +from .rm_action import Rm +from .rmi_action import Rmi + +__all__ = ['Images', 'Ps', 'Rm', 'Rmi'] diff --git a/contrib/python/pypodman/lib/actions/images_action.py b/contrib/python/pypodman/lib/actions/images_action.py new file mode 100644 index 000000000..f6a7497e5 --- /dev/null +++ b/contrib/python/pypodman/lib/actions/images_action.py @@ -0,0 +1,88 @@ +"""Remote client commands dealing with images.""" +import operator +from collections import OrderedDict + +import humanize +import podman + +from .. import AbstractActionBase, Report, ReportColumn + + +class Images(AbstractActionBase): + """Class for Image manipulation.""" + + @classmethod + def subparser(cls, parent): + """Add Images commands to parent parser.""" + parser = parent.add_parser('images', help='list images') + super().subparser(parser) + parser.add_argument( + '--sort', + choices=['created', 'id', 'repository', 'size', 'tag'], + default='created', + type=str.lower, + help=('Change sort ordered of displayed images.' + ' (default: %(default)s)')) + + group = parser.add_mutually_exclusive_group() + group.add_argument( + '--digests', + action='store_true', + help='Include digests with images. (default: %(default)s)') + parser.set_defaults(class_=cls, method='list') + + def __init__(self, args): + """Construct Images class.""" + super().__init__(args) + + self.columns = OrderedDict({ + 'name': + ReportColumn('name', 'REPOSITORY', 40), + 'tag': + ReportColumn('tag', 'TAG', 10), + 'id': + ReportColumn('id', 'IMAGE ID', 12), + 'created': + ReportColumn('created', 'CREATED', 12), + 'size': + ReportColumn('size', 'SIZE', 8), + 'repoDigests': + ReportColumn('repoDigests', 'DIGESTS', 35), + }) + + def list(self): + """List images.""" + images = sorted( + self.client.images.list(), + key=operator.attrgetter(self._args.sort)) + if len(images) == 0: + return 0 + + 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) diff --git a/contrib/python/pypodman/lib/actions/ps_action.py b/contrib/python/pypodman/lib/actions/ps_action.py new file mode 100644 index 000000000..4bbec5578 --- /dev/null +++ b/contrib/python/pypodman/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(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) diff --git a/contrib/python/pypodman/lib/actions/rm_action.py b/contrib/python/pypodman/lib/actions/rm_action.py new file mode 100644 index 000000000..bd8950bd6 --- /dev/null +++ b/contrib/python/pypodman/lib/actions/rm_action.py @@ -0,0 +1,51 @@ +"""Remote client command for deleting containers.""" +import sys + +import podman + +from .. import AbstractActionBase + + +class Rm(AbstractActionBase): + """Class for removing containers from storage.""" + + @classmethod + def subparser(cls, parent): + """Add Rm command to parent parser.""" + parser = parent.add_parser('rm', help='delete container(s)') + parser.add_argument( + '-f', + '--force', + action='store_true', + help=('force delete of running container(s).' + ' (default: %(default)s)')) + parser.add_argument( + 'targets', nargs='*', help='container id(s) to delete') + parser.set_defaults(class_=cls, method='remove') + + def __init__(self, args): + """Construct Rm class.""" + super().__init__(args) + if len(args.targets) < 1: + raise ValueError('You must supply at least one container id' + ' or name to be deleted.') + + def remove(self): + """Remove container(s).""" + for id in self._args.targets: + try: + ctnr = self.client.containers.get(id) + ctnr.remove(self._args.force) + print(id) + 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) diff --git a/contrib/python/pypodman/lib/actions/rmi_action.py b/contrib/python/pypodman/lib/actions/rmi_action.py new file mode 100644 index 000000000..91f0deeaf --- /dev/null +++ b/contrib/python/pypodman/lib/actions/rmi_action.py @@ -0,0 +1,50 @@ +"""Remote client command for deleting images.""" +import sys + +import podman + +from .. import AbstractActionBase + + +class Rmi(AbstractActionBase): + """Clas for removing images from storage.""" + + @classmethod + def subparser(cls, parent): + """Add Rmi command to parent parser.""" + parser = parent.add_parser('rmi', help='delete image(s)') + parser.add_argument( + '-f', + '--force', + action='store_true', + help=('force delete of image(s) and associated containers.' + ' (default: %(default)s)')) + parser.add_argument('targets', nargs='*', help='image id(s) to delete') + parser.set_defaults(class_=cls, method='remove') + + def __init__(self, args): + """Construct Rmi class.""" + super().__init__(args) + if len(args.targets) < 1: + raise ValueError('You must supply at least one image id' + ' or name to be deleted.') + + def remove(self): + """Remove image(s).""" + for id in self._args.targets: + try: + img = self.client.images.get(id) + img.remove(self._args.force) + print(id) + except podman.ImageNotFound as e: + sys.stdout.flush() + print( + 'Image {} 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) diff --git a/contrib/python/pypodman/lib/config.py b/contrib/python/pypodman/lib/config.py new file mode 100644 index 000000000..e687697ef --- /dev/null +++ b/contrib/python/pypodman/lib/config.py @@ -0,0 +1,212 @@ +import argparse +import curses +import getpass +import inspect +import logging +import os +import sys + +import pkg_resources + +import pytoml + +# TODO: setup.py and obtain __version__ from rpm.spec +try: + __version__ = pkg_resources.get_distribution('pypodman').version +except Exception: + __version__ = '0.0.0' + + +class HelpFormatter(argparse.RawDescriptionHelpFormatter): + """Set help width to screen size.""" + + def __init__(self, *args, **kwargs): + """Construct HelpFormatter using screen width.""" + if 'width' not in kwargs: + kwargs['width'] = 80 + try: + height, width = curses.initscr().getmaxyx() + kwargs['width'] = width + finally: + curses.endwin() + super().__init__(*args, **kwargs) + + +class PodmanArgumentParser(argparse.ArgumentParser): + """Default remote podman configuration.""" + + def __init__(self, **kwargs): + """Construct the parser.""" + kwargs['add_help'] = True + kwargs['allow_abbrev'] = True + kwargs['description'] = __doc__ + kwargs['formatter_class'] = HelpFormatter + + super().__init__(**kwargs) + + def initialize_parser(self): + """Initialize parser without causing recursion meltdown.""" + self.add_argument( + '--version', + action='version', + version='%(prog)s v. ' + __version__) + self.add_argument( + '--log-level', + choices=['DEBUG', 'INFO', 'WARNING', 'ERROR', 'CRITICAL'], + default='WARNING', + type=str.upper, + help='set logging level for events. (default: %(default)s)', + ) + self.add_argument( + '--run-dir', + metavar='DIRECTORY', + help=('directory to place local socket bindings.' + ' (default: XDG_RUNTIME_DIR/pypodman')) + self.add_argument( + '--user', + default=getpass.getuser(), + help='Authenicating user on remote host. (default: %(default)s)') + self.add_argument( + '--host', help='name of remote host. (default: None)') + self.add_argument( + '--remote-socket-path', + metavar='PATH', + help=('path of podman socket on remote host' + ' (default: /run/podman/io.projectatomic.podman)')) + self.add_argument( + '--identity-file', + metavar='PATH', + help=('path to ssh identity file. (default: ~user/.ssh/id_dsa)')) + self.add_argument( + '--config-home', + metavar='DIRECTORY', + help=('home of configuration "pypodman.conf".' + ' (default: XDG_CONFIG_HOME/pypodman')) + + actions_parser = self.add_subparsers( + dest='subparser_name', help='actions') + + # pull in plugin(s) code for each subcommand + for name, obj in inspect.getmembers( + sys.modules['lib.actions'], + lambda member: inspect.isclass(member)): + if hasattr(obj, 'subparser'): + try: + obj.subparser(actions_parser) + except NameError as e: + logging.critical(e) + logging.warning( + 'See subparser configuration for Class "{}"'.format( + name)) + sys.exit(3) + + def parse_args(self, args=None, namespace=None): + """Parse command line arguments, backed by env var and config_file.""" + self.initialize_parser() + cooked = super().parse_args(args, namespace) + return self.resolve_configuration(cooked) + + def resolve_configuration(self, args): + """Find and fill in any arguments not passed on command line.""" + args.xdg_runtime_dir = os.environ.get('XDG_RUNTIME_DIR', '/tmp') + args.xdg_config_home = os.environ.get('XDG_CONFIG_HOME', + os.path.expanduser('~/.config')) + args.xdg_config_dirs = os.environ.get('XDG_CONFIG_DIRS', '/etc/xdg') + + # Configuration file(s) are optional, + # required arguments may be provided elsewhere + config = {'default': {}} + dirs = args.xdg_config_dirs.split(':') + dirs.extend((args.xdg_config_home, args.config_home)) + for dir_ in dirs: + if dir_ is None: + continue + try: + with open(os.path.join(dir_, 'pypodman/pypodman.conf'), + 'r') as stream: + config.update(pytoml.load(stream)) + except OSError: + pass + + def reqattr(name, value): + if value: + setattr(args, name, value) + return value + self.error('Required argument "%s" is not configured.' % name) + + reqattr( + 'run_dir', + getattr(args, 'run_dir') + or os.environ.get('RUN_DIR') + or config['default'].get('run_dir') + or os.path.join(args.xdg_runtime_dir, 'pypodman') + ) # yapf: disable + + setattr( + args, + 'host', + getattr(args, 'host') + or os.environ.get('HOST') + or config['default'].get('host') + ) # yapf:disable + + reqattr( + 'user', + getattr(args, 'user') + or os.environ.get('USER') + or config['default'].get('user') + or getpass.getuser() + ) # yapf:disable + + reqattr( + 'remote_socket_path', + getattr(args, 'remote_socket_path') + or os.environ.get('REMOTE_SOCKET_PATH') + or config['default'].get('remote_socket_path') + or '/run/podman/io.projectatomic.podman' + ) # yapf:disable + + reqattr( + 'log_level', + getattr(args, 'log_level') + or os.environ.get('LOG_LEVEL') + or config['default'].get('log_level') + or logging.WARNING + ) # yapf:disable + + setattr( + args, + 'identity_file', + getattr(args, 'identity_file') + or os.environ.get('IDENTITY_FILE') + or config['default'].get('identity_file') + or os.path.expanduser('~{}/.ssh/id_dsa'.format(args.user)) + ) # yapf:disable + + if not os.path.isfile(args.identity_file): + args.identity_file = None + + if args.host: + args.local_socket_path = os.path.join(args.run_dir, + "podman.socket") + else: + args.local_socket_path = args.remote_socket_path + + args.local_uri = "unix:{}".format(args.local_socket_path) + args.remote_uri = "ssh://{}@{}{}".format(args.user, args.host, + args.remote_socket_path) + return args + + def exit(self, status=0, message=None): + """Capture message and route to logger.""" + if message: + log = logging.info if status == 0 else logging.error + log(message) + super().exit(status) + + def error(self, message): + """Capture message and route to logger.""" + logging.error('{}: {}'.format(self.prog, message)) + logging.error("Try '{} --help' for more information.".format( + self.prog)) + super().exit(2) diff --git a/contrib/python/pypodman/lib/future_abstract.py b/contrib/python/pypodman/lib/future_abstract.py new file mode 100644 index 000000000..75a1d42db --- /dev/null +++ b/contrib/python/pypodman/lib/future_abstract.py @@ -0,0 +1,29 @@ +"""Utilities for with-statement contexts. See PEP 343.""" + +import abc + +import _collections_abc + +try: + from contextlib import AbstractContextManager +except ImportError: + # Copied from python3.7 library as "backport" + class AbstractContextManager(abc.ABC): + """An abstract base class for context managers.""" + + def __enter__(self): + """Return `self` upon entering the runtime context.""" + return self + + @abc.abstractmethod + def __exit__(self, exc_type, exc_value, traceback): + """Raise any exception triggered within the runtime context.""" + return None + + @classmethod + def __subclasshook__(cls, C): + """Check whether subclass is considered a subclass of this ABC.""" + if cls is AbstractContextManager: + return _collections_abc._check_methods(C, "__enter__", + "__exit__") + return NotImplemented diff --git a/contrib/python/pypodman/lib/pypodman.py b/contrib/python/pypodman/lib/pypodman.py new file mode 100755 index 000000000..4bc71a9cc --- /dev/null +++ b/contrib/python/pypodman/lib/pypodman.py @@ -0,0 +1,76 @@ +#!/usr/bin/env python3 +"""Remote podman client.""" + +import logging +import os +import sys + +import lib.actions +from lib import PodmanArgumentParser + +assert lib.actions # silence pyflakes + + +def main(): + """Entry point.""" + # Setup logging so we use stderr and can change logging level later + # Do it now before there is any chance of a default setup hardcoding crap. + log = logging.getLogger() + fmt = logging.Formatter('%(asctime)s | %(levelname)-8s | %(message)s', + '%Y-%m-%d %H:%M:%S %Z') + stderr = logging.StreamHandler(stream=sys.stderr) + stderr.setFormatter(fmt) + log.addHandler(stderr) + log.setLevel(logging.WARNING) + + parser = PodmanArgumentParser() + args = parser.parse_args() + + log.setLevel(args.log_level) + logging.debug('Logging initialized at level {}'.format( + logging.getLevelName(logging.getLogger().getEffectiveLevel()))) + + def want_tb(): + """Add traceback when logging events.""" + return log.getEffectiveLevel() == logging.DEBUG + + try: + if not os.path.exists(args.run_dir): + os.makedirs(args.run_dir) + except PermissionError as e: + logging.critical(e, exc_info=want_tb()) + sys.exit(6) + + # class_(args).method() are set by the sub-command's parser + returncode = None + try: + obj = args.class_(args) + except Exception as e: + logging.critical(repr(e), exc_info=want_tb()) + logging.warning('See subparser "{}" configuration.'.format( + args.subparser_name)) + sys.exit(5) + + try: + returncode = getattr(obj, args.method)() + except AttributeError as e: + logging.critical(e, exc_info=want_tb()) + logging.warning('See subparser "{}" configuration.'.format( + args.subparser_name)) + returncode = 3 + except KeyboardInterrupt: + pass + except ( + ConnectionRefusedError, + ConnectionResetError, + TimeoutError, + ) as e: + logging.critical(e, exc_info=want_tb()) + logging.info('Review connection arguments for correctness.') + returncode = 4 + + return 0 if returncode is None else returncode + + +if __name__ == '__main__': + sys.exit(main()) diff --git a/contrib/python/pypodman/lib/report.py b/contrib/python/pypodman/lib/report.py new file mode 100644 index 000000000..25fe2ae0d --- /dev/null +++ b/contrib/python/pypodman/lib/report.py @@ -0,0 +1,67 @@ +"""Report Manager.""" +import sys +from collections import namedtuple + +from .future_abstract import AbstractContextManager + + +class ReportColumn(namedtuple('ReportColumn', 'key display width default')): + """Hold attributes of output column.""" + + __slots__ = () + + def __new__(cls, key, display, width, default=None): + """Add defaults for attributes.""" + return super(ReportColumn, cls).__new__(cls, key, display, width, + default) + + +class Report(AbstractContextManager): + """Report Manager.""" + + def __init__(self, columns, heading=True, epilog=None, file=sys.stdout): + """Construct Report. + + columns is a mapping for named fields to column headings. + headers True prints headers on table. + epilog will be printed when the report context is closed. + """ + self._columns = columns + self._file = file + self._heading = heading + self.epilog = epilog + self._format = None + + def row(self, **fields): + """Print row for report.""" + if self._heading: + hdrs = {k: v.display for (k, v) in self._columns.items()} + print(self._format.format(**hdrs), flush=True, file=self._file) + self._heading = False + fields = {k: str(v) for k, v in fields.items()} + print(self._format.format(**fields)) + + def __exit__(self, exc_type, exc_value, traceback): + """Leave Report context and print epilog if provided.""" + if self.epilog: + print(self.epilog, flush=True, file=self._file) + + def layout(self, iterable, keys, truncate=True): + """Use data and headings build format for table to fit.""" + format = [] + + for key in keys: + value = max(map(lambda x: len(str(x.get(key, ''))), iterable)) + # print('key', key, 'value', value) + + if truncate: + row = self._columns.get( + key, ReportColumn(key, key.upper(), len(key))) + if value < row.width: + step = row.width if value == 0 else value + value = max(len(key), step) + elif value > row.width: + value = row.width if row.width != 0 else value + + format.append('{{{0}:{1}.{1}}}'.format(key, value)) + self._format = ' '.join(format) diff --git a/contrib/python/pypodman/requirements.txt b/contrib/python/pypodman/requirements.txt new file mode 100644 index 000000000..f9cd4f904 --- /dev/null +++ b/contrib/python/pypodman/requirements.txt @@ -0,0 +1,4 @@ +humanize +podman +pytoml +setuptools>=39.2.0 diff --git a/contrib/python/pypodman/setup.py b/contrib/python/pypodman/setup.py new file mode 100644 index 000000000..0483eb71c --- /dev/null +++ b/contrib/python/pypodman/setup.py @@ -0,0 +1,44 @@ +import os + +from setuptools import find_packages, setup + +root = os.path.abspath(os.path.dirname(__file__)) + +with open(os.path.join(root, 'README.md')) as me: + readme = me.read() + +with open(os.path.join(root, 'requirements.txt')) as r: + requirements = r.read().splitlines() + +print(find_packages(where='pypodman', exclude=['test'])) + +setup( + name='pypodman', + version=os.environ.get('PODMAN_VERSION', '0.0.0'), + description='A client for communicating with a Podman server', + author_email='jhonce@redhat.com', + author='Jhon Honce', + license='Apache Software License', + long_description=readme, + entry_points={'console_scripts': [ + 'pypodman = lib.pypodman:main', + ]}, + include_package_data=True, + install_requires=requirements, + keywords='varlink libpod podman pypodman', + packages=find_packages(exclude=['test']), + python_requires='>=3', + zip_safe=True, + url='http://github.com/projectatomic/libpod', + classifiers=[ + 'Development Status :: 3 - Alpha', + 'Intended Audience :: Developers', + 'Intended Audience :: System Administrators', + 'License :: OSI Approved :: Apache Software License', + 'Operating System :: MacOS :: MacOS X', + 'Operating System :: Microsoft :: Windows', + 'Operating System :: POSIX', + 'Programming Language :: Python :: 3.6', + 'Topic :: System :: Systems Administration', + 'Topic :: Utilities', + ]) diff --git a/contrib/python/pypodman/test/test_report.py b/contrib/python/pypodman/test/test_report.py new file mode 100644 index 000000000..280a9a954 --- /dev/null +++ b/contrib/python/pypodman/test/test_report.py @@ -0,0 +1,23 @@ +from __future__ import absolute_import + +import unittest + +from report import Report, ReportColumn + + +class TestReport(unittest.TestCase): + def setUp(self): + pass + + def test_report_column(self): + rc = ReportColumn('k', 'v', 3) + self.assertEqual(rc.key, 'k') + self.assertEqual(rc.display, 'v') + self.assertEqual(rc.width, 3) + self.assertIsNone(rc.default) + + rc = ReportColumn('k', 'v', 3, 'd') + self.assertEqual(rc.key, 'k') + self.assertEqual(rc.display, 'v') + self.assertEqual(rc.width, 3) + self.assertEqual(rc.default, 'd') diff --git a/contrib/python/requirements.txt b/contrib/python/requirements.txt deleted file mode 100644 index d294af3c7..000000000 --- a/contrib/python/requirements.txt +++ /dev/null @@ -1,3 +0,0 @@ -varlink>=26.1.0 -setuptools>=39.2.0 -python-dateutil>=2.7.3 diff --git a/contrib/python/setup.py b/contrib/python/setup.py deleted file mode 100644 index c9db30199..000000000 --- a/contrib/python/setup.py +++ /dev/null @@ -1,38 +0,0 @@ -import os - -from setuptools import find_packages, setup - - -root = os.path.abspath(os.path.dirname(__file__)) - -with open(os.path.join(root, 'README.md')) as me: - readme = me.read() - -with open(os.path.join(root, 'requirements.txt')) as r: - requirements = r.read().splitlines() - - -setup( - name='podman', - version=os.environ.get('PODMAN_VERSION', '0.0.0'), - description='A client for communicating with a Podman server', - long_description=readme, - author='Jhon Honce', - author_email='jhonce@redhat.com', - url='http://github.com/projectatomic/libpod', - license='Apache Software License', - python_requires='>=3', - include_package_data=True, - install_requires=requirements, - packages=find_packages(exclude=['test']), - zip_safe=True, - keywords='varlink libpod podman', - classifiers=[ - 'Development Status :: 3 - Alpha', - 'Intended Audience :: Developers', - 'Topic :: Software Development', - 'License :: OSI Approved :: Apache Software License', - 'Programming Language :: Python :: 3.6', - ]) -# Not supported -# long_description_content_type='text/markdown', diff --git a/contrib/python/test/__init__.py b/contrib/python/test/__init__.py deleted file mode 100644 index e69de29bb..000000000 diff --git a/contrib/python/test/podman_testcase.py b/contrib/python/test/podman_testcase.py deleted file mode 100644 index f96a3a013..000000000 --- a/contrib/python/test/podman_testcase.py +++ /dev/null @@ -1,107 +0,0 @@ -import contextlib -import functools -import itertools -import os -import subprocess -import time -import unittest - -from varlink import VarlinkError - -MethodNotImplemented = 'org.varlink.service.MethodNotImplemented' - - -class PodmanTestCase(unittest.TestCase): - """Hide the sausage making of initializing storage.""" - - @classmethod - def setUpClass(cls): - if hasattr(PodmanTestCase, 'alpine_process'): - PodmanTestCase.tearDownClass() - - def run_cmd(*args): - cmd = list(itertools.chain(*args)) - try: - pid = subprocess.Popen( - cmd, - close_fds=True, - stdout=subprocess.PIPE, - stderr=subprocess.PIPE) - out, err = pid.communicate() - except OSError as e: - print('{}: {}({})'.format(cmd, e.strerror, e.returncode)) - except ValueError as e: - print('{}: {}'.format(cmd, e.message)) - raise - else: - return out.strip() - - tmpdir = os.environ.get('TMPDIR', '/tmp') - podman_args = [ - '--storage-driver=vfs', - '--root={}/crio'.format(tmpdir), - '--runroot={}/crio-run'.format(tmpdir), - '--cni-config-dir={}/cni/net.d'.format(tmpdir), - ] - - run_podman = functools.partial(run_cmd, ['podman'], podman_args) - - id = run_podman(['pull', 'alpine']) - setattr(PodmanTestCase, 'alpine_id', id) - - run_podman(['pull', 'busybox']) - run_podman(['images']) - - run_cmd(['rm', '-f', '{}/alpine_gold.tar'.format(tmpdir)]) - run_podman([ - 'save', '--output', '{}/alpine_gold.tar'.format(tmpdir), 'alpine' - ]) - - PodmanTestCase.alpine_log = open( - os.path.join('/tmp/', 'alpine.log'), 'w') - - cmd = ['podman'] - cmd.extend(podman_args) - # cmd.extend(['run', '-d', 'alpine', 'sleep', '500']) - cmd.extend(['run', '-dt', 'alpine', '/bin/sh']) - PodmanTestCase.alpine_process = subprocess.Popen( - cmd, - stdout=PodmanTestCase.alpine_log, - stderr=subprocess.STDOUT, - ) - - PodmanTestCase.busybox_log = open( - os.path.join('/tmp/', 'busybox.log'), 'w') - - cmd = ['podman'] - cmd.extend(podman_args) - cmd.extend(['create', 'busybox']) - PodmanTestCase.busybox_process = subprocess.Popen( - cmd, - stdout=PodmanTestCase.busybox_log, - stderr=subprocess.STDOUT, - ) - # give podman time to start ctnr - time.sleep(2) - - # Close our handle of file - PodmanTestCase.alpine_log.close() - PodmanTestCase.busybox_log.close() - - @classmethod - def tearDownClass(cls): - try: - PodmanTestCase.alpine_process.kill() - assert 0 == PodmanTestCase.alpine_process.wait(500) - delattr(PodmanTestCase, 'alpine_process') - - PodmanTestCase.busybox_process.kill() - assert 0 == PodmanTestCase.busybox_process.wait(500) - except Exception as e: - print('Exception: {}'.format(e)) - raise - - @contextlib.contextmanager - def assertRaisesNotImplemented(self): - with self.assertRaisesRegex(VarlinkError, MethodNotImplemented): - yield diff --git a/contrib/python/test/test_client.py b/contrib/python/test/test_client.py deleted file mode 100644 index e642c8add..000000000 --- a/contrib/python/test/test_client.py +++ /dev/null @@ -1,37 +0,0 @@ -from __future__ import absolute_import - -import unittest -from unittest.mock import patch - -import podman -from podman.client import BaseClient, Client, LocalClient, RemoteClient - - -class TestClient(unittest.TestCase): - def setUp(self): - pass - - @patch('podman.libs.system.System.ping', return_value=True) - def test_local(self, mock_ping): - p = Client( - uri='unix:/run/podman', - interface='io.projectatomic.podman', - ) - - self.assertIsInstance(p._client, LocalClient) - self.assertIsInstance(p._client, BaseClient) - - mock_ping.assert_called_once() - - @patch('os.path.isfile', return_value=True) - @patch('podman.libs.system.System.ping', return_value=True) - def test_remote(self, mock_ping, mock_isfile): - p = Client( - uri='unix:/run/podman', - interface='io.projectatomic.podman', - remote_uri='ssh://user@hostname/run/podmain/podman', - identity_file='~/.ssh/id_rsa') - - self.assertIsInstance(p._client, BaseClient) - mock_ping.assert_called_once() - mock_isfile.assert_called_once() diff --git a/contrib/python/test/test_containers.py b/contrib/python/test/test_containers.py deleted file mode 100644 index ec2dcde03..000000000 --- a/contrib/python/test/test_containers.py +++ /dev/null @@ -1,234 +0,0 @@ -import os -import signal -import unittest -from test.podman_testcase import PodmanTestCase - -import podman - - -class TestContainers(PodmanTestCase): - @classmethod - def setUpClass(cls): - super().setUpClass() - - @classmethod - def tearDownClass(cls): - super().tearDownClass() - - def setUp(self): - self.tmpdir = os.environ['TMPDIR'] - self.host = os.environ['PODMAN_HOST'] - - self.pclient = podman.Client(self.host) - self.loadCache() - - def tearDown(self): - pass - - def loadCache(self): - self.containers = list(self.pclient.containers.list()) - - self.alpine_ctnr = next( - iter([c for c in self.containers if 'alpine' in c['image']] or []), - None) - - if self.alpine_ctnr and self.alpine_ctnr.status != 'running': - self.alpine_ctnr.start() - - def test_list(self): - self.assertGreaterEqual(len(self.containers), 2) - self.assertIsNotNone(self.alpine_ctnr) - self.assertIn('alpine', self.alpine_ctnr.image) - - def test_delete_stopped(self): - before = len(self.containers) - - self.alpine_ctnr.stop() - target = self.alpine_ctnr.id - actual = self.pclient.containers.delete_stopped() - self.assertIn(target, actual) - - self.loadCache() - after = len(self.containers) - - self.assertLess(after, before) - TestContainers.setUpClass() - - def test_get(self): - actual = self.pclient.containers.get(self.alpine_ctnr.id) - for k in ['id', 'status', 'ports']: - self.assertEqual(actual[k], self.alpine_ctnr[k]) - - with self.assertRaises(podman.ContainerNotFound): - self.pclient.containers.get("bozo") - - def test_attach(self): - # StringIO does not support fileno() so we had to go old school - input = os.path.join(self.tmpdir, 'test_attach.stdin') - output = os.path.join(self.tmpdir, 'test_attach.stdout') - - with open(input, 'w+') as mock_in, open(output, 'w+') as mock_out: - # double quote is indeed in the expected place - mock_in.write('echo H"ello, World"; exit\n') - mock_in.seek(0, 0) - - ctnr = self.pclient.images.get(self.alpine_ctnr.image).container( - detach=True, tty=True) - ctnr.attach(stdin=mock_in.fileno(), stdout=mock_out.fileno()) - ctnr.start() - - mock_out.flush() - mock_out.seek(0, 0) - output = mock_out.read() - self.assertIn('Hello', output) - - ctnr.remove(force=True) - - def test_processes(self): - actual = list(self.alpine_ctnr.processes()) - self.assertGreaterEqual(len(actual), 2) - - def test_start_stop_wait(self): - ctnr = self.alpine_ctnr.stop() - self.assertFalse(ctnr['running']) - - ctnr.start() - self.assertTrue(ctnr.running) - - ctnr.stop() - self.assertFalse(ctnr['containerrunning']) - - actual = ctnr.wait() - self.assertGreaterEqual(actual, 0) - - def test_changes(self): - actual = self.alpine_ctnr.changes() - - self.assertListEqual( - sorted(['changed', 'added', 'deleted']), sorted( - list(actual.keys()))) - - # TODO: brittle, depends on knowing history of ctnr - self.assertGreaterEqual(len(actual['changed']), 2) - self.assertGreaterEqual(len(actual['added']), 3) - self.assertEqual(len(actual['deleted']), 0) - - def test_kill(self): - self.assertTrue(self.alpine_ctnr.running) - ctnr = self.alpine_ctnr.kill(signal.SIGKILL) - self.assertFalse(ctnr.running) - - def test_inspect(self): - actual = self.alpine_ctnr.inspect() - self.assertEqual(actual.id, self.alpine_ctnr.id) - # TODO: Datetime values from inspect missing offset in CI instance - # self.assertEqual( - # datetime_parse(actual.created), - # datetime_parse(self.alpine_ctnr.createdat)) - - def test_export(self): - target = os.path.join(self.tmpdir, 'alpine_export_ctnr.tar') - - actual = self.alpine_ctnr.export(target) - self.assertEqual(actual, target) - self.assertTrue(os.path.isfile(target)) - self.assertGreater(os.path.getsize(target), 0) - - def test_commit(self): - # TODO: Test for STOPSIGNAL when supported by OCI - # TODO: Test for message when supported by OCI - details = self.pclient.images.get(self.alpine_ctnr.image).inspect() - changes = ['ENV=' + i for i in details.containerconfig['env']] - changes.append('CMD=/usr/bin/zsh') - changes.append('ENTRYPOINT=/bin/sh date') - changes.append('ENV=TEST=test_containers.TestContainers.test_commit') - changes.append('EXPOSE=80') - changes.append('EXPOSE=8888') - changes.append('LABEL=unittest=test_commit') - changes.append('USER=bozo:circus') - changes.append('VOLUME=/data') - changes.append('WORKDIR=/data/application') - - id = self.alpine_ctnr.commit( - 'alpine3', author='Bozo the clown', changes=changes, pause=True) - img = self.pclient.images.get(id) - self.assertIsNotNone(img) - - details = img.inspect() - self.assertEqual(details.author, 'Bozo the clown') - self.assertListEqual(['/usr/bin/zsh'], details.containerconfig['cmd']) - self.assertListEqual(['/bin/sh date'], - details.containerconfig['entrypoint']) - self.assertIn('TEST=test_containers.TestContainers.test_commit', - details.containerconfig['env']) - self.assertTrue( - [e for e in details.containerconfig['env'] if 'PATH=' in e]) - self.assertDictEqual({ - '80': {}, - '8888': {}, - }, details.containerconfig['exposedports']) - self.assertDictEqual({'unittest': 'test_commit'}, details.labels) - self.assertEqual('bozo:circus', details.containerconfig['user']) - self.assertEqual({'/data': {}}, details.containerconfig['volumes']) - self.assertEqual('/data/application', - details.containerconfig['workingdir']) - - def test_remove(self): - before = len(self.containers) - - with self.assertRaises(podman.ErrorOccurred): - self.alpine_ctnr.remove() - - self.assertEqual( - self.alpine_ctnr.id, self.alpine_ctnr.remove(force=True)) - self.loadCache() - after = len(self.containers) - - self.assertLess(after, before) - TestContainers.setUpClass() - - def test_restart(self): - self.assertTrue(self.alpine_ctnr.running) - before = self.alpine_ctnr.runningfor - - ctnr = self.alpine_ctnr.restart() - self.assertTrue(ctnr.running) - - after = self.alpine_ctnr.runningfor - - # TODO: restore check when restart zeros counter - # self.assertLess(after, before) - - def test_rename(self): - with self.assertRaisesNotImplemented(): - self.alpine_ctnr.rename('new_alpine') - - def test_resize_tty(self): - with self.assertRaisesNotImplemented(): - self.alpine_ctnr.resize_tty(132, 43) - - def test_pause_unpause(self): - self.assertTrue(self.alpine_ctnr.running) - - ctnr = self.alpine_ctnr.pause() - self.assertEqual(ctnr.status, 'paused') - - ctnr = self.alpine_ctnr.unpause() - self.assertTrue(ctnr.running) - self.assertTrue(ctnr.status, 'running') - - def test_stats(self): - self.assertTrue(self.alpine_ctnr.running) - - actual = self.alpine_ctnr.stats() - self.assertEqual(self.alpine_ctnr.id, actual.id) - self.assertEqual(self.alpine_ctnr.names, actual.name) - - def test_logs(self): - self.assertTrue(self.alpine_ctnr.running) - actual = list(self.alpine_ctnr.logs()) - self.assertIsNotNone(actual) - - -if __name__ == '__main__': - unittest.main() diff --git a/contrib/python/test/test_images.py b/contrib/python/test/test_images.py deleted file mode 100644 index 14bf90992..000000000 --- a/contrib/python/test/test_images.py +++ /dev/null @@ -1,172 +0,0 @@ -import itertools -import os -import unittest -from collections import Counter -from datetime import datetime, timezone -from test.podman_testcase import PodmanTestCase - -import podman - - -class TestImages(PodmanTestCase): - @classmethod - def setUpClass(cls): - super().setUpClass() - - @classmethod - def tearDownClass(cls): - super().tearDownClass() - - def setUp(self): - self.tmpdir = os.environ['TMPDIR'] - self.host = os.environ['PODMAN_HOST'] - - self.pclient = podman.Client(self.host) - self.images = self.loadCache() - - def tearDown(self): - pass - - def loadCache(self): - with podman.Client(self.host) as pclient: - self.images = list(pclient.images.list()) - - self.alpine_image = next( - iter([ - i for i in self.images - if 'docker.io/library/alpine:latest' in i['repoTags'] - ] or []), None) - - return self.images - - def test_list(self): - actual = self.loadCache() - self.assertGreaterEqual(len(actual), 2) - self.assertIsNotNone(self.alpine_image) - - def test_build(self): - path = os.path.join(self.tmpdir, 'ctnr', 'Dockerfile') - img, logs = self.pclient.images.build( - dockerfile=[path], - tags=['alpine-unittest'], - ) - self.assertIsNotNone(img) - self.assertIn('localhost/alpine-unittest:latest', img.repoTags) - self.assertLess( - podman.datetime_parse(img.created), datetime.now(timezone.utc)) - self.assertTrue(logs) - - def test_create(self): - img_details = self.alpine_image.inspect() - - actual = self.alpine_image.container() - self.assertIsNotNone(actual) - self.assertEqual(actual.status, 'configured') - - ctnr = actual.start() - self.assertIn(ctnr.status, ['running', 'exited']) - - ctnr_details = ctnr.inspect() - for e in img_details.containerconfig['env']: - self.assertIn(e, ctnr_details.config['env']) - - def test_export(self): - path = os.path.join(self.tmpdir, 'alpine_export.tar') - target = 'oci-archive:{}:latest'.format(path) - - actual = self.alpine_image.export(target, False) - self.assertTrue(actual) - self.assertTrue(os.path.isfile(path)) - - def test_get(self): - actual = self.pclient.images.get(self.alpine_image.id) - self.assertEqual(actual, self.alpine_image) - - def test_history(self): - records = [] - bucket = Counter() - for record in self.alpine_image.history(): - self.assertIn(record.id, (self.alpine_image.id, '')) - bucket[record.id] += 1 - records.append(record) - - self.assertGreater(bucket[self.alpine_image.id], 0) - self.assertEqual(sum(bucket.values()), len(records)) - - def test_inspect(self): - actual = self.alpine_image.inspect() - self.assertEqual(actual.id, self.alpine_image.id) - - def test_push(self): - path = '{}/alpine_push'.format(self.tmpdir) - target = 'dir:{}'.format(path) - self.alpine_image.push(target) - - self.assertTrue(os.path.isfile(os.path.join(path, 'manifest.json'))) - self.assertTrue(os.path.isfile(os.path.join(path, 'version'))) - - def test_tag(self): - self.assertEqual(self.alpine_image.id, - self.alpine_image.tag('alpine:fubar')) - self.loadCache() - self.assertIn('alpine:fubar', self.alpine_image.repoTags) - - def test_remove(self): - before = self.loadCache() - - # assertRaises doesn't follow the import name :( - with self.assertRaises(podman.ErrorOccurred): - self.alpine_image.remove() - - actual = self.alpine_image.remove(force=True) - self.assertEqual(self.alpine_image.id, actual) - after = self.loadCache() - - self.assertLess(len(after), len(before)) - TestImages.setUpClass() - self.loadCache() - - def test_import_delete_unused(self): - before = self.loadCache() - # create unused image, so we have something to delete - source = os.path.join(self.tmpdir, 'alpine_gold.tar') - new_img = self.pclient.images.import_image( - source, - 'alpine2:latest', - 'unittest.test_import', - ) - after = self.loadCache() - - self.assertEqual(len(before) + 1, len(after)) - self.assertIsNotNone( - next(iter([i for i in after if new_img in i['id']] or []), None)) - - actual = self.pclient.images.delete_unused() - self.assertIn(new_img, actual) - - after = self.loadCache() - self.assertGreaterEqual(len(before), len(after)) - - TestImages.setUpClass() - self.loadCache() - - def test_pull(self): - before = self.loadCache() - actual = self.pclient.images.pull('prom/busybox:latest') - after = self.loadCache() - - self.assertEqual(len(before) + 1, len(after)) - self.assertIsNotNone( - next(iter([i for i in after if actual in i['id']] or []), None)) - - def test_search(self): - actual = self.pclient.images.search('alpine', 25) - names, length = itertools.tee(actual) - - for img in names: - self.assertIn('alpine', img.name) - self.assertTrue(0 < len(list(length)) <= 25) - - -if __name__ == '__main__': - unittest.main() diff --git a/contrib/python/test/test_libs.py b/contrib/python/test/test_libs.py deleted file mode 100644 index 202bed1d8..000000000 --- a/contrib/python/test/test_libs.py +++ /dev/null @@ -1,53 +0,0 @@ -import datetime -import unittest - -import podman - - -class TestLibs(unittest.TestCase): - def setUp(self): - pass - - def tearDown(self): - pass - - def test_parse(self): - expected = datetime.datetime.strptime( - '2018-05-08T14:12:53.797795-0700', '%Y-%m-%dT%H:%M:%S.%f%z') - for v in [ - '2018-05-08T14:12:53.797795191-07:00', - '2018-05-08T14:12:53.797795-07:00', - '2018-05-08T14:12:53.797795-0700', - '2018-05-08 14:12:53.797795191 -0700 MST', - ]: - actual = podman.datetime_parse(v) - self.assertEqual(actual, expected) - - expected = datetime.datetime.strptime( - '2018-05-08T14:12:53.797795-0000', '%Y-%m-%dT%H:%M:%S.%f%z') - for v in [ - '2018-05-08T14:12:53.797795191Z', - '2018-05-08T14:12:53.797795191z', - ]: - actual = podman.datetime_parse(v) - self.assertEqual(actual, expected) - - actual = podman.datetime_parse(datetime.datetime.now().isoformat()) - self.assertIsNotNone(actual) - - def test_parse_fail(self): - for v in [ - 'There is no time here.', - ]: - with self.assertRaises(ValueError): - podman.datetime_parse(v) - - def test_format(self): - expected = '2018-05-08T18:24:52.753227-07:00' - dt = podman.datetime_parse(expected) - actual = podman.datetime_format(dt) - self.assertEqual(actual, expected) - - -if __name__ == '__main__': - unittest.main() diff --git a/contrib/python/test/test_runner.sh b/contrib/python/test/test_runner.sh deleted file mode 100755 index 602e0d6fd..000000000 --- a/contrib/python/test/test_runner.sh +++ /dev/null @@ -1,141 +0,0 @@ -#!/bin/bash - -# podman needs to play some games with resources -if [[ $(id -u) != 0 ]]; then - echo >&2 $0 must be run as root. - exit 2 -fi - -# setup path to find new binaries _NOT_ system binaries -if [[ ! -x ../../bin/podman ]]; then - echo 1>&2 Cannot find podman binary from libpod root directory. Run \"make binaries\" - exit 1 -fi -export PATH=../../bin:$PATH - -function usage { - echo 1>&2 $0 [-v] [-h] [test.TestCase|test.TestCase.step] -} - -while getopts "vh" arg; do - case $arg in - v ) VERBOSE='-v' ;; - h ) usage ; exit 0;; - \? ) usage ; exit 2;; - esac -done -shift $((OPTIND -1)) - -function cleanup { - # aggressive cleanup as tests may crash leaving crap around - umount '^(shm|nsfs)' - umount '\/run\/netns' - rm -r "$1" -} - -# Create temporary directory for storage -export TMPDIR=`mktemp -d /tmp/podman.XXXXXXXXXX` -trap "cleanup $TMPDIR" EXIT - -function umount { - # xargs -r always ran once, so write any mount points to file first - mount |awk "/$1/"' { print $3 }' >${TMPDIR}/mounts - if [[ -s ${TMPDIR}/mounts ]]; then - xargs <${TMPDIR}/mounts -t umount - fi -} - -function showlog { - [[ -s $1 ]] && cat <<-EOT -$1 ===== -$(cat "$1") - -EOT -} - -# Need locations to store stuff -mkdir -p ${TMPDIR}/{podman,crio,crio-run,cni/net.d,ctnr,tunnel} - -# Cannot be done in python unittest fixtures. EnvVar not picked up. -export REGISTRIES_CONFIG_PATH=${TMPDIR}/registry.conf -cat >$REGISTRIES_CONFIG_PATH <<-EOT - [registries.search] - registries = ['docker.io'] - [registries.insecure] - registries = [] - [registries.block] - registries = [] -EOT - -export CNI_CONFIG_PATH=${TMPDIR}/cni/net.d -cat >$CNI_CONFIG_PATH/87-podman-bridge.conflist <<-EOT -{ - "cniVersion": "0.3.0", - "name": "podman", - "plugins": [{ - "type": "bridge", - "bridge": "cni0", - "isGateway": true, - "ipMasq": true, - "ipam": { - "type": "host-local", - "subnet": "10.88.0.0/16", - "routes": [{ - "dst": "0.0.0.0/0" - }] - } - }, - { - "type": "portmap", - "capabilities": { - "portMappings": true - } - } - ] -} -EOT - -cat >$TMPDIR/ctnr/hello.sh <<-EOT -echo 'Hello, World' -EOT - -cat >$TMPDIR/ctnr/Dockerfile <<-EOT -FROM alpine:latest -COPY ./hello.sh /tmp/hello.sh -RUN chmod 755 /tmp/hello.sh -ENTRYPOINT ["/tmp/hello.sh"] -EOT - -export PODMAN_HOST="unix:${TMPDIR}/podman/io.projectatomic.podman" -PODMAN_ARGS="--storage-driver=vfs \ - --root=${TMPDIR}/crio \ - --runroot=${TMPDIR}/crio-run \ - --cni-config-dir=$CNI_CONFIG_PATH \ - " -if [[ -n $VERBOSE ]]; then - PODMAN_ARGS="$PODMAN_ARGS --log-level=debug" -fi -PODMAN="podman $PODMAN_ARGS" - -# document what we're about to do... -$PODMAN --version - -set -x -# Run podman in background without systemd for test purposes -$PODMAN varlink --timeout=0 ${PODMAN_HOST} >/tmp/test_runner.output 2>&1 & - -if [[ -z $1 ]]; then - export PYTHONPATH=. - python3 -m unittest discover -s . $VERBOSE -else - export PYTHONPATH=.:./test - python3 -m unittest $1 $VERBOSE -fi - -set +x -pkill -9 podman -pkill -9 conmon - -showlog /tmp/test_runner.output -showlog /tmp/alpine.log -showlog /tmp/busybox.log diff --git a/contrib/python/test/test_system.py b/contrib/python/test/test_system.py deleted file mode 100644 index 3f6ca57a2..000000000 --- a/contrib/python/test/test_system.py +++ /dev/null @@ -1,62 +0,0 @@ -import os -import unittest -from urllib.parse import urlparse - -import podman -import varlink - - -class TestSystem(unittest.TestCase): - def setUp(self): - self.host = os.environ['PODMAN_HOST'] - self.tmpdir = os.environ['TMPDIR'] - - def tearDown(self): - pass - - def test_bad_address(self): - with self.assertRaisesRegex(varlink.client.ConnectionError, - "Invalid address 'bad address'"): - podman.Client('bad address') - - def test_ping(self): - with podman.Client(self.host) as pclient: - self.assertTrue(pclient.system.ping()) - - def test_remote_ping(self): - host = urlparse(self.host) - remote_uri = 'ssh://root@localhost/{}'.format(host.path) - - local_uri = 'unix:{}/tunnel/podman.sock'.format(self.tmpdir) - with podman.Client( - uri=local_uri, - remote_uri=remote_uri, - identity_file=os.path.expanduser('~/.ssh/id_rsa'), - ) as remote_client: - remote_client.system.ping() - - def test_versions(self): - with podman.Client(self.host) as pclient: - # Values change with each build so we cannot test too much - self.assertListEqual( - sorted([ - 'built', 'client_version', 'git_commit', 'go_version', - 'os_arch', 'version' - ]), sorted(list(pclient.system.versions._fields))) - pclient.system.versions - self.assertIsNot(podman.__version__, '0.0.0') - - def test_info(self): - with podman.Client(self.host) as pclient: - actual = pclient.system.info() - # Values change too much to do exhaustive testing - self.assertIsNotNone(actual.podman['go_version']) - self.assertListEqual( - sorted([ - 'host', 'insecure_registries', 'podman', 'registries', - 'store' - ]), sorted(list(actual._fields))) - - -if __name__ == '__main__': - unittest.main() diff --git a/contrib/python/test/test_tunnel.py b/contrib/python/test/test_tunnel.py deleted file mode 100644 index 2522df0ab..000000000 --- a/contrib/python/test/test_tunnel.py +++ /dev/null @@ -1,79 +0,0 @@ -from __future__ import absolute_import - -import time -import unittest -from unittest.mock import MagicMock, patch - -import podman -from podman.libs.tunnel import Context, Portal, Tunnel - - -class TestTunnel(unittest.TestCase): - def setUp(self): - self.tunnel_01 = MagicMock(spec=Tunnel) - self.tunnel_02 = MagicMock(spec=Tunnel) - - def test_portal_ops(self): - portal = Portal(sweap=500) - portal['unix:/01'] = self.tunnel_01 - portal['unix:/02'] = self.tunnel_02 - - self.assertEqual(portal.get('unix:/01'), self.tunnel_01) - self.assertEqual(portal.get('unix:/02'), self.tunnel_02) - - del portal['unix:/02'] - with self.assertRaises(KeyError): - portal['unix:/02'] - self.assertEqual(len(portal), 1) - - def test_portal_reaping(self): - portal = Portal(sweap=0.5) - portal['unix:/01'] = self.tunnel_01 - portal['unix:/02'] = self.tunnel_02 - - self.assertEqual(len(portal), 2) - for entry in portal: - self.assertIn(entry, (self.tunnel_01, self.tunnel_02)) - - time.sleep(1) - portal.reap() - self.assertEqual(len(portal), 0) - - def test_portal_no_reaping(self): - portal = Portal(sweap=500) - portal['unix:/01'] = self.tunnel_01 - portal['unix:/02'] = self.tunnel_02 - - portal.reap() - self.assertEqual(len(portal), 2) - for entry in portal: - self.assertIn(entry, (self.tunnel_01, self.tunnel_02)) - - @patch('subprocess.Popen') - @patch('os.path.exists', return_value=True) - @patch('weakref.finalize') - def test_tunnel(self, mock_finalize, mock_exists, mock_Popen): - context = Context( - 'unix:/01', - 'io.projectatomic.podman', - '/tmp/user/socket', - '/run/podman/socket', - 'user', - 'hostname', - '~/.ssh/id_rsa', - ) - tunnel = Tunnel(context).bore('unix:/01') - - cmd = [ - 'ssh', - '-nNTq', - '-L', - '{}:{}'.format(context.local_socket, context.remote_socket), - '-i', - context.identity_file, - 'ssh://{}@{}'.format(context.username, context.hostname), - ] - - mock_finalize.assert_called_once_with(tunnel, tunnel.close, 'unix:/01') - mock_exists.assert_called_once_with(context.local_socket) - mock_Popen.assert_called_once_with(cmd, close_fds=True) diff --git a/contrib/spec/podman.spec.in b/contrib/spec/podman.spec.in index 7a5c1be85..9d8acce23 100644 --- a/contrib/spec/podman.spec.in +++ b/contrib/spec/podman.spec.in @@ -209,6 +209,22 @@ Summary: Python 3 bindings for %{name} %description -n python3-%{name} This package contains Python 3 bindings for %{name}. + +%package -n python3-py%{name} +BuildArch: noarch +BuildRequires: python3-devel +BuildRequires: python3-setuptools +BuildRequires: python3-varlink + +Requires: python3-setuptools +Requires: python3-varlink +Requires: python3-dateutil + +Provides: python3-py%{name} = %{version}-%{release} +Summary: Python 3 tool for %{name} + +%description -n python3-py%{name} +This package contains Python 3 tool for %{name}. %endif # varlink %if 0%{?with_devel} @@ -389,7 +405,12 @@ BUILDTAGS=$BUILDTAGS make binaries docs %if %{with varlink} #untar contents for python-podman -pushd contrib/python/dist +pushd contrib/python/podman/dist +tar zxf %{name}*.tar.gz +popd + +#untar contents for python-pypodman +pushd contrib/python/pypodman/dist tar zxf %{name}*.tar.gz popd %endif #varlink @@ -400,7 +421,12 @@ install -dp %{buildroot}%{_unitdir} %if %{with varlink} #install python-podman -pushd contrib/python +pushd contrib/python/podman +%{__python3} setup.py install --root %{buildroot} +popd + +#install python-pypodman +pushd contrib/python/pypodman %{__python3} setup.py install --root %{buildroot} popd %endif #varlink -- cgit v1.2.3-54-g00ecf