[PATCH] Allow varnish_reload_vcl to discard old VCL after reload

Delacroix, Gauthier Gauthier.Delacroix at coreye.fr
Tue Jun 16 19:08:25 CEST 2015


Here is an updated patch including your requests.
There is still only one parameter which disables the feature (by default) if different from zero or a positive integer, otherwise keeps N lasts available configurations.
I assumed vcl.list displays configs in the loading order, which seems to be always true.
I've removed the exit on error (just logged).

Gauthier

---
 redhat/varnish.params     |  6 ++++++
 redhat/varnish.sysconfig  |  6 ++++++
 redhat/varnish_reload_vcl | 22 +++++++++++++++++++++-
 3 files changed, 33 insertions(+), 1 deletion(-)

diff --git a/redhat/varnish.params b/redhat/varnish.params
index 636e975..964018b 100644
--- a/redhat/varnish.params
+++ b/redhat/varnish.params
@@ -4,6 +4,12 @@
 # Set this to 1 to make systemd reload try to switch vcl without restart. 
 RELOAD_VCL=1
 
+# Set this to 0 or more to discard old VCL configurations on reload.
+# If greater than 0, these last configurations will be kept available.
+# Any other value will disable configuration discarding.
+# To make this work, you need to set RELOAD_VCL variable to 1.
+KEEP_AVAILABLE_VCL=
+
 # Main configuration file. You probably want to change it.
 VARNISH_VCL_CONF=/etc/varnish/default.vcl 
 
diff --git a/redhat/varnish.sysconfig b/redhat/varnish.sysconfig
index 6aa2354..c1dd404 100644
--- a/redhat/varnish.sysconfig
+++ b/redhat/varnish.sysconfig
@@ -24,6 +24,12 @@ NPROCS="unlimited"
 # use Alternative 3, Advanced configuration, below
 RELOAD_VCL=1
 
+# Set this to 0 or more to discard old VCL configurations on reload.
+# If greater than 0, the n last configurations will be kept available.
+# Any other value will disable configuration discarding.
+# To make this work, you need to set RELOAD_VCL variable to 1.
+KEEP_AVAILABLE_VCL=
+
 # This file contains 4 alternatives, please use only one.
 
 ## Alternative 1, Minimal configuration, no VCL
diff --git a/redhat/varnish_reload_vcl b/redhat/varnish_reload_vcl
index be3c21a..5f12c52 100755
--- a/redhat/varnish_reload_vcl
+++ b/redhat/varnish_reload_vcl
@@ -10,7 +10,7 @@
 # The following environment variables have to be set:
 # RELOAD_VCL, VARNISH_VCL_CONF, VARNISH_ADMIN_LISTEN_PORT
 # The following are optional:
-# VARNISH_SECRET_FILE, VARNISH_ADMIN_LISTEN_ADDRESS
+# VARNISH_SECRET_FILE, VARNISH_ADMIN_LISTEN_ADDRESS, KEEP_AVAILABLE_VCL
 #
 # Requires GNU bash and GNU date
 #
@@ -26,6 +26,7 @@ print_debug() {
   echo "
 Parsed configuration:
 RELOAD_VCL=\"$RELOAD_VCL\"
+KEEP_AVAILABLE_VCL=\"$KEEP_AVAILABLE_VCL\"
 VARNISH_VCL_CONF=\"$VARNISH_VCL_CONF\"
 VARNISH_ADMIN_LISTEN_ADDRESS=\"$VARNISH_ADMIN_LISTEN_ADDRESS\"
 VARNISH_ADMIN_LISTEN_PORT=\"$VARNISH_ADMIN_LISTEN_PORT\"
@@ -108,6 +109,25 @@ else
   echo "$VARNISHADM vcl.use failed"
   exit 1
 fi
+
+if [ "$KEEP_AVAILABLE_VCL" -ge 0 ] 2> /dev/null; then
+  $debug && echo "Keeping last $KEEP_AVAILABLE_VCL available configs"
+  available=`$VARNISHADM vcl.list | grep "^available"`
+  available_count=`echo "$available" | wc -l`
+  $debug && echo "Found $available_count available configs"
+  discard_count=$((available_count - KEEP_AVAILABLE_VCL))
+  $debug && echo "Discarding $discard_count configs"
+  if [ $discard_count -gt 0 ]; then
+    echo "$available" | head -n $discard_count | awk '{ print $3 }' | while read discard; do
+      if $VARNISHADM vcl.discard $discard > /dev/null; then
+        $debug && echo "$VARNISHADM vcl.discard $discard succeded"
+      else
+        echo "$VARNISHADM vcl.discard $discard failed"
+      fi
+    done
+  fi
+fi
+
 $VARNISHADM vcl.list
 echo Done
 exit 0
-- 
1.8.3.1



More information about the varnish-dev mailing list