From 3bfe07b4f583b9526b616ec7e49332994ca5bc2e Mon Sep 17 00:00:00 2001 From: Jhon Honce Date: Thu, 16 Aug 2018 15:51:26 -0700 Subject: Support Attach subcommand in pypodman * Fix some random error handling Signed-off-by: Jhon Honce Closes: #1296 Approved by: rhatdan --- contrib/python/podman/podman/client.py | 2 +- .../pypodman/pypodman/lib/actions/__init__.py | 2 + .../pypodman/pypodman/lib/actions/attach_action.py | 68 ++++++++++++++++++++++ contrib/python/pypodman/pypodman/main.py | 3 + 4 files changed, 74 insertions(+), 1 deletion(-) create mode 100644 contrib/python/pypodman/pypodman/lib/actions/attach_action.py (limited to 'contrib/python') diff --git a/contrib/python/podman/podman/client.py b/contrib/python/podman/podman/client.py index b61506cda..24df65e23 100644 --- a/contrib/python/podman/podman/client.py +++ b/contrib/python/podman/podman/client.py @@ -45,7 +45,7 @@ class BaseClient(): raise ValueError('interface is required and cannot be None') unsupported = set(kwargs.keys()).difference( - ('uri', 'interface', 'remote_uri', 'port', 'identity_file')) + ('uri', 'interface', 'remote_uri', 'identity_file')) if unsupported: raise ValueError('Unknown keyword arguments: {}'.format( ', '.join(unsupported))) 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.', -- cgit v1.2.3-54-g00ecf