diff options
-rw-r--r-- | .SRCINFO | 36 | ||||
-rw-r--r-- | .gitignore | 11 | ||||
-rw-r--r-- | PKGBUILD | 163 | ||||
-rw-r--r-- | forge-server.install | 55 | ||||
-rw-r--r-- | forged-backup.service | 12 | ||||
-rw-r--r-- | forged-backup.timer | 10 | ||||
-rw-r--r-- | forged.conf | 29 | ||||
-rw-r--r-- | forged.service | 13 | ||||
-rwxr-xr-x | forged.sh | 492 |
9 files changed, 821 insertions, 0 deletions
diff --git a/.SRCINFO b/.SRCINFO new file mode 100644 index 000000000000..e7f7188a45e1 --- /dev/null +++ b/.SRCINFO @@ -0,0 +1,36 @@ +pkgbase = forge-server-1.14.4 + pkgdesc = Minecraft Forge server unit files, script and jar + pkgver = 1.14.4_28.2.0 + pkgrel = 1 + url = https://minecraftforge.net + install = forge-server.install + arch = any + license = custom + depends = java-runtime-headless=8 + depends = screen + depends = sudo + depends = bash + depends = awk + depends = sed + optdepends = tar: needed in order to create world backups + optdepends = netcat: required in order to suspend an idle server + provides = forge-server=1.14.4_28.2.0 + noextract = forge-1.14.4-28.2.0.jar + backup = etc/conf.d/forge-1.14.4 + source = forged-backup.service + source = forged-backup.timer + source = forged.service + source = forged.conf + source = forged.sh + source = forge-1.14.4-28.2.0-installer.jar::https://files.minecraftforge.net/maven/net/minecraftforge/forge/1.14.4-28.2.0/forge-1.14.4-28.2.0-installer.jar + source = LICENSE-forge-server-1.14.4-1.14.4-28.2.0.txt::https://raw.githubusercontent.com/MinecraftForge/MinecraftForge/1.14.x/LICENSE.txt + sha512sums = 6aeccb9c87675cf1099e7c21ec58c0c2cd8331aed1146f5a845e4e489556ef0e96b15cc1ea00d216bad616167ed1707fec40c41c53d94ea3627cfdf8f8f9be33 + sha512sums = 1e0223cb0ea2fb810bdc94517a4f111df2da528cb04dcf94b8e3d22fc6aa244c55c9039ed97cd7abcccea889015d6016e284cd1b74de3c7dc4bd136bd20a2d4d + sha512sums = eb1727f9ad39dd45049c025ee4ee598783ffc544f996a71dc620ffeffdad5ae86871d8d93236ef47802c5b5f7842f38febab3438a88cfd52b60bf62172e79b6f + sha512sums = 92bc57b28228d237326d891868dd85e394376330c10f5389015c4de17a11c7b916e42ab9fb9d2c52684a829036f705f2c0fae97259658f3c9667cc8fa951cce9 + sha512sums = c874e0cd74fa432c4be170eb2cecca23a88062eef2ea38904fba6e6a8f9e97bf3b6775d28faa36b90ffd12e7fae1cbf24aee27d37451ae152e20bf179d06e6c4 + sha512sums = 2a2a0acf737bd918ff1cd86d9239c5d6b0cba9c2906044121cb8b15212c9f73d11740e0588161363b15ed1a662210922bb6ae7256aaadf9376fab6a649aa1cf6 + sha512sums = 3da10d63a5edee4bc8bcd3d5c2730771062f7fa58626a8c51635fbe96bfbceca3ff6937cfaad3e17f16a94ef95137f7c78cc6dac1c846a6b9a8f18d3c6355973 + +pkgname = forge-server-1.14.4 + diff --git a/.gitignore b/.gitignore new file mode 100644 index 000000000000..92fdfcfe7214 --- /dev/null +++ b/.gitignore @@ -0,0 +1,11 @@ +* +!/forged-backup.service +!/forged-backup.timer +!/forged.conf +!/forged.service +!/forged.sh +!/forge-server.install +!/forge-server-custom.install +!/PKGBUILD +!/.gitignore +!/.SRCINFO diff --git a/PKGBUILD b/PKGBUILD new file mode 100644 index 000000000000..6344c4047ea7 --- /dev/null +++ b/PKGBUILD @@ -0,0 +1,163 @@ +# Maintainer: Nitroretro <nitroretro@protonmail.com> + +# Based on the `minecraft-server` AUR package by: +## Maintainer: Gordian Edenhofer <gordian.edenhofer@gmail.com> +## Contributor: Philip Abernethy <chais.z3r0@gmail.com> +## Contributor: sowieso <sowieso@dukun.de> + +_ver="1.14.4_28.2.0-1" +_minecraft_ver_latest="1.15.2" + +IFS="-" read -ra _ver_temp <<< "$_ver" +IFS="_" read -ra _pkgver_temp <<< "${_ver_temp[0]}" +IFS="." read -ra _minecraft_ver_temp <<< "${_pkgver_temp[0]}" + +_minecraft_ver=${_pkgver_temp[0]} +_minecraft_ver_major=${_minecraft_ver_temp[0]:-0} +_minecraft_ver_minor=${_minecraft_ver_temp[1]:-0} +_minecraft_ver_patch=${_minecraft_ver_temp[2]:-0} +_forge_ver=${_pkgver_temp[1]} + +_pkgver=${_ver_temp[0]//_/-} + +if [ "$_minecraft_ver" = "$_minecraft_ver_latest" ]; then + pkgname="forge-server" + _forge_name="forge" +else + pkgname="forge-server-${_minecraft_ver}" + _forge_name="forge-${_minecraft_ver}" +fi +pkgver=${_ver_temp[0]} +pkgrel=${_ver_temp[1]} +pkgdesc="Minecraft Forge server unit files, script and jar" +arch=("any") +url="https://minecraftforge.net" +license=("custom") +depends=("java-runtime-headless=8" "screen" "sudo" "bash" "awk" "sed") +optdepends=("tar: needed in order to create world backups" + "netcat: required in order to suspend an idle server") +provides=("forge-server=${pkgver}") +backup=("etc/conf.d/${_forge_name}") +install="forge-server.install" +source=("forged-backup.service" + "forged-backup.timer" + "forged.service" + "forged.conf" + "forged.sh") +noextract=("forge-${_pkgver}.jar") +sha512sums=('6aeccb9c87675cf1099e7c21ec58c0c2cd8331aed1146f5a845e4e489556ef0e96b15cc1ea00d216bad616167ed1707fec40c41c53d94ea3627cfdf8f8f9be33' + '1e0223cb0ea2fb810bdc94517a4f111df2da528cb04dcf94b8e3d22fc6aa244c55c9039ed97cd7abcccea889015d6016e284cd1b74de3c7dc4bd136bd20a2d4d' + 'eb1727f9ad39dd45049c025ee4ee598783ffc544f996a71dc620ffeffdad5ae86871d8d93236ef47802c5b5f7842f38febab3438a88cfd52b60bf62172e79b6f' + '92bc57b28228d237326d891868dd85e394376330c10f5389015c4de17a11c7b916e42ab9fb9d2c52684a829036f705f2c0fae97259658f3c9667cc8fa951cce9' + 'c874e0cd74fa432c4be170eb2cecca23a88062eef2ea38904fba6e6a8f9e97bf3b6775d28faa36b90ffd12e7fae1cbf24aee27d37451ae152e20bf179d06e6c4' + '2a2a0acf737bd918ff1cd86d9239c5d6b0cba9c2906044121cb8b15212c9f73d11740e0588161363b15ed1a662210922bb6ae7256aaadf9376fab6a649aa1cf6' + '3da10d63a5edee4bc8bcd3d5c2730771062f7fa58626a8c51635fbe96bfbceca3ff6937cfaad3e17f16a94ef95137f7c78cc6dac1c846a6b9a8f18d3c6355973') + +# -- Forge Installer -- # +if [ "$_minecraft_ver_minor" = 7 ]; then + source+=("forge-${_pkgver}-installer.jar"::"https://files.minecraftforge.net/maven/net/minecraftforge/forge/${_pkgver}-${_minecraft_ver}/forge-${_pkgver}-${_minecraft_ver}-installer.jar") +else + source+=("forge-${_pkgver}-installer.jar"::"https://files.minecraftforge.net/maven/net/minecraftforge/forge/${_pkgver}/forge-${_pkgver}-installer.jar") +fi + +[ "$_minecraft_ver_minor" = 5 ] && source+=("https://files.minecraftforge.net/fmllibs/fml_libs15.zip") && noextract+=("fml_libs15.zip") +# -- /Forge Installer -- # + +# -- Licenses -- # +_licenses=() +_license_suffix="-${pkgname}-${_pkgver}.txt" + +if [ "$_minecraft_ver_minor" -ge 10 ]; then + _branch="${_minecraft_ver_major}.${_minecraft_ver_minor}.x" +elif [ "$_minecraft_ver_minor" -ge 7 ]; then + _branch="$_minecraft_ver" +elif [ "$_minecraft_ver_minor" = 6 ]; then + _branch="1.6" +else + _branch="v7.9" +fi + +_add_license() { + _path=$1 + _repo=${2:-MinecraftForge} + _github_branch=${3:-"$_branch"} + _filename="$(basename "$_path")${_license_suffix}" + + _licenses+=("$_filename") + source+=("$_filename"::"https://raw.githubusercontent.com/MinecraftForge/${_repo}/${_github_branch}/${_path}.txt") +} + +if [ "$_minecraft_ver_minor" -ge 13 ]; then + _add_license "LICENSE" +elif [ "$_minecraft_ver_minor" = 12 ]; then + _add_license "LICENSE-Paulscode%20IBXM%20Library" + _add_license "LICENSE-Paulscode%20SoundSystem%20CodecIBXM" + _add_license "LICENSE" +elif [ "$_minecraft_ver_minor" -ge 7 ]; then + _add_license "MinecraftForge-License" + _add_license "Paulscode%20IBXM%20Library%20License" + _add_license "Paulscode%20SoundSystem%20CodecIBXM%20License" + + case "$_minecraft_ver_minor" in + 8) _add_license "LICENSE-fml";; + 7) _add_license "fml/LICENSE-fml";; + *) _add_license "LICENSE-fml" && _add_license "LICENSE-new";; + esac +elif [ "$_minecraft_ver_minor" = 6 ]; then + _add_license "install/MinecraftForge-License" + _add_license "install/Paulscode%20IBXM%20Library%20License" + _add_license "install/Paulscode%20SoundSystem%20CodecIBXM%20License" + _add_license "LICENSE-fml" "FML" "902772ed0cb6c22c4cd7ad9b0ec7a02961b5e016" +else + _add_license "LICENSE-fml" +fi +# -- /Licenses -- # + +prepare() { + [ "$_minecraft_ver_minor" = 10 ] && mkdir mods + [ "$_minecraft_ver_minor" = 5 ] && unzip fml_libs15.zip -d lib + java -jar "forge-${_pkgver}-installer.jar" --installServer +} + +package() { + _server_root="${pkgdir}/srv/${_forge_name}" + + # Install forged + install -Dm644 "forged-backup.service" "${pkgdir}/usr/lib/systemd/system/${_forge_name}d-backup.service" + install -Dm644 "forged-backup.timer" "${pkgdir}/usr/lib/systemd/system/${_forge_name}d-backup.timer" + install -Dm644 "forged.service" "${pkgdir}/usr/lib/systemd/system/${_forge_name}d.service" + install -Dm644 "forged.conf" "${pkgdir}/etc/conf.d/${_forge_name}" + install -Dm755 "forged.sh" "${pkgdir}/usr/bin/${_forge_name}d" + + # Install Forge + _forge_jar="forge-${_pkgver}.jar" + [ "$_minecraft_ver_minor" -le 12 ] && _forge_jar="forge-${_pkgver}-universal.jar" + [ "$_minecraft_ver_minor" = 7 ] && _forge_jar="forge-${_pkgver}-${_minecraft_ver}-universal.jar" + [ "$_minecraft_ver_minor" -le 6 ] && _forge_jar="minecraftforge-universal-${_pkgver}.jar" + + install -Dm644 "$_forge_jar" "${_server_root}/$_forge_jar" + ln -s "$_forge_jar" "${_server_root}/forge.jar" + find libraries -type f -print0 | xargs -0 -i@ install -Dm644 "@" "${_server_root}/@" + [ "$_minecraft_ver_minor" = 5 ] && find lib -type f -print0 | xargs -0 -i@ install -Dm644 "@" "${_server_root}/@" + + # Install Minecraft Server (for 1.12.2 or lower) + if [ "$_minecraft_ver_minor" = 5 ]; then + install -Dm644 "minecraft_server.${_minecraft_ver}.jar" "${_server_root}/minecraft_server.jar" + elif [ "$_minecraft_ver_minor" -le 12 ]; then + install -Dm644 "minecraft_server.${_minecraft_ver}.jar" "${_server_root}/minecraft_server.${_minecraft_ver}.jar" + fi + + # Link log files + mkdir -p "${pkgdir}/var/log/" + install -dm2755 "${_server_root}/logs" + ln -s "/srv/${_forge_name}/logs" "${pkgdir}/var/log/${_forge_name}" + + # Install licenses + for _license in "${_licenses[@]}"; do + _filename="$(basename "$_license" "$_license_suffix").txt" + _filename="${_filename//\%20/ }" + install -Dm644 "$_license" "${pkgdir}/usr/share/licenses/${pkgname}/$_filename" + done + + chmod g+ws "${_server_root}" +} diff --git a/forge-server.install b/forge-server.install new file mode 100644 index 000000000000..24f8b92613a0 --- /dev/null +++ b/forge-server.install @@ -0,0 +1,55 @@ +_forge_name="forge-1.14.4" +_forge_user="forge-1-14-4" +_conflict_ver="1.14.4_28.1.0-3" +_good_ver="1.14.4-28.1.0-9" +_server_root="/srv/${_forge_name}" + +post_install() { + if ! getent group "$_forge_user" &>/dev/null; then + echo "Adding ${_forge_user} system group..." + groupadd -r "$_forge_user" 1>/dev/null + fi + + if ! getent passwd "$_forge_user" &>/dev/null; then + echo "Adding ${_forge_user} system user..." + useradd -r -g "${_forge_user}" -d "$_server_root" "$_forge_user" 1>/dev/null + fi + + chown -R "${_forge_user}:${_forge_user}" "$_server_root" + + echo "The world data is stored under ${_server_root} and the server runs as the forge user to increase security." + echo "Use the forged script under /usr/bin/${_forge_name}d to start, stop or backup the server." + echo "Adjust the configuration file under /etc/conf.d/${_forge_name} to your liking." + echo "Make sure to change the SERVER_START_CMD variable if you want to use a JVM other than OpenJDK 8." + echo "Please note that you need Java 8 in order to run Forge." + echo "For the server to start you have to accept the EULA in ${_server_root}/eula.txt!" + echo "The EULA file is generated after the first server start." +} + +post_upgrade() { + if [ "$(vercmp "$2" "$_conflict_ver")" -ge 0 ] && [ "$(vercmp "$2" "$_good_ver")" -lt 0 ]; then + post_install + + echo "==> NOTICE: All filenames have been changed from \"forged\" to \"${_forge_name}d\"!" + echo "==> For example:" + echo "==> /usr/lib/systemd/system/forged.service -> /usr/lib/systemd/system/${_forge_name}d.service" + echo "==> /usr/bin/forged -> /usr/bin/${_forge_name}d" + echo "==> /srv/forge -> ${_server_root}" + + if [ -d "/srv/forge" ]; then + find "/srv/forge" -mindepth 1 -maxdepth 1 | while read -r _file; do + mv "$_file" "${_server_root}${_file#"/srv/forge"}" + done + rm -rf "/srv/forge" + echo "==> Game saves in /srv/forge were moved to ${_server_root}." + fi + + echo "==> The forge user was preserved on your system." + fi + chown -R "${_forge_user}:${_forge_user}" "${_server_root}" +} + +post_remove() { + [ -d "$_server_root" ] && echo "Game saves in ${_server_root} were kept on your system." + echo "The ${_forge_user} user was preserved on your system." +} diff --git a/forged-backup.service b/forged-backup.service new file mode 100644 index 000000000000..0a934356afd4 --- /dev/null +++ b/forged-backup.service @@ -0,0 +1,12 @@ +[Unit] +Description=Minecraft Forge 1.14.4 Server World Backup +After=local-fs.target + +[Service] +Type=oneshot +ExecStart=/usr/bin/forge-1.14.4d backup +User=forge-1-14-4 +Group=forge-1-14-4 + +[Install] +WantedBy=multi-user.target diff --git a/forged-backup.timer b/forged-backup.timer new file mode 100644 index 000000000000..26f0c4fb972c --- /dev/null +++ b/forged-backup.timer @@ -0,0 +1,10 @@ +[Unit] +Description=Daily Minecraft Forge 1.14.4 Server Backup + +[Timer] +OnCalendar=daily +AccuracySec=5min +Persistent=true + +[Install] +WantedBy=multi-user.target diff --git a/forged.conf b/forged.conf new file mode 100644 index 000000000000..b0381c90bbd5 --- /dev/null +++ b/forged.conf @@ -0,0 +1,29 @@ +# THIS IS THE CONFIGURATION FILE FOR THE MANAGING SCRIPT NOT FOR THE ACTUAL SERVER +# Variables are interpreted in bash. Simply using bash-syntax is sufficient. + +# General parameters +SERVER_ROOT="/srv/forge-1.14.4" +BACKUP_DEST="/srv/forge-1.14.4/backup" +BACKUP_PATHS="world" # World paths separated by spaces relative to SERVER_ROOT +BACKUP_FLAGS="-z" +KEEP_BACKUPS="10" +GAME_USER="forge-1-14-4" +MAIN_EXECUTABLE="forge.jar" +SESSION_NAME="forge-1.14.4" + +# System parameters for java +# -Xms sets the intial heap size (must be a multiple of 1024 and greater than 2MB, no spaces!) +# -Xmx sets the maximum heap size (must be a multiple of 1024 and greater than 2MB, no spaces!) +# -XX:ParallelGCThreads defines the number of threads the server may use +SERVER_START_CMD="/usr/lib/jvm/java-8-openjdk/jre/bin/java -Xms512M -Xmx1024M -XX:ParallelGCThreads=1 -jar './${MAIN_EXECUTABLE}' nogui" + +# System parameters for the actual game server +# Describes whether a daemon process which stops the server if it is not used by a player +# within IDLE_IF_TIME seconds should be started. The GAME_PORT is not inhereted to the server! +IDLE_SERVER=false # true or false +# Ensure that if SESSION_NAME is passed through the command line and therefore set to read only by the script, +# IDLE_SESSION_NAME gets altered according to the command line and not the configurtion file, hence invoke the variable +IDLE_SESSION_NAME="idle_server_${SESSION_NAME}" +GAME_PORT="25565" # used to listen for incoming connections when the server is down +CHECK_PLAYER_TIME="30" # in seconds +IDLE_IF_TIME="1200" # in seconds diff --git a/forged.service b/forged.service new file mode 100644 index 000000000000..78c674298dac --- /dev/null +++ b/forged.service @@ -0,0 +1,13 @@ +[Unit] +Description=Minecraft Forge 1.14.4 Server +After=local-fs.target network.target multi-user.target + +[Service] +Type=forking +ExecStart=/usr/bin/forge-1.14.4d start +ExecStop=/usr/bin/forge-1.14.4d stop +User=forge-1-14-4 +Group=forge-1-14-4 + +[Install] +WantedBy=multi-user.target diff --git a/forged.sh b/forged.sh new file mode 100755 index 000000000000..6d40bb94bd88 --- /dev/null +++ b/forged.sh @@ -0,0 +1,492 @@ +#!/bin/bash + +# The actual program name +declare -r myname="forge-1.14.4d" +declare -r game="forge-1.14.4" + +# General rule for the variable-naming-schema: +# Variables in capital letters may be passed through the command line others not. +# Avoid altering any of those later in the code since they may be readonly (IDLE_SERVER is an exception!) + +# You may use this script for any game server of your choice, just alter the config file +[[ -n "${SERVER_ROOT}" ]] && declare -r SERVER_ROOT=${SERVER_ROOT} || SERVER_ROOT="/srv/${game}" +[[ -n "${BACKUP_DEST}" ]] && declare -r BACKUP_DEST=${BACKUP_DEST} || BACKUP_DEST="/srv/${game}/backup" +[[ -n "${BACKUP_PATHS}" ]] && declare -r BACKUP_PATHS=${BACKUP_PATHS} || BACKUP_PATHS="world" +[[ -n "${BACKUP_FLAGS}" ]] && declare -r BACKUP_FLAGS=${BACKUP_FLAGS} || BACKUP_FLAGS="-z" +[[ -n "${KEEP_BACKUPS}" ]] && declare -r KEEP_BACKUPS=${KEEP_BACKUPS} || KEEP_BACKUPS="10" +[[ -n "${GAME_USER}" ]] && declare -r GAME_USER=${GAME_USER} || GAME_USER="forge-1-14-4" +[[ -n "${MAIN_EXECUTABLE}" ]] && declare -r MAIN_EXECUTABLE=${MAIN_EXECUTABLE} || MAIN_EXECUTABLE="forge.jar" +[[ -n "${SESSION_NAME}" ]] && declare -r SESSION_NAME=${SESSION_NAME} || SESSION_NAME="${game}" + +# Command and parameter declaration with which to start the server +[[ -n "${SERVER_START_CMD}" ]] && declare -r SERVER_START_CMD=${SERVER_START_CMD} || SERVER_START_CMD="/usr/lib/jvm/java-8-openjdk/jre/bin/java -Xms512M -Xmx1024M -XX:ParallelGCThreads=1 -jar './${MAIN_EXECUTABLE}' nogui" + +# System parameters for the control script +[[ -n "${IDLE_SERVER}" ]] && tmp_IDLE_SERVER=${IDLE_SERVER} || IDLE_SERVER="false" +[[ -n "${IDLE_SESSION_NAME}" ]] && declare -r IDLE_SESSION_NAME=${IDLE_SESSION_NAME} || IDLE_SESSION_NAME="idle_server_${SESSION_NAME}" +[[ -n "${GAME_PORT}" ]] && declare -r GAME_PORT=${GAME_PORT} || GAME_PORT="25565" +[[ -n "${CHECK_PLAYER_TIME}" ]] && declare -r CHECK_PLAYER_TIME=${CHECK_PLAYER_TIME} || CHECK_PLAYER_TIME="30" +[[ -n "${IDLE_IF_TIME}" ]] && declare -r IDLE_IF_TIME=${IDLE_IF_TIME} || IDLE_IF_TIME="1200" + +# Additional configuration options which only few may need to alter +[[ -n "${GAME_COMMAND_DUMP}" ]] && declare -r GAME_COMMAND_DUMP=${GAME_COMMAND_DUMP} || GAME_COMMAND_DUMP="/tmp/${myname}_${SESSION_NAME}_command_dump.txt" + +# Variables passed over the command line will always override the one from a config file +source /etc/conf.d/"${game}" 2>/dev/null || >&2 echo "Could not source /etc/conf.d/${game}" + +# Preserve the content of IDLE_SERVER without making it readonly +[[ -n ${tmp_IDLE_SERVER} ]] && IDLE_SERVER=${tmp_IDLE_SERVER} + + +# Strictly disallow uninitialized Variables +set -u +# Exit if a single command breaks and its failure is not handled accordingly +set -e + +# Check whether sudo is needed at all +if [[ "$(whoami)" == "${GAME_USER}" ]]; then + SUDO_CMD="" +else + SUDO_CMD="sudo -u ${GAME_USER}" +fi + +# Choose which flavor of netcat is to be used +if command -v netcat &> /dev/null; then + NETCAT_CMD="netcat" +elif command -v ncat &> /dev/null; then + NETCAT_CMD="ncat" +else + NETCAT_CMD="" +fi + +# Check for sudo rigths +if [[ "$(${SUDO_CMD} whoami)" != "${GAME_USER}" ]]; then + >&2 echo -e "You have \e[39;1mno permission\e[0m to run commands as $GAME_USER user." + exit 21 +fi + +# Pipe any given argument to the game server console, +# sleep for $sleep_time and return its output if $return_stdout is set +game_command() { + if [[ -z "${return_stdout:-}" ]]; then + ${SUDO_CMD} screen -S "${SESSION_NAME}" -X stuff "$(printf "%s\r" "$*")" + else + ${SUDO_CMD} screen -S "${SESSION_NAME}" -X log on + ${SUDO_CMD} screen -S "${SESSION_NAME}" -X stuff "$(printf "%s\r" "$*")" + sleep "${sleep_time:-0.3}" + ${SUDO_CMD} screen -S "${SESSION_NAME}" -X log off + ${SUDO_CMD} cat "${GAME_COMMAND_DUMP}" + ${SUDO_CMD} rm "${GAME_COMMAND_DUMP}" + fi +} + +# Check whether there are player on the server through list +is_player_online() { + response="$(sleep_time=0.6 return_stdout=true game_command list)" + # Delete leading line and free response string from fancy characters + response="$(echo "${response}" | sed -r -e 's/\x1B\[([0-9]{1,2}(;[0-9]{1,2})*)?[JKmsuG]//g')" + # The list command prints a line containing the usernames after the last occurrence of ": " + # and since playernames may not contain this string the clean player-list can easily be retrieved. + # Otherwise check the first digit after the last occurrence of "There are". If it is 0 then there + # are no players on the server. Should this test fail as well. Assume that a player is online. + if [[ $(echo "${response}" | grep ":" | sed -e 's/.*\: //' | tr -d '\n' | wc -c) -le 1 ]]; then + # No player is online + return 0 + elif [[ "x$(echo "${response}" | grep "There are" | sed -r -e 's/.*\: //' -e 's/^([^.]+).*$/\1/; s/^[^0-9]*([0-9]+).*$/\1/' | tr -d '\n')" == "x0" ]]; then + # No player is online + return 0 + else + # A player is online (or it could not be determined) + return 1 + fi +} + +# Check whether the server is visited by a player otherwise shut it down +idle_server_daemon() { + # This function is run within a screen session of the GAME_USER therefore SUDO_CMD can be omitted + if [[ "$(whoami)" != "${GAME_USER}" ]]; then + >&2 echo "Somehow this hidden function was not executed by the ${GAME_USER} user." + >&2 echo "This should not have happend. Are you messing around with this script? :P" + exit 22 + fi + + # Time in seconds for which no player was on the server + no_player=0 + + while true; do + echo -e "no_player: ${no_player}s\tcheck_player_time: ${CHECK_PLAYER_TIME}s\tidle_if_time: ${IDLE_IF_TIME}s" + # Retry in ${CHECK_PLAYER_TIME} seconds + sleep ${CHECK_PLAYER_TIME} + + if screen -S "${SESSION_NAME}" -Q select . > /dev/null; then + # Game server is up and running + if [[ "$(screen -S "${SESSION_NAME}" -ls | sed -n "s/.*${SESSION_NAME}\s\+//gp")" == "(Attached)" ]]; then + # An administrator is connected to the console, pause player checking + echo "An admin is connected to the console. Pause player checking." + # Check for active player + elif SUDO_CMD="" is_player_online; then + # No player was seen on the server through list + no_player=$(( no_player + CHECK_PLAYER_TIME )) + # Stop the game server if no player was active for at least ${IDLE_IF_TIME} + if [[ "${no_player}" -ge "${IDLE_IF_TIME}" ]]; then + IDLE_SERVER="false" ${myname} stop + # Wait for game server to go down + for i in {1..100}; do + screen -S "${SESSION_NAME}" -Q select . > /dev/null || break + [[ $i -eq 100 ]] && echo -e "An \e[39;1merror\e[0m occurred while trying to reset the idle_server!" + sleep 0.1 + done + # Reset timer and give the player 300 seconds to connect after pinging + no_player=$(( IDLE_IF_TIME - 300 )) + # Game server is down, listen on port ${GAME_PORT} for incoming connections + echo -n "Netcat: " + ${NETCAT_CMD} -v -l -p ${GAME_PORT} && echo "Netcat caught an connection. The server is coming up again..." + IDLE_SERVER="false" ${myname} start + fi + else + # Reset timer since there is an active player on the server + no_player=0 + fi + else + # Reset timer and give the player 300 seconds to connect after pinging + no_player=$(( IDLE_IF_TIME - 300 )) + # Game server is down, listen on port ${GAME_PORT} for incoming connections + echo -n "Netcat: " + ${NETCAT_CMD} -v -l -p ${GAME_PORT} && echo "Netcat caught an connection. The server is coming up again..." + IDLE_SERVER="false" ${myname} start + fi + done +} + +# Start the server if it is not already running +server_start() { + # Start the game server + if ${SUDO_CMD} screen -S "${SESSION_NAME}" -Q select . > /dev/null; then + echo "A screen ${SESSION_NAME} session is already running. Please close it first." + else + echo -en "Starting server..." + ${SUDO_CMD} screen -dmS "${SESSION_NAME}" /bin/bash -c "cd '${SERVER_ROOT}'; ${SERVER_START_CMD}" + ${SUDO_CMD} screen -S "${SESSION_NAME}" -X logfile "${GAME_COMMAND_DUMP}" + echo -e "\e[39;1m done\e[0m" + fi + + if [[ "${IDLE_SERVER,,}" == "true" ]]; then + # Check for the availability of the netcat (nc) binaries + if [[ -z "${NETCAT_CMD}" ]]; then + >&2 echo "The netcat binaries are needed for suspending an idle server." + exit 12 + fi + + # Start the idle server daemon + if ${SUDO_CMD} screen -S "${IDLE_SESSION_NAME}" -Q select . > /dev/null; then + ${SUDO_CMD} screen -S "${IDLE_SESSION_NAME}" -X quit + # Restart as soon as the idle_server_daemon has shut down completely + for i in {1..100}; do + if ! ${SUDO_CMD} screen -S "${IDLE_SESSION_NAME}" -Q select . > /dev/null; then + ${SUDO_CMD} screen -dmS "${IDLE_SESSION_NAME}" /bin/bash -c "${myname} idle_server_daemon" + break + fi + [[ $i -eq 100 ]] && echo -e "An \e[39;1merror\e[0m occurred while trying to reset the idle_server!" + sleep 0.1 + done + else + echo -en "Starting idle server daemon..." + ${SUDO_CMD} screen -dmS "${IDLE_SESSION_NAME}" /bin/bash -c "${myname} idle_server_daemon" + echo -e "\e[39;1m done\e[0m" + fi + fi +} + +# Stop the server gracefully by saving everything prior and warning the users +server_stop() { + # Quit the idle daemon + if [[ "${IDLE_SERVER,,}" == "true" ]]; then + # Check for the availability of the netcat (nc) binaries + if [[ -z "${NETCAT_CMD}" ]]; then + >&2 echo "The netcat binaries are needed for suspending an idle server." + exit 12 + fi + + if ${SUDO_CMD} screen -S "${IDLE_SESSION_NAME}" -Q select . > /dev/null; then + echo -en "Stopping idle server daemon..." + ${SUDO_CMD} screen -S "${IDLE_SESSION_NAME}" -X quit + echo -e "\e[39;1m done\e[0m" + else + echo "The corresponding screen session for ${IDLE_SESSION_NAME} was already dead." + fi + fi + + # Gracefully exit the game server + if ${SUDO_CMD} screen -S "${SESSION_NAME}" -Q select . > /dev/null; then + # Game server is up and running, gracefully stop the server when there are still active players + + # Check for active player + if is_player_online; then + # No player was seen on the server through list + echo -en "Server is going down..." + game_command stop + else + # Player(s) were seen on the server through list (or an error occurred) + # Warning the users through the server console + game_command say "Server is going down in 10 seconds! HURRY UP WITH WHATEVER YOU ARE DOING!" + game_command save-all + echo -en "Server is going down in..." + for i in {1..10}; do + game_command say "down in... $(( 10 - i ))" + echo -n " $(( 10 - i ))" + sleep 1 + done + game_command stop + fi + + # Finish as soon as the server has shut down completely + for i in {1..100}; do + if ! ${SUDO_CMD} screen -S "${SESSION_NAME}" -Q select . > /dev/null; then + echo -e "\e[39;1m done\e[0m" + break + fi + [[ $i -eq 100 ]] && echo -e "\e[39;1m timed out\e[0m" + sleep 0.1 + done + else + echo "The corresponding screen session for ${SESSION_NAME} was already dead." + fi +} + +# Print whether the server is running and if so give some information about memory usage and threads +server_status() { + # Print status information about the idle daemon + if [[ "${IDLE_SERVER,,}" == "true" ]]; then + # Check for the availability of the netcat (nc) binaries + if [[ -z "${NETCAT_CMD}" ]]; then + >&2 echo "The netcat binaries are needed for suspending an idle server." + exit 12 + fi + + if ${SUDO_CMD} screen -S "${IDLE_SESSION_NAME}" -Q select . > /dev/null; then + echo -e "Idle server daemon status:\e[39;1m running\e[0m" + else + echo -e "Idle server daemon status:\e[39;1m stopped\e[0m" + fi + fi + + # Print status information for the game server + if ${SUDO_CMD} screen -S "${SESSION_NAME}" -Q select . > /dev/null; then + echo -e "Status:\e[39;1m running\e[0m" + + # Calculating memory usage + for p in $(${SUDO_CMD} pgrep -f "${MAIN_EXECUTABLE}"); do + ps -p"${p}" -O rss | tail -n 1; + done | gawk '{ count ++; sum += $2 }; END {count --; print "Number of processes =", count, "(screen, bash,", count-2, "x server)"; print "Total memory usage =", sum/1024, "MB" ;};' + else + echo -e "Status:\e[39;1m stopped\e[0m" + fi +} + +# Restart the complete server by shutting it down and starting it again +server_restart() { + if ${SUDO_CMD} screen -S "${SESSION_NAME}" -Q select . > /dev/null; then + server_stop + server_start + else + server_start + fi +} + +# Backup the directories specified in BACKUP_PATHS +backup_files() { + # Check for the availability of the tar binaries + if ! command -v tar &> /dev/null; then + >&2 echo "The tar binaries are needed for a backup." + exit 11 + fi + + echo "Starting backup..." + fname="$(date +%Y_%m_%d_%H.%M.%S).tar.gz" + ${SUDO_CMD} mkdir -p "${BACKUP_DEST}" + if ${SUDO_CMD} screen -S "${SESSION_NAME}" -Q select . > /dev/null; then + game_command save-off + game_command save-all + sync && wait + ${SUDO_CMD} tar -C "${SERVER_ROOT}" -cf "${BACKUP_DEST}/${fname}" ${BACKUP_PATHS} --totals ${BACKUP_FLAGS} 2>&1 | grep -v "tar: Removing leading " + game_command save-on + else + ${SUDO_CMD} tar -C "${SERVER_ROOT}" -cf "${BACKUP_DEST}/${fname}" ${BACKUP_PATHS} --totals ${BACKUP_FLAGS} 2>&1 | grep -v "tar: Removing leading " + fi + echo -e "\e[39;1mbackup completed\e[0m\n" + + echo -n "Only keeping the last ${KEEP_BACKUPS} backups and removing the other ones..." + backup_count=$(for f in "${BACKUP_DEST}"/[0-9_.]*; do echo "${f}"; done | wc -l) + if [[ $(( backup_count - KEEP_BACKUPS )) -gt 0 ]]; then + for old_backup in $(for f in "${BACKUP_DEST}"/[0-9_.]*; do echo "${f}"; done | head -n"$(( backup_count - KEEP_BACKUPS ))"); do + ${SUDO_CMD} rm "${old_backup}"; + done + echo -e "\e[39;1m done\e[0m ($(( backup_count - KEEP_BACKUPS)) backup(s) pruned)" + else + echo -e "\e[39;1m done\e[0m (no backups pruned)" + fi +} + +# Restore backup +backup_restore() { + # Check for the availability of the tar binaries + if ! command -v tar &> /dev/null; then + >&2 echo "The tar binaries are needed for a backup." + exit 11 + fi + + # Only allow the user to restore a backup if the server is down + if ${SUDO_CMD} screen -S "${SESSION_NAME}" -Q select . > /dev/null; then + >&2 echo -e "The \e[39;1mserver should be down\e[0m in order to restore the world data." + exit 3 + fi + + # Either let the user choose a backup or expect one as an argument + if [[ $# -lt 1 ]]; then + echo "Please enter the corresponding number for the backup to be restored: " + i=1 + for f in "${BACKUP_DEST}"/[0-9_.]*; do + echo -e " \e[39;1m$i)\e[0m\t$f" + i=$(( i + 1 )) + done + echo -en "Restore backup number: " + + # Read in user input + read -r user_choice + + # Interpeting the input + if [[ $user_choice =~ ^-?[0-9]+$ ]]; then + n=1 + for f in "${BACKUP_DEST}"/[0-9_.]*; do + [[ ${n} -eq $user_choice ]] && fname="$f" + n=$(( n + 1 )) + done + if [[ -z $fname ]]; then + >&2 echo -e "\e[39;1mFailed\e[0m to interpret your input. Please enter the digit of the presented options." + exit 5 + fi + else + >&2 echo -e "\e[39;1mFailed\e[0m to interpret your input. Please enter a valid digit for one of the presented options." + exit 6 + fi + elif [[ $# -eq 1 ]]; then + # Check for the existance of the specified file + if [[ -f "$1" ]]; then + fname="$1" + else + if [[ -f "${BACKUP_DEST}"/"$1" ]]; then + fname="${BACKUP_DEST}"/"$1" + else + >&2 echo -e "Sorry, but '$1', is \e[39;1mnot a valid file\e[0m, neither in your current directory nor in the backup folder." + exit 4 + fi + fi + elif [[ $# -gt 1 ]]; then + >&2 echo -e "\e[39;1mToo many arguments.\e[0m Please pass only the filename for the world data as an argument." + >&2 echo "Or alternatively no arguments at all to chose from a list of available backups." + exit 7 + fi + + echo "Restoring backup..." + if ${SUDO_CMD} tar -xf "${fname}" -C "${SERVER_ROOT}" 2>&1; then + echo -e "\e[39;1mRestoration completed\e[0m" + else + echo -e "\e[39;1mFailed to restore backup.\e[0m" + fi +} + +# Run the given comman at the game server console +server_command() { + if [[ $# -lt 1 ]]; then + >&2 echo "No server command specified. Try 'help' for a list of commands." + exit 1 + fi + + if ${SUDO_CMD} screen -S "${SESSION_NAME}" -Q select . > /dev/null; then + return_stdout=true game_command "$@" + else + echo "There is no ${SESSION_NAME} session to connect to." + fi +} + +# Enter the screen game session +server_console() { + if ${SUDO_CMD} screen -S "${SESSION_NAME}" -Q select . > /dev/null; then + # Circumvent a permission bug related to running GNU screen as a different user, + # see e.g. https://serverfault.com/questions/116775/sudo-as-different-user-and-running-screen + ${SUDO_CMD} script -q -c "screen -S \"${SESSION_NAME}\" -rx" /dev/null + else + echo "There is no ${SESSION_NAME} session to connect to." + fi +} + +# Help function, no arguments required +help() { + cat <<-EOF + This script was design to easily control any ${game} server. Quite every parameter for a given + ${game} server derivative can be altered by editing the variables in the configuration file. + + Usage: ${myname} {start|stop|status|backup|restore|command <command>|console} + start Start the ${game} server + stop Stop the ${game} server + restart Restart the ${game} server + status Print some status information + backup Backup the world data + restore [filename] Restore the world data from a backup + command <command> Run the given comman at the ${game} server console + console Enter the server console through a screen session + + Copyright (c) Gordian Edenhofer <gordian.edenhofer@gmail.com> + EOF +} + +case "${1:-}" in + start) + server_start + ;; + + stop) + server_stop + ;; + + status) + server_status + ;; + + restart) + server_restart + ;; + + console) + server_console + ;; + + command) + server_command "${@:2}" + ;; + + backup) + backup_files + ;; + + restore) + backup_restore "${@:2}" + ;; + + idle_server_daemon) + # This shall be a hidden function which should only be invoced internally + idle_server_daemon + ;; + + -h|--help) + help + exit 0 + ;; + + *) + help + exit 1 + ;; +esac + +exit 0 |