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.
This commit is contained in:
Daniel Ceregatti 2022-07-10 11:26:16 -07:00
parent 13f3cda4cb
commit f6153d02b5

View file

@ -1,7 +1,5 @@
#!/usr/bin/env bash #!/usr/bin/env bash
set -eE
# Colors # Colors
default="\e[0m" default="\e[0m"
red="\e[31m" red="\e[31m"
@ -12,6 +10,21 @@ blue="\e[34m"
magenta="\e[35m" magenta="\e[35m"
cyan="\e[36m" 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 # Make sure we don't start collecting core files
# FIXME Set this in Docker somewhere # FIXME Set this in Docker somewhere
ulimit -c 0 ulimit -c 0
@ -114,30 +127,18 @@ fn_loadconfig_dayz(){
echo echo
exit 1 exit 1
fi fi
# Handle the server configuration file # Handle the initial server configuration file
if [ ! -f ${SERVER_CFG_DST} ] if [ ! -f ${SERVER_CFG_DST} ]
then then
echo "Creating initial server configuration file" echo "Creating initial server configuration file"
cp "${SERVER_CFG_SRC}" "${SERVER_CFG_DST}" 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 fi
# battleye config and rconpassword setup # battleye config and rconpassword setup
# The server creates a new file from this file, which it then uses. # The server creates a new file from this file, which it then uses.
# Let's make sure to delete it first # 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" 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 then
passwd=$(openssl rand -base64 8 | tr -dc 'A-Za-z0-9') passwd=$(openssl rand -base64 8 | tr -dc 'A-Za-z0-9')
if [ "${passwd}" == "" ]; then if [ "${passwd}" == "" ]; then
@ -149,17 +150,25 @@ fn_loadconfig_dayz(){
else else
cat > "${BE_SERVER_FILE}" <<EOF cat > "${BE_SERVER_FILE}" <<EOF
RConPassword ${passwd} RConPassword ${passwd}
RestrictRCon 1 RestrictRCon 0
RConPort ${rcon_port} RConPort ${rcon_port}
EOF EOF
fi fi
printf "[ ${cyan}INFO${default} ] New RCON password: ${yellow}${passwd}${default}\n"
else else
passwd=$(grep RConPassword ${BE_SERVER_FILE}) if [ -f "${BE_SERVER_FILE}" ]
then
FILE="${BE_SERVER_FILE}"
elif [ -f "${ALT_BE_SERVER_FILE}" ]
then
FILE="${ALT_BE_SERVER_FILE}"
fi
passwd=$(grep RConPassword ${FILE} | awk '{print $2}')
# printf "[ ${cyan}INFO${default} ] Using existing RCON password: ${yellow}${passwd}${default}\n"
fi fi
printf "[ ${cyan}INFO${default} ] ${yellow}RCON password: ${passwd}${default}\n"
cp /usr/local/py3rcon/configexample.json ~/py3rcon.config.json cp /usr/local/py3rcon/configexample.json ~/py3rcon.config.json
jq --arg port 2303 --arg rcon_password b0fNIBVfkM \ jq --arg port 2303 --arg rcon_password b0fNIBVfkM \
'.logfile="py3rcon.log" | .server.port=$port | .server.rcon_password=$rcon_password | del(.repeatMessage)' \ '.logfile="py3rcon.log" | .loglevel=0 | .server.port=$port | .server.rcon_password=$rcon_password | del(.repeatMessage)' \
/usr/local/py3rcon/configexample.json \ /usr/local/py3rcon/configexample.json \
> ~/py3rcon.config.json > ~/py3rcon.config.json
} }
@ -173,22 +182,23 @@ fn_start_dayz(){
echo echo
exit exit
fi 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 fn_workshop_mods
printf "[ ${green}DayZ${default} ] Starting server...\n"
cd ${SERVER_FILES} cd ${SERVER_FILES}
./DayZServer $dayzparameter "$workshop" || ( # Run the server. Allow docker to restart the container if the script exits with a code other than 0. This is so we can
echo # safely shut the container down without killing the server within.
echo -e "${yellow}========================================== error.log ==========================================" printf "[ ${green}DayZ${default} ] Server starting...\n"
find "${HOME}" -name error.log -exec cat {} \; -exec rm -f {} \; ./DayZServer $dayzparameter
echo EXIT_CODE=$?
echo -e "${yellow}========================================== script*.log ========================================" if [[ ${EXIT_CODE} = "139" ]]
find "${HOME}" -name "script*.log" -exec cat {} \; -exec rm -f {} \; then
echo EXIT_CODE=0
echo -e "${yellow}========================================== *.RPT ==============================================" fi
find "${HOME}" -name "*.RPT" -exec cat {} \; -exec rm -f {} \; printf "[ ${yellow}DayZ${default} ] Server stopped. Exit code: ${EXIT_CODE}\n"
echo exit ${EXIT_CODE}
echo -e "${yellow}========================================== End crash log ======================================"
)
} }
fn_stop_dayz(){ fn_stop_dayz(){
@ -249,7 +259,20 @@ fn_install_dayz(){
} }
fn_runupdate_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(){ fn_update_dayz(){
@ -332,7 +355,7 @@ fn_workshop_mods(){
} }
fn_rcon(){ fn_rcon(){
exec py3rcon.py --gui ~/py3rcon.config.json exec /usr/local/py3rcon/py3rcon.py --gui ~/py3rcon.config.json
} }
fn_status(){ fn_status(){
@ -350,6 +373,10 @@ fn_status(){
if [ -f "${STEAM_LOGIN}" ] if [ -f "${STEAM_LOGIN}" ]
then then
LOGGED_IN="${YES}" LOGGED_IN="${YES}"
if grep -q anonymous "${STEAM_LOGIN}"
then
ANONYMOUS="(as anonymous)"
fi
fi fi
# Running or not # Running or not
if pidof DayZServer > /dev/null if pidof DayZServer > /dev/null
@ -360,26 +387,27 @@ fn_status(){
echo -e " echo -e "
Status: Status:
Logged in to Steam: ${LOGGED_IN} Logged in to Steam: ${LOGGED_IN} ${ANONYMOUS}
Server files installed: ${INSTALLED} Server files installed: ${INSTALLED}
Mods installed: ${MOD_INSTALLED}${MOD_LIST} Mods installed: ${MOD_INSTALLED}${MOD_LIST}
Server running: ${RUNNING} Server running: ${RUNNING}
" "
} }
fn_loadconfig_dayz
case "${1}" in case "${1}" in
install) install)
fn_loadconfig_dayz
fn_install_dayz fn_install_dayz
;; ;;
login) login)
fn_loadconfig_dayz
fn_steam_login fn_steam_login
;; ;;
rcon) rcon)
fn_rcon "${2}" fn_rcon "${2}"
;; ;;
start) start)
fn_loadconfig_dayz
fn_start_dayz fn_start_dayz
;; ;;
status) status)
@ -389,9 +417,11 @@ case "${1}" in
fn_stop_dayz fn_stop_dayz
;; ;;
update) update)
fn_loadconfig_dayz
fn_update_dayz fn_update_dayz
;; ;;
workshop) workshop)
fn_loadconfig_dayz
fn_workshop_mods "${2}" fn_workshop_mods "${2}"
;; ;;
**) **)