git: b16c0ca7d04e - main - sysutils/loki: add promtail rc.d script

From: Robert Clausecker <fuz_at_FreeBSD.org>
Date: Thu, 19 Oct 2023 07:08:11 UTC
The branch main has been updated by fuz:

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

commit b16c0ca7d04e67ba0dd9e2ff7a5cde372a79171b
Author:     Eren Türkay <turkay.eren@gmail.com>
AuthorDate: 2023-10-06 09:46:13 +0000
Commit:     Robert Clausecker <fuz@FreeBSD.org>
CommitDate: 2023-10-19 07:07:26 +0000

    sysutils/loki: add promtail rc.d script
    
    Promtail is a log reader and it can be run as a service to gather the
    logs to be sent to remote loki server. For nodes that need to send logs
    only, promtail service and configuration file is needed
    
    PR:             274194, 274473
    Signed-off-by:  Christopher Beppler <freebsd@funzi.org>
    Approved by:    Chirstopher Beppler <freebsd@funzi.org> (maintainer)
---
 UIDs                                               |  2 +-
 sysutils/loki/Makefile                             | 13 +++-
 .../loki/files/patch-promtail-local-config.yaml    | 11 ++++
 sysutils/loki/files/promtail.in                    | 70 ++++++++++++++++++++++
 sysutils/loki/pkg-plist                            |  4 ++
 5 files changed, 96 insertions(+), 4 deletions(-)

diff --git a/UIDs b/UIDs
index d3900898c438..89a17db9381a 100644
--- a/UIDs
+++ b/UIDs
@@ -763,7 +763,7 @@ foreman_proxy:*:812:812::0:0:Foreman Smart Proxy:/usr/local/share/foreman-proxy:
 hitch:*:813:813::0:0:Hitch TLS Proxy:/nonexistent:/usr/sbin/nologin
 puppet:*:814:814::0:0:Puppet Daemon:/nonexistent:/usr/sbin/nologin
 uchiwa:*:815:815::0:0:Uchiwa Dashboard:/nonexistent:/usr/sbin/nologin
-# free: 816
+promtail:*:816:967::0:0:Promtail:/var/db/promtail:/usr/sbin/nologin
 rslsync:*:817:817::0:0:Resilio Sync Daemon:/nonexistent:/usr/sbin/nologin
 jenkins:*:818:818::0:0:Jenkins CI:/usr/local/jenkins:/bin/sh
 rundeck:*:819:819::0:0:Rundeck:/usr/local/rundeck:/bin/sh
diff --git a/sysutils/loki/Makefile b/sysutils/loki/Makefile
index e1053064ed62..a459876e2fb2 100644
--- a/sysutils/loki/Makefile
+++ b/sysutils/loki/Makefile
@@ -326,7 +326,7 @@ GH_TUPLE=	\
 		xdg-go:stringprep:v1.0.4:xdg_go_stringprep/vendor/github.com/xdg-go/stringprep \
 		yuin:gopher-lua:v1.1.0:yuin_gopher_lua/vendor/github.com/yuin/gopher-lua
 
-USE_RC_SUBR=	loki
+USE_RC_SUBR=	loki promtail
 
 GO_TARGET=	./cmd/loki ./cmd/loki-canary ./cmd/logcli ./clients/cmd/promtail
 
@@ -334,12 +334,17 @@ LOKI_USER?=	loki
 LOKI_GROUP?=	loki
 LOKI_DATADIR?=	/var/db/${PORTNAME}
 
-USERS=		${LOKI_USER}
+PROMTAIL_USER?=	promtail
+PROMTAIL_DATADIR?=	/var/db/promtail
+
+USERS=		${LOKI_USER} ${PROMTAIL_USER}
 GROUPS=		${LOKI_GROUP}
 
 SUB_LIST=	LOKI_USER=${LOKI_USER} \
 		LOKI_GROUP=${LOKI_GROUP} \
-		LOKI_DATADIR=${LOKI_DATADIR}
+		LOKI_DATADIR=${LOKI_DATADIR} \
+		PROMTAIL_USER=${PROMTAIL_USER} \
+		PROMTAIL_DATADIR=${PROMTAIL_DATADIR}
 
 PLIST_SUB=	${SUB_LIST}
 
@@ -372,7 +377,9 @@ post-extract:
 
 post-install:
 	@${CP} ${WRKSRC}/cmd/loki/loki-local-config.yaml ${STAGEDIR}${PREFIX}/etc/loki.yaml.sample
+	@${CP} ${WRKSRC}/clients/cmd/promtail/promtail-local-config.yaml ${STAGEDIR}${PREFIX}/etc/promtail.yaml.sample
 	@${MKDIR} ${STAGEDIR}${LOKI_DATADIR}
 	@${MKDIR} ${STAGEDIR}${LOKI_DATADIR}/rules-temp
