mirror of
https://bitbucket.org/atlassian/dc-deployments-automation.git
synced 2025-12-14 00:43:06 -06:00
DCD-1205: Simplify the pipelines generator.
This commit is contained in:
@@ -6,57 +6,25 @@ PIPELINE_TEMPLATE_J2_FILE = 'templates/bitbucket-pipelines.yml.j2'
|
||||
ROLES_DIR = 'roles/'
|
||||
|
||||
|
||||
class Pipeline:
|
||||
def generate_pipeline(self):
|
||||
template_string = self._load_template_as_string()
|
||||
template = Template(template_string)
|
||||
steps = self._build_steps()
|
||||
generated_output = template.render(parallel_steps=steps)
|
||||
print(generated_output)
|
||||
|
||||
def _build_steps(self):
|
||||
return [Step(scenario_name, self._build_script_commands(index))
|
||||
for index, (scenario_rel_path, scenario_name) in
|
||||
enumerate(self._find_all_scenarios(), 1)]
|
||||
|
||||
@staticmethod
|
||||
def _build_script_commands(index):
|
||||
return ScriptCommand(f"./bin/run-tests-in-batches --batch {index}").all_commands()
|
||||
|
||||
@staticmethod
|
||||
def _find_all_scenarios():
|
||||
scenario_dirs = []
|
||||
for root, dirs, files in os.walk(Path(os.path.join(os.path.dirname(__file__), "..", ROLES_DIR))):
|
||||
[scenario_dirs.append(root) for f in files if f.endswith("molecule.yml")]
|
||||
scenarios = map(lambda d: (d, f"{Path(d).parts[2]}/{Path(d).parts[4]}"), scenario_dirs)
|
||||
return scenarios
|
||||
|
||||
@staticmethod
|
||||
def _load_template_as_string():
|
||||
path = Path(os.path.join(os.path.dirname(__file__), PIPELINE_TEMPLATE_J2_FILE))
|
||||
return path.read_text()
|
||||
def find_all_scenarios():
|
||||
scenario_dirs = []
|
||||
for root, dirs, files in os.walk(Path(os.path.join(os.path.dirname(__file__), "..", ROLES_DIR))):
|
||||
[scenario_dirs.append(Path(root)) for f in files if f.endswith("molecule.yml")]
|
||||
return scenario_dirs
|
||||
|
||||
|
||||
class Step:
|
||||
def __init__(self, name, script_commands=None):
|
||||
if script_commands is None:
|
||||
script_commands = []
|
||||
self.name = name
|
||||
self.scriptCommands = script_commands
|
||||
|
||||
|
||||
class ScriptCommand:
|
||||
PACKAGE_INSTALL_COMMAND = "apt-get update && ./bin/install-ansible --dev"
|
||||
|
||||
def __init__(self, test_command):
|
||||
self.test_command = test_command
|
||||
|
||||
def all_commands(self):
|
||||
return [self.PACKAGE_INSTALL_COMMAND, self.test_command]
|
||||
def load_template():
|
||||
path = Path(os.path.join(os.path.dirname(__file__), PIPELINE_TEMPLATE_J2_FILE))
|
||||
return Template(path.read_text())
|
||||
|
||||
|
||||
def main():
|
||||
Pipeline().generate_pipeline()
|
||||
template = load_template()
|
||||
|
||||
scenario_paths = find_all_scenarios()
|
||||
generated_output = template.render(scenario_paths=scenario_paths)
|
||||
|
||||
print(generated_output)
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
|
||||
@@ -18,9 +18,10 @@ pipelines:
|
||||
- step:
|
||||
name: Pre Parallelization stage
|
||||
script:
|
||||
- echo "Running tests in {{ parallel_steps|length }} batches"
|
||||
- echo "Running tests in {{ scenario_paths|length }} batches"
|
||||
|
||||
- step:
|
||||
name: Check if number of batches match actual number of scenarios
|
||||
name: Check if the template is up-to-date
|
||||
script:
|
||||
- |
|
||||
actual_scenario_count=$(find ./roles -type f -name "molecule.yml" -exec dirname {} ';' | wc -l | sed -e 's/^[[:space:]]*//')
|
||||
@@ -30,14 +31,15 @@ pipelines:
|
||||
echo "Mismatch between expected and actual number [${actual_scenario_count}] of scenarios. Please look at https://bitbucket.org/atlassian/dc-deployments-automation/src/master/DEVELOPMENT.md for instructions on how to fix this error."
|
||||
exit $GREP_RETURN_CODE
|
||||
fi
|
||||
|
||||
- parallel:
|
||||
{% for parallel_step in parallel_steps -%}
|
||||
{% for spath in scenario_paths -%}
|
||||
- step:
|
||||
name: {{ parallel_step.name }}
|
||||
name: {{ spath.parts[2] }}/{{ spath.parts[4] }}
|
||||
services:
|
||||
- docker
|
||||
script:
|
||||
{% for scriptCommand in parallel_step.scriptCommands -%}
|
||||
- {{ scriptCommand }}
|
||||
{% endfor %}
|
||||
- ./bin/install-ansible --dev
|
||||
- cd roles/{{ spath.parts[2] }}
|
||||
- molecule test -s {{ spath.parts[4] }}
|
||||
{% endfor %}
|
||||
|
||||
Reference in New Issue
Block a user