Skip to content

Commit

Permalink
Merge pull request #1823 from jwhonce/wip/cmd
Browse files Browse the repository at this point in the history
Implement pypodman start command
  • Loading branch information
openshift-merge-robot committed Nov 19, 2018
2 parents 47ffaae + 0d21b90 commit 4eecc8c
Show file tree
Hide file tree
Showing 4 changed files with 97 additions and 18 deletions.
8 changes: 6 additions & 2 deletions contrib/python/podman/podman/libs/_containers_attach.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,13 @@ def attach(self, eot=4, stdin=None, stdout=None):
"""
if stdin is None:
stdin = sys.stdin.fileno()
elif hasattr(stdin, 'fileno'):
stdin = stdin.fileno()

if stdout is None:
stdout = sys.stdout.fileno()
elif hasattr(stdout, 'fileno'):
stdout = stdout.fileno()

with self._client() as podman:
attach = podman.GetAttachSockets(self._id)
Expand Down Expand Up @@ -49,7 +53,7 @@ def attach(self, eot=4, stdin=None, stdout=None):
def resize_handler(self):
"""Send the new window size to conmon."""

def wrapped(signum, frame):
def wrapped(signum, frame): # pylint: disable=unused-argument
packed = fcntl.ioctl(self.pseudo_tty.stdout, termios.TIOCGWINSZ,
struct.pack('HHHH', 0, 0, 0, 0))
rows, cols, _, _ = struct.unpack('HHHH', packed)
Expand All @@ -67,7 +71,7 @@ def wrapped(signum, frame):
def log_handler(self):
"""Send command to reopen log to conmon."""

def wrapped(signum, frame):
def wrapped(signum, frame): # pylint: disable=unused-argument
with open(self.pseudo_tty.control_socket, 'w') as skt:
# send conmon reopen log message
skt.write('2\n')
Expand Down
2 changes: 2 additions & 0 deletions contrib/python/pypodman/pypodman/lib/actions/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
from pypodman.lib.actions.rmi_action import Rmi
from pypodman.lib.actions.run_action import Run
from pypodman.lib.actions.search_action import Search
from pypodman.lib.actions.start_action import Start
from pypodman.lib.actions.version_action import Version

__all__ = [
Expand All @@ -48,5 +49,6 @@
'Rmi',
'Run',
'Search',
'Start',
'Version',
]
76 changes: 76 additions & 0 deletions contrib/python/pypodman/pypodman/lib/actions/start_action.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
"""Remote client command for starting containers."""
import sys

import podman
from pypodman.lib import AbstractActionBase, BooleanAction


class Start(AbstractActionBase):
"""Class for starting container."""

@classmethod
def subparser(cls, parent):
"""Add Start command to parent parser."""
parser = parent.add_parser('start', help='start container')
parser.add_argument(
'--attach',
'-a',
action=BooleanAction,
default=False,
help="Attach container's STDOUT and STDERR (default: %(default)s)")
parser.add_argument(
'--detach-keys',
metavar='KEY(s)',
default=4,
help='Override the key sequence for detaching a container.'
' (format: a single character [a-Z] or ctrl-<value> where'
' <value> is one of: a-z, @, ^, [, , or _) (default: ^D)')
parser.add_argument(
'--interactive',
'-i',
action=BooleanAction,
default=False,
help="Attach container's STDIN (default: %(default)s)")
# TODO: Implement sig-proxy
parser.add_argument(
'--sig-proxy',
action=BooleanAction,
default=False,
help="Proxy received signals to the process (default: %(default)s)"
)
parser.add_argument(
'containers',
nargs='+',
help='containers to start',
)
parser.set_defaults(class_=cls, method='start')

def start(self):
"""Start provided containers."""
stdin = sys.stdin if self.opts['interactive'] else None
stdout = sys.stdout if self.opts['attach'] else None

try:
for ident in self._args.containers:
try:
ctnr = self.client.containers.get(ident)
ctnr.attach(
eot=self.opts['detach_keys'],
stdin=stdin,
stdout=stdout)
ctnr.start()
except podman.ContainerNotFound as e:
sys.stdout.flush()
print(
'Container "{}" not found'.format(e.name),
file=sys.stderr,
flush=True)
else:
print(ident)
except podman.ErrorOccurred as e:
sys.stdout.flush()
print(
'{}'.format(e.reason).capitalize(),
file=sys.stderr,
flush=True)
return 1
29 changes: 13 additions & 16 deletions contrib/python/pypodman/pypodman/lib/parser_actions.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ def __init__(self,
const=None,
default=None,
type=None,
choices=('True', 'False'),
choices=None,
required=False,
help=None,
metavar='{True,False}'):
Expand All @@ -59,7 +59,7 @@ def __call__(self, parser, namespace, values, option_string=None):
try:
val = BooleanValidate()(values)
except ValueError:
parser.error('{} must be True or False.'.format(self.dest))
parser.error('"{}" must be True or False.'.format(option_string))
else:
setattr(namespace, self.dest, val)

Expand Down Expand Up @@ -96,7 +96,6 @@ def __init__(self,

def __call__(self, parser, namespace, values, option_string=None):
"""Convert and Validate input."""
print(self.dest)
items = getattr(namespace, self.dest, None) or []
items = copy.copy(items)

Expand All @@ -105,9 +104,9 @@ def __call__(self, parser, namespace, values, option_string=None):

opt, val = values.split('=', 1)
if opt not in choices:
parser.error('{} is not a supported "--change" option,'
parser.error('Option "{}" is not supported by argument "{}",'
' valid options are: {}'.format(
opt, ', '.join(choices)))
opt, option_string, ', '.join(choices)))
items.append(values)
setattr(namespace, self.dest, items)

Expand All @@ -127,8 +126,8 @@ def __init__(self,
help=None,
metavar='UNIT'):
"""Create UnitAction object."""
help = (help or metavar or dest
) + ' (format: <number>[<unit>], where unit = b, k, m or g)'
help = (help or metavar or dest)\
+ ' (format: <number>[<unit>], where unit = b, k, m or g)'
super().__init__(
option_strings=option_strings,
dest=dest,
Expand All @@ -148,15 +147,15 @@ def __call__(self, parser, namespace, values, option_string=None):
except ValueError:
if not values[:-1].isdigit():
msg = ('{} must be a positive integer,'
' with optional suffix').format(self.dest)
' with optional suffix').format(option_string)
parser.error(msg)
if not values[-1] in ('b', 'k', 'm', 'g'):
msg = '{} only supports suffices of: b, k, m, g'.format(
self.dest)
option_string)
parser.error(msg)
else:
if val <= 0:
msg = '{} must be a positive integer'.format(self.dest)
msg = '{} must be a positive integer'.format(option_string)
parser.error(msg)

setattr(namespace, self.dest, values)
Expand All @@ -174,19 +173,16 @@ def __init__(self,
type=int,
choices=None,
required=False,
help=None,
help='Must be a positive integer.',
metavar=None):
"""Create PositiveIntAction object."""
self.message = '{} must be a positive integer'.format(dest)
help = help or self.message

super().__init__(
option_strings=option_strings,
dest=dest,
nargs=nargs,
const=const,
default=default,
type=int,
type=type,
choices=choices,
required=required,
help=help,
Expand All @@ -198,7 +194,8 @@ def __call__(self, parser, namespace, values, option_string=None):
setattr(namespace, self.dest, values)
return

parser.error(self.message)
msg = '{} must be a positive integer'.format(option_string)
parser.error(msg)


class PathAction(argparse.Action):
Expand Down

0 comments on commit 4eecc8c

Please sign in to comment.