summaryrefslogtreecommitdiffstats
path: root/main/varnish/varnishd.initd
blob: 7c39a0396493cda320fea109e039f4b31e080d9c (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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
#!/sbin/runscript
# Copyright 1999-2006 Gentoo Foundation
# Distributed under the terms of the GNU General Public License v2
# $Header: /var/cvsroot/gentoo-x86/www-servers/varnish/files/varnishd.initd,v 1.7 2009/08/30 06:28:07 hollow Exp $

extra_commands="configtest"
extra_started_commands="reload flush"
command=/usr/sbin/varnishd

instance=${SVCNAME#*.}
instance_opt="-n $instance"
if [ "$instance" = "$SVCNAME" ]; then
	instance=""
	instance_opt=""
fi

default_conf="${instance:-default}.vcl"
: ${VARNISH_CONF:=${CFG_FILE:-"/etc/varnish/$default_conf"}}
: ${VARNISHD_PID_FILE:="/var/run/varnish/${SVCNAME}.pid"}
: ${VARNISHD_CC_COMMAND:="exec cc -fpic -shared -Wl,-x ${VARNISHD_PLUGIN_CFLAGS} -o %o %s"}
: ${VARNISHNCSA_PID_FILE:="/var/run/varnish/varnishncsa${instance:+.}${instance}.pid"}


depend() {
	need net
	after firewall
}

checkconfig() {
	if ! $command -C -f ${VARNISH_CONF} -p "cc_command=${VARNISHD_CC_COMMAND}" >/dev/null 2>&1; then
		error "$SVCNAME has deteced an error in your setup:"
		$command -C -f ${VARNISH_CONF}
	fi
}

configtest() {
	ebegin "Checking ${SVCNAME} configuration"
	checkconfig
	eend $?
}

start() {
	checkconfig || return 1
	ebegin "Starting ${SVCNAME}"
        checkpath --directory --owner varnish:varnish \
	          --mode 755 ${VARNISHD_PID_FILE%/*}
	#allow varnishd to lock logfile to memory
	ulimit -l 82000
	start-stop-daemon --quiet --start \
	                  --pidfile ${VARNISHD_PID_FILE} \
			  --exec $command \
			  -- \
			  -f ${VARNISH_CONF} \
			  -P ${VARNISHD_PID_FILE} \
			  -u varnish -g varnish \
			  -p "cc_command=${VARNISHD_CC_COMMAND}" \
			  $instance_opt \
			  ${VARNISHD_OPTS} &> /dev/null
	eend $?

	if [ "${VARNISHNCSA_ARGS}" != "" ]; then
		ebegin "Starting varnish logging"
		start-stop-daemon --quiet --start \
				--pidfile ${VARNISHNCSA_PID_FILE} \
				--exec /usr/bin/varnishncsa \
				-- \
				-D -P ${VARNISHNCSA_PID_FILE} \
				$instance_opt \
				${VARNISHNCSA_ARGS}
		eend $?
	fi
}

stop() {
	ebegin "Stopping varnish"
	start-stop-daemon --quiet --stop --pidfile ${VARNISHD_PID_FILE}
	eend $?

	if [ -e ${VARNISHNCSA_PID_FILE} ]; then
		ebegin "Stopping varnish logging"
		start-stop-daemon --quiet --stop \
			--pidfile ${VARNISHNCSA_PID_FILE}
		eend $?
	fi
}

reload() {
	checkconfig || return 1

	ebegin "Reloading varnish"

	# purge unused old configurations
	DISCARDS=$(/usr/bin/varnishadm -T $ADMINHOSTPORT vcl.list |
		sed -ne "s/^available *0 *\(reload.*\)/ \\1/p")
	for VCL in $DISCARDS ; do
		/usr/bin/varnishadm -T $ADMINHOSTPORT vcl.discard $VCL > /dev/null
	done

	# reload new one
	NOW=$(date +%Y%m%d-%H%M%S-%s)
	/usr/bin/varnishadm -T $ADMINHOSTPORT vcl.load reload-$NOW ${VARNISH_CONF} > /dev/null
	/usr/bin/varnishadm -T $ADMINHOSTPORT vcl.use  reload-$NOW > /dev/null

	eend $?
}

flush() {
	ebegin "Flushing varnish cache"
	/usr/bin/varnishadm -T $ADMINHOSTPORT ban obj.http.host "~" "." > /dev/null
	eend $?
}