Skip to content

Instantly share code, notes, and snippets.

@dbsdsun
Created March 2, 2016 22:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dbsdsun/e8ce0bb4088503974e20 to your computer and use it in GitHub Desktop.
Save dbsdsun/e8ce0bb4088503974e20 to your computer and use it in GitHub Desktop.
When varnish works with other system, vcl configure is dynamically generated by the front system and set with vcl.inline. There is not a vcl file any more. In this case, we have to use vcl.inline. the following is my varnish_vcl_reload shell script using vcl.inline. Any suggestions are welcome. — wilson.sun330@gmail.com
#!/bin/bash
#
# reload active vcl
# Sometimes, vcl_init needs to be re-run by reloading the active vcl.
# For example, a geoip database is loaded in vcl_init. When the database is updated,
# vcl_init needs to be re-run.
#
# This script saves the currently active vcl to a shell variable, and reloads it to varnish
# Wislon Sun <wilson.sun330@gmail>
#
# This is free software, distributed under the standard 2 clause BSD license,
# see the LICENSE file in the Varnish documentation directory
#
# Requires GNU bash
#
usage="$(basename "$0") [-n ident] [-t timeout] [-S secretfile] -T [address]:port
where:
-h show this help text
-n,t,S,T refer to varnishadm"
while getopts ':n:t:S:T:d:h' option; do
case $option in
n) ident=$OPTARG
;;
t) timeout=$OPTARG
;;
S) secretfile=$OPTARG
;;
T) addressport=$OPTARG
;;
h) echo "$usage"
exit
;;
:) printf "missing argument for -%s\n" "$OPTARG" >&2
echo "$usage" >&2
exit 1
;;
\?) echo "illegal option: -$OPTARG" >&2
echo "$usage" >&2
exit 1
;;
esac
done
# Done parsing, set up command
VARNISHADM="varnishadm"
if [ ! -z "$ident" ]; then
VARNISHADM="$VARNISHADM -n $ident"
fi
if [ ! -z "$timeout" ]; then
VARNISHADM="$VARNISHADM -t $timeout"
fi
if [ ! -z "$secretfile" ]; then
VARNISHADM="$VARNISHADM -S $secretfile"
fi
if [ ! -z "$addressport" ]; then
VARNISHADM="$VARNISHADM -T $addressport"
fi
# Check if we are able to connect at all
if ! $VARNISHADM vcl.list >> /dev/null 2>&1; then
echo "Unable to run \"$VARNISHADM vcl.list\""
exit 1
fi
# find current config
current_config=$( $VARNISHADM vcl.list | awk ' /^active/ { print $3 } ' )
# save current config to a variable
vclcontent=` $VARNISHADM vcl.show $current_config | sed 's/["\]/\\\\&/g' | sed ':a;N;$!ba;s/\n/\\\\n/g' `
timestamp=`date +%Y%m%d_%H%M%S`
$VARNISHADM vcl.inline "vreload$timestamp" '"'$vclcontent'"' >> /dev/null 2>&1
vstring=`varnishadm -n aeroflow vcl.list|grep "vreload$timestamp"`
if [ ! -z "$vstring" ]; then
$VARNISHADM vcl.use "vreload$timestamp" >> /dev/null 2>&1
$VARNISHADM vcl.discard $current_config >> /dev/null 2>&1
echo "successfully reload current vcl config"
exit 0
else
echo "failed to save current vcl config"
exit 1
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment