#!/sbin/openrc-run
# managed by ovpmon deploy/native  (install.sh / update.sh replace this file only while this marker line is present)
#
# OpenVPN Profiler API (FastAPI under uvicorn, APP_PROFILER), OpenRC (Alpine): PKI (easy-rsa),
# profiles, server settings. Install: see ovpmon-api. Settings come from /etc/ovpmon/env.
#
# Runs as ovpmon. Anything that needs root (install server.conf, publish the CRL, start/stop OpenVPN)
# goes through `doas /usr/local/sbin/ovpmon-helper <fixed args>` (rules: /etc/doas.conf or
# /etc/doas.d/ovpmon.conf, configuration: /etc/ovpmon/helper.conf).
# Started without --reload and without depending on openvpn.

name="ovpmon-profiler"
description="OpenVPN Profiler API (uvicorn)"

supervisor="supervise-daemon"
respawn_delay=3
respawn_max=0

command_user="ovpmon:ovpmon"
directory="/opt/OpenVPN-Monitoring-Simple/APP_PROFILER"
command="/opt/OpenVPN-Monitoring-Simple/APP_PROFILER/venv/bin/python"
command_args="-m uvicorn main:app --host 127.0.0.1 --port 8000"

output_log="/var/log/ovpmon/ovpmon-profiler.log"
error_log="/var/log/ovpmon/ovpmon-profiler.log"

depend() {
	need net
	after firewall
}

start_pre() {
	checkpath -f -m 0640 -o ovpmon:ovpmon "$output_log" || return 1
	[ -r /etc/ovpmon/env ] || { eerror "/etc/ovpmon/env is missing"; return 1; }
	set -a
	. /etc/ovpmon/env
	set +a
	# extend, never replace: OpenRC itself needs /lib/rc/bin (ebegin, eend) on the PATH
	export PATH="/usr/local/sbin:/usr/local/bin:$PATH"
}
