From df60d13f2a9a63620a739a563ff5875022527b75 Mon Sep 17 00:00:00 2001 From: Vyacheslav Boyko Date: Fri, 20 Sep 2024 22:42:41 +0300 Subject: [PATCH] linux SystemD service with installator for Logs-Preparer --- logs-preparer/install.sh | 95 +++++++++++++++++++++++++++++ logs-preparer/logs-preparer.service | 17 ++++++ 2 files changed, 112 insertions(+) create mode 100644 logs-preparer/install.sh create mode 100644 logs-preparer/logs-preparer.service diff --git a/logs-preparer/install.sh b/logs-preparer/install.sh new file mode 100644 index 0000000..400916f --- /dev/null +++ b/logs-preparer/install.sh @@ -0,0 +1,95 @@ +#!/bin/bash + +#echo "Service name:" +#read SERVICE +#echo "Rabbit host:" +#read RHOST +#echo "Rabbit pass:" +#read RPASS + +HELP=$(cat <<'EOF' +Missing required args: + +-s - SERVICE name +-h - RabbitMQ HOST +-p - RabbitMQ PASS +EOF +) + +VECTOR=$(which vector) + +if [ -z $VECTOR ]; then + echo "You need https://vector.dev to be installed" +fi + +echo "VECTOR=$VECTOR" + +while getopts ":s:h:p:" opt; do + case $opt in + s) SERVICE="$OPTARG" + ;; + h) RHOST="$OPTARG" + ;; + p) RPASS="$OPTARG" + ;; + \?) echo "Invalid option -$OPTARG" >&2 + exit 1 + ;; + esac + + case $OPTARG in + -*) echo "Option $opt needs a valid argument" + exit 1 + ;; + esac +done + +if [ -z $SERVICE ]; then + echo "$HELP" + exit -1 +fi +if [ -z $RHOST ]; then + echo "$HELP" + exit -1 +fi +if [ -z $RPASS ]; then + echo "$HELP" + exit -1 +fi + +printf "SERVICE = %s\n" "$SERVICE" +printf "HOST = %s\n" "$RHOST" +printf "PASS = %s\n" "$RPASS" + +FNAME="$SERVICE-logs-preparer.service" +#SERVICEFILE="/etc/systemd/system/$FNAME" +SERVICEFILE="$HOME/.config/systemd/user/$FNAME" + +echo "FNAME = $FNAME" +echo "SERVICEFILE = $SERVICEFILE" + +if [ -f $SERVICEFILE ]; then + echo "Disabling current service..." + systemctl --user stop $FNAME + systemctl --user disable $FNAME + systemctl --user daemon-reload + sudo rm $SERVICEFILE +fi + +echo "Creating service file..." + +cat logs-preparer.service | sed 's??'`pwd`'?' | sed 's//'$RHOST'/' | sed 's//'$RPASS'/' | sed 's//'$SERVICE'/' | sed 's??'$VECTOR'?' | sudo tee -a "$SERVICEFILE" > /dev/null + +echo "Reloading daemons..." + +systemctl --user daemon-reload + +echo "Enabling daemon for user..." + +systemctl --user enable $FNAME + +echo "Starting daemon for user..." + +systemctl --user start $FNAME + + diff --git a/logs-preparer/logs-preparer.service b/logs-preparer/logs-preparer.service new file mode 100644 index 0000000..1dbe79f --- /dev/null +++ b/logs-preparer/logs-preparer.service @@ -0,0 +1,17 @@ +[Unit] +Description=Logs-Preparer for +After=network.target + +[Service] +Type=simple +WorkingDirectory= +Environment=RABBITMQ_HOST= +Environment=RABBITMQ_PASS= +ExecStart= --config /vector.yaml +CPUSchedulingPolicy=idle +IOSchedulingClass=3 +Restart=always +RestartSec=3 + +[Install] +WantedBy=default.target