summaryrefslogtreecommitdiff
path: root/contrib/python/pypodman
diff options
context:
space:
mode:
Diffstat (limited to 'contrib/python/pypodman')
-rw-r--r--contrib/python/pypodman/pypodman/lib/actions/__init__.py2
-rw-r--r--contrib/python/pypodman/pypodman/lib/actions/attach_action.py68
-rwxr-xr-xcontrib/python/pypodman/pypodman/main.py3
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.',