+	@${MKDIR} ${STAGEDIR}${PROMTAIL_DATADIR}
 
 .include <bsd.port.mk>
diff --git a/sysutils/loki/files/patch-promtail-local-config.yaml b/sysutils/loki/files/patch-promtail-local-config.yaml
new file mode 100644
index 000000000000..b02e5db10eb0
--- /dev/null
+++ b/sysutils/loki/files/patch-promtail-local-config.yaml
@@ -0,0 +1,11 @@
+--- clients/cmd/promtail/promtail-local-config.yaml.orig	2023-10-05 01:40:22.549761000 +0000
++++ clients/cmd/promtail/promtail-local-config.yaml	2023-10-05 01:41:28.869261000 +0000
+@@ -3,7 +3,7 @@
+   grpc_listen_port: 0
+ 
+ positions:
+-  filename: /tmp/positions.yaml
++  filename: /var/db/promtail/positions.yaml
+ 
+ clients:
+   - url: http://localhost:3100/loki/api/v1/push
diff --git a/sysutils/loki/files/promtail.in b/sysutils/loki/files/promtail.in
new file mode 100644
index 000000000000..ff8c9464cbec
--- /dev/null
+++ b/sysutils/loki/files/promtail.in
@@ -0,0 +1,70 @@
+#!/bin/sh
+
+# PROVIDE: promtail
+# REQUIRE: LOGIN
+# KEYWORD: shutdown
+
+# Add the following lines to /etc/rc.conf to enable promtail
+# promtail_enable="YES"
+#
+# promtail_enable (bool):
+#     Set it to YES to enable promtail
+#     Set to NO by default
+# promtail_user (string):
+#     Set user that promtail will run under
+#     Default is "%%PROMTAIL_USER%%"
+# promtail_group (string):
+#     Set group that own promtail files
+#     Default is "%%LOKI_GROUP%%"
+# promtail_config (string)
+#     Set full path to config file
+#     Default is "%%PREFIX%%/etc/promtail.yaml"
+# promtail_logfile (string)
+#     Set full path to log file
+#     Default is "/var/log/promtail/promtail.log"
+# promtail_loglevel (string)
+#     Set log level. Only log messages with the given severity or above.
+#     Valid levels: [debug, info, warn, error]
+#     Default is "warn"
+# promtail_args (string)
+#     Set additional command line arguments
+#     Default is ""
+
+. /etc/rc.subr
+
+name=promtail
+rcvar=promtail_enable
+
+load_rc_config $name
+
+: ${promtail_enable:="NO"}
+: ${promtail_user:="%%PROMTAIL_USER%%"}
+: ${promtail_group:="%%LOKI_GROUP%%"}
+: ${promtail_config:="%%PREFIX%%/etc/promtail.yaml"}
+: ${promtail_logfile:="/var/log/promtail/promtail.log"}
+: ${promtail_loglevel:="warn"}
+
+pidfile="/var/run/${name}/${name}.pid"
+required_files="${promtail_config}"
+
+procname="%%PREFIX%%/bin/promtail"
+command="/usr/sbin/daemon"
+command_args="-p ${pidfile} -t ${name} -o ${promtail_logfile} \
+	${procname} \
+	--config.file=${promtail_config} \
+	--log.level=${promtail_loglevel} \
+	${promtail_args}"
+
+start_precmd="promtail_start_precmd"
+
+promtail_start_precmd() {
+	if [ ! -d "/var/run/${name}" ]; then
+		install -d -m 0750 -o ${promtail_user} -g ${promtail_group} "/var/run/${name}"
+	fi
+
+	if [ ! -d "/var/log/promtail" ]; then
+		install -d -m 0750 -o ${promtail_user} -g ${promtail_group} "/var/log/promtail"
+	fi
+}
+
+run_rc_command "$1"
diff --git a/sysutils/loki/pkg-plist b/sysutils/loki/pkg-plist
index b39abf641010..301c053577b5 100644
--- a/sysutils/loki/pkg-plist
+++ b/sysutils/loki/pkg-plist
@@ -3,11 +3,15 @@ bin/loki-canary
 bin/logcli
 bin/promtail
 @sample etc/loki.yaml.sample
+@sample etc/promtail.yaml.sample
 @owner %%LOKI_USER%%
 @group %%LOKI_GROUP%%
 @mode 750
 @dir %%LOKI_DATADIR%%
 @dir %%LOKI_DATADIR%%/rules-temp
+@owner %%PROMTAIL_USER%%
+@mode 750
+@dir %%PROMTAIL_DATADIR%%
 @mode
 @group
 @owner