From cf177110412dce388444af873826dbdc5406f12e Mon Sep 17 00:00:00 2001 From: Namrata Sitlani Date: Mon, 9 Jan 2017 20:23:16 +0000 Subject: [PATCH] Add OSC Plugin for openstack appcontainer run This change implements the "openstack appcontainer run" command Based on existing zun command: zun run Partially Implements:blueprint zun-osc-plugin Co-Authored-By: Sheel Rana Depends-On: I2120e2ae9bda2eea84f1459f768405bd7c677849 Change-Id: I45afcb2b554f87fc019ec171a83bd7dfae14e5ba --- setup.cfg | 1 + zunclient/osc/v1/containers.py | 74 ++++++++++++++++++++++++++++++++++ 2 files changed, 75 insertions(+) diff --git a/setup.cfg b/setup.cfg index 5cd5047b..1bf9b860 100644 --- a/setup.cfg +++ b/setup.cfg @@ -44,6 +44,7 @@ openstack.container.v1 = appcontainer_logs = zunclient.osc.v1.containers:LogsContainer appcontainer_kill = zunclient.osc.v1.containers:KillContainer appcontainer_stop = zunclient.osc.v1.containers:StopContainer + appcontainer_run = zunclient.osc.v1.containers:RunContainer [build_sphinx] source-dir = doc/source diff --git a/zunclient/osc/v1/containers.py b/zunclient/osc/v1/containers.py index 6fe90305..a045cd1f 100644 --- a/zunclient/osc/v1/containers.py +++ b/zunclient/osc/v1/containers.py @@ -403,3 +403,77 @@ class StopContainer(command.Command): except Exception as e: print("Stop for container %(container)s failed: %(e)s" % {'container': container, 'e': e}) + + +class RunContainer(command.ShowOne): + """Creates and run a new container""" + + log = logging.getLogger(__name__ + ".RunContainer") + + def get_parser(self, prog_name): + parser = super(RunContainer, self).get_parser(prog_name) + parser.add_argument( + '--name', + metavar='', + help='name of the container') + parser.add_argument( + 'image', + metavar='', + help='name or ID of the image') + parser.add_argument( + '--command', + metavar='', + help='Send command to the container') + parser.add_argument( + '--cpu', + metavar='', + help='The number of virtual cpus.') + parser.add_argument( + '--memory', + metavar='', + help='The container memory size in MiB') + parser.add_argument( + '--environment', + metavar='', + action='append', default=[], + help='The environment variables') + parser.add_argument( + '--workdir', + metavar='', + help='The working directory for commands to run in') + parser.add_argument( + '--label', + metavar='', + action='append', default=[], + help='Adds a map of labels to a container. ' + 'May be used multiple times.') + parser.add_argument( + '--image-pull-policy', + dest='image_pull_policy', + metavar='', + choices=['never', 'always', 'ifnotpresent'], + help='The policy which determines if the image should ' + 'be pulled prior to starting the container. ' + 'It can have following values: ' + '"ifnotpresent": only pull the image if it does not ' + 'already exist on the node. ' + '"always": Always pull the image from repositery.' + '"never": never pull the image') + return parser + + def take_action(self, parsed_args): + client = _get_client(self, parsed_args) + opts = {} + opts['name'] = parsed_args.name + opts['image'] = parsed_args.image + opts['command'] = parsed_args.command + opts['memory'] = parsed_args.memory + opts['cpu'] = parsed_args.cpu + opts['environment'] = zun_utils.format_args(parsed_args.environment) + opts['workdir'] = parsed_args.workdir + opts['labels'] = zun_utils.format_args(parsed_args.label) + opts['image_pull_policy'] = parsed_args.image_pull_policy + + container = client.containers.run(**opts) + columns = _container_columns(container) + return columns, utils.get_item_properties(container, columns)