forked from lloesche/valheim-server-docker
-
Notifications
You must be signed in to change notification settings - Fork 0
/
common
228 lines (192 loc) · 6.31 KB
/
common
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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
# 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
vp_config_path="/config/valheimplus"
just_started=${just_started:-true}
# 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
# Supervisor config files
supervisor_http_server_conf=/usr/local/etc/supervisor/conf.d/http_server.conf
# Status httpd config files
status_http_server_conf=/usr/local/etc/supervisor/conf.d/status_http_server.conf
status_http_server_updater_conf=/usr/local/etc/supervisor/conf.d/status_http_server_updater.conf
cmd_valheim_status=/usr/local/bin/valheim-status
cmd_valheim_logfilter=/usr/local/bin/valheim-logfilter
cmd_supervisorctl=/usr/local/bin/supervisorctl
# log levels
debug=50
info=40
warn=30
error=20
critical=10
fatal=5
log_level=${log_level:-$debug}
debug() { logstd $debug "(DEBUG) $*"; }
info() { logstd $info "(INFO) $*"; }
warn() { logstd $warn "(WARN) $*"; }
error() { logerr $error "(ERROR) $*"; }
critical() { logerr $critical "(CRITIAL) $*"; }
fatal() { logerr $fatal "(FATAL) $*"; exit 1; }
logstd() {
local log_at_level
log_at_level="$1"; shift
printline "$log_at_level" "$*"
}
logerr() {
local log_at_level
log_at_level="$1"; shift
printline "$log_at_level" "$*" >&2
}
printline() {
local log_at_level
local log_data
log_at_level="$1"; shift
log_data="$*"
if [ "$log_at_level" -le "$log_level" ]; then
printf "%s [%i] %s\\n" \
"$(date +'%Y-%m-%d %H:%M:%S')" \
$$ \
"$log_data"
fi
}
ensure_permissions() {
chmod "$CONFIG_DIRECTORY_PERMISSIONS" /config
chmod -f "$CONFIG_FILE_PERMISSIONS" /config/*.txt
if [ -d /config/worlds ]; then
chmod "$WORLDS_DIRECTORY_PERMISSIONS" /config/worlds
chmod "$WORLDS_FILE_PERMISSIONS" /config/worlds/*
fi
if [ "$VALHEIM_PLUS" = true ] && [ -d /config/valheimplus ]; then
chmod "$VALHEIM_PLUS_CONFIG_DIRECTORY_PERMISSIONS" /config/valheimplus
chmod "$VALHEIM_PLUS_CONFIG_FILE_PERMISSIONS" /config/valheimplus/*
fi
}
server_is_idle() {
if [ "$SERVER_PUBLIC" = 1 ]; then
"$cmd_valheim_status" > /dev/null 2>&1
else
if timeout 3 tcpdump udp port "$SERVER_PORT" -n -s 84 -c 1 > /dev/null 2>&1; then
return 1
else
return 0
fi
fi
}
server_is_running() {
test "$(supervisorctl status valheim-server | awk '{print $2}')" = RUNNING
}
server_is_listening() {
awk -v server_port="$SERVER_PORT" '
BEGIN {
exit_code = 1
}
{
if ($1 ~ /^[0-9]/) {
split($2, local_bind, ":")
listening_port = sprintf("%d", "0x" local_bind[2])
if (listening_port == server_port) {
exit_code = 0
exit
}
}
}
END {
exit exit_code
}
' /proc/net/udp*
}
check_lock() {
local pidfile
local predecessor_pid
local numre
pidfile=$1
predecessor_pid=$(<"$pidfile")
numre='^[0-9]+$'
if [[ "$predecessor_pid" =~ $numre ]] ; then
debug "Sending SIGUSR1 to PID $predecessor_pid"
if kill -USR1 "$predecessor_pid" &> /dev/null; then
fatal "Process with PID $predecessor_pid already running - exiting"
else
info "Removing stale PID file and starting run"
clear_lock_and_run "$pidfile"
fi
else
warn "Predecessor PID is corrupt - clearing lock and running"
clear_lock_and_run "$pidfile"
fi
}
clear_lock_and_run() {
local pidfile
pidfile=$1
clear_lock "$pidfile"
main
}
clear_lock() {
local pidfile
pidfile=$1
info "Releasing PID file $pidfile"
rm -f "$1"
}
error_handler() {
local ec
local line_no
local func_call_line
local command
local stack
ec=$1
line_no=$2
func_call_line=$3
command="$4"
stack="$5"
error "Error in line $line_no command '$command' exited with code $ec in $stack called in line $func_call_line"
return "$ec"
}
write_valheim_plus_config() {
if [ -d "$vp_config_path" ]; then
if env | grep "^$VALHEIM_PLUS_CFG_ENV_PREFIX" > /dev/null; then
/usr/local/bin/vpenvconf --verbose --config "$vp_config_path/valheim_plus.cfg" --env-prefix "$VALHEIM_PLUS_CFG_ENV_PREFIX"
fi
fi
}
write_restart_file() {
local mode
local reason
reason=$1
if [ "$just_started" = true ] && [ "$reason" = just_started ]; then
mode="start"
else
mode="restart"
fi
if [ ! -f "$valheim_restartfile" ]; then
debug "Writing file to $mode Valheim server"
echo "$mode" > "$valheim_restartfile"
fi
}
update_server_status() {
local status
status=$1
echo "$status" > "$SERVER_STATUS_FILE"
}