dockerng: Allow both cmd and command to be used to specify command

This commit is contained in:
Erik Johnson 2015-09-23 09:11:29 -05:00
parent a789303d75
commit 7d4eaac8ae
2 changed files with 52 additions and 15 deletions

View File

@ -343,7 +343,7 @@ argument name:
'''
VALID_CREATE_OPTS = {
'command': {
'cmd': {
'path': 'Config:Cmd',
},
'hostname': {
@ -1099,25 +1099,25 @@ def _validate_input(action,
raise SaltInvocationError(key + ' must be a list of strings')
# Custom validation functions for container creation options
def _valid_command(): # pylint: disable=unused-variable
def _valid_cmd(): # pylint: disable=unused-variable
'''
Must be either a string or a list of strings. Value will be translated
to a list of strings
'''
if kwargs.get('command') is None:
if kwargs.get('cmd') is None:
# No need to validate
return
if isinstance(kwargs['command'], six.string_types):
if isinstance(kwargs['cmd'], six.string_types):
# Translate command into a list of strings
try:
kwargs['command'] = shlex.split(kwargs['command'])
kwargs['cmd'] = shlex.split(kwargs['cmd'])
except AttributeError:
pass
try:
_valid_stringlist('command')
_valid_stringlist('cmd')
except SaltInvocationError:
raise SaltInvocationError(
'command must be a string or list of strings'
'cmd must be a string or list of strings'
)
def _valid_user(): # pylint: disable=unused-variable
@ -2507,10 +2507,13 @@ def create(image,
image
Image from which to create the container
command
cmd or command
Command to run in the container
Example: ``command=bash``
Example: ``cmd=bash`` or ``command=bash``
.. versionchanged:: 2015.8.1
``cmd`` is now also accepted
hostname
Hostname of the container. If not provided, and if a ``name`` has been
@ -2643,6 +2646,14 @@ def create(image,
# Create a CentOS 7 container that will stay running once started
salt myminion dockerng.create centos:7 name=mycent7 interactive=True tty=True command=bash
'''
if 'command' in kwargs:
if 'cmd' in kwargs:
raise SaltInvocationError(
'Only one of \'cmd\' and \'command\' can be used. Both '
'arguments are equivalent.'
)
kwargs['cmd'] = kwargs.pop('command')
try:
# Try to inspect the image, if it fails then we know we need to pull it
# first.
@ -2670,8 +2681,10 @@ def create(image,
# Added to manage api change in 1.19.
# mem_limit and memswap_limit must be provided in host_config object
if salt.utils.version_cmp(version()['ApiVersion'], '1.18') == 1:
create_kwargs['host_config'] = docker.utils.create_host_config(mem_limit=create_kwargs.get('mem_limit'),
memswap_limit=create_kwargs.get('memswap_limit'))
create_kwargs['host_config'] = docker.utils.create_host_config(
mem_limit=create_kwargs.get('mem_limit'),
memswap_limit=create_kwargs.get('memswap_limit')
)
if 'mem_limit' in create_kwargs:
del create_kwargs['mem_limit']
if 'memswap_limit' in create_kwargs:

View File

@ -331,8 +331,8 @@ def _compare(actual, create_kwargs, runtime_kwargs):
continue
elif isinstance(data, list):
# Compare two sorted lists of items. Won't work for "command"
# or "entrypoint" because those are both shell commands and the
# Compare two sorted lists of items. Won't work for "cmd" or
# "entrypoint" because those are both shell commands and the
# original order matters. It will, however, work for "volumes"
# because even though "volumes" is a sub-dict nested within the
# "actual" dict sorted(somedict) still just gives you a sorted
@ -416,6 +416,7 @@ def image_present(name,
'changes': {},
'result': False,
'comment': ''}
if build is not None and load is not None:
ret['comment'] = 'Only one of \'build\' or \'load\' is permitted.'
return ret
@ -731,9 +732,18 @@ def running(name,
**CONTAINER CONFIGURATION PARAMETERS**
command
cmd or command
Command to run in the container
.. code-block:: yaml
foo:
dockerng.running:
- image: bar/baz:latest
- cmd: bash
OR
.. code-block:: yaml
foo:
@ -741,6 +751,9 @@ def running(name,
- image: bar/baz:latest
- command: bash
.. versionchanged:: 2015.8.1
``cmd`` is now also accepted
hostname
Hostname of the container. If not provided, and if a ``name`` has been
provided, the ``hostname`` will default to the ``name`` that was
@ -798,7 +811,8 @@ def running(name,
- tty: True
detach : True
If ``True``, run ``command`` in the background (daemon mode)
If ``True``, run the container's command in the background (daemon
mode)
.. code-block:: yaml
@ -1330,6 +1344,16 @@ def running(name,
ret['comment'] = 'The \'image\' argument is required'
return ret
if 'command' in kwargs:
if 'cmd' in kwargs:
ret['comment'] = (
'Only one of \'cmd\' and \'command\' can be used. Both '
'arguments are equivalent.'
)
ret['result'] = False
return ret
kwargs['cmd'] = kwargs.pop('command')
try:
image = ':'.join(_get_repo_tag(image))
except TypeError: