Gentoo Websites Logo
Go to: Gentoo Home Documentation Forums Lists Bugs Planet Store Wiki Get Gentoo!
Bug 475600 - www-servers/varnish - add configuration sanity test and reload functionality to init.d script
Summary: www-servers/varnish - add configuration sanity test and reload functionality ...
Status: RESOLVED FIXED
Alias: None
Product: Gentoo Linux
Classification: Unclassified
Component: [OLD] Server (show other bugs)
Hardware: All Linux
: Normal enhancement (vote)
Assignee: Anthony Basile
URL:
Whiteboard:
Keywords: PATCH
Depends on:
Blocks:
 
Reported: 2013-07-03 09:01 UTC by Tomáš Mózes
Modified: 2013-07-09 06:31 UTC (History)
0 users

See Also:
Package list:
Runtime testing required: ---


Attachments
varnish.confd.patch (varnish.confd.patch,754 bytes, patch)
2013-07-08 16:30 UTC, Tomáš Mózes
Details | Diff
varnish.initd.patch (varnish.initd.patch,1.89 KB, patch)
2013-07-08 16:30 UTC, Tomáš Mózes
Details | Diff

Note You need to log in before you can comment on or make changes to this bug.
Description Tomáš Mózes 2013-07-03 09:01:04 UTC
Having a syntax error in varnish vcl, currently you get:
# /etc/init.d/varnishd start
 * Starting varnish ...                [ !! ]
 * Starting varnish logging ...        [ ok ]
 
Varnish has a parameter -C:
-C     Print VCL code compiled to C language and exit. Specify the VCL file to compile with the -f option.

By using this option before starting, an admin can be warned like this:

# /etc/init.d/varnishd start
 * varnishd has detected an error in your setup:
Message from VCC-compiler:
Expected one of
        'acl', 'sub', 'backend', 'director', 'probe',  or 'import'
Found: 'foo' at
('input' Line 1 Pos 1)
foo
###

Running VCC-compiler failed, exit 1

VCL compilation failed
 * ERROR: varnishd failed to start

# diff -u /usr/portage/www-servers/varnish/files/varnishd.confd /etc/conf.d/varnishd
--- /usr/portage/www-servers/varnish/files/varnishd.confd       2013-06-18 03:01:34.000000000 +0200
+++ /etc/conf.d/varnishd        2013-07-03 10:49:53.115279076 +0200
@@ -1,12 +1,15 @@
 # /etc/conf.d/varnishd

+VARNISHD="/usr/sbin/varnishd"
+CONFIGFILE="/etc/varnish/default.vcl"
+
 # Listen on 127.0.0.1:8080 and connect to backend 127.0.0.1:80
 # Ignore the config file, /etc/varnish/default.vcl
 VARNISHD_OPTS="-a 127.0.0.1:8080 -b 127.0.0.1:80"

 # Alternatively, don't listen to a backend and use
 # the config file
-#VARNISHD_OPTS="-a 127.0.0.1:8080 -f /etc/varnish/default.vcl"
+#VARNISHD_OPTS="-a 127.0.0.1:8080 -f $CONFIGFILE"

 # arguments passed to varnishncsa
 # please see the varnishncsa man page for more options


# diff -u /usr/portage/www-servers/varnish/files/varnishd.initd /etc/init.d/varnishd
--- /usr/portage/www-servers/varnish/files/varnishd.initd       2013-03-14 13:31:33.000000000 +0100
+++ /etc/init.d/varnishd        2013-07-03 10:48:24.051115766 +0200
@@ -3,13 +3,36 @@
 # Distributed under the terms of the GNU General Public License v2
 # $Header: /var/cvsroot/gentoo-x86/www-servers/varnish/files/varnishd.initd,v 1.8 2013/03/14 12:18:17 blueness Exp $

+extra_commands="configtest"
+
+description_configtest="Run syntax tests for configuration files."
+
 depend() {
        need net
 }

