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 7c67777f4a)
(cherry picked from commit e56cdbff2e)
This commit is contained in:
Sofer Athlan-Guyot 2020-07-09 13:05:12 +02:00
parent 2f6304e42a
commit 6b5fde5e91
2 changed files with 27 additions and 11 deletions

View File

@ -217,4 +217,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
}

View File

@ -115,14 +115,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:
@ -139,9 +143,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
@ -258,12 +267,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]