Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save frobware/a54639628a12f38e123e9c838641d7f1 to your computer and use it in GitHub Desktop.
Save frobware/a54639628a12f38e123e9c838641d7f1 to your computer and use it in GitHub Desktop.
Track haproxy config changes
commit 22b7604a9aa18ac729707f5c0d82b17ad3ea80ab
Author: Andrew McDermott <amcdermo@redhat.com>
Date: Fri Mar 15 13:11:47 2024 +0000
reloads: track changes via diff
diff --git a/images/router/haproxy/reload-haproxy b/images/router/haproxy/reload-haproxy
index e0733ec2..cf94d121 100755
--- a/images/router/haproxy/reload-haproxy
+++ b/images/router/haproxy/reload-haproxy
@@ -73,6 +73,56 @@ if [ -n "${ROUTER_SHUTDOWN-}" ]; then
exit 1
fi
+set -x
+
+# Base directory for reloads
+base_dir="reloads"
+
+# Directories for storing snapshots, unified diffs, and traditional diffs
+snapshot_dir="${base_dir}/snapshots"
+unified_diff_dir="${base_dir}/changes/unified"
+traditional_diff_dir="${base_dir}/changes/traditional"
+
+# Generate a timestamp
+current_date=$(date "+%Y-%m-%d_%H%M%S")
+
+mkdir -p "$snapshot_dir"
+mkdir -p "$unified_diff_dir"
+mkdir -p "$traditional_diff_dir"
+
+# Snapshot directory for this change
+current_snapshot_dir="${snapshot_dir}/${current_date}"
+mkdir -p "$current_snapshot_dir"
+
+# Save snapshots if the previous configuration exists
+if [ -f "${config_file}.last" ]; then
+ cp "${config_file}.last" "${current_snapshot_dir}/haproxy.config.prev"
+ cp "$config_file" "${current_snapshot_dir}/haproxy.config.curr"
+ diff -u "${config_file}.last" "$config_file" > "${unified_diff_dir}/${current_date}.diff"
+ diff "${config_file}.last" "$config_file" > "${traditional_diff_dir}/${current_date}.diff"
+fi
+
+prune_dir() {
+ local dir=$1
+ pushd "$dir" > /dev/null 2>&1 || { echo "Failed to enter directory $dir"; return; }
+ num_files=$(ls -1 | wc -l)
+ num_delete=$((num_files - 250))
+
+ if [ "$num_delete" -gt 0 ]; then
+ # Delete the oldest files if there are more files than the target
+ ls -1tr | head -n "$num_delete" | xargs -I {} rm -rf "{}"
+ fi
+
+ popd > /dev/null 2>&1
+}
+
+# Prune snapshots, unified diffs, and traditional diffs directories
+prune_dir "$snapshot_dir"
+prune_dir "$unified_diff_dir"
+prune_dir "$traditional_diff_dir"
+
+set +x
+
reload_status=0
if [ -n "$old_pids" ]; then
/usr/sbin/haproxy -f $config_file -p $pid_file -x /var/lib/haproxy/run/haproxy.sock -sf $old_pids
@@ -82,5 +132,7 @@ else
reload_status=$?
fi
+cp $config_file ${config_file}.last
+
[ $reload_status -ne 0 ] && exit $reload_status
haproxyHealthCheck
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment