diff --git a/tasks/haproxy_redirects_collector.yml b/tasks/haproxy_redirects_collector.yml new file mode 100644 index 0000000000000000000000000000000000000000..34e1d05af6d4c76fb9f88902a2a51b30311a19ca --- /dev/null +++ b/tasks/haproxy_redirects_collector.yml @@ -0,0 +1,14 @@ +--- +# file: roles/drupal/tasks/haproxy_collector.yml + +- block: + + - name: Install HAProxy redirects collector + template: + src: scripts/config/haproxy_redirects_collector.php.jinja2 + dest: /usr/local/bin/haproxy_redirects_collector.php + owner: root + group: root + mode: 0775 + tags: + - haproxy_redirects_collector diff --git a/tasks/main.yml b/tasks/main.yml index e7b8a65952bbddf2f4d511f2a794f7c946aa821b..763d1b13bc231040d487169be446dc8735218a2f 100644 --- a/tasks/main.yml +++ b/tasks/main.yml @@ -37,6 +37,9 @@ - SetPermissions - mysql + - name: Install HAProxy redirects collector + include_tasks: haproxy_redirects_collector.yml + 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/haproxy_redirects_collector.php.jinja2 b/templates/scripts/config/haproxy_redirects_collector.php.jinja2 new file mode 100755 index 0000000000000000000000000000000000000000..2094fcdf3328212ef0d0f10023efbf8ee70cc38f --- /dev/null +++ b/templates/scripts/config/haproxy_redirects_collector.php.jinja2 @@ -0,0 +1,89 @@ +#!/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 }}/redirects/*.json'; +{% endif %} +{% endfor %} +$output_file = '/etc/ansible/facts.d/proxy_redirect_maps.fact'; + +/** + * Merge two arrays one level deep by using array_merge(). + * + * @param array $arr1 + * First array. + * @param array $arr2 + * Second array; if arr2 contains a key from arr1 the result will have the value of arr2. + * Numeric keys will be added as new (numeric) keys. + * + * @return array + * Resulting merged array. + */ +function array_merge_one_level($arr1, $arr2) { + // Make sure the parameters are arrays. + if (!is_array($arr1)) { + $arr1 = []; + } + if (!is_array($arr2)) { + $arr2 = []; + } + + // Use the first array as basis. + $arr3 = $arr1; + + // Merge the second array into the basis. + foreach ($arr2 as $key => $value) { + // New keys will simply be added. + if (!isset($arr3[$key])) { + $arr3[$key] = $value; + } + // If either value is not an array just set the value as it is. + elseif (!is_array($arr3[$key]) || !is_array($value)) { + $arr3[$key] = $value; + } + // Existing key, exiting value and new one are arrays: merge them. + else { + $arr3[$key] = array_merge($arr3[$key], $value); + } + } + + // Return the result. + return $arr3; +} + +// 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 redirect definition list. +$redirect_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); + + // Merge the decoded file contents to the result. + $redirect_definitions = array_merge_one_level($redirect_definitions, $input_file_decoded); +} + +// JSON encode the result. +$redirect_content = json_encode($redirect_definitions); + +// Write the JSON encoded result to the output file. +file_put_contents($output_file, $redirect_content); diff --git a/templates/scripts/update/update.jinja2 b/templates/scripts/update/update.jinja2 index 63b60153c6143d565318a8fea0ce6a9ce59615db..a38ccd1fb6be20deb8c01bf172397d7254462a91 100644 --- a/templates/scripts/update/update.jinja2 +++ b/templates/scripts/update/update.jinja2 @@ -61,6 +61,11 @@ if [ -f 'post-update.sh' ]; then ./post-update.sh fi +if [ -f '/usr/local/bin/haproxy_redirects_collector.php' ]; then + echo '> haproxy_redirects_collector.php'; + /usr/local/bin/haproxy_redirects_collector.php; +fi; + rm "$LOCKFILE" echo '> done'