Skip to content

Commit

Permalink
custom messages, fix auto reboot, countdown, etc.
Browse files Browse the repository at this point in the history
  • Loading branch information
kimzuni committed Apr 27, 2024
1 parent 85df12d commit 5aee4ef
Show file tree
Hide file tree
Showing 8 changed files with 232 additions and 178 deletions.
9 changes: 6 additions & 3 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ RUN apt-get update && \
curl \
net-tools \
ca-certificates \
lib32gcc1-amd64-cross \
lib32gcc-s1-amd64-cross=12.2.0-14cross1 \
procps=2:4.0.2-3 \
gettext-base=0.21-12 \
xdg-user-dirs=0.18-1 \
Expand Down Expand Up @@ -70,10 +70,14 @@ ENV TZ="UTC" \
AUTO_UPDATE_ENABLED=false \
AUTO_UPDATE_CRON_EXPRESSION="0 * * * *" \
AUTO_UPDATE_WARN_MINUTES=15 \
AUTO_UPDATE_WARN_MESSAGE="Server will update in remaining_time minutes." \
AUTO_REBOOT_ENABLED=false \
AUTO_REBOOT_CRON_EXPRESSION="0 0 * * *" \
AUTO_REBOOT_WARN_MINUTES=15 \
AUTO_REBOOT_WARN_MESSAGE="Server will reboot in remaining_time minutes." \
AUTO_REBOOT_EVEN_IF_PLAYERS_ONLINE=false \
BROADCAST_COUNTDOWN_MTIMES="1 5 10 15 30 60" \
BROADCAST_COUNTDOWN_SUSPEND_MESSAGE="Suspends countdown because there are no players." \
TARGET_COMMIT_ID= \
DISCORD_WEBHOOK_URL="" \
DISCORD_SUPPRESS_NOTIFICATIONS=false \
Expand Down Expand Up @@ -125,7 +129,6 @@ ENV TZ="UTC" \
DISCORD_ERR_BACKUP_DELETE_MESSAGE_URL= \
DISCORD_BROADCAST_MESSAGE_ENABLE=true \
DISCORD_BROADCAST_MESSAGE_URL= \
BROADCAST_COUNTDOWN_MTIMES="1 5 10 15 30 60" \
DISABLE_GENERATE_SETTINGS=false \
ENABLE_PLAYER_LOGGING=true \
PLAYER_LOGGING_POLL_PERIOD=5 \
Expand All @@ -135,7 +138,7 @@ ENV TZ="UTC" \
ARG GIT_VERSION_TAG=unspecified

COPY --chmod=755 ./scripts /home/steam/server
RUN for file in backup.sh update.sh restore.sh broadcast.sh; do \
RUN for file in backup.sh update.sh restore.sh reboot.sh broadcast.sh; do \
mv /home/steam/server/"$file" /usr/local/bin/"${file%.sh}"; \
done

Expand Down
161 changes: 86 additions & 75 deletions docs/en/README.md

Large diffs are not rendered by default.

162 changes: 87 additions & 75 deletions docs/kr/README.md

Large diffs are not rendered by default.

6 changes: 2 additions & 4 deletions scripts/broadcast.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,9 @@
# shellcheck source=scripts/helper_functions.sh
source "/home/steam/server/helper_functions.sh"

# Given a message this will broadcast in discord
# Returns 0 on success
# Returns 1 if not able to broadcast
# Given a message and level this will broadcast in discord

message="$1"
level="$2"

DiscordMessage "Broadcast" "$message" "$level" "$DISCORD_BROADCAST_MESSAGE_ENABLE" "$DISCORD_BROADCAST_MESSAGE_URL"
DiscordMessage "Broadcast" "$message" "$level" "$DISCORD_BROADCAST_MESSAGE_ENABLE" "$DISCORD_BROADCAST_MESSAGE_URL"
57 changes: 40 additions & 17 deletions scripts/helper_functions.sh
Original file line number Diff line number Diff line change
Expand Up @@ -135,12 +135,9 @@ DiscordMessage() {
fi
}

# Given a message this will broadcast in discord
# Returns 0 on success
# Returns 1 if not able to broadcast
# Given a message and level this will broadcast in discord
broadcast_command() {
broadcast "$@"
return $?
}

