commit e5883a31e1ad853aeeae0b1433c96c6be3d60edb Author: Fierelier Date: Tue Apr 23 04:34:16 2024 +0200 Initial commit diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..6a6f3d8 --- /dev/null +++ b/LICENSE @@ -0,0 +1,9 @@ +MIT License + +Copyright (c) 2024 + +Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. diff --git a/README.txt b/README.txt new file mode 100644 index 0000000..1dca466 --- /dev/null +++ b/README.txt @@ -0,0 +1 @@ +Prerequisites: gosu screen psmisc diff --git a/app/app b/app/app new file mode 100755 index 0000000..f80f102 --- /dev/null +++ b/app/app @@ -0,0 +1,10 @@ +#!/usr/bin/env bash +set -e +APP="$(realpath "$BASH_SOURCE")" +APP_DIR="$(dirname "$APP")" +APP_NAME="$(basename "$APP_DIR")" +APP_SERVICES="${APP_DIR}.services" +if [ "$1" == "source" ]; then + return +fi +source "$APP_DIR/bin/runcmd" "$@" diff --git a/app/bin/attach b/app/bin/attach new file mode 100644 index 0000000..a395326 --- /dev/null +++ b/app/bin/attach @@ -0,0 +1,5 @@ +#!/usr/bin/env bash +"$APP" onearg "$@" +"$APP" exist "$1" +source "$APP" service_env "$1" +exec screen -r "$SERVICE_SCREEN_NAME" diff --git a/app/bin/debug b/app/bin/debug new file mode 100644 index 0000000..b928d86 --- /dev/null +++ b/app/bin/debug @@ -0,0 +1,9 @@ +#!/usr/bin/env bash +"$APP" onearg "$@" +"$APP" exist "$1" +source "$APP" service_env "$1" +if ! [ "$SERVICE_RUN" = "" ]; then + SERVICE_RUN_RAW="cd \"$SERVICE_CD\"; echo \"* pwd: \$PWD\"; exec ${SERVICE_RUN//"/\\"}" + echo "* bash: $SERVICE_RUN_RAW" + gosu "$SERVICE_USER" bash -c "$SERVICE_RUN_RAW" +fi diff --git a/app/bin/exist b/app/bin/exist new file mode 100644 index 0000000..50859e6 --- /dev/null +++ b/app/bin/exist @@ -0,0 +1,5 @@ +#!/usr/bin/env bash +if ! [ -d "$APP_SERVICES/$1" ]; then + echo "Service '$1' does not exist." + exit 1 +fi diff --git a/app/bin/init b/app/bin/init new file mode 100644 index 0000000..b1e14e2 --- /dev/null +++ b/app/bin/init @@ -0,0 +1,28 @@ +#!/usr/bin/env bash +set +e +echo "$APP_NAME service: $1" +if [ "$1" = "start" ]; then + "$APP" list | while read -r SERVICE; do + "$APP" start "$SERVICE" + done + exit 0 +fi + +if [ "$1" = "stop" ]; then + "$APP" list | while read -r SERVICE; do + "$APP" stop "$SERVICE" + done + exit 0 +fi + +if [ "$1" = "restart" ]; then + "$APP" list | while read -r SERVICE; do + "$APP" stop "$SERVICE" + done + "$APP" list | while read -r SERVICE; do + "$APP" start "$SERVICE" + done + exit 0 +fi + +exit 1 diff --git a/app/bin/install b/app/bin/install new file mode 100644 index 0000000..2f3f5b1 --- /dev/null +++ b/app/bin/install @@ -0,0 +1,23 @@ +#!/usr/bin/env bash +"$APP" onearg "$@" +"$APP" exist "$1" +source "$APP" service_env "$1" +set +e +"$APP" stop "$1" +useradd -d "$SERVICE_HOME" "$SERVICE_USER" +set -e +if [ "$SERVICE_PRIVATE" = "1" ]; then + chown -hR "$SERVICE_USER" "$SERVICE_DIR" + chgrp -hR "$SERVICE_USER" "$SERVICE_DIR" + chmod -R o= "$SERVICE_DIR" +fi + +for f in "$SERVICE_DIR/root"/*; do + if [ -e "$f" ]; then + mv "$f" "/" + fi +done + +if [ -e "$SERVICE_DIR/install" ]; then + source "$SERVICE_DIR/install" +fi diff --git a/app/bin/list b/app/bin/list new file mode 100644 index 0000000..2b95797 --- /dev/null +++ b/app/bin/list @@ -0,0 +1,6 @@ +#!/usr/bin/env bash +for d in "$APP_SERVICES"/*/; do + if [ -e "$d" ]; then + echo "$(basename "$d")" + fi +done diff --git a/app/bin/onearg b/app/bin/onearg new file mode 100644 index 0000000..6d8eb85 --- /dev/null +++ b/app/bin/onearg @@ -0,0 +1,10 @@ +#!/usr/bin/env bash +if [ "$1" = "" ]; then + echo "No argument given." + exit 1 +fi + +if ! [ "$2" = "" ]; then + echo "Only one argument can be given." + exit 1 +fi diff --git a/app/bin/runcmd b/app/bin/runcmd new file mode 100644 index 0000000..dc1ee91 --- /dev/null +++ b/app/bin/runcmd @@ -0,0 +1,2 @@ +#!/usr/bin/env bash +source "$APP_DIR/bin/""$@" diff --git a/app/bin/screen_exists b/app/bin/screen_exists new file mode 100644 index 0000000..90de810 --- /dev/null +++ b/app/bin/screen_exists @@ -0,0 +1,3 @@ +#!/usr/bin/env bash +screen -ls "$1" >/dev/null 2>&1 +echo $? diff --git a/app/bin/service_env b/app/bin/service_env new file mode 100644 index 0000000..9efd20b --- /dev/null +++ b/app/bin/service_env @@ -0,0 +1,7 @@ +#!/usr/bin/env bash +"$APP" exist "$1" +SERVICE_USER="${APP_NAME}_$1" +SERVICE_SCREEN_NAME="$SERVICE_USER" +SERVICE_DIR="$APP_SERVICES/$1" +SERVICE_HOME="$SERVICE_DIR/home" +source "$SERVICE_DIR/settings" diff --git a/app/bin/start b/app/bin/start new file mode 100644 index 0000000..2810ff2 --- /dev/null +++ b/app/bin/start @@ -0,0 +1,24 @@ +#!/usr/bin/env bash +"$APP" onearg "$@" +"$APP" exist "$1" +source "$APP" service_env "$1" + +echo "[$APP_NAME] Starting '$1' ..." +if [ "$SERVICE_RUN" = "" ]; then + exit 0 +fi + +set +e +ps -u "$SERVICE_USER" +if [ "$?" = "0" ]; then + echo "Service is already running." + exit 0 +fi +set -e +SERVICE_RUN_RAW="cd \"$SERVICE_CD\"; exec ${SERVICE_RUN//"/\\"}" +if [ "$SERVICE_SCREEN" = "1" ]; then + screen -dmS "$SERVICE_SCREEN_NAME" gosu "$SERVICE_USER" bash -c "$SERVICE_RUN_RAW" +else + gosu "$SERVICE_USER" nohup bash -c "$SERVICE_RUN_RAW" > /dev/null 2>&1 & +fi + diff --git a/app/bin/stop b/app/bin/stop new file mode 100644 index 0000000..fcee1c2 --- /dev/null +++ b/app/bin/stop @@ -0,0 +1,50 @@ +#!/usr/bin/env bash +"$APP" onearg "$@" +"$APP" exist "$1" + +echo "[$APP_NAME] Stopping '$1' ..." +source "$APP" service_env "$1" +if [ "$SERVICE_STOP" = "0" ]; then + exit 0 +fi +set +e +ps -u "$SERVICE_USER" +if ! [ "$?" = "0" ]; then + echo "Service already stopped." + exit 0 +fi + +if [ "$SERVICE_SCREEN" = "1" ] && ! [ "$SERVICE_SCREEN_EXIT_COMMAND" = "" ]; then + echo "[$APP_NAME] * Sending backspaces to screen ..." + if [ "$SERVICE_SCREEN_EXIT_BACKSPACE" = "1" ]; then + screen -S "$SERVICE_SCREEN_NAME" -X stuff "" + screen -S "$SERVICE_SCREEN_NAME" -X stuff "" + screen -S "$SERVICE_SCREEN_NAME" -X stuff "" + screen -S "$SERVICE_SCREEN_NAME" -X stuff "" + screen -S "$SERVICE_SCREEN_NAME" -X stuff "" + screen -S "$SERVICE_SCREEN_NAME" -X stuff "" + screen -S "$SERVICE_SCREEN_NAME" -X stuff "" + screen -S "$SERVICE_SCREEN_NAME" -X stuff "" + screen -S "$SERVICE_SCREEN_NAME" -X stuff "" + screen -S "$SERVICE_SCREEN_NAME" -X stuff "" + fi + echo "[$APP_NAME] * Sending exit command to screen ..." + screen -S "$SERVICE_SCREEN_NAME" -X stuff "$SERVICE_SCREEN_EXIT_COMMAND^M" + echo "[$APP_NAME] * Waiting for service to end ..." + "$APP" user_wait "$SERVICE_USER" "$SERVICE_TIMEOUT_SCREEN_EXIT" + if [ "$?" = "0" ]; then + exit 0 + fi +fi + +echo "[$APP_NAME] * Terminating service ..." +killall -15 -u "$SERVICE_USER" +echo "[$APP_NAME] * Waiting for service to end ..." +"$APP" user_wait "$SERVICE_USER" "$SERVICE_TIMEOUT_TERMINATE" +if [ "$?" = "0" ]; then + exit 0 +fi + +echo "[$APP_NAME] * Killing service." +killall -9 -u "$SERVICE_USER" +exit 0 diff --git a/app/bin/user_wait b/app/bin/user_wait new file mode 100644 index 0000000..c026a09 --- /dev/null +++ b/app/bin/user_wait @@ -0,0 +1,12 @@ +#!/usr/bin/env bash +set +e +COUNT=0 +while [ "$COUNT" -le "$2" ]; do + ps -u "$1" + if [ "$?" != "0" ]; then + exit 0 + fi + sleep 1 + COUNT=$(($COUNT + 1)) +done +exit 1 diff --git a/install b/install new file mode 100755 index 0000000..9bd1b92 --- /dev/null +++ b/install @@ -0,0 +1,68 @@ +#!/usr/bin/env bash +has_param() { + local term="$1" + shift + for arg; do + if [ "$arg" = "$1" ]; then + return 0 + fi + done + return 1 +} + +if ! has_param "-f" "$@"; then + set -e +fi + +INSTALL_HOME="$(realpath "$BASH_SOURCE")" +INSTALL_HOME="$(dirname "$INSTALL_HOME")" +INSTALL_APPNAME="svchelper" +INSTALL_SERVICE="$INSTALL_APPNAME" +INSTALL_SERVICE_PATH="/etc/init.d/$INSTALL_SERVICE" +INSTALL_INPUT="$INSTALL_HOME/app" +INSTALL_OUTPUT="/opt/$INSTALL_APPNAME" +INSTALL_BIN="/usr/local/bin/$INSTALL_APPNAME" + +echo "Uninstalling '$INSTALL_APPNAME' ..." +if [ -e "$INSTALL_SERVICE_PATH" ]; then + service "$INSTALL_SERVICE" stop + update-rc.d "$INSTALL_SERVICE" remove + rm "$INSTALL_SERVICE_PATH" +fi +if [ -e "$INSTALL_BIN" ]; then + rm "$INSTALL_BIN" +fi +if [ -e "$INSTALL_OUTPUT" ]; then + rm -rf "$INSTALL_OUTPUT" +fi + +if has_param "-u" "$@"; then + exit 0 +fi + +echo "Installing '$INSTALL_APPNAME' ..." +cp -r "$INSTALL_INPUT" "$INSTALL_OUTPUT" +ln -s "$INSTALL_OUTPUT/app" "$INSTALL_BIN" + +echo '#!/bin/bash' >"$INSTALL_SERVICE_PATH" +echo '### BEGIN INIT INFO' >>"$INSTALL_SERVICE_PATH" +echo '# Provides: '"$INSTALL_SERVICE" >>"$INSTALL_SERVICE_PATH" +echo '# Required-Start: $all' >>"$INSTALL_SERVICE_PATH" +echo '# Required-Stop: $all' >>"$INSTALL_SERVICE_PATH" +echo '# Default-Start: 2 3 4 5' >>"$INSTALL_SERVICE_PATH" +echo '# Default-Stop: 0 6' >>"$INSTALL_SERVICE_PATH" +echo '# Short-Description: Service helper' >>"$INSTALL_SERVICE_PATH" +echo '### END INIT INFO' >>"$INSTALL_SERVICE_PATH" +echo '' >>"$INSTALL_SERVICE_PATH" +echo "exec bash \"$INSTALL_BIN\" init \"\$1\"" >>"$INSTALL_SERVICE_PATH" +chmod +x "$INSTALL_SERVICE_PATH" +update-rc.d "$INSTALL_SERVICE" defaults + +source "$INSTALL_BIN" source +mkdir -p "$APP_SERVICES" +set +e +"$APP" list | while read -r SERVICE; do + "$APP" install "$SERVICE" +done +service "$INSTALL_SERVICE" start +exit 0 diff --git a/services/hello_world/home/hello b/services/hello_world/home/hello new file mode 100755 index 0000000..e83da8b --- /dev/null +++ b/services/hello_world/home/hello @@ -0,0 +1,3 @@ +#!/usr/bin/env bash +echo "hello, world!" +read -p "Press RETURN to quit." a diff --git a/services/hello_world/root-exclude b/services/hello_world/root-exclude new file mode 100644 index 0000000..e69de29 diff --git a/services/hello_world/root-include b/services/hello_world/root-include new file mode 100644 index 0000000..e69de29 diff --git a/services/hello_world/settings b/services/hello_world/settings new file mode 100644 index 0000000..924f1fb --- /dev/null +++ b/services/hello_world/settings @@ -0,0 +1,19 @@ +#!/bin/sh +# If command is defined, it will create and run a service unit +SERVICE_RUN='./hello' +# Working directory +SERVICE_CD="$SERVICE_HOME" +# Create a screen for the service? +SERVICE_SCREEN=1 +# Make service private. Sets the owner of all files to the service's, and the permissions for other users to 0 (no access). For more exact control, use a post-setup script. +SERVICE_PRIVATE=1 +# Whether to make an attempt to quit the service. Set to 0 if it doesn't need to end +SERVICE_EXIT=1 +# If filled out, it will enter this text into the service's screen. If your server needs a command to stop, enter it here. +SERVICE_SCREEN_EXIT_COMMAND='bababooey' +# Inject 10,000 backspace key presses before entering the exit command into screen +SERVICE_SCREEN_EXIT_BACKSPACE=1 +# How many seconds to wait before terminating the processes +SERVICE_TIMEOUT_SCREEN_EXIT=30 +# How many seconds to wait before killing the processes +SERVICE_TIMEOUT_TERMINATE=30 diff --git a/uninstall b/uninstall new file mode 100755 index 0000000..4f4e0d1 --- /dev/null +++ b/uninstall @@ -0,0 +1,5 @@ +#!/usr/bin/env bash +set -e +INSTALL_HOME="$(readlink -f "$0")" +INSTALL_HOME="$(dirname "$INSTALL_HOME")" +exec "$INSTALL_HOME/install" -u