-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserman.sh
More file actions
executable file
·417 lines (384 loc) · 12.1 KB
/
serman.sh
File metadata and controls
executable file
·417 lines (384 loc) · 12.1 KB
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
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
#!/bin/bash
# SerMan - The Server Instance Manager
# A script to manage minecraft server instances.
# load environment
SCRIPT_DIR=$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)
cd ${SCRIPT_DIR}
source ./common/utils.sh
# Load variables
if [ ! -f "./load_config.sh" ]; then
echo "[ERROR] load_config.sh not found in the script directory." >&2
exit 1
fi
source ./load_config.sh
# Check for required commands
check_prerequisites
VERBOSE=false
# Parse command line arguments into flags and positional arguments
parse_args flags args "$@"
# parse the flags
for flag in "${flags[@]}"; do
case "$flag" in
-v)
VERBOSE=true
;;
esac
done
# parse arguments
operations=("list" "backup" "uninstall" "status" "start" "stop")
backup_operations=("create" "delete" "list")
print_usage() {
if [ -z "${args[0]}" ]; then
local IFS='|'
local ops="${operations[*]}"
echo "Usage: serman.sh <$ops>" >&2
else
case "${args[0]}" in
list) ;;
backup)
case "${args[1]}" in
create)
echo "Usage: serman.sh backup create <server_alias>" >&2
;;
delete)
echo "Usage: serman.sh backup delete <server_alias> <backup_id|'latest'>" >&2
;;
list)
echo "Usage: serman.sh backup list <server_alias> [--json-format]" >&2
;;
*)
local IFS='|'
local backup_ops="${backup_operations[*]}"
echo "Usage: serman.sh backup <${backup_ops[*]}>" >&2
;;
esac
;;
uninstall)
echo "Usage: serman.sh uninstall <server_alias> [--delete-backups]" >&2
;;
status)
echo "Usage: serman.sh status" >&2
;;
start)
echo "Usage: serman.sh start <server_alias> [--run-startup-commands]" >&2
;;
stop)
echo "Usage: serman.sh stop <server_alias> [<delay_seconds>] [<note>] [<reason>] [--wait] [--run-shutdown-commands]" >&2
;;
*)
echo "[ERROR] Unknown operation: ${args[0]}" >&2
;;
esac
fi
}
if [ -z "${args[0]}" ]; then
print_usage
exit 1
fi
# logging
log() {
$VERBOSE && echo "$@" >&2
}
# Check if the server installation is correct
check_server_installation() {
local server_directory="$1"
if [ ! -d "$server_directory" ]; then
log "[ERROR] Server directory '$server_directory' does not exist."
return 1
fi
local files_to_check=("server.jar" "server.properties" "bin/startServer.sh")
for file in "${files_to_check[@]}"; do
if [ ! -f "${server_directory}/${file}" ]; then
log "[ERROR] Required file '$file' is missing in '$server_directory'."
return 1
fi
done
return 0
}
list_installed_servers() {
for server_dir in "${MINECRAFT_SERVER_DIR}"/*; do
# skip if not a directory
[ -d "$server_dir" ] || continue
server_alias=$(basename "$server_dir")
# check if the server directory is a valid server installation
check_server_installation "$server_dir" && echo "$server_alias" || continue
done
}
read_server_ip_port() {
local -n _ip_port="$1"
local server_alias="$2"
local server_properties_file="${MINECRAFT_SERVER_DIR}/${server_alias}/server.properties"
server_ip="$(grep '^server-ip=' "$server_properties_file" | cut -d'=' -f2)"
server_port="$(grep '^server-port=' "$server_properties_file" | cut -d'=' -f2)"
_ip_port=("$server_ip" "$server_port")
}
operation="${args[0]}"
case "$operation" in
list)
list_installed_servers
;;
backup)
backup_operation="${args[1]}"
if [ -z "$backup_operation" ] || ! in_array "$backup_operation" "${backup_operations[@]}"; then
print_usage "${args[0]}"
exit 1
fi
case "$backup_operation" in
create)
server_alias="${args[2]}"
if [ -z "$server_alias" ]; then
print_usage "${args[0]}" "${args[1]}"
exit 1
fi
# check if server installation is corret
server_directory="${MINECRAFT_SERVER_DIR}/${server_alias}"
check_server_installation "$server_directory" || {
echo "[ERROR] Server '$server_alias' is corrupted." >&2
$VERBOSE || echo "[INFO] Run again with -v for verbose logging." >&2
exit 1
}
bash -c '
source ./helpers/backup_server.sh
' _ "$server_alias"
;;
delete)
server_alias="${args[2]}"
if [ -z "$server_alias" ]; then
print_usage "${args[0]}" "${args[1]}"
exit 1
fi
# check if server installation is corret
server_directory="${MINECRAFT_SERVER_DIR}/${server_alias}"
check_server_installation "$server_directory" || {
echo "[ERROR] Server '$server_alias' is corrupted." >&2
$VERBOSE || echo "[INFO] Run again with -v for verbose logging." >&2
exit 1
}
backup_id="${args[3]}"
if [ -z "$backup_id" ]; then
print_usage "${args[0]}" "${args[1]}" "${args[2]}"
exit 1
fi
backup_infos="$(./serman.sh backup list "$server_alias" --json-format)"
if [ "$?" -ne 0 ]; then
echo "[ERROR] Failed to list backups for server '$server_alias'." >&2
exit 1
fi
if [ "$backup_id" == "latest" ]; then
latest_timestamp=$(echo "$backup_infos" | jq -r '.[].unix_time' | sort -n | tail -n1)
backup_meta="$(echo "$backup_infos" | jq -rc 'sort_by(.unix_time) | last')"
else
backup_meta="$(echo "$backup_infos" | jq -rc --arg id "$backup_id" '.[] | select(.id == $id)')"
fi
matching_count=$(echo "$backup_meta" | jq -s 'length')
if [ "$matching_count" -eq 0 ]; then
echo "[ERROR] Backup with ID '$backup_id' not found for server '$server_alias'." >&2
exit 1
elif [ "$matching_count" -gt 1 ]; then
echo "[ERROR] Found multiple backups with the same ID. This should not happen and needs manual fixing!" >&2
exit 1
fi
rm -rf "$(echo "$backup_meta" | jq -rc '.path')"
if [ $? -ne 0 ]; then
echo "[ERROR] Failed to delete backup with ID '$backup_id' for server '$server_alias'." >&2
exit 1
fi
echo "[INFO] Successfully deleted backup with ID '$backup_id' for server '$server_alias'." >&2
latest_path="$(
./serman.sh backup list "$server_alias" --json-format |
jq -rc 'sort_by(.unix_time) | last | .path'
)"
if [ "$latest_path" == "null" ]; then
log "[INFO] No backups left for server '$server_alias'. Removing 'latest' symlink and backup directory."
rm -rf "${MINECRAFT_SERVER_BACKUP_DIR}/${server_alias}"
if [ $? -ne 0 ]; then
log "[ERROR] Failed to remove backup directory for server '$server_alias'."
exit 1
fi
exit 0
fi
log "[INFO] Linking latest backup to $latest_path" >&2
ln -sf "$latest_path" "${MINECRAFT_SERVER_BACKUP_DIR}/${server_alias}/latest"
if [ $? -ne 0 ]; then
echo "[ERROR] Failed to create symlink for latest backup." >&2
exit 1
fi
;;
list)
server_alias="${args[2]}"
if [ -z "$server_alias" ]; then
print_usage "${args[0]}" "${args[1]}"
exit 1
fi
# list backup information
if in_array "--json-format" "${flags[@]}"; then
declare -a json_objs
else
declare -a table_rows
fi
table_delim='|'
for backup_dir in "${MINECRAFT_SERVER_BACKUP_DIR}/${server_alias}"/*; do
# skip symlinks
[ -L "$backup_dir" ] && continue
# skip if not a directory
[ -d "$backup_dir" ] || continue
check_server_installation "$backup_dir" || continue
backup_path="${backup_dir}"
backup_id="$(basename "$backup_path")"
backup_unix="$(get_creation_or_mod_time "$backup_path")"
if in_array "--json-format" "${flags[@]}"; then
json_objs+=("$(
jq -nrc --arg id "$backup_id" --arg unix_time "$backup_unix" --arg path "$backup_path" \
'{id: $id, unix_time: $unix_time, path: $path}'
)")
else
table_rows+=("${backup_id}${table_delim}${backup_unix}${table_delim}${backup_path}")
fi
done
if in_array "--json-format" "${flags[@]}"; then
IFS=',' joined_json="${json_objs[*]}"
output="$(jq -nr "[${joined_json}]")"
else
output="$(print_table "$table_delim" " " "Backup ID${table_delim}Backup Time${table_delim}Path" "${table_rows[@]}")"
fi
echo "$output"
;;
*)
echo "[ERROR] Unknown backup operation: $backup_operation" >&2
print_usage "${args[0]}"
exit 1
;;
esac
;;
uninstall)
server_alias="${args[1]}"
if [ -z "$server_alias" ]; then
print_usage "${args[0]}"
exit 1
fi
if in_array "--delete-backups" "${flags[@]}"; then
delete_backups=true
else
delete_backups=false
fi
# check if server installation is correct
server_directory="${MINECRAFT_SERVER_DIR}/${server_alias}"
if [ ! -d "$server_directory" ]; then
log "[ERROR] Server '$server_alias' does not exist."
exit 1
fi
rm -rf "$server_directory"
log "[INFO] Uninstalled server '$server_alias'."
if $delete_backups; then
backup_dir="${MINECRAFT_SERVER_BACKUP_DIR}/${server_alias}"
if [ -d "$backup_dir" ]; then
rm -rf "$backup_dir"
log "[INFO] Deleted backups for server '$server_alias'."
else
log "[INFO] No backups found for server '$server_alias'."
fi
fi
;;
status)
installed_servers="$(list_installed_servers)"
[ -z "$installed_servers" ] && exit 0
parsing_delimiter='|'
out_delimiter=' : '
status_lines=()
while IFS='' read -r alias; do
status_lines+=("${alias}${parsing_delimiter}$(get_server_status "$alias")")
done <<<"$installed_servers"
if [ ${#status_lines[@]} -eq 0 ]; then
log "[INFO] No servers installed."
exit 0
fi
print_table "$parsing_delimiter" "$out_delimiter" "" "${status_lines[@]}"
;;
start)
server_alias="${args[1]}"
if [ -z "$server_alias" ]; then
print_usage "${args[0]}"
exit 1
fi
# check if server installation is correct
server_directory="${MINECRAFT_SERVER_DIR}/${server_alias}"
check_server_installation "$server_directory" || {
echo "[ERROR] Server '$server_alias' is corrupted." >&2
exit 1
}
# check if server is already running
server_status=$(get_server_status "$server_alias")
if [[ "$server_status" == "Running" ]]; then
echo "[ERROR] Server '$server_alias' is already running." >&2
exit 1
fi
# Sart the server
source "${server_directory}/bin/startServer.sh"
echo "[INFO] Starting server '$server_alias'."
declare -a server_info
read_server_ip_port server_info "$server_alias"
run_startup_commands=false
in_array "--run-startup-commands" "${flags[@]}" && run_startup_commands=true
if $run_startup_commands; then
# wait for server to get online
while ! check_server_port "${server_info[0]}" "${server_info[1]}"; do
sleep 1
done
command_file="${server_directory}/bin/on-start"
while IFS='' read -r command; do
send_server_command "$server_alias" "$command"
done < <(get_mc_commands "$command_file")
fi
;;
stop)
server_alias="${args[1]}"
delay_seconds="${args[2]:-0}"
if ! is_number "$delay_seconds"; then
echo "[ERROR] Delay seconds must be a valid number." >&2
print_usage "${args[0]}"
exit 1
fi
stop_note="${args[3]:-null}"
kick_reason="${args[4]:-The server was stopped.}"
wait_flag=false
in_array "--wait" "${flags[@]}" && wait_flag=true
if [ -z "$server_alias" ] || [ -z "$delay_seconds" ]; then
print_usage "${args[0]}"
exit 1
fi
# check if server installation is correct
server_directory="${MINECRAFT_SERVER_DIR}/${server_alias}"
check_server_installation "$server_directory" || {
echo "[ERROR] Server '$server_alias' is corrupted." >&2
exit 1
}
# check if server is running
server_status=$(get_server_status "$server_alias")
if [[ "$server_status" != "Running" ]]; then
echo "[ERROR] Server '$server_alias' is not running." >&2
exit 1
fi
command_file=""
in_array "--run-shutdown-commands" "${flags[@]}" && command_file="${server_directory}/bin/on-stop"
if $wait_flag; then
(
source ./helpers/schedule_server_stop.sh "$server_alias" "$command_file" "$delay_seconds" "$stop_note" "$kick_reason"
)
if [ $? -ne 0 ]; then
echo "[ERROR] Failed to stop server '$server_alias'." >&2
exit 1
else
echo "[INFO] Server '$server_alias' successfully stopped."
fi
else
(
source ./helpers/schedule_server_stop.sh "$server_alias" "$command_file" "$delay_seconds" "$stop_note" "$kick_reason"
) >/dev/null 2>&1 &
fi
;;
*)
print_usage
exit 1
;;
esac
exit 0