# Saves then shutdowns the server
Expand All @@ -158,26 +155,30 @@ shutdown_server() {
# Returns 2 if mtime is not an integer
countdown_message() {
local mtime="$1"
local message_prefix="$2"
local message="$2"
local return_val=0
local minute="minutes"

if [[ "${mtime}" =~ ^[0-9]+$ ]]; then
for ((i = "${mtime}" ; i > 0 ; i--)); do
# Only do countdown if there are players
# Checking for players every minute
player_check || break

if [ "$mtime" -eq "$i" ] || [[ " $BROADCAST_COUNTDOWN_MTIMES " == *" $i "* ]]; then
if [ "$i" -eq 1 ]; then
minute="minute"
message="${message//minutes/minute}"
fi
broadcast_command "${message_prefix} in ${i} ${minute}" "warn"
broadcast_command "${message//remaining_time/$i}" "warn"
fi

# Only do countdown if there are players
# Checking for players every minute
if ! player_check; then
break
fi
sleep 59s
done

if [ "$i" -eq 0 ]; then
sleep 1s
elif [ "$1" -ne "$mtime" ]; then
broadcast_command "${BROADCAST_COUNTDOWN_SUSPEND_MESSAGE}" "warn"
fi
# If there are players but mtime is empty
elif [ -z "${mtime}" ]; then
return_val=1
Expand Down Expand Up @@ -223,8 +224,23 @@ get_latest_version() {

# Use it when you have to wait for it to be saved automatically because it does not support RCON.
wait_save() {
LogAction "Waiting for the server to be saved..."
broadcast_command "Waiting for the server to be saved..." "in-progress"
local title="$1"
local message="$2"
local level="$3"
local enabled="$4"
local webhook_url="$5"
local timestamp livetime

if ! player_check; then
livetime="$(date "+%s")"
timestamp="$(grep "RemovePlayer" "$SERVER_LOG_PATH" | tail -1 | awk -F "\\\[|\\\]" '{printf("%s/%s/%s %s:%s:%s\n", $2, $3, $4, $5, $6, $7)}' | date_to_timestamp)"
if [ -z "$timestamp" ] || save_check "$((livetime - timestamp))"; then
return
fi
fi

LogWarn "$message"
DiscordMessage "$title" "$message" "$level" "$enabled" "$webhook_url"

while ! save_check; do
sleep 1s
Expand All @@ -245,15 +261,22 @@ save_check() {
| awk -F "\\\[|\\\]" '{print $2}' \
| sed "s/Jan/1/g; s/Feb/2/g; s/Mar/3/g; s/Apr/4/g; s/May/5/g; s/Jun/6/g; s/Jul/7/g; s/Aug/8/g; s/Sep/9/g; s/Oct/10/g; s/Nov/11/g; s/Dec/12/g" \
| awk -F ":| |," '{if (($NF == "PM" && $5 != 12) || ($NF == "AM" && $5 == 12)) $5 = ($5+12)%24; printf("%s/%s/%s %s:%s:%s\n", $4, $1, $2, $5, $6, $7)}' \
| sort --version-sort | tail -1 | (read -r time; test -n "$time" && date -d "$time" "+%s")
| sort --version-sort | tail -1 | date_to_timestamp
)

if [ $((livetime - savetime)) -ge $((spare)) ]; then
if [ $((livetime - savetime)) -ge $((spare - 5)) ]; then
return 1
fi
return 0
}

date_to_timestamp() {
read -r time
if [ -n "$time" ]; then
date -d "$time" "+%s" 2> /dev/null
fi
}

Server_Info() {
local HTTP URL="$CFG_COMMUNITY_WEBSITE"
if ! [[ "$URL" =~ ^https?:// ]] && [ -n "$URL" ]; then
Expand Down
4 changes: 2 additions & 2 deletions scripts/auto_reboot.sh → scripts/reboot.sh
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,11 @@ if [ "${AUTO_REBOOT_EVEN_IF_PLAYERS_ONLINE,,}" != true ]; then
fi
fi

countdown_message "${AUTO_REBOOT_WARN_MINUTES}" "Server will reboot"
countdown_message "${AUTO_REBOOT_WARN_MINUTES}" "${AUTO_REBOOT_WARN_MESSAGE}"
countdown_exit_code=$?
case "${countdown_exit_code}" in
0 )
wait_save
wait_save "Stop" "Waiting for the server to be saved before reboot..." "warn" "$DISCORD_PRE_SHUTDOWN_MESSAGE_ENABLED" "$DISCORD_PRE_SHUTDOWN_MESSAGE_URL"
shutdown_server
;;
1 )
Expand Down
7 changes: 7 additions & 0 deletions scripts/start.sh
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,13 @@ if [ "${AUTO_UPDATE_ENABLED,,}" = true ] && [ "${UPDATE_ON_BOOT}" = true ]; then
supercronic -quiet -test "/home/steam/server/crontab" || exit
fi

if [ "${AUTO_REBOOT_ENABLED,,}" = true ]; then
LogInfo "AUTO_REBOOT_ENABLED=${AUTO_REBOOT_ENABLED,,}"
LogInfo "Adding cronjob for auto rebooting"
echo "$AUTO_REBOOT_CRON_EXPRESSION bash /usr/local/bin/reboot" >> "/home/steam/server/crontab"
supercronic -quiet -test "/home/steam/server/crontab" || exit
fi

if [ -s "/home/steam/server/crontab" ]; then
supercronic -passthrough-logs "/home/steam/server/crontab" &
LogInfo "Cronjobs started"
Expand Down
4 changes: 2 additions & 2 deletions scripts/update.sh
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,11 @@ if [ "${UPDATE_ON_BOOT,,}" != true ]; then
exit 1
fi

countdown_message "${AUTO_UPDATE_WARN_MINUTES}" "Server will update"
countdown_message "${AUTO_UPDATE_WARN_MINUTES}" "${AUTO_UPDATE_WARN_MESSAGE}"
countdown_exit_code=$?
case "${countdown_exit_code}" in
0 )
wait_save
wait_save "Update" "Waiting for the server to be saved before update..." "warn" "$DISCORD_PRE_UPDATE_BOOT_MESSAGE_ENABLED" "$DISCORD_PRE_UPDATE_BOOT_MESSAGE_URL"
LogAction "Updating the server from $CURRENT_COMMIT to ${TARGET_COMMIT_ID:-$LATEST_COMMIT}."
backup
shutdown_server
Expand Down

0 comments on commit 5aee4ef

Please sign in to comment.