forked from MinecraftServerControl/mscs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
msctl
executable file
·2536 lines (2428 loc) · 87.2 KB
/
msctl
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/sh
# ---------------------------------------------------------------------------
# Copyright (c) 2011-2016, Jason M. Wood <sandain@hotmail.com>
#
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are met:
#
# 1. Redistributions of source code must retain the above copyright notice,
# this list of conditions and the following disclaimer.
# 2. Redistributions in binary form must reproduce the above copyright
# notice, this list of conditions and the following disclaimer in the
# documentation and/or other materials provided with the distribution.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
# ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
# LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
# POSSIBILITY OF SUCH DAMAGE.
# ---------------------------------------------------------------------------
# ---------------------------------------------------------------------------
# Minecraft Server Control Script
#
# A powerful command-line control script for Linux-powered Minecraft servers.
# ---------------------------------------------------------------------------
# Get executable name
# ---------------------------------------------------------------------------
PROG=$(basename $0)
# Required Software
# ---------------------------------------------------------------------------
# Detect its presence and location for later.
JAVA=$(which java)
PERL=$(which perl)
PYTHON=$(which python)
WGET=$(which wget)
RDIFF_BACKUP=$(which rdiff-backup)
SOCAT=$(which socat)
# Script Usage
# ---------------------------------------------------------------------------
usage() {
cat <<EOF
Usage: $PROG [<options>] <action>
Actions:
start <world>
Start the Minecraft world server. Start all world servers by default.
stop <world>
Stop the Minecraft world server. Stop all world servers by default.
force-stop <world>
Forcibly stop the Minecraft world server. Forcibly stop all world
servers by default.
restart <world>
Restart the Minecraft world server. Restart all world servers by default.
force-restart <world>
Forcibly restart the Minecraft world server. Forcibly restart all world
servers by default.
create <world> <port> [<ip>]
Create a Minecraft world server. The world name and port must be
provided, the IP address is usually blank. Without arguments, create a
a default world at the default port.
delete <world>
Delete a Minecraft world server.
disable <world>
Temporarily disable a world server.
enable <world>
Enable a disabled world server.
ls <option>
Display a list of worlds.
Options:
enabled Display a list of enabled worlds, default.
disabled Display a list of disabled worlds.
running Display a list of running worlds.
stopped Display a list of stopped worlds.
If no option, all available worlds are listed.
list <option>
Same as 'ls' but more detailed.
status <world>
Display the status of the Minecraft world server. Display the status of
all world servers by default.
sync <world>
Synchronize the data stored in the mirror images of the Minecraft world
server. Synchronizes all of the world servers by default. This option
is only available when the mirror image option is enabled.
broadcast <command>
Broadcast a command to all running Minecraft world servers.
send <world> <command>
Send a command to a Minecraft world server.
console <world>
Connect to the Minecraft world server's console. Hit <Ctrl-D> to detach.
watch <world>
Watch the log file for the Minecraft world server.
logrotate <world>
Rotate the log file for the Minecraft world. Rotate the log file for
all worlds by default.
backup <world>
Backup the Minecraft world. Backup all worlds by default.
list-backups <world>
List the datetime of the backups for the world.
restore-backup <world> <datetime>
Restore a backup for a world that was taken at the datetime.
map <world>
Run the Minecraft Overviewer mapping software on the Minecraft world.
Map all worlds by default.
update <world>
Update the server software for the Minecraft world server. Update
server software for all worlds by default.
query <world>
Run a detailed Query on the Minecraft world server.
Options:
-c <config_file>
Read configuration from <config_files> instead of default locations.
-l <location>
Uses <location> as the base path for data. Overrides configuration file
options.
EOF
}
# ---------------------------------------------------------------------------
# Internal Methods
# ---------------------------------------------------------------------------
#
# NOTE: Nothing below this point should need to be edited directly.
# ---------------------------------------------------------------------------
# ---------------------------------------------------------------------------
# Get the PID of the Java process for the world server.
#
# @param 1 The world server of interest.
# @return The Java PID.
# ---------------------------------------------------------------------------
getJavaPID() {
local PID MCUSER
MCUSER=$(whoami)
PID=$(
ps -a -U $MCUSER -o pid,comm,args -ww |
$PERL -ne 'if ($_ =~ /^\s*(\d+)\s+java.+mscs-world='$1'$/) { print $1; }'
)
printf "%d\n" $PID
}
# ---------------------------------------------------------------------------
# Get the amount of memory used by the Java process for the world server.
#
# @param 1 The world server of interest.
# @return The amount of memory used.
# ---------------------------------------------------------------------------
getJavaMemory() {
local PID
PID=$(getJavaPID "$1")
ps -h -p $PID -o rss
}
# ---------------------------------------------------------------------------
# Check to see if the world server is running.
#
# @param 1 The world server of interest.
# @return A 0 if the server is thought to be running, a 1 otherwise.
# ---------------------------------------------------------------------------
serverRunning() {
# Try to determine if the world is running.
if [ $(getJavaPID "$1") -gt 0 ]; then
return 0
else
return 1
fi
}
# ---------------------------------------------------------------------------
# Send a command to the world server.
#
# @param 1 The world server of interest.
# @param 2 The command to send.
# ---------------------------------------------------------------------------
sendCommand() {
echo "$2" | $PERL -e '
while (<>) { $_ =~ s/[\r\n]+//g; $cmd .= $_; } print "$cmd\r";
' >> $WORLDS_LOCATION/$1/console.in
}
# ---------------------------------------------------------------------------
# Check whether the item is in the list.
#
# @param 1 The item being searched for.
# @param 2 The list being searched.
# @return A 0 if the list contains the item, a 1 otherwise.
# ---------------------------------------------------------------------------
listContains() {
local MATCH ITEM
MATCH=1
for ITEM in $2; do
if [ "$ITEM" = "$1" ]; then
MATCH=0
fi
done
return $MATCH
}
# ---------------------------------------------------------------------------
# Compare two datetime strings.
#
# @param 1 The first datetime string.
# @param 2 The second datetime string.
# @return The result of the comparison: -1, 0, 1.
# ---------------------------------------------------------------------------
compareTime() {
local T1 T2
T1=$(date --date="$1" +%s)
T2=$(date --date="$2" +%s)
echo $(($T1 < $T2 ? -1 : $T1 > $T2 ? 1 : 0))
}
# ---------------------------------------------------------------------------
# Compare two Minecraft version numbers.
#
# @param 1 The first Minecraft version number.
# @param 2 The second Minecraft version number.
# @return The result of the comparison: -1, 0, 1.
# ---------------------------------------------------------------------------
compareMinecraftVersions() {
local T1 T2
T1=$(getMinecraftVersionReleaseTime "$1")
T2=$(getMinecraftVersionReleaseTime "$2")
compareTime "$T1" "$T2"
}
# ---------------------------------------------------------------------------
# Create a world.
#
# @param 1 The world server to create.
# @param 2 The port of the world server.
# @param 3 The IP address of the world server.
# ---------------------------------------------------------------------------
createWorld() {
# Create a basic server.properties file. Values not supplied here
# will use default values when the world server is first started.
mkdir -p "$WORLDS_LOCATION/$1"
setServerPropertiesValue "$1" "level-name" "$1"
setServerPropertiesValue "$1" "server-port" "$2"
setServerPropertiesValue "$1" "server-ip" "$3"
setServerPropertiesValue "$1" "enable-query" "true"
setServerPropertiesValue "$1" "query.port" "$2"
setMSCSValue "$1" "mscs-enabled" "true"
}
# ---------------------------------------------------------------------------
# Delete a world.
#
# @param 1 The world server to delete.
# ---------------------------------------------------------------------------
deleteWorld() {
# Delete the world directory.
rm -Rf "$WORLDS_LOCATION/$1"
}
# ---------------------------------------------------------------------------
# Disable a world.
#
# @param 1 The world server to disable.
# ---------------------------------------------------------------------------
disableWorld() {
# Disable the world.
setMSCSValue "$1" "mscs-enabled" "false"
}
# ---------------------------------------------------------------------------
# Enable a world.
#
# @param 1 The world server to enable.
# ---------------------------------------------------------------------------
enableWorld() {
# Enable the world.
setMSCSValue "$1" "mscs-enabled" "true"
}
# ---------------------------------------------------------------------------
# Grab the list of enabled worlds.
#
# @return The list of enabled worlds.
# ---------------------------------------------------------------------------
getEnabledWorlds() {
local WORLD WORLDS
mkdir -p "$WORLDS_LOCATION"
WORLDS=""
for WORLD in $(ls $WORLDS_LOCATION); do
if [ -d $WORLDS_LOCATION/$WORLD ]; then
if [ "$(getMSCSValue $WORLD 'mscs-enabled' 'true')" = "true" ]; then
WORLDS="$WORLDS $WORLD"
fi
fi
done
echo $WORLDS
}
# ---------------------------------------------------------------------------
# Grab the list of disabled worlds.
#
# @return The list of disabled worlds.
# ---------------------------------------------------------------------------
getDisabledWorlds() {
local WORLD WORLDS
WORLDS=""
for WORLD in $(ls $WORLDS_LOCATION); do
if [ -d $WORLDS_LOCATION/$WORLD ]; then
if [ "$(getMSCSValue $WORLD 'mscs-enabled' 'true')" != "true" ]; then
WORLDS="$WORLDS $WORLD"
fi
fi
done
echo $WORLDS
}
# ---------------------------------------------------------------------------
# Grab the list of all available worlds.
#
# @return The list of all available worlds.
# ---------------------------------------------------------------------------
getAvailableWorlds() {
echo $(getEnabledWorlds) $(getDisabledWorlds)
}
# ---------------------------------------------------------------------------
# Check to see if the world is enabled.
#
# @param 1 The world of interest.
# @return A 0 if the world is enabled, a 1 otherwise.
# ---------------------------------------------------------------------------
isWorldEnabled() {
local WORLDS
WORLDS=$(getEnabledWorlds)
if [ -n "$1" ] && listContains "$1" "$WORLDS"; then
return 0
else
return 1
fi
}
# ---------------------------------------------------------------------------
# Check to see if the world is available (exists).
#
# @param 1 The world of interest.
# @return A 0 if the world is available, a 1 otherwise.
# ---------------------------------------------------------------------------
isWorldAvailable() {
local WORLDS
WORLDS=$(getAvailableWorlds)
if [ -n "$1" ] && listContains "$1" "$WORLDS"; then
return 0
else
return 1
fi
}
# ---------------------------------------------------------------------------
# Get the value of a key from the provided file.
#
# @param 1 The file containing the key/value combo.
# @param 2 The key to get.
# @param 3 The default value.
# ---------------------------------------------------------------------------
getValue() {
local KEY VALUE
# Make sure the file exists.
if [ -e "$1" ]; then
# Find the key/value combo.
KEY=$($PERL -ne '
# Remove single and double quotes plus CR and LF.
$_ =~ s/[\x22\x27\r\n]//g;
# Remove comments.
$_ =~ s/^\s*\x23.*//;
# Extract the key.
if ($_ =~ /^\s*('$2')\s*=\s*.*$/i) { print lc $1; }
' $1)
VALUE=$($PERL -ne '
# Remove single and double quotes plus CR and LF.
$_ =~ s/[\x22\x27\r\n]//g;
# Remove comments.
$_ =~ s/^\s*\x23.*//;
# Extract the value.
if ($_ =~ /^\s*'$2'\s*=\s*(.*)$/i) { print $1; }
' $1)
fi
# Return the value if found, the default value if not.
if [ -n "$KEY" ] && [ -n "$VALUE" ]; then
echo "$VALUE"
else
echo "$3"
fi
}
# ---------------------------------------------------------------------------
# Modify the value of a key/value combo in the provided file.
#
# @param 1 The file containing the key/value combo.
# @param 2 The key to modify.
# @param 3 The value to assign to the key.
# ---------------------------------------------------------------------------
setValue() {
local KEY_VALUE
# Make sure that the file exists.
touch "$1"
# Replace the key/value combo if it already exists, otherwise just
# append it to the end of the file.
KEY_VALUE=$($PERL -ne '
$_ =~ s/[\r]//;
if ($_ =~ /^('$2'=.*)$/) { print "$1"; }
' $1)
if [ -n "$KEY_VALUE" ]; then
$PERL -i -ne '
$_ =~ s/[\r]//;
if ($_ =~ /^'$2'=.*$/) { print "'$2'='$3'\n"; } else { print; }
' $1
else
printf "$2=$3\n" >> "$1"
fi
}
# ---------------------------------------------------------------------------
# Get the value of a key in the mscs.defaults file.
#
# @param 1 The key to get.
# @param 2 The default value.
# ---------------------------------------------------------------------------
getDefaultsValue() {
getValue "$MSCS_DEFAULTS" "$1" "$2"
}
# ---------------------------------------------------------------------------
# Get the value of the EULA boolean.
#
# @param 1 The world server of interest.
# ---------------------------------------------------------------------------
getEULAValue() {
local EULA_FILE
EULA_FILE=$WORLDS_LOCATION/$1/eula.txt
getValue "$EULA_FILE" "eula" "true" | $PERL -ne 'print lc'
}
# ---------------------------------------------------------------------------
# Get the value of a key in a mscs.properties file.
#
# @param 1 The world server of interest.
# @param 2 The key to get.
# @param 3 The default value.
# ---------------------------------------------------------------------------
getMSCSValue() {
local PROPERTY_FILE
PROPERTY_FILE=$WORLDS_LOCATION/$1/mscs.properties
getValue "$PROPERTY_FILE" "$2" "$3"
}
# ---------------------------------------------------------------------------
# Modify the value of a key/value combo in a mscs.properties file.
#
# @param 1 The world server of interest.
# @param 2 The key to modify.
# @param 3 The value to assign to the key.
# ---------------------------------------------------------------------------
setMSCSValue() {
local PROPERTY_FILE
PROPERTY_FILE=$WORLDS_LOCATION/$1/mscs.properties
setValue "$PROPERTY_FILE" "$2" "$3"
}
# ---------------------------------------------------------------------------
# Get the value of a key in a server.properties file.
#
# @param 1 The world server of interest.
# @param 2 The key to get.
# @param 3 The default value.
# ---------------------------------------------------------------------------
getServerPropertiesValue() {
local PROPERTY_FILE
PROPERTY_FILE="$WORLDS_LOCATION/$1/server.properties"
getValue "$PROPERTY_FILE" "$2" "$3"
}
# ---------------------------------------------------------------------------
# Modify the value of a key/value combo in a server.properties file.
#
# @param 1 The world server of interest.
# @param 2 The key to modify.
# @param 3 The value to assign to the key.
# ---------------------------------------------------------------------------
setServerPropertiesValue() {
local PROPERTY_FILE
PROPERTY_FILE=$WORLDS_LOCATION/$1/server.properties
setValue "$PROPERTY_FILE" "$2" "$3"
}
# ---------------------------------------------------------------------------
# Get the current Minecraft version number.
#
# @param 1 The world server.
# @return The current Minecraft version number.
# ---------------------------------------------------------------------------
getCurrentMinecraftVersion() {
local VERSION TYPE
# Make sure the server software directory exists.
mkdir -p "$DEFAULT_SERVER_LOCATION"
# Determine the version type for the current world.
TYPE=$(getMSCSValue "$1" "mscs-version-type" "$DEFAULT_VERSION_TYPE")
if [ -s $VERSIONS_JSON ]; then
# Make a backup copy of the version_manifest.json file.
cp -p "$VERSIONS_JSON" "$VERSIONS_JSON.bak"
# Delete the version_manifest.json file if it is old.
find "$VERSIONS_JSON" -mmin +"$VERSIONS_DURATION" -delete
fi
# Download the version_manifest.json file if it does not exist.
if [ ! -s $VERSIONS_JSON ]; then
$WGET --no-use-server-timestamps -qO "$VERSIONS_JSON" "$MINECRAFT_VERSIONS_URL"
if [ $? -ne 0 ]; then
if [ -s $VERSIONS_JSON.bak ]; then
printf "Error downloading the Minecraft version_manifest.json file, using an old copy.\n"
cp -p "$VERSIONS_JSON.bak" "$VERSIONS_JSON"
else
printf "Error downloading the Minecraft version_manifest.json file.\n"
exit 1
fi
fi
fi
# Extract the current version information.
VERSION=$($PERL -0777ne '
use JSON;
$json = decode_json ($_);
print $json->{latest}{'$TYPE'};
' $VERSIONS_JSON)
# Print an error and exit if the version string is empty.
if [ -z "$VERSION" ]; then
printf "Error detecting the current Minecraft version.\n"
exit 1
fi
echo "$VERSION"
}
# ---------------------------------------------------------------------------
# Get the release time of the Minecraft version number.
#
# @param 1 The Minecraft version number.
# @return The release time of the Minecraft version number.
# ---------------------------------------------------------------------------
getMinecraftVersionReleaseTime() {
$PERL -0777ne '
use JSON;
$json = decode_json ($_);
foreach $ver (@{$json->{versions}}) {
print $ver->{releaseTime} if ($ver->{id} eq "'$1'");
}
' $VERSIONS_JSON
}
# ---------------------------------------------------------------------------
# Retrieve the version of the client for the world.
#
# @param 1 The world server.
# @return CLIENT_VERSION
# ---------------------------------------------------------------------------
getClientVersion() {
local CURRENT_VERSION
CURRENT_VERSION=$(getCurrentMinecraftVersion "$1")
if [ $? -ne 0 ]; then
printf "$CURRENT_VERSION\n"
exit 1
fi
# Get the client version, use the default version if not provided.
getMSCSValue "$1" "mscs-client-version" "$DEFAULT_CLIENT_VERSION" |
$PERL -ne '
$current_version="'$CURRENT_VERSION'";
$_ =~ s/\$CURRENT_VERSION/$current_version/g;
print;
'
}
# ---------------------------------------------------------------------------
# Retrieve the .jar file name for the client for the world.
#
# @param 1 The world server.
# @return CLIENT_JAR
# ---------------------------------------------------------------------------
getClientJar() {
local CURRENT_VERSION CLIENT_VERSION
CURRENT_VERSION=$(getCurrentMinecraftVersion "$1")
if [ $? -ne 0 ]; then
printf "$CURRENT_VERSION\n"
exit 1
fi
CLIENT_VERSION=$(getClientVersion "$1")
if [ $? -ne 0 ]; then
printf "$CLIENT_VERSION\n"
exit 1
fi
# Get the client jar, use the default value if not provided.
getMSCSValue "$1" "mscs-client-jar" "$DEFAULT_CLIENT_JAR" |
$PERL -ne '
$current_version="'$CURRENT_VERSION'";
$client_version="'$CLIENT_VERSION'";
$_ =~ s/\$CURRENT_VERSION/$current_version/g;
$_ =~ s/\$CLIENT_VERSION/$client_version/g;
print;
'
}
# ---------------------------------------------------------------------------
# Retrieve the location of the client files for the world.
#
# @param 1 The world server.
# @return CLIENT_LOCATION
# ---------------------------------------------------------------------------
getClientLocation() {
local CURRENT_VERSION CLIENT_VERSION
CURRENT_VERSION=$(getCurrentMinecraftVersion "$1")
if [ $? -ne 0 ]; then
printf "$CURRENT_VERSION\n"
exit 1
fi
CLIENT_VERSION=$(getClientVersion "$1")
if [ $? -ne 0 ]; then
printf "$CLIENT_VERSION\n"
exit 1
fi
# Get the client location, use the default value if not provided.
getMSCSValue "$1" "mscs-client-location" "$DEFAULT_CLIENT_LOCATION" |
$PERL -ne '
$current_version="'$CURRENT_VERSION'";
$client_version="'$CLIENT_VERSION'";
$_ =~ s/\$CURRENT_VERSION/$current_version/g;
$_ =~ s/\$CLIENT_VERSION/$client_version/g;
print;
'
}
# ---------------------------------------------------------------------------
# Retrieve the URL to download the client for the world.
#
# @param 1 The world server.
# @return CLIENT_URL
# ---------------------------------------------------------------------------
getClientURL() {
local CURRENT_VERSION CLIENT_VERSION
CURRENT_VERSION=$(getCurrentMinecraftVersion "$1")
if [ $? -ne 0 ]; then
printf "$CURRENT_VERSION\n"
exit 1
fi
CLIENT_VERSION=$(getClientVersion "$1")
if [ $? -ne 0 ]; then
printf "$CLIENT_VERSION\n"
exit 1
fi
# Get the client download URL, use the default value if not provided.
getMSCSValue "$1" "mscs-client-url" "$DEFAULT_CLIENT_URL" |
$PERL -ne '
$current_version="'$CURRENT_VERSION'";
$client_version="'$CLIENT_VERSION'";
$_ =~ s/\$CURRENT_VERSION/$current_version/g;
$_ =~ s/\$CLIENT_VERSION/$client_version/g;
$_ =~ s/\\\:/\:/g;
print;
'
}
# ---------------------------------------------------------------------------
# Retrieve the version of the server running the world.
#
# @param 1 The world server.
# @return SERVER_VERSION
# ---------------------------------------------------------------------------
getServerVersion() {
local CURRENT_VERSION
CURRENT_VERSION=$(getCurrentMinecraftVersion "$1")
if [ $? -ne 0 ]; then
printf "$CURRENT_VERSION\n"
exit 1
fi
# Get the server version, use the default version if not provided.
getMSCSValue "$1" "mscs-server-version" "$DEFAULT_SERVER_VERSION" |
$PERL -ne '
$current_version="'$CURRENT_VERSION'";
$_ =~ s/\$CURRENT_VERSION/$current_version/g;
print;
'
}
# ---------------------------------------------------------------------------
# Retrieve the .jar file name for the server running the world.
#
# @param 1 The world server.
# @return SERVER_JAR
# ---------------------------------------------------------------------------
getServerJar() {
local CURRENT_VERSION SERVER_VERSION SERVER_JAR
CURRENT_VERSION=$(getCurrentMinecraftVersion "$1")
if [ $? -ne 0 ]; then
printf "$CURRENT_VERSION\n"
exit 1
fi
SERVER_VERSION=$(getServerVersion "$1")
if [ $? -ne 0 ]; then
printf "$SERVER_VERSION\n"
exit 1
fi
# Get the server jar, use the default value if not provided.
getMSCSValue "$1" "mscs-server-jar" "$DEFAULT_SERVER_JAR" |
$PERL -ne '
$current_version="'$CURRENT_VERSION'";
$server_version="'$SERVER_VERSION'";
$_ =~ s/\$CURRENT_VERSION/$current_version/g;
$_ =~ s/\$SERVER_VERSION/$server_version/g;
print;
'
}
# ---------------------------------------------------------------------------
# Retrieve the location of the server files for the server running the world.
#
# @param 1 The world server.
# @return SERVER_LOCATION
# ---------------------------------------------------------------------------
getServerLocation() {
local CURRENT_VERSION SERVER_VERSION
CURRENT_VERSION=$(getCurrentMinecraftVersion "$1")
if [ $? -ne 0 ]; then
printf "$CURRENT_VERSION\n"
exit 1
fi
SERVER_VERSION=$(getServerVersion "$1")
if [ $? -ne 0 ]; then
printf "$SERVER_VERSION\n"
exit 1
fi
# Get the server location, use the default value if not provided.
getMSCSValue "$1" "mscs-server-location" "$DEFAULT_SERVER_LOCATION" |
$PERL -ne '
$current_version="'$CURRENT_VERSION'";
$server_version="'$SERVER_VERSION'";
$_ =~ s/\$CURRENT_VERSION/$current_version/g;
$_ =~ s/\$SERVER_VERSION/$server_version/g;
print;
'
}
# ---------------------------------------------------------------------------
# Retrieve the URL to download the server running the world.
#
# @param 1 The world server.
# @return SERVER_URL
# ---------------------------------------------------------------------------
getServerURL() {
local CURRENT_VERSION SERVER_VERSION
CURRENT_VERSION=$(getCurrentMinecraftVersion "$1")
if [ $? -ne 0 ]; then
printf "$CURRENT_VERSION\n"
exit 1
fi
SERVER_VERSION=$(getServerVersion "$1")
if [ $? -ne 0 ]; then
printf "$SERVER_VERSION\n"
exit 1
fi
# Get the server download URL, use the default value if not provided.
getMSCSValue "$1" "mscs-server-url" "$DEFAULT_SERVER_URL" |
$PERL -ne '
$current_version="'$CURRENT_VERSION'";
$server_version="'$SERVER_VERSION'";
$_ =~ s/\$CURRENT_VERSION/$current_version/g;
$_ =~ s/\$SERVER_VERSION/$server_version/g;
$_ =~ s/\\\:/\:/g;
print;
'
}
# ---------------------------------------------------------------------------
# Retrieve the command to start the server running the world.
#
# @param 1 The world server.
# @return SERVER_COMMAND
# ---------------------------------------------------------------------------
getServerCommand() {
local CURRENT_VERSION SERVER_VERSION SERVER_JAR SERVER_LOCATION
local SERVER_ARGS INITIAL_MEMORY MAXIMUM_MEMORY
CURRENT_VERSION=$(getCurrentMinecraftVersion "$1")
if [ $? -ne 0 ]; then
printf "$CURRENT_VERSION\n"
exit 1
fi
SERVER_VERSION=$(getServerVersion "$1")
if [ $? -ne 0 ]; then
printf "$SERVER_VERSION\n"
exit 1
fi
SERVER_JAR=$(getServerJar "$1")
if [ $? -ne 0 ]; then
printf "$SERVER_JAR\n"
exit 1
fi
SERVER_LOCATION=$(getServerLocation "$1")
if [ $? -ne 0 ]; then
printf "$SERVER_LOCATION\n"
exit 1
fi
# Get the server arguments, use the default value if not provided.
SERVER_ARGS=$(
getMSCSValue "$1" "mscs-server-args" "$DEFAULT_SERVER_ARGS"
)
# Get the initial memory, use the default value if not provided.
INITIAL_MEMORY=$(
getMSCSValue "$1" "mscs-initial-memory" "$DEFAULT_INITIAL_MEMORY"
)
# Get the maximum memory, use the default value if not provided.
MAXIMUM_MEMORY=$(
getMSCSValue "$1" "mscs-maximum-memory" "$DEFAULT_MAXIMUM_MEMORY"
)
# Get the server command, use the default value if not provided.
getMSCSValue "$1" "mscs-server-command" "$DEFAULT_SERVER_COMMAND" |
$PERL -ne '
$java = "'$JAVA'";
$current_version="'$CURRENT_VERSION'";
$server_version="'$SERVER_VERSION'";
$server_jar = "'$SERVER_JAR'";
$server_location = "'$SERVER_LOCATION'";
$server_args = "'$SERVER_ARGS'";
$initial_memory = "'$INITIAL_MEMORY'";
$maximum_memory = "'$MAXIMUM_MEMORY'";
$_ =~ s/\$JAVA/$java/g;
$_ =~ s/\$CURRENT_VERSION/$current_version/g;
$_ =~ s/\$SERVER_VERSION/$server_version/g;
$_ =~ s/\$SERVER_JAR/$server_jar/g;
$_ =~ s/\$SERVER_LOCATION/$server_location/g;
$_ =~ s/\$SERVER_ARGS/$server_args/g;
$_ =~ s/\$INITIAL_MEMORY/$initial_memory/g;
$_ =~ s/\$MAXIMUM_MEMORY/$maximum_memory/g;
print;
'
}
# ---------------------------------------------------------------------------
# Create a lock file for a world. This will help avoid having multiple long
# running processes running at the same time. This code was inspired by
# http://bencane.com/2015/09/22/preventing-duplicate-cron-job-executions/
#
# @param 1 The world server.
# @param 2 The type of lock file.
# @return TRUE if lock file created, FALSE otherwise.
# ---------------------------------------------------------------------------
createLockFile() {
local PID LOCKFILE
LOCKFILE=$WORLDS_LOCATION/$1/lock-$2.pid
# Delete the old LOCKFILE.
find $LOCKFILE -mmin +"$LOCKFILE_DURATION" -delete > /dev/null 2>&1
# Check to see if the LOCKFILE exists.
if [ -s $LOCKFILE ]; then
PID=$(cat $LOCKFILE)
# LOCKFILE exists, check to see if its process is running.
ps -p $PID > /dev/null 2>&1
if [ $? -eq 0 ]; then
# LOCKFILE exists and the process is running, return FALSE.
return 1
else
# Process not found assume it is not running.
echo $$ > $LOCKFILE
if [ $? -ne 0 ]; then
# Error creating LOCKFILE, return FALSE.
return 1
fi
fi
else
# LOCKFILE does not exists.
echo $$ > $LOCKFILE
if [ $? -ne 0 ]; then
# Error creating LOCKFILE, return FALSE.
return 1
fi
fi
# Success creating LOCKFILE, return TRUE.
return 0
}
# ---------------------------------------------------------------------------
# Remove a lock file for a world.
#
# @param 1 The world server.
# @param 2 The type of lock file.
# ---------------------------------------------------------------------------
removeLockFile() {
rm -f $WORLDS_LOCATION/$1/lock-$2.pid
}
# ---------------------------------------------------------------------------
# Remove old world log files and rotate log files from old servers (v < 1.7).
#
# @param 1 The world server generating the log to rotate.
# ---------------------------------------------------------------------------
rotateLog() {
local WORLD_DIR DATE LOG NUMBER
WORLD_DIR="$WORLDS_LOCATION/$1"
# Make sure the log directory exists.
mkdir -p "$WORLD_DIR/logs"
# Delete old log files.
find "$WORLD_DIR/logs" -type f -mtime +"$LOG_DURATION" -delete
# Archive and rotate the log files for old Minecraft servers (Versions 1.7
# and greater do this automatically).
if [ -e "$WORLD_DIR/server.log" ] &&
[ $(wc -l < $WORLD_DIR/server.log) -ge 1 ]; then
# Make a copy of the log file in the world's logs directory.
DATE=$(date +%F)
cp "$WORLD_DIR/server.log" "$WORLD_DIR/logs/$DATE-0.log"
gzip "$WORLD_DIR/logs/$DATE-0.log"
# Empty the contents of the worlds log file.
cp /dev/null "$WORLD_DIR/server.log"
# Rotate the files in the world's logs directory.
for LOG in $(ls -r $WORLD_DIR/logs/$DATE-*.log.gz); do
NUMBER=$(echo $LOG | cut -d "." -f 1 | cut -d "-" -f 4)
NUMBER=$(($NUMBER+1))
mv -f $LOG "$WORLD_DIR/logs/$DATE-$NUMBER.log.gz"
done
fi
}
# ---------------------------------------------------------------------------
# Watch the world latest.log file.
#
# @param 1 The world server generating the log to watch.
# ---------------------------------------------------------------------------
watchLog() {
local WORLD_DIR
WORLD_DIR="$WORLDS_LOCATION/$1"
# Make sure that the latest.log file exists.
if [ -e "$WORLD_DIR/logs/latest.log" ]; then
# Watch the log file of worlds running Minecraft 1.7 +.
tail -n0 -f --pid=$(getJavaPID "$1") $WORLD_DIR/logs/latest.log
elif [ -e "$WORLD_DIR/server.log" ]; then
# Watch the log file of worlds running older Minecraft servers.
tail -n0 -f --pid=$(getJavaPID "$1") $WORLD_DIR/server.log
fi
}
# ---------------------------------------------------------------------------
# Synchronizes the data stored in the mirror images.
#
# @param 1 The world server to sync.
# ---------------------------------------------------------------------------
syncMirrorImage() {
# Sync the world server.
cp -Ru "$WORLDS_LOCATION/$1/$1/*" "$WORLDS_LOCATION/$1/$1-original"
if [ $? -ne 0 ]; then
printf "Error synchronizing mirror images for world $1.\n"
exit 1
fi
}
# ---------------------------------------------------------------------------
# Display the server console.
#
# @param 1 The world server to connect to.
# ---------------------------------------------------------------------------
serverConsole() {
local LINE WORLD_DIR
# Make sure that the world's directory exists.
WORLD_DIR="$WORLDS_LOCATION/$1"
# Follow the console output buffer file.
tail -f --pid=$$ $WORLD_DIR/console.out &