Initial commit

This commit is contained in:
Fierelier 2024-04-23 04:34:16 +02:00
commit e5883a31e1
22 changed files with 299 additions and 0 deletions

9
LICENSE Normal file
View File

@ -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.

1
README.txt Normal file
View File

@ -0,0 +1 @@
Prerequisites: gosu screen psmisc

10
app/app Executable file
View File

@ -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" "$@"

5
app/bin/attach Normal file
View File

@ -0,0 +1,5 @@
#!/usr/bin/env bash
"$APP" onearg "$@"
"$APP" exist "$1"
source "$APP" service_env "$1"
exec screen -r "$SERVICE_SCREEN_NAME"

9
app/bin/debug Normal file
View File

@ -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

5
app/bin/exist Normal file
View File

@ -0,0 +1,5 @@
#!/usr/bin/env bash
if ! [ -d "$APP_SERVICES/$1" ]; then
echo "Service '$1' does not exist."
exit 1
fi

28
app/bin/init Normal file
View File

@ -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

23
app/bin/install Normal file
View File

@ -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

6
app/bin/list Normal file
View File

@ -0,0 +1,6 @@
#!/usr/bin/env bash
for d in "$APP_SERVICES"/*/; do
if [ -e "$d" ]; then
echo "$(basename "$d")"
fi
done

10
app/bin/onearg Normal file
View File

@ -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

2
app/bin/runcmd Normal file
View File

@ -0,0 +1,2 @@
#!/usr/bin/env bash
source "$APP_DIR/bin/""$@"

3
app/bin/screen_exists Normal file
View File

@ -0,0 +1,3 @@
#!/usr/bin/env bash
screen -ls "$1" >/dev/null 2>&1
echo $?

7
app/bin/service_env Normal file
View File

@ -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"

24
app/bin/start Normal file
View File

@ -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

50
app/bin/stop Normal file
View File

@ -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

12
app/bin/user_wait Normal file
View File

@ -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

68
install Executable file
View File

@ -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

View File

@ -0,0 +1,3 @@
#!/usr/bin/env bash
echo "hello, world!"
read -p "Press RETURN to quit." a

View File

View File

View File

@ -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

5
uninstall Executable file
View File

@ -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