diff --git a/paunch/builder/base.py b/paunch/builder/base.py index f06776b..cbf035e 100644 --- a/paunch/builder/base.py +++ b/paunch/builder/base.py @@ -238,6 +238,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): """Prepare the exec command args, from the container configuration. diff --git a/paunch/builder/compose1.py b/paunch/builder/compose1.py index 5f9e409..c2b4abf 100644 --- a/paunch/builder/compose1.py +++ b/paunch/builder/compose1.py @@ -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') diff --git a/paunch/builder/podman.py b/paunch/builder/podman.py index bf6d424..5bd9044 100644 --- a/paunch/builder/podman.py +++ b/paunch/builder/podman.py @@ -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') diff --git a/paunch/tests/test_builder_base.py b/paunch/tests/test_builder_base.py index b55a5c4..6b9e97e 100644 --- a/paunch/tests/test_builder_base.py +++ b/paunch/tests/test_builder_base.py @@ -536,7 +536,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', @@ -545,7 +545,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'], @@ -563,7 +563,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',