#!/bin/sh

# This sample rc script eliminate the need to use sysutils/py-supervisor to
# run NetBox as a system service. Only www/py-gunicorn is needed as a WSGI.
#
# Of course a working HTTP server like Apache/nginx is still required to make
# use of the gunicorn WSGI.

#
# PROVIDE: netbox
# REQUIRE: DAEMON
# KEYWORD: shutdown
#
# Add the following line to /etc/rc.conf.local or /etc/rc.conf
# to enable netbox:
#
# netbox_enable (bool):			Set to NO by default.
#					Set it to YES to enable netbox.
# netbox_use_config (bool):		Set to NO by default.
#					If set, read the configuration parameter from file defined in
#					"netbox_config" instead using rc variables.
# netbox_config (str):			Default to "%%LOCALBASE%%/etc/${name}.conf.py}"
#					Config file for gunicorn's netbox config file
# netbox_command (str):			Default to "%%LOCALBASE%%/bin/gunicorn"
#					Path to gunicorn to run netbox
# netbox_bind (str):    		Default to "localhost:8001"
#					Interface and port to bind to
# netbox_workers (int):			Default to "5"
#					Number of gunicorn workers
# netbox_threads (int):			Default to "3"
#					Number of worker threads per handling request
# netbox_timeout (int):			Default to "120"
#					Worker timeout for gunicorn
# netbox_max_requests (int):		Default to "5000"
#					Maximum number of requests a worker process will process before respawning
# netbox_max_requests_jitter (int):	Default to "500"
#					Maximum number jitter to add to "netbox_max_requests"
# netbox_extra_args (str):		Not set by default
#					Extra arguments that are passed to gunicorn
#

. /etc/rc.subr

name="netbox"
rcvar=netbox_enable
netbox_path=%%DATADIR%%
extra_commands="upgrade"

load_rc_config $name

start_precmd="netbox_precmd"
upgrade_cmd="netbox_upgrade"
command=${netbox_program:-%%LOCALBASE%%/bin/gunicorn}
procname=${netbox_procname:-%%PYTHON_CMD%%}
netbox_chdir=${netbox_path}
pidfile=${netbox_pidfile:-/var/run/${name}/${name}.pid}
netbox_user=${netbox_user:-%%WWWOWN%%}
netbox_use_config=${netbox_use_config:-no}
netbox_config=${netbox_config:-%%LOCALBASE%%/etc/${name}.conf.py}
netbox_bind=${netbox_bind:-localhost:8001}
netbox_workers=${netbox_workers:-5}
netbox_threads=${netbox_threads:-3}
netbox_timeout=${netbox_timeout:-120}
netbox_max_requests=${netbox_max_requests:-5000}
netbox_max_requests_jitter=${netbox_max_requests_jitter:-500}

# Read settings from confguration file if set
if checkyesno netbox_use_config && [ -f "${netbox_config}" ]; then 
	command_args="${netbox_args} -D \
		--log-syslog --log-syslog-prefix ${name} \
		--log-syslog-to unix:///var/run/log#dgram \
		--disable-redirect-access-to-syslog \
		-p ${pidfile} --pythonpath ${netbox_path} \
		-c ${netbox_config} \
		netbox.wsgi"
else
	command_args="${netbox_args} -D \
		--log-syslog --log-syslog-prefix ${name} \
		--log-syslog-to unix:///var/run/log#dgram \
		--disable-redirect-access-to-syslog \
		-p ${pidfile} --pythonpath ${netbox_path} \
		-b ${netbox_bind} -w ${netbox_workers} --threads ${netbox_threads} -t ${netbox_timeout} \
		--max-requests ${netbox_max_requests} --max-requests-jitter ${netbox_max_requests_jitter} \
		${netbox_extra_args} \
		netbox.wsgi"
fi

netbox_precmd()
{
	install -d -o ${netbox_user} `dirname ${pidfile}`
}

netbox_upgrade()
{
	if [ -f ${pidfile} ]; then
		cd ${netbox_path}
		%%PYTHON_CMD%% manage.py migrate
		%%PYTHON_CMD%% manage.py trace_paths --no-input
		%%LOCALBASE%%/bin/mkdocs build
		%%PYTHON_CMD%% manage.py collectstatic --no-input
		%%PYTHON_CMD%% manage.py remove_stale_contenttypes --no-input
		%%PYTHON_CMD%% manage.py reindex --lazy
		%%PYTHON_CMD%% manage.py clearsessions
		echo "Please restart NetBox and the WSGI environment."
	else
		echo "NetBox is not running!"
	fi
}

run_rc_command "$1"
