Browse Source

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 0560631b2d)
changes/38/705438/1
Emilien Macchi 1 month ago
parent
commit
8ff14f1ff3
2 changed files with 22 additions and 1 deletions
  1. +12
    -0
      paunch/tests/test_utils_common.py
  2. +10
    -1
      paunch/utils/common.py

+ 12
- 0
paunch/tests/test_utils_common.py View File

@@ -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))

+ 10
- 1
paunch/utils/common.py View File

@@ -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

Loading…
Cancel
Save