aboutsummaryrefslogtreecommitdiff
path: root/www/hs-postgrest/files/postgrest.in
blob: 7ad89ce8615a6c5c84f6fede4756989a91b70476 (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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
#!/bin/sh

# PROVIDE: postgrest
# REQUIRE: LOGIN
# BEFORE: nginx
# KEYWORD: shutdown
#
# Add postgrest_enable="YES" to /etc/rc.conf to enable PostgREST:
#
# Additional variables you can define are:
#
# postgrest_user:					Username to run PostgREST
#							Default: %%POSTGREST_USER%%
# postgrest_group:					Group to run PostgREST
#							Default: %%POSTGREST_GROUP%%
# postgrest_profile:					Profile list
#							Default: default
# postgrest_syslog_output_enable:			Set to enable syslog output.
#							Default: YES
# postgrest_syslog_output_tag				Set syslog tag if syslog enabled.
#							Default: postgrest
# postgrest_syslog_output_priority: 			Set syslog priority if syslog enabled.
#							Default: info
# postgrest_syslog_output_facility:			Set syslog facility if syslog enabled.
#							Default: daemon
# postgrest_{{ profile }}_config:			Configuration file to run PostgREST
#							Default: %%ETCDIR%%/default.conf
# postgrest_{{ profile }}_syslog_output_enable		Set to enable syslog output.
#							Default: YES
# postgrest_{{ profile }}_syslog_output_tag		Set syslog tag if syslog enabled.
#							Default: postgrest
# postgrest_{{ profile }}_syslog_output_priority: 	Set syslog priority if syslog enabled.
#							Default: info
# postgrest_{{ profile }}_syslog_output_facility:	Set syslog facility if syslog enabled.
#							Default: daemon

. /etc/rc.subr

name="postgrest"
rcvar="postgrest_enable"
start_precmd="postgrest_start_precmd"
start_cmd="postgrest_start"
stop_cmd="postgrest_stop"
reload_cmd="postgrest_reload"
extra_commands="reload"

load_rc_config $name

: ${postgrest_enable:="NO"}
: ${postgrest_user:="%%POSTGREST_USER%%"}
: ${postgrest_group:="%%POSTGREST_GROUP%%"}
: ${postgrest_profile:="default"}
: ${postgrest_default_config:="%%ETCDIR%%/default.conf"}
: ${postgrest_syslog_output_enable:="YES"}

postgrest_start_profile()
{
	local _profile _pidfile _child_pidfile _config
	local _syslog_output_enable _syslog_output_tag _syslog_output_priority _syslog_output_facility _syslog_output_flags
	local procname command command_args

	_profile=$1

	_pidfile="/var/run/${name}/${_profile}.pid"
	_child_pidfile="/var/run/${name}/${_profile}.child.pid"

	eval _config=\${${name}_${_profile}_config}
	eval _syslog_output_enable=\${${name}_syslog_output_enable}
	eval _syslog_output_enable=\${${name}_${_profile}_syslog_output_enable:-${_syslog_output_enable}}
	eval _syslog_output_tag=\${${name}_syslog_output_tag:-${name}}
	eval _syslog_output_tag=\${${name}_${_profile}_syslog_output_tag:-"${_syslog_output_tag}"}
	eval _syslog_output_priority=\${${name}_syslog_output_priority}
	eval _syslog_output_priority=\${${name}_${_profile}_syslog_output_priority:-"${_syslog_output_priority}"}
	eval _syslog_output_facility=\${${name}_syslog_output_facility}
	eval _syslog_output_facility=\${${name}_${_profile}_syslog_output_facility:-"${_syslog_output_facility}"}

	if [ ! -r "${_config}" ]; then
		warn "${name}.${_profile}: config file does not exist"
		return 1
	fi

	if checkyesno _syslog_output_enable; then
		if [ -n "${_syslog_output_tag}" ]; then
			_syslog_output_flags="-T ${_syslog_output_tag}"
		fi
		if [ -n "${_syslog_output_priority}" ]; then
			_syslog_output_flags="${_syslog_output_flags} -s ${_syslog_output_priority}"
		fi
		if [ -n "${_syslog_output_facility}" ]; then
			_syslog_output_flags="${_syslog_output_flags} -l ${_syslog_output_facility}"
		fi
	fi

	procname="%%PREFIX%%/sbin/postgrest"
	command="/usr/sbin/daemon"
	command_args="-f ${_syslog_output_flags} -p ${_child_pidfile} -P ${_pidfile} -t ${name}.${_profile} ${procname} ${_config}"

	su -m ${postgrest_user} -c "${command} ${rc_flags} ${command_args}"
	rc=$?

	return ${rc}
}

postgrest_start_precmd()
{
	if [ ! -d "/var/run/${name}" ]; then
		install -d -m 0750 -o ${postgrest_user} -g ${postgrest_group} "/var/run/${name}"
	fi
}

postgrest_start()
{
	local _ok _profile

	if [ -n "$*" ]; then
		_ok=
		for _profile in $@; do
			postgrest_start_profile ${_profile} || continue
			_ok="${_ok} ${_profile}"
		done

		if [ -n "${_ok}" ]; then
			echo "Starting ${name}:${_ok}."
		fi
	fi
}

postgrest_stop()
{
	local _ok _profile _pidfile

	if [ -n "$*" ]; then
		_ok=
		for _profile in $@; do
			_pidfile="/var/run/${name}/${_profile}.pid"
			/bin/pkill -F "${_pidfile}" 2>/dev/null || continue
			_ok="${_ok} ${_profile}"
		done

		if [ -n "${_ok}" ]; then
			echo "Stopping ${name}:${_ok}."
		fi
	fi
}

postgrest_reload()
{
	local _ok _profile _pidfile

	if [ -n "$*" ]; then
		_ok=
		for _profile in $@; do
			_pidfile="/var/run/${name}/${_profile}.child.pid"
			/bin/pkill -USR1 -F "${_pidfile}" 2>/dev/null || continue
			_ok="${_ok} ${_profile}"
		done

		if [ -n "${_ok}" ]; then
			echo "Reloading ${name}:${_ok}."
		fi
	fi
}

case $# in
1) run_rc_command $@ ${postgrest_profile} ;;
*) run_rc_command $@ ;;
esac