git: 7f80d7465ec9 - main - www/oauth2-proxy: convert rc script to be instance-aware

From: Alexander Leidinger <netchild_at_FreeBSD.org>
Date: Wed, 08 May 2024 09:42:23 UTC
The branch main has been updated by netchild:

URL: https://cgit.FreeBSD.org/ports/commit/?id=7f80d7465ec9d85666472f99df16ba71b1e8c9d5

commit 7f80d7465ec9d85666472f99df16ba71b1e8c9d5
Author:     Alexander Leidinger <netchild@FreeBSD.org>
AuthorDate: 2024-05-08 09:34:27 +0000
Commit:     Alexander Leidinger <netchild@FreeBSD.org>
CommitDate: 2024-05-08 09:42:19 +0000

    www/oauth2-proxy: convert rc script to be instance-aware
    
    Like the tomcat ports or openhab, make the rc script instance aware.
    To use it:
     - cp /usr/local/etc/oauth2-proxy.cfg.sample /usr/local/etc/oauth2-proxy-myapp.cfg
     - vi /usr/local/etc/oauth2-proxy-myapp.cfg
     - ln -s oauth2_proxy /usr/local/etc/rc.d/oauth2_proxy_myapp
     - sysctl oauth2_proxy_myapp_enable=YES
     - service oauth2_proxy_myapp start
    
    Approved by:    maintainer timeout (3 weeks)
    PR:             278325
---
 UPDATING                                           | 15 +++++++
 www/oauth2-proxy/Makefile                          |  4 +-
 .../files/{oauth2-proxy.in => oauth2_proxy.in}     | 50 ++++++++++++++--------
 3 files changed, 49 insertions(+), 20 deletions(-)

diff --git a/UPDATING b/UPDATING
index 090819e783e1..4ad684dbe534 100644
--- a/UPDATING
+++ b/UPDATING
@@ -5,6 +5,21 @@ they are unavoidable.
 You should get into the habit of checking this file for changes each time
 you update your ports collection, before attempting any port upgrades.
 
+202408:
+  AFFECTS: www/oauth2-proxy
+  AUTHOR: netchild@FreeBSD.org
+
+  The start script is converted to be instance aware.  To use it:
+   - cp /usr/local/etc/oauth2-proxy.cfg.sample /usr/local/etc/oauth2-proxy-myapp.cfg
+   - vi /usr/local/etc/oauth2-proxy-myapp.cfg
+   - ln -s oauth2_proxy /usr/local/etc/rc.d/oauth2_proxy_myapp
+   - sysctl oauth2_proxy_myapp_enable=YES
+   - service oauth2_proxy_myapp start
+  This allows to easily start multiple instances on the same system.
+
+  As part of this, the start script was renamed to oauth2_proxy. This only
+  affects manual restarts, no change in rc.conf is needed.
+
 20240430:
   AFFECTS: audio/py-pyradio
   AUTHOR: nivit@FreeBSD.org
diff --git a/www/oauth2-proxy/Makefile b/www/oauth2-proxy/Makefile
index 29d9e5edb8ed..a34af1989feb 100644
--- a/www/oauth2-proxy/Makefile
+++ b/www/oauth2-proxy/Makefile
@@ -1,7 +1,7 @@
 PORTNAME=	oauth2-proxy
 PORTVERSION=	7.5.1
 DISTVERSIONPREFIX=	v
-PORTREVISION=	4
+PORTREVISION=	5
 CATEGORIES=	www
 
 MAINTAINER=	freebsd@rheinwolf.de
@@ -13,7 +13,7 @@ LICENSE_FILE=	${WRKSRC}/LICENSE
 
 USES=		go:modules
 
-USE_RC_SUBR=	oauth2-proxy
+USE_RC_SUBR=	oauth2_proxy
 
 USE_GITHUB=	yes
 GH_TUPLE=	\
diff --git a/www/oauth2-proxy/files/oauth2-proxy.in b/www/oauth2-proxy/files/oauth2_proxy.in
similarity index 51%
rename from www/oauth2-proxy/files/oauth2-proxy.in
rename to www/oauth2-proxy/files/oauth2_proxy.in
index 17122e3eff13..5c8aeab10dcb 100644
--- a/www/oauth2-proxy/files/oauth2-proxy.in
+++ b/www/oauth2-proxy/files/oauth2_proxy.in
@@ -16,21 +16,35 @@
 
 . /etc/rc.subr
 
