From e56cdbff2e9562668092069d1d3e07a0e80b5da1 Mon Sep 17 00:00:00 2001 From: Sofer Athlan-Guyot Date: Thu, 9 Jul 2020 13:05:12 +0200 Subject: [PATCH] Add a strict option to per role tasks file generation. For upgrade and deployment we generate tasks files based on the step tasks must run in. By default, all tasks that doesn't have a matching conditional are included in all step files. We add a option to be able to control this behavior. PER_STEP_TASKS becomes a dictionary with key being the current config collected and the value is an array. Each element of the array map to the steps: element 0 is step0 and so on. That element tell the _write_tasks_per_step function if we include tasks with no conditional when it is False or excluded when it is True. This structure opens four possibilities: - allow to have step task of different length: for instance if we want to expand post update tasks which have a length of four (not DEFAULT_STEPS_MAX) - In testing, we can identify the tasks with no conditional by putting all normal step to strict (set True for every step) and add a extra one set to False. The extra one will have the steps with no conditional. - Identify when the conditional matcher in _write_tasks_per_step fails. - Eventually, set everything to strict to force the developer to properly add conditional to every tasks. As it is, the change has no impact and keep the current default behavior. Change-Id: I2033efd47c09707797a4b48a853517d42f584e76 (cherry picked from commit 7c67777f4a256bfc8c5285294769671f0922d479) --- tripleo_common/constants.py | 7 ++++++- tripleo_common/utils/config.py | 31 +++++++++++++++++++++---------- 2 files changed, 27 insertions(+), 11 deletions(-) diff --git a/tripleo_common/constants.py b/tripleo_common/constants.py index dfcd22a6f..3e966b1c3 100644 --- a/tripleo_common/constants.py +++ b/tripleo_common/constants.py @@ -220,4 +220,9 @@ EXCLUSIVE_NEUTRON_DRIVERS = ['ovn', 'openvswitch'] DEFAULT_STEPS_MAX = 6 -PER_STEP_TASKS = ['upgrade_tasks', 'deploy_steps_tasks'] +_PER_STEP_TASK_STRICTNESS = [False for i in range(DEFAULT_STEPS_MAX)] + +PER_STEP_TASKS = { + 'upgrade_tasks': _PER_STEP_TASK_STRICTNESS, + 'deploy_steps_tasks': _PER_STEP_TASK_STRICTNESS +} diff --git a/tripleo_common/utils/config.py b/tripleo_common/utils/config.py index 0a0ca34e4..38215dfa5 100644 --- a/tripleo_common/utils/config.py +++ b/tripleo_common/utils/config.py @@ -121,14 +121,18 @@ class Config(object): return os.fdopen( os.open(path, os.O_WRONLY | os.O_CREAT | os.O_TRUNC, 0o600), 'w') - def _write_tasks_per_step(self, tasks, filepath, step): + def _write_tasks_per_step(self, tasks, filepath, step, strict=False): - def step_in_task(task, step): + def step_in_task(task, step, strict): whenexpr = task.get('when', None) if whenexpr is None: - # If no step is defined, it will be executed for all - # steps. - return True + if not strict: + # If no step is defined, it will be executed for all + # steps if strict is false + return True + else: + # We only want the task with the step defined. + return False if not isinstance(whenexpr, list): whenexpr = [whenexpr] for w in whenexpr: @@ -145,9 +149,14 @@ class Config(object): return True else: return False + # No match + if strict: + return False return True - tasks_per_step = [task for task in tasks if step_in_task(task, step)] + tasks_per_step = [task for task in tasks if step_in_task(task, + step, + strict)] with self._open_file(filepath) as conf_file: yaml.safe_dump(tasks_per_step, conf_file, default_flow_style=False) return tasks_per_step @@ -264,12 +273,14 @@ class Config(object): # run per step. # We include it here to allow the CI to pass until THT # changed is not merged. - if config in constants.PER_STEP_TASKS: - for i in range(constants.DEFAULT_STEPS_MAX): + if config in constants.PER_STEP_TASKS.keys(): + for i in range(len(constants.PER_STEP_TASKS[config])): filepath = os.path.join(role_path, '%s_step%s.yaml' % (config, i)) - self._write_tasks_per_step(role[config], - filepath, i) + self._write_tasks_per_step( + role[config], + filepath, + i, constants.PER_STEP_TASKS[config][i]) try: data = role[config]