From f6153d02b522a6c522bdc7aecb17268c0ac5dbda Mon Sep 17 00:00:00 2001 From: Daniel Ceregatti Date: Sun, 10 Jul 2022 11:26:16 -0700 Subject: [PATCH] Refactor to allow cleanup to happen in an exit trap. Remove loop to allow docker to handle restarts on crashes. Hacks exist for now to force exit 0 because the server always crashes on exit. Rework how the config is loaded. It need not always happen. --- files/dayzserver | 110 ++++++++++++++++++++++++++++++----------------- 1 file changed, 70 insertions(+), 40 deletions(-) diff --git a/files/dayzserver b/files/dayzserver index 2ddfb43..bc8a663 100755 --- a/files/dayzserver +++ b/files/dayzserver @@ -1,7 +1,5 @@ #!/usr/bin/env bash -set -eE - # Colors default="\e[0m" red="\e[31m" @@ -12,6 +10,21 @@ blue="\e[34m" magenta="\e[35m" cyan="\e[36m" +# Make sure to report and clean up on exit, as these files remain in the container's volume +function report() { + echo + echo -e "${yellow}========================================== error.log ==========================================" + find "${HOME}" -name error.log -exec head {} \; -exec rm -f {} \; + echo + echo -e "${yellow}========================================== script*.log ========================================" + find "${HOME}" -name "script*.log" -exec head {} \; -exec rm -f {} \; + echo + echo -e "${yellow}========================================== *.RPT ==============================================" + find "${HOME}" -name "*.RPT" -exec ls -la {} \; -exec rm -f {} \; + echo + echo -e "${yellow}========================================== End crash log ======================================" +} + # Make sure we don't start collecting core files # FIXME Set this in Docker somewhere ulimit -c 0 @@ -114,30 +127,18 @@ fn_loadconfig_dayz(){ echo exit 1 fi - # Handle the server configuration file + # Handle the initial server configuration file if [ ! -f ${SERVER_CFG_DST} ] then echo "Creating initial server configuration file" cp "${SERVER_CFG_SRC}" "${SERVER_CFG_DST}" - elif ! diff -q "${SERVER_CFG_SRC}" "${SERVER_CFG_DST}" - then - echo "=========================================================================" - diff -Nau --color "${SERVER_CFG_SRC}" "${SERVER_CFG_DST}" || echo "" - echo "=========================================================================" - if fn_prompt_yn "The new server configuration file differs from what's installed. Use it?" - then - echo "Updating the server configuration file" - cp "${SERVER_CFG_SRC}" "${SERVER_CFG_DST}" - else - echo "NOT updating the server configuration file" - fi fi # battleye config and rconpassword setup # The server creates a new file from this file, which it then uses. # Let's make sure to delete it first - rm -f "${HOME}/serverfiles/battleye/beserver_x64_active*" BE_SERVER_FILE="${HOME}/serverfiles/battleye/beserver_x64.cfg" - if [ ! -f "${BE_SERVER_FILE}" ] + ALT_BE_SERVER_FILE=$(find ${HOME}/serverfiles/battleye -name "beserver_x64_active*") + if [ ! -f "${BE_SERVER_FILE}" ] && [ ! -f "${ALT_BE_SERVER_FILE}" ] then passwd=$(openssl rand -base64 8 | tr -dc 'A-Za-z0-9') if [ "${passwd}" == "" ]; then @@ -149,17 +150,25 @@ fn_loadconfig_dayz(){ else cat > "${BE_SERVER_FILE}" < ~/py3rcon.config.json } @@ -173,22 +182,23 @@ fn_start_dayz(){ echo exit fi + # Do the report on exit. Set here so that it only happens once we're starting the server, and not for other actions. + trap ' + report + ' EXIT fn_workshop_mods - printf "[ ${green}DayZ${default} ] Starting server...\n" cd ${SERVER_FILES} - ./DayZServer $dayzparameter "$workshop" || ( - echo - echo -e "${yellow}========================================== error.log ==========================================" - find "${HOME}" -name error.log -exec cat {} \; -exec rm -f {} \; - echo - echo -e "${yellow}========================================== script*.log ========================================" - find "${HOME}" -name "script*.log" -exec cat {} \; -exec rm -f {} \; - echo - echo -e "${yellow}========================================== *.RPT ==============================================" - find "${HOME}" -name "*.RPT" -exec cat {} \; -exec rm -f {} \; - echo - echo -e "${yellow}========================================== End crash log ======================================" - ) + # Run the server. Allow docker to restart the container if the script exits with a code other than 0. This is so we can + # safely shut the container down without killing the server within. + printf "[ ${green}DayZ${default} ] Server starting...\n" + ./DayZServer $dayzparameter + EXIT_CODE=$? + if [[ ${EXIT_CODE} = "139" ]] + then + EXIT_CODE=0 + fi + printf "[ ${yellow}DayZ${default} ] Server stopped. Exit code: ${EXIT_CODE}\n" + exit ${EXIT_CODE} } fn_stop_dayz(){ @@ -249,7 +259,20 @@ fn_install_dayz(){ } fn_runupdate_dayz(){ - ${STEAMCMD} +force_install_dir ${SERVER_FILES} +login "${steamlogin}" +app_update "${appid}" +quit + if ! diff -q "${SERVER_CFG_SRC}" "${SERVER_CFG_DST}" + then + echo "=========================================================================" + diff -Nau --color "${SERVER_CFG_SRC}" "${SERVER_CFG_DST}" || echo "" + echo "=========================================================================" + if fn_prompt_yn "The new server configuration file differs from what's installed. Use it?" + then + echo "Updating the server configuration file" + cp "${SERVER_CFG_SRC}" "${SERVER_CFG_DST}" + else + echo "NOT updating the server configuration file" + fi + fi + ${STEAMCMD} +force_install_dir ${SERVER_FILES} +login "${steamlogin}" +app_update "${appid}" +quit } fn_update_dayz(){ @@ -332,7 +355,7 @@ fn_workshop_mods(){ } fn_rcon(){ - exec py3rcon.py --gui ~/py3rcon.config.json + exec /usr/local/py3rcon/py3rcon.py --gui ~/py3rcon.config.json } fn_status(){ @@ -350,6 +373,10 @@ fn_status(){ if [ -f "${STEAM_LOGIN}" ] then LOGGED_IN="${YES}" + if grep -q anonymous "${STEAM_LOGIN}" + then + ANONYMOUS="(as anonymous)" + fi fi # Running or not if pidof DayZServer > /dev/null @@ -360,26 +387,27 @@ fn_status(){ echo -e " Status: - Logged in to Steam: ${LOGGED_IN} + Logged in to Steam: ${LOGGED_IN} ${ANONYMOUS} Server files installed: ${INSTALLED} Mods installed: ${MOD_INSTALLED}${MOD_LIST} Server running: ${RUNNING} " } -fn_loadconfig_dayz - case "${1}" in install) + fn_loadconfig_dayz fn_install_dayz ;; login) + fn_loadconfig_dayz fn_steam_login ;; rcon) fn_rcon "${2}" ;; start) + fn_loadconfig_dayz fn_start_dayz ;; status) @@ -389,9 +417,11 @@ case "${1}" in fn_stop_dayz ;; update) + fn_loadconfig_dayz fn_update_dayz ;; workshop) + fn_loadconfig_dayz fn_workshop_mods "${2}" ;; **)