From f09156d5f24a32c96f14b02888b7142bbe021947 Mon Sep 17 00:00:00 2001
From: Eric Zillmann <zillmann@arocom.de>
Date: Mon, 27 Jan 2020 13:28:40 +0100
Subject: [PATCH] ansible-inventories/arocom#2840 added script to merge all
 project specific redirects into one file and use script in deployment

---
 tasks/haproxy_redirects_collector.yml         | 14 +++
 tasks/main.yml                                |  3 +
 .../haproxy_redirects_collector.php.jinja2    | 89 +++++++++++++++++++
 templates/scripts/update/update.jinja2        |  5 ++
 4 files changed, 111 insertions(+)
 create mode 100644 tasks/haproxy_redirects_collector.yml
 create mode 100755 templates/scripts/config/haproxy_redirects_collector.php.jinja2

diff --git a/tasks/haproxy_redirects_collector.yml b/tasks/haproxy_redirects_collector.yml
new file mode 100644
index 0000000..34e1d05
--- /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 e7b8a65..763d1b1 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 0000000..2094fcd
--- /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 63b6015..a38ccd1 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'
-- 
GitLab