-name=oauth2_proxy
-rcvar=oauth2_proxy_enable
+case $0 in
+/etc/rc*)
+	# during boot (shutdown) $0 is /etc/rc (/etc/rc.shutdown),
+	# so get the name of the script from $_file
+	name=$_file
+	;;
+*)
+	name=$0
+	;;
+esac
+ 
+name=${name##*/}
+rcvar=${name}_enable
 desc="OAuth 2.0 proxy server"
 
-load_rc_config $name
+load_rc_config "$name"
 
-: ${oauth2_proxy_enable:=NO}
-: ${oauth2_proxy_user:=%%USER%%}
-: ${oauth2_proxy_group:=%%GROUP%%}
-: ${oauth2_proxy_flags:=""}
-: ${oauth2_proxy_restart:=60}
+eval "${rcvar}=\${${rcvar}:-'NO'}"
+eval "_oauth2_proxy_user=\${${name}_user:-'%%USER%%'}"
+eval "_oauth2_proxy_group=\${${name}_group:-'%%GROUP%%'}"
+eval "_oauth2_proxy_flags=\${${name}_flags:-''}"
+eval "_oauth2_proxy_restart=\${${name}_restart:-'60'}"
 
-pidfile=%%RUN_DIR%%/%%PORTNAME%%.pid
+configname=$(echo ${name}.cfg | sed -e 's:_:-:g')
+
+pidfile=%%RUN_DIR%%/${name}.pid
+childpidfile=%%RUN_DIR%%/${name}_child.pid
 command=/usr/sbin/daemon
-command_args="-u ${oauth2_proxy_user} -o %%LOG_DIR%%/%%PORTNAME%%.out -t %%PORTNAME%% -R ${oauth2_proxy_restart} -P ${pidfile}"
+command_args="-u ${_oauth2_proxy_user} -o %%LOG_DIR%%/${name}.out -t ${name} -R ${_oauth2_proxy_restart} -P ${pidfile} -p ${childpidfile}"
 
 start_cmd="oauth2_proxy_start"
 stop_cmd="oauth2_proxy_stop"
@@ -38,19 +52,19 @@ stop_cmd="oauth2_proxy_stop"
 oauth2_proxy_start()
 {
 	if [ ! -d "%%LOG_DIR%%" ]; then
-		install -d -o ${oauth2_proxy_user} %%LOG_DIR%%
+		install -d -o ${_oauth2_proxy_user} %%LOG_DIR%%
 	fi
 	if [ ! -d "%%RUN_DIR%%" ]; then
-		install -d -o ${oauth2_proxy_user} %%RUN_DIR%%
+		install -d -o ${_oauth2_proxy_user} %%RUN_DIR%%
 	fi
 
-	chown -R ${oauth2_proxy_user} %%LOG_DIR%%
+	chown -R ${_oauth2_proxy_user} %%LOG_DIR%%
 
-	echo "Starting %%PORTNAME%%."
+	echo "Starting ${name}."
         ${command} ${command_args} \
                 %%PREFIX%%/bin/%%PORTNAME%% \
-                --config %%PREFIX%%/etc/%%PORTNAME%%.cfg \
-                ${oauth2_proxy_flags}
+                --config %%PREFIX%%/etc/${configname} \
+                ${_oauth2_proxy_flags}
 }
 
 oauth2_proxy_stop()
@@ -58,7 +72,7 @@ oauth2_proxy_stop()
     local pid_daemon
     local pid_child
 
-    echo "Stopping %%PORTNAME%%."
+    echo "Stopping ${name}."
 
     pid_daemon=$(check_pidfile ${pidfile} ${command})
     if [ ! -z "${pid_daemon}" ]; then
@@ -66,7 +80,7 @@ oauth2_proxy_stop()
     fi
 
 
-    pid_child=$(pgrep -U ${oauth2_proxy_user} -f %%PREFIX%%/bin/%%PORTNAME%%)
+    pid_child=$(check_pidfile ${childpidfile} %%PREFIX%%/bin/%%PORTNAME%%)
     if [ ! -z "${pid_child}" ]; then
         kill -TERM ${pid_child}
     fi