diff options
Diffstat (limited to 'testing/pgbouncer/pgbouncer.initd')
-rw-r--r-- | testing/pgbouncer/pgbouncer.initd | 136 |
1 files changed, 70 insertions, 66 deletions
diff --git a/testing/pgbouncer/pgbouncer.initd b/testing/pgbouncer/pgbouncer.initd index 20479dc22d..a44f6aeeb8 100644 --- a/testing/pgbouncer/pgbouncer.initd +++ b/testing/pgbouncer/pgbouncer.initd @@ -2,87 +2,91 @@ extra_started_commands="reload" -depend() { - use net - after postgresql -} +: ${user:="pgbouncer"} +: ${group:="postgresql"} +: ${cfgfile:="/etc/pgbouncer/pgbouncer.ini"} +: ${nice_timeout:=60} +: ${force_quit:="no"} +: ${force_quit_timeout:=2} -get_config() { - [ -f "${INIFILE}" ] || eend 1 "'${INIFILE}' not found" +name="PgBouncer" +command="/usr/bin/pgbouncer" +command_args="-q $cfgfile" +command_background="yes" - eval echo $(sed -e 's:;.*::' "${INIFILE}" | \ - awk '$1 == "'$1'" { print ($2 == "=" ? $3 : $2) }') -} +pidfile="/run/$RC_SVCNAME.pid" +start_stop_daemon_args=" + --user $user + --group $group" -PIDFILE="$(get_config pidfile)" -UNIX_SOCKET_DIR="$(get_config unix_socket_dir)" - -prep() { - if [ -n "${UNIX_SOCKET_DIR}" ] ; then - checkpath -o postgres:postgres -m 0775 -d "${UNIX_SOCKET_DIR}" \ - || return 1 - fi - checkpath -o pgbouncer:postgres -m 0755 -d "$(dirname ${PIDFILE})" \ - || return 1 - checkpath -o pgbouncer:postgres -m 0644 -f "${PIDFILE}" \ - || return 1 - checkpath -o pgbouncer:postgres -m 0755 -d "$(dirname $(get_config logfile))" \ - || return 1 - checkpath -o pgbouncer:postgres -m 0640 -f "$(get_config logfile)" \ - || return 1 - - return 0 +required_files="$cfgfile" + +depend() { + use net + after postgresql } -start() { - ebegin "Starting PgBouncer" - prep - local ret=$? - if [ $ret -ne 0 ] ; then - eend $ret - exit $ret - fi - start-stop-daemon --start \ - --pidfile ${PIDFILE} \ - --user pgbouncer \ - --exec /usr/bin/pgbouncer -- -q -d "${INIFILE}" - eend $? +start_pre() { + local socket_dir=$(get_config unix_socket_dir) + if [ -n "$socket_dir" ]; then + checkpath -d -m 0755 -o postgres:postgres "$socket_dir" || return 1 + fi + + local logfile="$(get_config logfile)" + if [ -n "$logfile" ]; then + checkpath -f -m 0640 -o $user:$group "$logfile" || return 1 + fi } stop() { - local seconds=$(( ${NICE_TIMEOUT} + ${FORCE_QUIT_TIMEOUT} )) - ebegin "Stopping PgBouncer (this can take up to ${seconds} seconds)" + local retry="SIGINT/$nice_timeout" + yesno "$force_quit" \ + && retry="$retry/SIGTERM/$force_quit_timeout" \ + || force_quit_timeout=0 - local retries=SIGINT/${NICE_TIMEOUT} + local seconds=$(( $nice_timeout + $force_quit_timeout )) - if [ "${FORCE_QUIT}" = "YES" ] ; then - einfo "FORCE_QUIT enabled." - retries="${retries}/SIGTERM/${FORCE_QUIT_TIMEOUT}" - fi + ebegin "Stopping $seconds (this can take up to $seconds seconds)" - # Loops through nice and force quit in one go. - start-stop-daemon --stop \ - --pidfile ${PIDFILE} \ - --retry ${retries} - - eend $? + start-stop-daemon --stop \ + --pidfile "$pidfile" \ + --retry "$retry" \ + --progress \ + --exec "$command" + eend $? } restart() { - if [ -n "${UNIX_SOCKET_DIR}" ] ; then - ebegin "Performing online restart of PgBouncer" - start-stop-daemon --start \ - --pidfile ${PIDFILE} \ - --user pgbouncer \ - --exec /usr/bin/pgbouncer -- -q -d -R "${INIFILE}" - eend $? - else - stop && start - fi + local socket_dir=$(get_config unix_socket_dir) + + if [ -n "$socket_dir" ]; then + ebegin "Performing online restart of $name" + "$command" -R "$command_args" + eend $? + else + stop && start + fi } reload() { - ebegin "Reloading PgBouncer configuration from '${INIFILE}'" - start-stop-daemon --signal HUP --pidfile ${PIDFILE} - eend $? + ebegin "Reloading $name configuration" + start-stop-daemon --signal HUP --pidfile "$pidfile" + eend $? +} + +get_config() { + local name="$1" + local default="${2:-}" + + if [ ! -f "$conffile" ]; then + printf '%s\n' "$default" + return 1 + fi + sed -En "/^\s*${name}\b/{ # find line starting with the name + s/^\s*${name}\s*=?\s*([^#]+).*/\1/; # capture the value + s/\s*$//; # trim trailing whitespaces + s/^['\"](.*)['\"]$/\1/; # remove delimiting quotes + p + }" "$conffile" \ + | grep . || printf '%s\n' "$default" } |