mirror of
https://github.com/containers/podman.git
synced 2025-06-19 16:33:24 +08:00
Support Attach subcommand in pypodman
* Fix some random error handling Signed-off-by: Jhon Honce <jhonce@redhat.com> Closes: #1296 Approved by: rhatdan
This commit is contained in:
@ -45,7 +45,7 @@ class BaseClient():
|
|||||||
raise ValueError('interface is required and cannot be None')
|
raise ValueError('interface is required and cannot be None')
|
||||||
|
|
||||||
unsupported = set(kwargs.keys()).difference(
|
unsupported = set(kwargs.keys()).difference(
|
||||||
('uri', 'interface', 'remote_uri', 'port', 'identity_file'))
|
('uri', 'interface', 'remote_uri', 'identity_file'))
|
||||||
if unsupported:
|
if unsupported:
|
||||||
raise ValueError('Unknown keyword arguments: {}'.format(
|
raise ValueError('Unknown keyword arguments: {}'.format(
|
||||||
', '.join(unsupported)))
|
', '.join(unsupported)))
|
||||||
|
@ -1,4 +1,5 @@
|
|||||||
"""Module to export all the podman subcommands."""
|
"""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.create_action import Create
|
||||||
from pypodman.lib.actions.images_action import Images
|
from pypodman.lib.actions.images_action import Images
|
||||||
from pypodman.lib.actions.ps_action import Ps
|
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
|
from pypodman.lib.actions.rmi_action import Rmi
|
||||||
|
|
||||||
__all__ = [
|
__all__ = [
|
||||||
|
'Attach',
|
||||||
'Create',
|
'Create',
|
||||||
'Images',
|
'Images',
|
||||||
'Ps',
|
'Ps',
|
||||||
|
@ -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
|
@ -45,6 +45,9 @@ def main():
|
|||||||
except AttributeError:
|
except AttributeError:
|
||||||
parser.print_help(sys.stderr)
|
parser.print_help(sys.stderr)
|
||||||
sys.exit(1)
|
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
|
except Exception as e: # pylint: disable=broad-except
|
||||||
logging.critical(repr(e), exc_info=want_tb())
|
logging.critical(repr(e), exc_info=want_tb())
|
||||||
logging.warning('See subparser "%s" configuration.',
|
logging.warning('See subparser "%s" configuration.',
|
||||||
|
Reference in New Issue
Block a user