diff options
author | Jhon Honce <jhonce@redhat.com> | 2018-08-16 15:51:26 -0700 |
---|---|---|
committer | Atomic Bot <atomic-devel@projectatomic.io> | 2018-08-20 15:26:09 +0000 |
commit | 3bfe07b4f583b9526b616ec7e49332994ca5bc2e (patch) | |
tree | f3e1f0f609fb2792dc2c125b8acc9d31cda8792c /contrib/python/pypodman | |
parent | 462c503a4762a0f20023d937a5fb05a55d4183a3 (diff) | |
download | podman-3bfe07b4f583b9526b616ec7e49332994ca5bc2e.tar.gz podman-3bfe07b4f583b9526b616ec7e49332994ca5bc2e.tar.bz2 podman-3bfe07b4f583b9526b616ec7e49332994ca5bc2e.zip |
Support Attach subcommand in pypodman
* Fix some random error handling
Signed-off-by: Jhon Honce <jhonce@redhat.com>
Closes: #1296
Approved by: rhatdan
Diffstat (limited to 'contrib/python/pypodman')
-rw-r--r-- | contrib/python/pypodman/pypodman/lib/actions/__init__.py | 2 | ||||
-rw-r--r-- | contrib/python/pypodman/pypodman/lib/actions/attach_action.py | 68 | ||||
-rwxr-xr-x | contrib/python/pypodman/pypodman/main.py | 3 |
3 files changed, 73 insertions, 0 deletions
diff --git a/contrib/python/pypodman/pypodman/lib/actions/__init__.py b/contrib/python/pypodman/pypodman/lib/actions/__init__.py index f594f05e5..098f033e9 100644 --- a/contrib/python/pypodman/pypodman/lib/actions/__init__.py +++ b/contrib/python/pypodman/pypodman/lib/actions/__init__.py @@ -1,4 +1,5 @@ """Module to export all the podman subcommands.""" +from pypodman.lib.actions.attach_action import Attach from pypodman.lib.actions.create_action import Create from pypodman.lib.actions.images_action import Images from pypodman.lib.actions.ps_action import Ps @@ -7,6 +8,7 @@ from pypodman.lib.actions.rm_action import Rm from pypodman.lib.actions.rmi_action import Rmi __all__ = [ + 'Attach', 'Create', 'Images', 'Ps', diff --git a/contrib/python/pypodman/pypodman/lib/actions/attach_action.py b/contrib/python/pypodman/pypodman/lib/actions/attach_action.py new file mode 100644 index 000000000..4a6d578f1 --- /dev/null +++ b/contrib/python/pypodman/pypodman/lib/actions/attach_action.py @@ -0,0 +1,68 @@ +"""Remote client command for attaching to a container.""" +import sys + +import podman +from pypodman.lib import AbstractActionBase + + +class Attach(AbstractActionBase): + """Class for attaching to a running container.""" + + @classmethod + def subparser(cls, parent): + """Add Attach command to parent parser.""" + parser = parent.add_parser('attach', help='attach to container') + parser.add_argument( + '--image', + help='image to instantiate and attach to', + ) + parser.add_argument( + 'command', + nargs='*', + help='image to instantiate and attach to', + ) + parser.set_defaults(class_=cls, method='attach') + + def __init__(self, args): + """Construct Attach class.""" + super().__init__(args) + if not args.image: + raise ValueError('You must supply at least one image id' + ' or name to be attached.') + + def attach(self): + """Attach to instantiated image.""" + args = { + 'detach': True, + 'tty': True, + } + if self._args.command: + args['command'] = self._args.command + + try: + try: + ident = self.client.images.pull(self._args.image) + img = self.client.images.get(ident) + except podman.ImageNotFound as e: + sys.stdout.flush() + print( + 'Image {} not found.'.format(e.name), + file=sys.stderr, + flush=True) + return 1 + + ctnr = img.create(**args) + ctnr.attach(eot=4) + + try: + ctnr.start() + print() + except (BrokenPipeError, KeyboardInterrupt): + print('\nContainer disconnected.') + except podman.ErrorOccurred as e: + sys.stdout.flush() + print( + '{}'.format(e.reason).capitalize(), + file=sys.stderr, + flush=True) + return 1 diff --git a/contrib/python/pypodman/pypodman/main.py b/contrib/python/pypodman/pypodman/main.py index 047edfa0d..e512dc483 100755 --- a/contrib/python/pypodman/pypodman/main.py +++ b/contrib/python/pypodman/pypodman/main.py @@ -45,6 +45,9 @@ def main(): except AttributeError: parser.print_help(sys.stderr) sys.exit(1) + except ValueError as e: + print(e, file=sys.stderr, flush=True) + sys.exit(1) except Exception as e: # pylint: disable=broad-except logging.critical(repr(e), exc_info=want_tb()) logging.warning('See subparser "%s" configuration.', |