builder: allow to pass a dict to 'environment'
To allow to pass container environment as a dict instead of a list. It's backward compatible, a list can still be passed. We introduce a new function "list_or_dict_arg" so we can re-use it for other parameters later if needed. Change-Id: I85999889d3328dc9d2116b8539ac959b39cb833a
This commit is contained in:
parent
2af0996fa1
commit
cfa2fc51ab
|
@ -231,6 +231,21 @@ class BaseBuilder(object):
|
|||
if v:
|
||||
cmd.append('%s=%s' % (arg, v))
|
||||
|
||||
def list_or_dict_arg(self, cconfig, cmd, key, arg):
|
||||
if key not in cconfig:
|
||||
return
|
||||
value = cconfig[key]
|
||||
if isinstance(value, dict):
|
||||
for k, v in sorted(value.items()):
|
||||
if v:
|
||||
cmd.append('%s=%s=%s' % (arg, k, v))
|
||||
elif k:
|
||||
cmd.append('%s=%s' % (arg, k))
|
||||
else:
|
||||
for v in value:
|
||||
if v:
|
||||
cmd.append('%s=%s' % (arg, v))
|
||||
|
||||
def cont_exec_args(self, cmd, container, delegate=None):
|
||||
"""Prepare the exec command args, from the container configuration.
|
||||
|
||||
|
|
|
@ -37,10 +37,7 @@ class ComposeV1Builder(base.BaseBuilder):
|
|||
if cconfig.get('detach', True):
|
||||
cmd.append('--detach=true')
|
||||
self.list_or_string_arg(cconfig, cmd, 'env_file', '--env-file')
|
||||
# TODO(sbaker): support the dict layout for this property
|
||||
for v in cconfig.get('environment', []):
|
||||
if v:
|
||||
cmd.append('--env=%s' % v)
|
||||
self.list_or_dict_arg(cconfig, cmd, 'environment', '--env')
|
||||
self.boolean_arg(cconfig, cmd, 'remove', '--rm')
|
||||
self.boolean_arg(cconfig, cmd, 'interactive', '--interactive')
|
||||
self.boolean_arg(cconfig, cmd, 'tty', '--tty')
|
||||
|
|
|
@ -59,10 +59,7 @@ class PodmanBuilder(base.BaseBuilder):
|
|||
raise ValueError('cont_log_path passed but not absolute.')
|
||||
|
||||
self.list_or_string_arg(cconfig, cmd, 'env_file', '--env-file')
|
||||
# TODO(sbaker): support the dict layout for this property
|
||||
for v in cconfig.get('environment', []):
|
||||
if v:
|
||||
cmd.append('--env=%s' % v)
|
||||
self.list_or_dict_arg(cconfig, cmd, 'environment', '--env')
|
||||
self.boolean_arg(cconfig, cmd, 'remove', '--rm')
|
||||
self.boolean_arg(cconfig, cmd, 'interactive', '--interactive')
|
||||
self.boolean_arg(cconfig, cmd, 'tty', '--tty')
|
||||
|
|
|
@ -503,7 +503,7 @@ three-12345678 three''', '', 0),
|
|||
)
|
||||
|
||||
@mock.patch('paunch.runner.DockerRunner', autospec=True)
|
||||
def test_container_run_args_lists_with_cpu(self, runner):
|
||||
def test_container_run_args_lists_with_cpu_and_dict_env(self, runner):
|
||||
config = {
|
||||
'one': {
|
||||
'image': 'centos:7',
|
||||
|
@ -512,7 +512,7 @@ three-12345678 three''', '', 0),
|
|||
'remove': True,
|
||||
'tty': True,
|
||||
'interactive': True,
|
||||
'environment': ['FOO=BAR', 'BAR=BAZ'],
|
||||
'environment': {'BAR': 'BAZ', 'FOO': 'BAR', 'SINGLE': ''},
|
||||
'env_file': ['/tmp/foo.env', '/tmp/bar.env'],
|
||||
'ulimit': ['nofile=1024', 'nproc=1024'],
|
||||
'volumes': ['/foo:/foo:rw', '/bar:/bar:ro'],
|
||||
|
@ -530,7 +530,7 @@ three-12345678 three''', '', 0),
|
|||
self.assertEqual(
|
||||
['docker', 'run', '--name', 'one',
|
||||
'--env-file=/tmp/foo.env', '--env-file=/tmp/bar.env',
|
||||
'--env=FOO=BAR', '--env=BAR=BAZ',
|
||||
'--env=BAR=BAZ', '--env=FOO=BAR', '--env=SINGLE',
|
||||
'--rm', '--interactive', '--tty',
|
||||
'--ulimit=nofile=1024', '--ulimit=nproc=1024',
|
||||
'--group-add=docker', '--group-add=zuul',
|
||||
|
|
Loading…
Reference in New Issue