aboutsummaryrefslogtreecommitdiff
path: root/www/varnish4/files/varnishd.in
blob: 53ba7d80c4daa5b06af743dc8a22f0f8c0654b9c (plain) (blame)
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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
#!/bin/sh

# PROVIDE: varnishd
# REQUIRE: DAEMON
# KEYWORD: shutdown

#
# Add the following line to /etc/rc.conf to enable varnishd:
#
# varnishd_enable="YES"
#
# Configuration variables and their default values:
#
# varnishd_pidfile - full path to the PID file.
#	default: "/var/run/varnishd.pid"
#
# varnishd_listen - address and port at which varnishd will listen for
#	client requests.
#	default: ":80"
#
# varnishd_admin - address and port at which varnishd will listen for
#	administrative commands.
#	default: "localhost:81"
#
# varnishd_backend - address of the backend server.
#	default: "localhost:8080"
#
# varnishd_config - name of the varnishd config file.
#	default: unset.
#
# varnishd_hash - hash algorithm
#       default: "critbit"
#
# varnishd_storage - storage method and parameters.
#	default: "file,/tmp,100M"
#
# varnishd_jailuser - unprivileged user for the child process.
#	default: "varnish"
#
# varnishd_flags - complete command line arguments.
#	default if varnishd_config is unset: "-j unix,user=${varnishd_jailuser} -P ${varnishd_pidfile} -a ${varnishd_listen} -T ${varnishd_admin} -b ${varnishd_backend} -s ${varnishd_storage} -h ${varnishd_hash} ${varnishd_extra_flags}"
#	default if varnishd_config is set: "-j unix,user=${varnishd_jailuser} -P ${varnishd_pidfile} -a ${varnishd_listen} -T ${varnishd_admin} -f ${varnishd_config} -s ${varnishd_storage} -h ${varnishd_hash} ${varnishd_extra_flags}"
#
# See varnishd(1) for a detailed overview of command-line options.
#

. /etc/rc.subr

name=varnishd
rcvar=varnishd_enable

load_rc_config ${name}

: ${varnishd_enable:=NO}
: ${varnishd_pidfile=/var/run/${name}.pid}
: ${varnishd_listen=:80}
: ${varnishd_admin=localhost:81}
: ${varnishd_backend=localhost:8080}
: ${varnishd_storage=file,/tmp,100M}
: ${varnishd_hash=critbit}
: ${varnishd_jailuser=varnish}

command="%%PREFIX%%/sbin/${name}"
pidfile="${varnishd_pidfile}"
configtest_cmd="varnishd_checkconfig"
reload_cmd="varnishd_reload"
restart_precmd="varnishd_checkconfig"
start_precmd="varnishd_precmd"
extra_commands="status reload configtest"

if [ -n "${varnishd_config}" ] ; then
	: ${varnishd_flags:="-j unix,user=${varnishd_jailuser} -P ${varnishd_pidfile} -a ${varnishd_listen} -T ${varnishd_admin} -f ${varnishd_config} -s ${varnishd_storage} -h ${varnishd_hash} ${varnishd_extra_flags}"}
else
	: ${varnishd_flags:="-j unix,user=${varnishd_jailuser} -P ${varnishd_pidfile} -a ${varnishd_listen} -T ${varnishd_admin} -b ${varnishd_backend} -s ${varnishd_storage} -h ${varnishd_hash} ${varnishd_extra_flags}"}
fi

varnishd_checkconfig()
{
	if [ -z "${varnishd_config}" ]; then
		echo "${name}: nothing to check, no configuration file defined, builtin VCL used"
	else
		echo "Performing sanity check on ${name} configuration:"
		if eval ${command} -C -f "${varnishd_config}" 2> /dev/null ; then
			echo "${name}: the configuration file ${varnishd_config} syntax is ok"
		else
			err 1 "${name}: the configuration file ${varnishd_config} syntax is NOT ok"
		fi
	fi
}

# Adapted from work done by Ingvar Hagelund <ingvar@redpill-linpro.com> (see redhat/varnish_reload_vcl)
varnishd_reload()
{
	local _current_config_name _new_config_name _varnishadm_cmd

	if [ -z "${varnishd_config}" ]; then
		echo "${name}: nothing to reload, no configuration file defined, builtin VCL used"
	else
		_new_config_name="rc.d_reloaded@$(date +%Y%m%d%H%M%S)"
		_varnishadm_cmd="%%PREFIX%%/bin/varnishadm ${varnish_cli_flags}"
		if ! eval ${_varnishadm_cmd} vcl.list > /dev/null; then
			err 1 "${name}: can't connect to varnishadm"
		fi
		_current_config_name=$(${_varnishadm_cmd} vcl.list | awk ' /^active/ { print $3 } ')
		if ! eval ${_varnishadm_cmd} vcl.load ${_new_config_name} ${varnishd_config} > /dev/null; then
			err 1 "${name}: vcl.load failed, you're still using previous rules (${_current_config_name})"
		fi
		if eval ${_varnishadm_cmd} vcl.use ${_new_config_name} > /dev/null; then
			echo "VCL file \"${varnishd_config}\" has been successfully loaded as \"${_new_config_name}\""
			echo "To remove previous loaded configurations, you should run \"${_varnishadm_cmd} vcl.discard <configname>\" by yourself"
		else
			err 1 "${name}: vcl.use failed, you're still using previous rules (${_current_config_name})"
		fi
	fi

	return 0
}

varnishd_precmd()
{
	# Check config before starting
	varnishd_checkconfig
}

run_rc_command "$1"