-
Notifications
You must be signed in to change notification settings - Fork 1
/
pz_restart_graceful.sh
146 lines (125 loc) · 3.78 KB
/
pz_restart_graceful.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
#!/bin/bash
set -o nounset -o errexit -o pipefail
shopt -s inherit_errexit
declare -r PZ_SERVER_STEAM_APPID=380870
declare -i DEFAULT_RESTART_MINS=30
declare -i ALERT_EVERY_MIN=5
# shellcheck source=./pz.bashrc
source /usr/local/etc/pz.bashrc
if [[ -f /usr/local/etc/pz.bashrc.local ]]; then
# use this file to specify any overrides
source /usr/local/etc/pz.bashrc.local
fi
function main() {
local restartMins="${1:-${DEFAULT_RESTART_MINS}}"
local restartReason="${2:-}"
# assure value is an integer
printf -v restartMins '%0.f' "${restartMins}"
if [[ ${restartMins} -le 0 ]]; then
# check for zero/negative as well
restartMins="${DEFAULT_RESTART_MINS}"
fi
local -i remainingTime=${restartMins}
while [[ ${remainingTime} -gt 0 ]]; do
remainingTime=$(checkPlayerCountAndBroadcast "${remainingTime}" "${restartReason}")
remainingTime=$(sleepAndReduceBy "${remainingTime}" "${ALERT_EVERY_MIN}")
done
restartAndBackup
}
function checkPlayerCountAndBroadcast() {
local -i remainingTime=${1}
local restartReason="${2:-}"
local -i playerCount
playerCount=$(getPlayerCount)
if [[ ${playerCount} -eq 0 ]]; then
# signal to immediately shutdown
echo 0
return
fi
local broadcastMsg="SERVER RESTART IN ${remainingTime} MINUTES"
if [[ -n "${restartReason}" ]]; then
broadcastMsg+=".<LINE>REASON: ${restartReason}"
fi
pz_broad "${broadcastMsg}" >/dev/null 2>&1
echo "${remainingTime}"
}
function getPlayerCount() {
local rconPlayersResponse rconPlayersHeader playerCount
rconPlayersResponse="$(pz_players)"
rconPlayersHeader="$(head -n1 <<< "${rconPlayersResponse}")"
# NOTE: if pz changes any of its output to commands this will "break"
###
### TODO: KEEP UP TO DATE
###
playerCount="$(sed -E 's/.*players connected \(([0-9]+)\).*/\1/i' <<< "${rconPlayersHeader}")"
# we'll make it smart, so if regex fails (same text after processing) we assume there are players
if [[ -z "${playerCount}" || "${rconPlayersHeader}" == "${playerCount}" ]]; then
echo "WARNING: regex failed to detect player count. Response: ${rconPlayersResponse}" 1>&2
# need some special #, one is as good as the next :P
echo 69
return
fi
if [[ ${playerCount} -gt 0 ]]; then
# for logging purposes print to stderr the users shown logged in
echo -e "STILL LOGGED IN:\n$(tail -n +2 <<< "${rconPlayersResponse}")" 1>&2
fi
echo "${playerCount}"
}
function restartAndBackup() {
pz_broad 'FLEE FLEE FLEE'
pz_stop
sleep 1s
pz_create_backup
runSteamCmdUpdate
pz_start
}
function runSteamCmdUpdate() {
if [[ ! -f "${PZ_RUN_STEAMCMD_UPDATE}" ]]; then
# use simple file to signal we should perform steamcmd update as well
return
fi
local -a STEAMCMD_PARAMS=(
+force_install_dir "${PZ_HOME}"
+login anonymous
+app_update "${PZ_SERVER_STEAM_APPID}" validate
+quit
)
sudo -u "${STEAMCMD_USER}" "${STEAMCMD_BIN}" "${STEAMCMD_PARAMS[@]}"
rm -f "${PZ_RUN_STEAMCMD_UPDATE}"
}
function sleepAndReduceBy() {
local -i current=${1}
local -i reduceByNearest=${2}
local -i reduceBy
if [[ ${current} -eq 0 ]]; then
# immediately bail
echo 0
return
elif [[ ${current} -le 1 ]]; then
# final iteration
reduceBy=1
current=0
elif [[ ${current} -gt ${reduceByNearest} ]]; then
local -i remainder
remainder=$((current % reduceByNearest))
if [[ ${remainder} -ne 0 ]]; then
reduceBy=${remainder}
else
reduceBy=${reduceByNearest}
fi
((current-=reduceBy))
else
# we want to sleep until current=1
local -i remainder
remainder=$((current % reduceByNearest))
if [[ ${remainder} -ne 0 ]]; then
reduceBy=$((remainder - 1))
else
reduceBy=$((reduceByNearest - 1))
fi
((current-=reduceBy))
fi
sleep "${reduceBy}m"
echo "${current}"
}
main "$@"