Skip to content

Commit

Permalink
Refactor logging and fix usage of empty password for V+ (lloesche#111)
Browse files Browse the repository at this point in the history
* Refactor logging and pid file handling

* Allow empty password when V+ is enabled and related bug fixes (Fixes lloesche#110)

* Misc. fixes
  • Loading branch information
lloesche authored Feb 26, 2021
1 parent 24c93e1 commit d755bc9
Show file tree
Hide file tree
Showing 8 changed files with 266 additions and 79 deletions.
117 changes: 110 additions & 7 deletions common
Original file line number Diff line number Diff line change
@@ -1,14 +1,75 @@
valheim_download_path=/opt/valheim/dl/server
valheim_install_path=/opt/valheim/server
vp_download_path=/opt/valheim/dl/plus
vp_install_path=/opt/valheim/plus
vp_zipfile=UnixServer.zip
valheim_restartfile="/tmp/valheim.restart"
vp_mergefile="$vp_download_path/merge"
# trap SIGUSR1 as it is being used to check
# for process aliveness when an existing
# pidfile is found
trap ':' USR1

# We are creating the following directory structure
# /opt/valheim/
# |___/dl/ <= downloads happen in here
# | |___/server/ <= vanilla server download
# | |___/plus/ <= ValheimPlus mod download
# |___/server/ <= vanilla server installation
# |___/plus/ <= merge of vanilla server and ValheimPlus mod
#
valheim_download_path=/opt/valheim/dl/server # Valheim server download directory
valheim_install_path=/opt/valheim/server # Valheim server installation directory
vp_download_path=/opt/valheim/dl/plus # ValheimPlus download directory
vp_install_path=/opt/valheim/plus # ValheimPlus installation directory
vp_zipfile=UnixServer.zip # Name of the ValheimPlus archive
valheim_restartfile="/tmp/valheim.restart" # Signaling file created by valheim-updater
# or valheim-plus-updater that describes
# if and how to restart the server
vp_mergefile="$vp_download_path/merge" # Signaling file created by valheim-updater
# that tells valheim-plus-updater that Valheim
# server was updated and needs to be merged
# with ValheimPlus
# Collection of PID files
valheim_server_pidfile=/var/run/valheim-server.pid
valheim_updater_pidfile=/var/run/valheim-updater.pid
valheim_backup_pidfile=/var/run/valheim-backup.pid

# log levels
debug=50
info=40
warn=30
error=20
critical=10
log_level=${log_level:-$debug}


debug() { logstd $debug "$*"; }
info() { logstd $info "$*"; }
warn() { logstd $warn "$*"; }
error() { logerr $error "$*"; }
critical() { logerr $critical "$*"; }


logstd() {
local log_at_level="$1"; shift
printline $log_at_level "$*"
}


logerr() {
local log_at_level="$1"; shift
printline $log_at_level "$*" >&2
}


printline() {
local log_at_level="$1"; shift
local log_data="$*"

if [ $log_at_level -le $log_level ]; then
printf "%s [%i] (%i) %s\n" \
"$(date +'%Y-%m-%d %H:%M:%S')" \
$$ \
$log_at_level \
"$log_data"
fi
}


ensure_permissions() {
chmod $CONFIG_DIRECTORY_PERMISSIONS /config
chmod -f $CONFIG_FILE_PERMISSIONS /config/*.txt
Expand All @@ -21,3 +82,45 @@ ensure_permissions() {
chmod $VALHEIM_PLUS_CONFIG_FILE_PERMISSIONS /config/valheimplus/*
fi
}


check_lock() {
local pidfile=$1
local predecessor_pid=$(<"$pidfile")
local numre='^[0-9]+$'
if [[ "$predecessor_pid" =~ $numre ]] ; then
debug "Sending SIGUSR1 to PID $predecessor_pid"
kill -USR1 "$predecessor_pid" &> /dev/null \
&& warn "Process with PID $predecessor_pid already running - exiting" \
|| (info "Removing stale PID file and starting run";
clear_lock_and_run $pidfile)
else
warn "Predecessor PID is corrupt - clearing lock and running"
clear_lock_and_run $pidfile
fi
}


clear_lock_and_run() {
local pidfile=$1
clear_lock $pidfile
main
}


clear_lock() {
local pidfile=$1
info "Releasing PID file $pidfile"
rm -f "$1"
}


error_handler() {
local ec=$1
local line_no=$2
local func_call_line=$3
local command="$4"
local stack="$5"
error "Error in line $line_no command '$command' exited with code $ec in $stack called in line $func_call_line"
exit $ec
}
3 changes: 1 addition & 2 deletions defaults
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ TZ=${TZ:-Etc/UTC}
WORLD_NAME=${WORLD_NAME:-Dedicated}
SERVER_NAME=${SERVER_NAME:-My Server}
SERVER_PORT=${SERVER_PORT:-2456}
SERVER_PASS=${SERVER_PASS:-secret}
SERVER_PASS=${SERVER_PASS-secret}
SERVER_PUBLIC=${SERVER_PUBLIC:-1}

# steamcmd.sh arguments
Expand All @@ -22,7 +22,6 @@ UPDATE_INTERVAL=${UPDATE_INTERVAL:-315360000}
# When we check for updates
UPDATE_CRON=${UPDATE_CRON-*/15 * * * *}


# What time we restart the valheim-server
# This is usful to mitigate the effects of memory/resource leaks
RESTART_CRON=${RESTART_CRON-0 5 * * *}
Expand Down
1 change: 1 addition & 0 deletions supervisord.conf
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ stderr_logfile=/dev/stderr
stderr_logfile_maxbytes=0
autostart=false
autorestart=true
startsecs=10
startretries=10
stopwaitsecs=90

Expand Down
52 changes: 33 additions & 19 deletions valheim-backup
Original file line number Diff line number Diff line change
@@ -1,66 +1,80 @@
#!/bin/bash
# valheim-backups runs permanently if BACKUPS=true
# and creates backups of the /config/worlds directory.

# Include defaults
. /usr/local/etc/valheim/defaults
. /usr/local/etc/valheim/common

# Remove trailing slash if any
BACKUPS_DIRECTORY=${BACKUPS_DIRECTORY%/}
pidfile=$valheim_backup_pidfile
next_backup=$(date +%s)
run=true


main() {
echo $$ > "$valheim_backup_pidfile"
cd /config
while [ $run = true ]; do
backup
flush_old
next_backup=$(($(date +%s)+$BACKUPS_INTERVAL))
while [ $run = true -a $(date +%s) -lt $next_backup ]; do
sleep 8
if (set -o noclobber; echo $$ > "$pidfile") 2> /dev/null; then
trap backup_now SIGHUP
trap shutdown SIGINT SIGTERM
trap 'error_handler $? $LINENO $BASH_LINENO "$BASH_COMMAND" $(printf "::%s" ${FUNCNAME[@]}); trap - ERR' ERR

cd /config
while [ $run = true ]; do
backup
flush_old
next_backup=$(($(date +%s)+$BACKUPS_INTERVAL))
while [ $run = true -a $(date +%s) -lt $next_backup ]; do
sleep 8
done
done
done
else
info "Found existing pid file - checking process"
check_lock $pidfile
fi
}


backup() {
if [ ! -d "/config/worlds" ]; then
echo "No Valheim worlds to backup"
debug "No Valheim worlds to backup"
return
fi
local backup_file="$BACKUPS_DIRECTORY/worlds-$(date +%Y%m%d-%H%M%S).zip"
echo "Backing up Valheim server worlds to $backup_file"
info "Backing up Valheim server worlds to $backup_file"
mkdir -p "$BACKUPS_DIRECTORY"
chmod $BACKUPS_DIRECTORY_PERMISSIONS "$BACKUPS_DIRECTORY"
zip -r "$backup_file" "worlds/"
chmod $BACKUPS_FILE_PERMISSIONS "$backup_file"
}


flush_old() {
if [ ! -d "$BACKUPS_DIRECTORY" ]; then
echo "No old backups to remove"
debug "No old backups to remove"
return
fi
echo "Removing backups older than $BACKUPS_MAX_AGE days"
info Removing backups older than $BACKUPS_MAX_AGE days
find "$BACKUPS_DIRECTORY" -type f -mtime +$BACKUPS_MAX_AGE -name 'worlds-*.zip' -print -exec rm -f "{}" \;
}


# This is a signal handler registered to SIGHUP
backup_now() {
echo "Received signal to backup world"
debug "Received signal to backup world"
next_backup=0
}


shutdown() {
rm -f "$valheim_backup_pidfile"
clear_lock $pidfile
run=false
}


if [ "X$BACKUPS" = Xtrue ]; then
trap backup_now SIGHUP
trap shutdown SIGINT SIGTERM
if [ "$BACKUPS" = true ]; then
main
else
echo "Backups have been turned off by env BACKUPS=$BACKUPS"
info "Backups have been turned off by env BACKUPS=$BACKUPS"
supervisorctl stop valheim-backup
fi
16 changes: 10 additions & 6 deletions valheim-bootstrap
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
#!/bin/bash
# valheim-bootstrap is the first of the valheim-* scripts
# that runs and prepares the system based on
# environment variables.

# Include defaults
. /usr/local/etc/valheim/defaults
. /usr/local/etc/valheim/common


mkdir -p "$valheim_download_path"
mkdir -p "$valheim_install_path"

Expand All @@ -13,32 +17,32 @@ if [ "$VALHEIM_PLUS" = true ]; then
fi

if [ -f "/usr/share/zoneinfo/$TZ" ]; then
echo "Setting timezone $TZ"
info "Setting timezone $TZ"
ln -snf /usr/share/zoneinfo/$TZ /etc/localtime
echo "$TZ" > /etc/timezone
else
echo "Error: unknown timezone $TZ"
error "Error: unknown timezone $TZ"
fi

crontab=$(mktemp)
if [ "$BACKUPS" = true -a -n "$BACKUPS_CRON" -a "$BACKUPS_INTERVAL" = "315360000" ]; then
echo "Creating cron to do world backups using schedule $BACKUPS_CRON"
debug "Creating cron to do world backups using schedule $BACKUPS_CRON"
tmpfile=$(mktemp)
echo "$BACKUPS_CRON [ -f $valheim_backup_pidfile ] && kill -HUP \$(cat $valheim_backup_pidfile)" >> $crontab
fi

if [ -n "$UPDATE_CRON" -a "$UPDATE_INTERVAL" = "315360000" ]; then
echo "Creating cron to check for updates using schedule $UPDATE_CRON"
debug "Creating cron to check for updates using schedule $UPDATE_CRON"
tmpfile=$(mktemp)
echo "$UPDATE_CRON [ -f $valheim_updater_pidfile ] && kill -HUP \$(cat $valheim_updater_pidfile)" >> $crontab
fi

if [ -n "$RESTART_CRON" ]; then
echo "Creating cron to restart valheim-server using schedule $RESTART_CRON"
debug "Creating cron to restart valheim-server using schedule $RESTART_CRON"
tmpfile=$(mktemp)
echo "$RESTART_CRON /usr/bin/supervisorctl restart valheim-server" >> $crontab
else
echo "Environment variable RESTART_CRON is empty - no automatic valheim-server restart scheduled"
debug "Environment variable RESTART_CRON is empty - no automatic valheim-server restart scheduled"
fi
crontab $crontab
rm -f $crontab
Expand Down
Loading

0 comments on commit d755bc9

Please sign in to comment.