+configtest() {
+       ebegin "Checking ${SVCNAME} configuration"
+       checkconfig
+       eend $?
+}
+
+checkconfig() {
+       ${VARNISHD} -C -f ${CONFIGFILE} >/dev/null 2>&1
+       ret=$?
+       if [ $ret -ne 0 ]; then
+               eerror "${SVCNAME} has detected an error in your setup:"
+               ${VARNISHD} -C -f ${CONFIGFILE}
+       fi
+
+       return $ret
+}
+
 start() {
+       checkconfig || return 1
+
        ebegin "Starting varnish"
-       start-stop-daemon --quiet --start --pidfile /var/run/varnishd.pid --exec /usr/sbin/varnishd -- -P /var/run/varnishd.pid ${VARNISHD_OPTS} &> /dev/null
+       start-stop-daemon --quiet --start --pidfile /var/run/varnishd.pid --exec ${VARNISHD} -- -P /var/run/varnishd.pid ${VARNISHD_OPTS} &> /dev/null
        eend $?

        if [ "${VARNISHNCSA_ARGS}" != "" ]; then
Comment 1 Tomáš Mózes 2013-07-08 15:15:54 UTC
It would also be nice to be able to reload the configuration without bringing varnish down. The attached patches add the configuration testing and the reload function (inspired by redhat/varnish_reload_vcl from the source code).


# diff -u /usr/portage/www-servers/varnish/files/varnishd.confd /etc/conf.d/varnishd
--- /usr/portage/www-servers/varnish/files/varnishd.confd       2013-06-18 03:01:34.000000000 +0200
+++ /etc/conf.d/varnishd        2013-07-08 17:10:44.755050255 +0200
@@ -1,12 +1,16 @@
 # /etc/conf.d/varnishd
 
+VARNISHD="/usr/sbin/varnishd"
+VARNISHADM="/usr/bin/varnishadm"
+CONFIGFILE="/etc/varnish/default.vcl"
+
 # Listen on 127.0.0.1:8080 and connect to backend 127.0.0.1:80
 # Ignore the config file, /etc/varnish/default.vcl
 VARNISHD_OPTS="-a 127.0.0.1:8080 -b 127.0.0.1:80"
 
 # Alternatively, don't listen to a backend and use 
 # the config file
-#VARNISHD_OPTS="-a 127.0.0.1:8080 -f /etc/varnish/default.vcl"
+#VARNISHD_OPTS="-a 127.0.0.1:8080 -f $CONFIGFILE"
 
 # arguments passed to varnishncsa
 # please see the varnishncsa man page for more options




# diff -u /usr/portage/www-servers/varnish/files/varnishd.initd /etc/init.d/varnishd
--- /usr/portage/www-servers/varnish/files/varnishd.initd       2013-03-14 13:31:33.000000000 +0100
+++ /etc/init.d/varnishd        2013-07-08 17:06:58.378153445 +0200
@@ -3,13 +3,38 @@
 # Distributed under the terms of the GNU General Public License v2
 # $Header: /var/cvsroot/gentoo-x86/www-servers/varnish/files/varnishd.initd,v 1.8 2013/03/14 12:18:17 blueness Exp $
 
+extra_commands="configtest"
+extra_started_commands="reload"
+
+description_configtest="Run syntax tests for configuration files."
+description_reload="Reloads the configuration."
+
 depend() {
        need net
 }
 
+configtest() {
+       ebegin "Checking ${SVCNAME} configuration"
+       checkconfig
+       eend $?
+}
+
+checkconfig() {
+       ${VARNISHD} -C -f ${CONFIGFILE} >/dev/null 2>&1
+       ret=$?
+       if [ $ret -ne 0 ]; then
+               eerror "${SVCNAME} has detected an error in your setup:"
+               ${VARNISHD} -C -f ${CONFIGFILE}
+       fi
+
+       return $ret
+}
+
 start() {
+       checkconfig || return 1
+
        ebegin "Starting varnish"
-       start-stop-daemon --quiet --start --pidfile /var/run/varnishd.pid --exec /usr/sbin/varnishd -- -P /var/run/varnishd.pid ${VARNISHD_OPTS} &> /dev/null
+       start-stop-daemon --quiet --start --pidfile /var/run/varnishd.pid --exec ${VARNISHD} -- -P /var/run/varnishd.pid ${VARNISHD_OPTS} &> /dev/null
        eend $?
 
        if [ "${VARNISHNCSA_ARGS}" != "" ]; then
@@ -30,3 +55,33 @@
                eend $?
        fi
 }
+
+reload() {
+       checkconfig || return 1
+
+       ebegin "Reloading varnish"
+
+       $VARNISHADM vcl.list >/dev/null 2>&1
+       ret=$?
+       if [ $ret -ne 0 ]; then
+               eerror "${SVCNAME} cannot list configuration"
+               return 1
+       fi
+
+       new_config="reload_$(date +%FT%H:%M:%S)"
+       $VARNISHADM vcl.load $new_config $CONFIGFILE >/dev/null 2>&1
+       ret=$?
+       if [ $ret -ne 0 ]; then
+               eerror "${SVCNAME} cannot load configuration"
+               return 1
+       fi
+
+       $VARNISHADM vcl.use $new_config >/dev/null 2>&1
+       ret=$?
+       if [ $ret -ne 0 ]; then
+               eerror "${SVCNAME} cannot switch configuration"
+               return 1
+       fi
+
+       eend 0
+}
Comment 2 Anthony Basile gentoo-dev 2013-07-08 15:36:57 UTC
can you make these into attachments, cutting and pasting adds white space damage
Comment 3 Tomáš Mózes 2013-07-08 16:30:17 UTC
Created attachment 352862 [details, diff]
varnish.confd.patch
Comment 4 Tomáš Mózes 2013-07-08 16:30:30 UTC
Created attachment 352864 [details, diff]
varnish.initd.patch
Comment 5 Anthony Basile gentoo-dev 2013-07-08 22:13:11 UTC
thanks!  done in varnish-3.0.4-r1.  we'll use the improved initd scripts when moving forward with stabilization.

test and reopen this bug if there's any problem.
Comment 6 Tomáš Mózes 2013-07-09 06:31:26 UTC
Thanks Anthony