From 8ff14f1ff3d8fcc0366a536c731beec74e023b19 Mon Sep 17 00:00:00 2001 From: Emilien Macchi Date: Wed, 8 Jan 2020 22:28:42 -0500 Subject: [PATCH] load_config: allow the config to be overriden This will enable an operator using the paunch Ansible module to override any container parameter e.g. the image, directly from Ansible without modifying the config that is on the host. Change-Id: I6639ab859b120aa9349dc72b0d6e7d575be20c7a (cherry picked from commit 0560631b2da0b8130e7c162cbd0ee43b00106f42) --- paunch/tests/test_utils_common.py | 12 ++++++++++++ paunch/utils/common.py | 11 ++++++++++- 2 files changed, 22 insertions(+), 1 deletion(-) diff --git a/paunch/tests/test_utils_common.py b/paunch/tests/test_utils_common.py index 9210f1c..1e8a9ef 100644 --- a/paunch/tests/test_utils_common.py +++ b/paunch/tests/test_utils_common.py @@ -39,8 +39,10 @@ class TestUtilsCommonConfig(base.TestCase): def setUp(self): super(TestUtilsCommonConfig, self).setUp() self.config_content = "{'image': 'docker.io/haproxy'}" + self.config_override = {'haproxy': {'image': 'quay.io/haproxy'}} self.open_func = 'paunch.utils.common.open' self.expected_config = {'haproxy': {'image': 'docker.io/haproxy'}} + self.expected_config_over = {'haproxy': {'image': 'quay.io/haproxy'}} self.container = 'haproxy' self.old_config_file = '/var/lib/tripleo-config/' + \ 'hashed-container-startup-config-step_1.json' @@ -104,3 +106,13 @@ class TestUtilsCommonConfig(base.TestCase): self.assertEqual( self.expected_config, common.load_config(self.old_config_file)) + + @mock.patch('os.path.isdir') + def test_load_config_dir_with_name_and_override(self, mock_isdir): + mock_isdir.return_value = True + mock_open = mock.mock_open(read_data=self.config_content) + with mock.patch(self.open_func, mock_open): + self.assertEqual( + self.expected_config_over, + common.load_config('/config_dir', self.container, + self.config_override)) diff --git a/paunch/utils/common.py b/paunch/utils/common.py index 6805a10..a2ebffa 100644 --- a/paunch/utils/common.py +++ b/paunch/utils/common.py @@ -86,8 +86,10 @@ def get_all_cpus(**args): return "0-" + str(psutil.cpu_count() - 1) -def load_config(config, name=None): +def load_config(config, name=None, overrides=None): container_config = {} + if overrides is None: + overrides = {} if os.path.isdir(config): # When the user gives a config directory and specify a container name, # we return the container config for that specific container. @@ -150,4 +152,11 @@ def load_config(config, name=None): with open(os.path.join(config), 'r') as f: container_config[name] = {} container_config[name].update(yaml.safe_load(f)) + + # Overrides + for k in overrides.keys(): + if k in container_config: + for mk, mv in overrides[k].items(): + container_config[k][mk] = mv + return container_config