diff --git a/tasks/crontabs_collector.yml b/tasks/crontabs_collector.yml new file mode 100644 index 0000000000000000000000000000000000000000..b1075923b98a1cb7ad9f661ca7e80379b14c72b9 --- /dev/null +++ b/tasks/crontabs_collector.yml @@ -0,0 +1,14 @@ +--- +# file: roles/drupal/tasks/crontabs_collector.yml + +- block: + + - name: Install crontabs collector + template: + src: scripts/config/crontabs_collector.php.jinja2 + dest: /usr/local/bin/crontabs_collector.php + owner: root + group: root + mode: 0775 + tags: + - crontabs_collector diff --git a/tasks/main.yml b/tasks/main.yml index 04b85a2231963850ae1d31010656847f74a394ec..14c13657db7dcc0ac483d862dc605cacdee60259 100644 --- a/tasks/main.yml +++ b/tasks/main.yml @@ -42,6 +42,11 @@ tags: - haproxy_redirects_collector + - name: Install crontabs collector + include_tasks: crontabs_collector.yml + tags: + - crontabs_collector + when: (not excluded_roles or "drupal" not in excluded_roles) and drupal_install_drupal and (collect_config is not defined or not collect_config) - block: diff --git a/templates/scripts/config/crontabs_collector.php.jinja2 b/templates/scripts/config/crontabs_collector.php.jinja2 new file mode 100755 index 0000000000000000000000000000000000000000..b7e3a32097e88a9d6cdb6300314a189f2b943912 --- /dev/null +++ b/templates/scripts/config/crontabs_collector.php.jinja2 @@ -0,0 +1,49 @@ +#!/usr/bin/php +<?php + +// Define input and output files. + +// Production files. +$input_files_definitions = []; +{% for drupal in drupal_settings|default([]) %} +{% if drupal.src.git.target2 is defined %} +$input_files_definitions[] = '{{ jailroot }}/{{ drupal.jail.name }}/var/www{{ drupal.webRoot|default("") }}{{ drupal.src.git.target2 }}/crons/*.json'; +{% endif %} +{% endfor %} +$output_file = '/etc/ansible/facts.d/crontabs.fact'; + +// Get the input file listing. +$input_files = []; + +foreach ($input_files_definitions as $input_files_definition) { + $input_files_tmp = glob($input_files_definition); + if (!is_array($input_files_tmp)) { + continue; + } + + $input_files = array_merge($input_files, $input_files_tmp); +} + +// Resulting crontab definition list. +$crontab_definitions = []; + +// Walk over the input files and merge each one to the result. +foreach ($input_files as $input_file) { + // Read the raw file contents. + $input_file_content = @file_get_contents($input_file); + // JSON decode the file contents. + $input_file_decoded = @json_decode($input_file_content, true); + // Make sure we have a valid list. + if (!is_array($input_file_decoded)) { + continue; + } + + // Merge the decoded file contents to the result. + $crontab_definitions = array_merge($crontab_definitions, $input_file_decoded); +} + +// JSON encode the result. +$crontab_content = json_encode($crontab_definitions); + +// Write the JSON encoded result to the output file. +file_put_contents($output_file, $crontab_content);