aboutsummaryrefslogtreecommitdiffstats
path: root/main/varnish/varnishd.initd
blob: b0ec918bb7ad54eaa34f5dfb8918dedb33756c03 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
#!/sbin/runscript
# Copyright 1999-2014 Gentoo Foundation
# Distributed under the terms of the GNU General Public License v2
# $Id$

VARNISHD_PID=${VARNISHD_PID:-/run/${SVCNAME}.pid}
CONFIGFILES="${CONFIGFILE:-/etc/varnish/default.vcl}"

command="${VARNISHD:-/usr/sbin/varnishd}"
command_args="-j unix,user=varnish -P ${VARNISHD_PID} -f ${CONFIGFILE} ${VARNISHD_OPTS}"
pidfile="${VARNISHD_PID}"

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_pre() {
	checkconfig || return 1
}

stop_pre() {
	if [ "${RC_CMD}" = "restart" ]; then
		checkconfig || return 1
	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
}