From e20aa7d68bf8a3d6fe6d42b289e3bcd1de5d93c5 Mon Sep 17 00:00:00 2001 From: Eric Zillmann <zillmann@arocom.de> Date: Tue, 7 Apr 2020 09:20:40 +0200 Subject: [PATCH] ansible-inventories/arocom#2838 added script and tasks for "crontabs_collector" --- tasks/crontabs_collector.yml | 14 ++++++ tasks/main.yml | 5 ++ .../config/crontabs_collector.php.jinja2 | 49 +++++++++++++++++++ 3 files changed, 68 insertions(+) create mode 100644 tasks/crontabs_collector.yml create mode 100755 templates/scripts/config/crontabs_collector.php.jinja2 diff --git a/tasks/crontabs_collector.yml b/tasks/crontabs_collector.yml new file mode 100644 index 0000000..b107592 --- /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 04b85a2..14c1365 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 0000000..b7e3a32 --- /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); -- GitLab