forked from msmhq/msm
-
Notifications
You must be signed in to change notification settings - Fork 0
/
msm
executable file
·4082 lines (3374 loc) · 122 KB
/
msm
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
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/bin/bash
# Minecraft Server Manager
# ========================
#
# A single init script for managing multiple Minecraft servers.
# Created by Marcus Whybrow
#
# http://marcuswhybrow.net/minecraft-server-manager/
#
### BEGIN INIT INFO
# Provides: msm
# Required-Start: $local_fs $remote_fs
# Required-Stop: $local_fs $remote_fs
# Should-Start: $network
# Should-Stop: $network
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: MSM: Minecraft init script
# Description: Minecraft Server Manager, an init script for Minecraft/Bukkit servers
### END INIT INFO
# See http://www.debian.org/doc/debian-policy/ch-opersys.html#s-sysvinit for
# more information on debain init.d scripts, which may help you understand
# this script.
# The Minecraft Server Manager version, use "msm version" to check yours.
VERSION="0.8.16"
# Source, if it exists, the msm profile.d script
if [ -f "/etc/profile.d/msm.sh" ]; then
source "/etc/profile.d/msm.sh"
fi
# $1: The file to follow links for
follow_links() {
unset RETURN
local file="$1"
while [[ -L "$file" ]]; do
file="$(readlink "$file")"
done
RETURN="$file"
}
# Get real script file location
follow_links "$0"; SCRIPT="$RETURN"
# Get the MSM_CONF environment variable or use the default location
CONF="${MSM_CONF:-/etc/msm.conf}"
# Get the MSM_BASH_COMPLETION environment variable or use default location
COMPLETION="${MSM_BASH_COMPLETION:-/etc/bash_completion.d/msm}"
follow_links "$COMPLETION"; COMPLETION="$RETURN"
### Config variables the user should not need/want to change
# The start of a regex to find a log line
LOG_REGEX="^[0-9]{4}-[0-9]{2}-[0-9]{2} [0-9]{2}:[0-9]{2}:[0-9]{2} \[.*\]"
# Lazy allocation status
ALLOCATED_SERVERS="false"
ALLOCATED_WORLDS="false"
# Global totals
NUM_WORLDS=0
NUM_SERVERS=0
COMMAND_COUNT=0
SETTING_COUNT=0
SERVER_SETTING_COUNT=0
VERSIONS_COUNT=0
### Utility Functions
# Executes the command "$2" as user "$1"
# $1: The user to execute the command as
# $2: The command to execute
as_user() {
local user="$(whoami)"
if [ "$user" == "$1" ]; then
bash -c "$2"
else
if [ "$user" == "root" ]; then
su - "$1" -s /bin/bash -c "$2"
else
if [[ "$1" == "root" ]]; then
error_exit INVALID_USER "This command must be executed as the user \"$1\"."
else
error_exit INVALID_USER "This command must be executed as the user \"$1\" or \"root\"."
fi
fi
fi
}
# Executes the command "$1" as SERVER_USER but returns stderr instead
as_user_stderr() {
as_user "$@" > /dev/null 2>&1
}
# Echo to stderr
echoerr() {
echo -e "$@" 1>&2
}
COLOUR_PURPLE="\e[1;35m"
COLOUR_RED="\e[1;31m"
COLOUR_CYAN="\e[1;36m"
COLOUR_GREEN="\e[1;32m"
COLOUR_RESET="\e[0m"
# Creates a coloured warning line
# $1 The warning to echo
msm_warning() {
echoerr "${COLOUR_PURPLE}[MSM Warning: ${1}]${COLOUR_RESET}"
}
msm_error() {
echoerr "${COLOUR_RED}[MSM Error: ${1}]${COLOUR_RESET}"
}
msm_info() {
echo -e "${COLOUR_CYAN}[MSM Info: ${1}]${COLOUR_RESET}"
}
msm_success() {
echo -e "${COLOUR_CYAN}[MSM: ${1}]${COLOUR_RESET}"
}
# Echos the first non-empty string in the arguments list
# $1->: Candidate strings for echoing
echo_fallback() {
for arg in "$@"; do
[ -z "$arg" ] && continue
echo "$arg" && break
done
}
# $1: The string to echo if present
echo_if() {
[ ! -z "$1" ] && echo "$1"
}
# Exit's the script
error_exit() {
case "$1" in
INVALID_USER) code=64;;
INVALID_COMMAND) code=65;;
INVALID_ARGUMENT) code=66;;
SERVER_STOPPED) code=67;;
SERVER_RUNNING) code=68;;
NAME_NOT_FOUND) code=69;;
FILE_NOT_FOUND) code=70;;
DUPLICATE_NAME) code=71;;
LOGS_NOT_ROLLED) code=72;;
CONF_ERROR) code=73;;
FATAL_ERROR) code=74;;
JAVA_NOT_INSTALLED) code=75;;
esac
echo "${2:-"Unknown Error"}" 1>&2
exit "${code:-$1}"
}
# Tests the bash version installed
# $1: The bash version required
is_bash_version() {
if [[ "$BASH_VERSION" =~ ^$1 ]]; then
return 0
fi
return 1
}
# Converts a string to be ready for use as a global
# variable name.
# $1: The string to convert
# RETURN: The name in uppercase and with underscores
to_global_name() {
unset RETURN
# Translate to uppercase, and replace dashes with underscores
local result="$1"
if is_bash_version 4; then
# Much faster than the `tr` command
result="${result//-/_}"
result="${result//./_}"
result="${result^^}" # to uppercase
else
result="$(echo "$result" | tr '[\-\.a-z]' '[\_\_A-Z]')"
fi
RETURN="$result"
}
# Converts a global BASH variable name to a server.properties file
# varibale name.
# $1: The string to convert
# RETURN: The name in lowercase and with dashes
to_properties_name() {
unset RETURN
# Translate to uppercase, and replace dashes with underscores
local result="$1"
if is_bash_version 4; then
# Much faster than the `tr` command
result="${result//_/-}"
result="${result,,}" # to lowercase
else
result="$(echo "$result" | tr '[\_A-Z]' '[\-a-z]')"
fi
RETURN="$result"
}
# A custom basename function which is faster
# than opening a subshell
# $1: The path to get the basename of
# RETURN: The basename of the path
quick_basename() {
unset RETURN
if [[ "$1" =~ \/([^\/]*)$ ]]; then
RETURN="${BASH_REMATCH[1]}"
fi
}
# A function used to print debug messages to stdout. Prevents messages from
# appearing unless in debug mode, and allows debug statements to be easily
# distinguished from necessary echo statements.
# $1: The message to output
debug() {
manager_property DEBUG
if [[ "$SETTINGS_DEBUG" == "true" ]]; then
echoerr "$1"
fi
}
# Determines whether "$1" is a valid name for a server or jar group directory
# It must only contain upper or lower case letters, digits, dashes or
# underscores.
# It must also not be one of a list of reserved names.
# $1: The name to check
is_valid_name() {
local valid="^[a-zA-Z0-9\_\-]+$"
local invalid="^(start|stop|restart|version|server|jargroup|all|config|update|help|\-\-.*)$"
if [[ "$1" =~ $valid ]]; then
if [[ "$1" =~ $invalid ]]; then
error_exit INVALID_ARGUMENT "Invalid name \"$1\": A name may not be any of the following reserved worlds \"start\", \"stop\", \"restart\", \"server\", \"version\", \"jargroup\", \"all\", \"config\", \"update\" or \"help\" or start with two dashes (--)."
else
return 0
fi
else
error_exit INVALID_ARGUMENT "Invalid name \"$1\": A name may only contain letters, numbers, dashes and unscores."
fi
}
# Gets the latest jar from a jar group, based upon the date and time encoded
# in the file name.
# $1: The directory to search
# RETURN: The latest file
get_latest_file() {
unset RETURN
local best_time=0
local best_file=""
while IFS= read -r -d $'\0' file; do
# Remove the path, leaving just the file name
local date_time="$(basename "$file" | awk -F '-' '{print $1 "-" $2 "-" $3 " " $4 ":" $5 ":" $6}')"
# Get the time in seconds since 1970 from file name
local seconds="$(date -d "$date_time" "+%s" 2> /dev/null)"
# If that is newer than the current best, override variables
if [[ "$seconds" -gt "$best_time" ]]; then
best_time="$seconds"
best_file="$file"
fi
done < <(find "$1" -maxdepth 1 -type f -print0)
RETURN="$best_file"
}
# Returns the current time as a UNIX timestamp (in seconds since 1970)
now() {
date +%s
}
### Log Utility Functions
# Gets the UNIX timestamp for a server log line
# $1: A server log line
# returns: Time in seconds since 1970-01-01 00:00:00 UTC
log_line_get_time() {
time_string="$(echo "$1" | awk '{print $1 " " $2}')"
date -d "$time_string" "+%s" 2> /dev/null
}
### World Utility Functions
### -----------------------
# Moves a world to RAM
# $1: the ID of the world to move
world_to_ram() {
manager_property RAMDISK_STORAGE_ENABLED
manager_property RAMDISK_STORAGE_PATH
server_property "${WORLD_SERVER_ID[$1]}" USERNAME
world_property "$1" RAMDISK_PATH
world_property "$1" FLAG_INRAM
world_property "$1" PATH
if [[ "$SETTINGS_RAMDISK_STORAGE_ENABLED" == "true" ]]; then
as_user "${SERVER_USERNAME[${WORLD_SERVER_ID[$1]}]}" "mkdir -p \"${WORLD_RAMDISK_PATH[$1]}\" && rsync -rt --exclude '$(basename "${WORLD_FLAG_INRAM[$1]}")' \"${WORLD_PATH[$1]}/\" \"${WORLD_RAMDISK_PATH[$1]}\""
fi
}
# Moves a world in RAM to disk
# $1: the ID of the world to move
world_to_disk() {
server_property "${WORLD_SERVER_ID[$1]}" USERNAME
world_property "$1" FLAG_INRAM
world_property "$1" RAMDISK_PATH
world_property "$1" PATH
as_user "${SERVER_USERNAME[${WORLD_SERVER_ID[$1]}]}" "rsync -rt --exclude '$(basename "${WORLD_FLAG_INRAM[$1]}")' \"${WORLD_RAMDISK_PATH[$1]}/\" \"${WORLD_PATH[$1]}\""
}
# Toggles a worlds ramdisk state
# $1: The ID of the world
world_toggle_ramdisk_state() {
world_property "$1" FLAG_INRAM
world_property "$1" RAMDISK_PATH
local sid="${WORLD_SERVER_ID[$1]}"
server_property "$sid" USERNAME
if [ -f "${WORLD_FLAG_INRAM[$1]}" ]; then
echo -n "Synchronising world \"${WORLD_NAME[$1]}\" to disk... "
world_to_disk "$1"
echo "Done."
echo -n "Removing RAM flag from world \"${WORLD_NAME[$1]}\"... "
as_user "${SERVER_USERNAME[$sid]}" "rm -f \"${WORLD_FLAG_INRAM[$1]}\""
echo "Done."
echo -n "Removing world \"${WORLD_NAME[$1]}\" from RAM... "
as_user "${SERVER_USERNAME[$sid]}" "rm -r \"${WORLD_RAMDISK_PATH[$1]}\""
echo "Done."
else
echo -n "Adding RAM flag to world \"${WORLD_NAME[$1]}\"... "
as_user "${SERVER_USERNAME[$sid]}" "touch \"${WORLD_FLAG_INRAM[$1]}\""
echo "Done."
echo -n "Copying world to RAM... "
world_to_ram "$1"
echo "Done."
fi
echo "Changes will only take effect after server is restarted."
}
# Backs up a world
# $1: The ID of the world
world_backup() {
manager_property WORLD_ARCHIVE_ENABLED
manager_property RDIFF_BACKUP_ENABLED
manager_property RSYNC_BACKUP_ENABLED
local server_id="${WORLD_SERVER_ID[$1]}"
local containing_dir="$(dirname "${WORLD_PATH[$1]}")"
local dir_name="$(basename "${WORLD_PATH[$1]}")"
world_property "$1" PATH
world_property "$1" BACKUP_PATH
echo -n "Entering in backup function ... "
if [[ "$SETTINGS_WORLD_ARCHIVE_ENABLED" == "true" ]]; then
echo -n "Backing up world \"${WORLD_NAME[$1]}\"... "
file_name="$(date "+%F-%H-%M-%S").zip"
server_property "$server_id" USERNAME
as_user "${SERVER_USERNAME[$server_id]}" "mkdir -p \"${WORLD_BACKUP_PATH[$1]}\" && cd \"$containing_dir\" && zip -rq \"${WORLD_BACKUP_PATH[$1]}/${file_name}\" \"${dir_name}\""
echo "Done."
fi
if [[ "$SETTINGS_RDIFF_BACKUP_ENABLED" == "true" ]]; then
echo -n "rdiff-backup world \"${WORLD_NAME[$1]}\"... "
server_property "$server_id" USERNAME
as_user "${SERVER_USERNAME[$server_id]}" "mkdir -p \"${RDIFF_BACKUP_PATH[$1]}\" && cd \"$containing_dir\" && nice -n \"$SETTINGS_RDIFF_BACKUP_NICE\" rdiff-backup \"${dir_name}\" \"${RDIFF_BACKUP_PATH[$1]}\" && nice -n \"$SETTINGS_RDIFF_BACKUP_NICE\" rdiff-backup --remove-older-than \"$SETTINGS_RDIFF_BACKUP_ROTATION\"D --force \"${RDIFF_BACKUP_PATH[$1]}\""
echo "Done."
fi
if [[ "$SETTINGS_RSYNC_BACKUP_ENABLED" == "true" ]]; then
echo -n "rsync-backup world \"${WORLD_NAME[$1]}\"... "
file_name="$(date "+%F-%H-%M-%S")"
server_property "$server_id" USERNAME
as_user "${SERVER_USERNAME[$server_id]}" "mkdir -p \"${RSYNC_BACKUP_PATH[$1]}\" && cd \"$containing_dir\" && rsync -aH --link-dest=\"${RSYNC_BACKUP_PATH[$1]}/latest\" \"${dir_name}\" \"${RSYNC_BACKUP_PATH[$1]}/${file_name}\" && rm -f \"${RSYNC_BACKUP_PATH[$1]}/latest\" && ln -s \"${file_name}\" \"${RSYNC_BACKUP_PATH[$1]}/latest\""
echo "Done."
fi
}
# Activates a world
# $1: The ID of the world
world_activate() {
server_property "${WORLD_SERVER_ID[$1]}" USERNAME
server_property "${WORLD_SERVER_ID[$1]}" WORLD_STORAGE_PATH
world_property "$1" INACTIVE_PATH
world_property "$1" ACTIVE_PATH
if [ -d "${WORLD_INACTIVE_PATH[$1]}" ]; then
echo -n "Moving world \"${WORLD_NAME[$1]}\" to the active worldstorage directory... "
local new_path="${SERVER_WORLD_STORAGE_PATH[${WORLD_SERVER_ID[$1]}]}"
as_user "${SERVER_USERNAME[${WORLD_SERVER_ID[$1]}]}" "mkdir -p \"$new_path\" && mv \"${WORLD_INACTIVE_PATH[$1]}\" \"$new_path\""
echo "Done."
else
if [ -d "${WORLD_ACTIVE_PATH[$1]}" ]; then
echo "World \"${WORLD_NAME[$1]}\" is already activate."
else
error_exit DIR_NOT_FOUND "Directory \"${WORLD_INACTIVE_PATH[$1]}\" could not be found."
fi
fi
}
# Deactivates a world
# $1: The ID of the world
world_deactivate() {
server_property "${WORLD_SERVER_ID[$1]}" USERNAME
server_property "${WORLD_SERVER_ID[$1]}" WORLD_STORAGE_INACTIVE_PATH
world_property "$1" ACTIVE_PATH
world_property "$1" INACTIVE_PATH
world_property "$1" PATH
if server_is_running "${WORLD_SERVER_ID[$1]}"; then
error_exit 68 "Worlds cannot be deactivated whilst the server is running."
else
if [ -d "${WORLD_ACTIVE_PATH[$1]}" ]; then
echo -n "Moving world \"${WORLD_NAME[$1]}\" to the inactive worldstorage directory... "
local new_path="${SERVER_WORLD_STORAGE_INACTIVE_PATH[${WORLD_SERVER_ID[$1]}]}"
as_user "${SERVER_USERNAME[${WORLD_SERVER_ID[$1]}]}" "mkdir -p \"$new_path\" && mv \"${WORLD_PATH[$1]}\" \"$new_path\""
echo "Done."
else
if [ -d "${WORLD_INACTIVE_PATH[$1]}" ]; then
echo "World \"${WORLD_NAME[$1]}\" is already deactivate."
else
error_exit DIR_NOT_FOUND "Directory \"${WORLD_ACTIVE_PATH[$1]}\" could not be found."
fi
fi
fi
}
# Get the value of a world property
# $1: The world ID
# $2: The property name
world_property() {
# Get the current value
eval local value=\"\${WORLD_$2[$1]}\"
# If it is empty, then set it
if [ -z "$value" ]; then
local sid="${WORLD_SERVER_ID[$1]}"
case "$2" in
NAME|PATH)
# Defined at allocation
return 0
;;
ACTIVE_PATH)
server_property "$sid" WORLD_STORAGE_PATH
WORLD_ACTIVE_PATH[$1]="${SERVER_WORLD_STORAGE_PATH[$sid]}/${WORLD_NAME[$1]}"
;;
INACTIVE_PATH)
server_property "$sid" WORLD_STORAGE_INACTIVE_PATH
WORLD_INACTIVE_PATH[$1]="${SERVER_WORLD_STORAGE_INACTIVE_PATH[$sid]}/${WORLD_NAME[$1]}"
;;
STATUS)
world_property "$1" ACTIVE_PATH
if [ -d "${WORLD_ACTIVE_PATH[$1]}" ]; then
WORLD_STATUS[$1]="active"
else
world_property "$1" INACTIVE_PATH
if [ -d "${WORLD_INACTIVE_PATH[$1]}" ]; then
WORLD_STATUS[$1]="inactive"
else
WORLD_STATUS[$1]="unknown"
fi
fi
;;
FLAG_INRAM)
world_property "$1" PATH
server_property "$sid" WORLDS_FLAG_INRAM
WORLD_FLAG_INRAM[$1]="${WORLD_PATH[$1]}/${SERVER_WORLDS_FLAG_INRAM[$sid]}"
;;
LINK)
server_property "$sid" PATH
WORLD_LINK[$1]="${SERVER_PATH[$sid]}/${WORLD_NAME[$1]}"
;;
BACKUP_PATH)
manager_property WORLD_ARCHIVE_PATH
manager_property WORLD_RDIFF_PATH
manager_property WORLD_RSYNC_PATH
WORLD_BACKUP_PATH[$1]="$SETTINGS_WORLD_ARCHIVE_PATH/${SERVER_NAME[$sid]}/${WORLD_NAME[$1]}"
RDIFF_BACKUP_PATH[$1]="$SETTINGS_WORLD_RDIFF_PATH/${SERVER_NAME[$sid]}/${WORLD_NAME[$1]}"
RSYNC_BACKUP_PATH[$1]="$SETTINGS_WORLD_RSYNC_PATH/${SERVER_NAME[$sid]}/${WORLD_NAME[$1]}"
;;
RAMDISK_PATH)
manager_property RAMDISK_STORAGE_ENABLED
# If the ramdisk path is set, get the path for this world
if [[ "$SETTINGS_RAMDISK_STORAGE_ENABLED" == "true" ]]; then
manager_property RAMDISK_STORAGE_PATH
WORLD_RAMDISK_PATH[$1]="${SETTINGS_RAMDISK_STORAGE_PATH}/${SERVER_NAME[$sid]}/${WORLD_NAME[$1]}"
fi
;;
INRAM)
world_property "$1" FLAG_INRAM
# Detect whether this world should be in ram
if [[ -e "${WORLD_FLAG_INRAM[$1]}" ]]; then
WORLD_INRAM[$1]="true"
else
WORLD_INRAM[$1]="false"
fi
;;
esac
fi
}
# $1: The world ID
world_dirty_properties() {
local index
# Removes properties for all servers if an index
# is not specified
if [ ! -z "$1" ] && [[ "$1" -ge 0 ]]; then
index="[$1]"
else
index=""
fi
unset WORLD_NAME$index
unset WORLD_PATH$index
unset WORLD_ACTIVE_PATH$index
unset WORLD_INACTIVE_PATH$index
unset WORLD_STATUS$index
unset WORLD_FLAG_INRAM$index
unset WORLD_LINK$index
unset WORLD_BACKUP_PATH$index
unset RDIFF_BACKUP_PATH$index
unset RSYNC_BACKUP_PATH$index
unset WORLD_RAMDISK_PATH$index
unset WORLD_INRAM$index
}
### Server Utility Functions
### ------------------------
# Returns the ID for a server.
# An ID is given to a server when loaded into memory, and can be used to lookup
# config information for that server
# $1: The name of the server
server_get_id() {
unset RETURN
for ((server=0; server<$NUM_SERVERS; server++)); do
if [[ "${SERVER_NAME[$server]}" == "$1" ]]; then
RETURN="$server"
return 0
fi
done
error_exit NAME_NOT_FOUND "Could not find id for server name \"$1\"."
}
# Returns the ID of a server's world.
# $1: The ID of the server
# $2: The name of the world
server_world_get_id() {
server_property "$1" WORLD_STORAGE_PATH
server_property "$1" WORLD_STORAGE_INACTIVE_PATH
unset RETURN
if [ -d "${SERVER_WORLD_STORAGE_PATH[$1]}/$2" ] || [ -d "${SERVER_WORLD_STORAGE_INACTIVE_PATH[$1]}/$2" ]; then
# If the directory exists
local start="${SERVER_WORLD_OFFSET[$1]}"
local max="$(( $start + ${SERVER_NUM_WORLDS[$1]} ))"
# For each of the servers worlds:
for ((i=$start; i<$max; i++)); do
if [[ "${WORLD_NAME[$i]}" == "$2" ]]; then
RETURN="$i"
return 0
fi
done
fi
error_exit NAME_NOT_FOUND "Could not find id for world \"$2\" for server \"${SERVER_NAME[$1]}\"."
}
# Returns 0 if the server $1 is running and 1 if not
# $1: The ID of the server
server_is_running() {
server_property "$1" SCREEN_NAME
server_property "$1" INVOCATION
if ps ax | grep -v grep | grep "${SERVER_SCREEN_NAME[$1]} ${SERVER_INVOCATION[$1]}" > /dev/null
then
return 0
else
return 1
fi
}
# Ensures the server has a jar file where it is expected to be
# $1: The id of the server
server_ensure_jar() {
server_property "$1" JAR_PATH
if [ -f "${SERVER_JAR_PATH[$1]}" ]; then
return 0
fi
error_exit FILE_NOT_FOUND "Could not find jar for server \"${SERVER_NAME[$1]}\": Expected \"${SERVER_JAR_PATH[$1]}\"."
}
# Read a value from the server configuration file
# $1: The id of the server
# $2: The setting name to read
server_read_config() {
unset RETURN
# Convert name into upper-case with underscores
# msm-setting => SERVER_SETTING
# setting => SERVER_PROPERTIES_SETTING
if [[ "$2" =~ ^msm\-(.*)$ ]]; then
to_global_name "${BASH_REMATCH[1]}"
else
to_global_name "PROPERTIES_$2"
fi
local name="$RETURN"
# Display the value of that setting
unset RETURN
server_property "$1" "$name"
eval RETURN=\"\${SERVER_$name[$1]}\"
}
# Creates symbolic links in the server directory (SETTINGS_SERVER_STORAGE_PATH) for each
# of the Minecraft worlds located in the world storage directory.
# $1: The id of the server for which links should be ensured
server_ensure_links() {
server_property "$1" USERNAME
server_property "$1" WORLD_STORAGE_PATH
# Ensure a directory for level-name exists in worldstorage.
# This allows a symlink to be created, and prevents new worlds
# being generated outside of worldstorage.
command_server_config "$1" "level-name"
as_user "${SERVER_USERNAME[$1]}" "mkdir -p \"${SERVER_WORLD_STORAGE_PATH[$1]}/$RETURN\""
server_worlds_allocate "$1"
echo -n "Maintaining world symbolic links... "
local start="${SERVER_WORLD_OFFSET[$1]}"
local max="$(( $start + ${SERVER_NUM_WORLDS[$1]} ))"
local output="false"
for ((i=$start; i<$max; i++)); do
world_property "$i" STATUS
world_property "$i" LINK
if [[ "${WORLD_STATUS[$i]}" != "active" ]]; then
# Remove the symbolic link if it exists
as_user "${SERVER_USERNAME[$1]}" "rm -f \"${WORLD_LINK[$i]}\""
continue
fi
world_property "$i" INRAM
# -L checks for the path being a link rather than a file
# ! -a, since it is within double square brackets means: the negation of
# the existence of the file. In other words: true if does not exist
if [[ -L "${WORLD_LINK[$i]}" || ! -a "${WORLD_LINK[$i]}" ]]; then
# If there is a symbolic link in the server direcotry to this world,
# or there is not a directory in the server directory containing this world.
# Get the original file path the symbolic link is pointing to
# If there is no link, link_target will contain nothing
link_target="$(readlink "${WORLD_LINK[$i]}")"
if "${WORLD_INRAM[$i]}"; then
# If this world is marked as loaded into RAM
world_property "$i" RAMDISK_PATH
if [ "${link_target}" != "${WORLD_RAMDISK_PATH[$i]}" ]; then
# If the symbolic link does not point to the RAM version of the world
# Remove the symbolic link if it exists
as_user "${SERVER_USERNAME[$1]}" "rm -f \"${WORLD_LINK[$i]}\""
# Create a new symbolic link pointing to the RAM version of the world
as_user "${SERVER_USERNAME[$1]}" "ln -s \"${WORLD_RAMDISK_PATH[$i]}\" \"${WORLD_LINK[$i]}\""
fi
else
# Otherwise the world is not loaded into RAM, and is just on disk
world_property "$i" PATH
if [ "${link_target}" != "${WORLD_PATH[$i]}" ]; then
# If the symbolic link does not point to the disk version of the world
# Remove the symbolic link if it exists
as_user "${SERVER_USERNAME[$1]}" "rm -f \"${WORLD_LINK[$i]}\""
# Create a new symbolic link pointing to the disk version of the world
as_user "${SERVER_USERNAME[$1]}" "ln -s \"${WORLD_PATH[$i]}\" \"${WORLD_LINK[$i]}\""
fi
fi
else
echoerr -en "\n Error: Could not create link for world \"${WORLD_NAME[$i]}\". The file \"${WORLD_LINK[$i]}\" already exists, and should not be overwritten automatically. Either remove this file, or rename \"${WORLD_NAME[$i]}\"."
output="true"
fi
done
if [[ "$output" == "true" ]]; then
echo -e "\nDone."
else
echo "Done."
fi
}
# Moves a servers worlds into RAM
# $1: The ID of the server
server_worlds_to_ram() {
manager_property RAMDISK_STORAGE_ENABLED
# Only proceed if there is a ramdisk path set in config
if [[ "$SETTINGS_RAMDISK_STORAGE_ENABLED" == "true" ]]; then
echo -n "Synchronising flagged worlds on disk to RAM... "
local i="${SERVER_WORLD_OFFSET[$1]}"
local max="$(( $i + ${SERVER_NUM_WORLDS[$1]} ))"
# For each of the servers worlds:
while [[ "$i" -lt "$max" ]]; do
world_property "$i" INRAM
world_property "$i" LINK
if "${WORLD_INRAM[$i]}" && [ -L "${WORLD_LINK[$i]}" ]; then
world_to_ram "$i"
fi
i="$(( $i + 1 ))"
done
echo "Done."
fi
}
# Moves a servers "in RAM" worlds back to disk
# $1: The ID of the server
server_worlds_to_disk() {
manager_property RAMDISK_STORAGE_ENABLED
if [[ "$SETTINGS_RAMDISK_STORAGE_ENABLED" == "true" ]]; then
echo -n "Synchronising worlds in RAM to disk... "
local i="${SERVER_WORLD_OFFSET[$1]}"
local max="$(( $i + ${SERVER_NUM_WORLDS[$1]} ))"
# For each of the servers worlds:
while [[ "$i" -lt "$max" ]]; do
world_property "$i" RAMDISK_PATH
if [ -d "${WORLD_RAMDISK_PATH[$i]}" ]; then
world_to_disk "$i"
fi
i="$(( $i + 1 ))"
done
echo "Done."
fi
}
# Watches a server's log for a specific line
# $1: The ID for the server
# $2: A UNIX timestamp (seconds since 1970) which the $3 line must be after
# $3: The regex that matches log lines
# $4: A timeout in seconds
# returns: When the line is found
server_log_get_line() {
server_property "$1" USERNAME
server_property "$1" LOG_PATH
unset RETURN
# Make sure there is a server log to check
as_user "${SERVER_USERNAME[$1]}" "touch ${SERVER_LOG_PATH[$1]}"
local regex="${LOG_REGEX} ($3)"
local timeout_deadline=$(( $(now) + $4 ))
# Read log, break if nothing is read in $4 seconds
while read -t $4 line; do
line_time="$(log_line_get_time "$line")"
# If the time is after the timeout deadline, break
[[ "$(now)" -gt "$timeout_deadline" ]] && break
# If the entry is old enough
if [[ "$line_time" -ge "$2" ]] && [[ "$line" =~ $regex ]]; then
# Return the line
RETURN="${BASH_REMATCH[1]}"
return 0
fi
done < <(as_user "${SERVER_USERNAME[$1]}" "tail --pid=$$ --follow --lines=20 --sleep-interval=0.1 \"${SERVER_LOG_PATH[$1]}\"")
}
# The same as server_log_get_line, but prints a dot instead of the log line
# to stdout, and retruns when line is found.
# $1: the ID of the server
# $2: A UNIX timestamp (seconds since 1970) which the $3 line must be after
# $3: The regex that matches log lines
# $4: A timeout in seconds
# returns: When the line is found
server_log_dots_for_lines() {
server_property "$1" USERNAME
server_property "$1" LOG_PATH
# Make sure there is a server log to check
as_user "${SERVER_USERNAME[$1]}" "touch ${SERVER_LOG_PATH[$1]}"
local regex="${LOG_REGEX} ($3)"
local timeout_deadline=$(( $(now) + $4 ))
# Read log, break if nothing is read in $4 seconds
while read -t $4 line; do
line_time="$(log_line_get_time "$line")"
# If the time is after the timeout deadline, break
[[ "$(now)" -gt "$timeout_deadline" ]] && break
# If the entry is old enough
if [[ "$line_time" -ge "$2" ]]; then
# Print a dot for this line
echo -n '.'
# and if it matches the regular expression, return
if [[ "$line" =~ $regex ]]; then
return 0
fi
fi
done < <(as_user "${SERVER_USERNAME[$1]}" "tail --pid=$$ --follow --lines=100 --sleep-interval=0.1 \"${SERVER_LOG_PATH[$1]}\"")
}
# Sends as string to a server for execution
# $1: The ID of the server
# $2: The line of text to enter into the server console
server_eval() {
server_property "$1" USERNAME
server_property "$1" SCREEN_NAME
as_user "${SERVER_USERNAME[$1]}" "screen -p 0 -S ${SERVER_SCREEN_NAME[$1]} -X eval 'stuff \"$2\"\015'"
}
# The same as server_eval, but also waits for a log entry before returning
# $1: The ID of the server
# $2: A line of text to enter into the server console
# $3: The regex that matches log lines
# $4: A timeout in seconds
# RETURN: The full entry found in the logs
server_eval_and_get_line() {
unset RETURN
time_now="$(now)"
server_eval "$1" "$2"
server_log_get_line "$1" "$time_now" "$3" "$4"
RETURN="$RETURN"
}
# The same as server_eval_and_get_line, but does not set RETURN
server_eval_and_wait() {
server_eval_and_get_line "$@"
unset RETURN # Do not return anything
}
# Executes a "version correct" command in a server's console.
# If the command has output to watch for, then wait until that
# output is found and return it, or until the timeout for that
# command
# $1: The ID of the server
# $2: The name of the command
# $3->: Command arguments in the form "argname=argvalue"
# $RETURN: The output found, if any
server_command() {
unset RETURN
# Load variables
eval server_property $1 CONSOLE_COMMAND_OUTPUT_$2
eval server_property $1 CONSOLE_COMMAND_PATTERN_$2
eval server_property $1 CONSOLE_COMMAND_TIMEOUT_$2
eval local output_regex=\"\${SERVER_CONSOLE_COMMAND_OUTPUT_$2[$1]}\"
eval local pattern=\"\${SERVER_CONSOLE_COMMAND_PATTERN_$2[$1]}\"
# Replace arguments in pattern
for arg in "${@:3}"; do
if [[ "$arg" =~ (.*)=(.*) ]]; then
pattern="${pattern//<${BASH_REMATCH[1]}>/${BASH_REMATCH[2]}}"
output_regex="${output_regex//<${BASH_REMATCH[1]}>/${BASH_REMATCH[2]}}"
fi
done
# If there is no output to watch for, execute the command immediately
# and return immediately
if [ -z "$output_regex" ]; then
server_eval "$1" "$pattern"
unset RETURN
else
# Otherwise execute the command and wait for the specified output
# or the timeout
eval local timeout=\"\${SERVER_CONSOLE_COMMAND_TIMEOUT_$2[$1]}\"
server_eval_and_get_line "$1" "$pattern" "$output_regex" "$timeout"
RETURN="$RETURN"
fi
}
# Gets the process ID for a server if running, otherwise it outputs nothing
# $1: The ID of the server
server_pid() {
server_property "$1" SCREEN_NAME
server_property "$1" INVOCATION
ps ax | grep -v grep | grep "${SERVER_SCREEN_NAME[$1]} ${SERVER_INVOCATION[$1]}" | awk '{print $1}'
}
# Waits for a server to stop by polling 10 times a second
# This approach is fairyl intensive, so only use when you are expecting the
# server to stop soon
# $1: The ID of the server to wait for
server_wait_for_stop() {
local pid="$(server_pid "$1")"
# if the process is still running, wait for it to stop
if [ ! -z "$pid" ]; then
while ps -p "$pid" > /dev/null; do
sleep 0.1
done
fi
}
# Sets a server's active/inactive state
# $1: The ID of the server
# $2: A string containing "active" or "inactive"
server_set_active() {
server_property "$1" USERNAME
server_property "$1" FLAG_ACTIVE_PATH
case "$2" in
active)
as_user "${SERVER_USERNAME[$1]}" "touch \"${SERVER_FLAG_ACTIVE_PATH[$1]}\""
SERVER_ACTIVE[$1]="true"
;;
inactive)
as_user "${SERVER_USERNAME[$1]}" "rm -f \"${SERVER_FLAG_ACTIVE_PATH[$1]}\""
SERVER_ACTIVE[$1]="false"
;;
*)
error_exit INVALID_ARGUMENT "Invalid argument."
;;
esac
}