-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathark_docker_manager.sh
1389 lines (1215 loc) · 47.4 KB
/
ark_docker_manager.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
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
export LC_ALL=C.UTF-8
export LANG=C.UTF-8
export LANGUAGE=C.UTF-8
# Color definitions
RED='\e[31m'
GREEN='\e[32m'
YELLOW='\e[33m'
BLUE='\e[34m'
MAGENTA='\e[35m'
CYAN='\e[36m'
RESET='\e[0m'
# Docker-Image-Name
IMAGE_NAME="ark-ascended-base"
# Base directory for all instances
BASE_DIR="$(cd "$(dirname "$(realpath "$0")")" && pwd)"
BINARIES_DIR="$BASE_DIR/server-files"
INSTANCES_DIR="$BASE_DIR/instances"
RCON_SCRIPT="$BASE_DIR/rcon.py"
ARK_RESTART_MANAGER="$BASE_DIR/ark_restart_manager.sh"
ARK_INSTANCE_MANAGER="$BASE_DIR/ark_docker_manager.sh"
# Function to check if required scripts are executable
check_executables() {
local required_files=("$RCON_SCRIPT" "$ARK_RESTART_MANAGER" "$ARK_INSTANCE_MANAGER")
for file in "${required_files[@]}"; do
if [ ! -x "$file" ]; then
echo -e "${RED}Error: Required file '$file' is not executable.${RESET}"
echo -e "${CYAN}Run 'chmod +x $file' to fix this issue.${RESET}"
exit 1
fi
done
}
# Check if Docker works properly for the script
check_docker() {
# Check if Docker is installed
if ! command -v docker &>/dev/null; then
echo -e "${RED}Error: Docker is not installed or not in PATH.${RESET}"
echo -e "${YELLOW}Please install Docker using your package manager.${RESET}"
exit 1
fi
# Check if the Docker daemon is running
if ! systemctl is-active --quiet docker; then
echo -e "${RED}Error: Docker daemon is not running.${RESET}"
echo -e "${YELLOW}Start it with:${RESET} ${GREEN}sudo systemctl start docker${RESET}"
echo -e "${YELLOW}Or enable and start it with:${RESET} ${GREEN}sudo systemctl enable --now docker${RESET}"
exit 1
fi
# Check if the user has access to Docker
if ! docker info &>/dev/null; then
if id -nG "$USER" | grep -qw docker; then
echo -e "${RED}You are in the 'docker' group, but your session does not reflect the updated group membership.${RESET}"
echo -e "${YELLOW}Run:${RESET} ${GREEN}newgrp docker${RESET} ${YELLOW}or log out and back in.${RESET}"
else
echo -e "${RED}Your user is not in the 'docker' group.${RESET}"
echo -e "${YELLOW}Add your user to the 'docker' group with:${RESET}"
echo -e "${GREEN}sudo usermod -aG docker $USER${RESET}"
echo -e "${YELLOW}Then log out and back in, or run:${RESET} ${GREEN}newgrp docker${RESET}"
fi
exit 1
fi
echo -e "${GREEN}Docker is installed, running, and accessible.${RESET}"
}
# Call the functions at the start of the script
check_executables
check_docker
#Sets up a symlink
setup_symlink() {
# Target directory for the symlink
local target_dir="$HOME/.local/bin"
# Name under which the script can be invoked
local script_name="asa-manager"
# Check if the target directory exists
if [ ! -d "$target_dir" ]; then
echo -e "Creating directory $target_dir..."
mkdir -p "$target_dir" || {
echo -e "Error: Could not create directory $target_dir."
exit 1
}
fi
# Create or update the symlink
echo -e "Creating or updating the symlink $target_dir/$script_name..."
ln -sf "$(realpath "$0")" "$target_dir/$script_name" || {
echo -e "Error: Could not create symlink."
exit 1
}
# Check if $HOME/.local/bin is in the PATH
if [[ ":$PATH:" != *":$target_dir:"* ]]; then
echo -e "Adding $target_dir to PATH..."
echo 'export PATH=$PATH:$HOME/.local/bin' >> "$HOME/.bashrc"
echo "The change will take effect after restarting the shell or running 'source ~/.bashrc'."
fi
echo -e "Setup completed. You can now run the script using 'asa-manager'."
}
# This function searches all instance_config.ini files in the $INSTANCES_DIR folder
# and collects the ports into arrays
check_for_duplicate_ports() {
declare -A port_occurrences
declare -A rcon_occurrences
declare -A query_occurrences
local duplicates_found=false
# Iterate over all instance folders
for instance_dir in "$INSTANCES_DIR"/*; do
if [ -d "$instance_dir" ]; then
local config_file="$instance_dir/instance_config.ini"
if [ -f "$config_file" ]; then
local instance_name
instance_name=$(basename "$instance_dir")
# Extract ports from the config
local game_port rcon_port query_port
game_port=$(grep -E "^Port=" "$config_file" | cut -d= -f2- | xargs)
rcon_port=$(grep -E "^RCONPort=" "$config_file" | cut -d= -f2- | xargs)
query_port=$(grep -E "^QueryPort=" "$config_file" | cut -d= -f2- | xargs)
# Ignore entries if they are empty
[ -z "$game_port" ] && game_port="NULL"
[ -z "$rcon_port" ] && rcon_port="NULL"
[ -z "$query_port" ] && query_port="NULL"
# Check for conflicts
if [ "$game_port" != "NULL" ]; then
if [ -n "${port_occurrences[$game_port]}" ]; then
echo -e "${RED}Conflict: Game port $game_port is used by both '${port_occurrences[$game_port]}' and '$instance_name'.${RESET}"
duplicates_found=true
else
port_occurrences[$game_port]="$instance_name"
fi
fi
if [ "$rcon_port" != "NULL" ]; then
if [ -n "${rcon_occurrences[$rcon_port]}" ]; then
echo -e "${RED}Conflict: RCON port $rcon_port is used by both '${rcon_occurrences[$rcon_port]}' and '$instance_name'.${RESET}"
duplicates_found=true
else
rcon_occurrences[$rcon_port]="$instance_name"
fi
fi
if [ "$query_port" != "NULL" ]; then
if [ -n "${query_occurrences[$query_port]}" ]; then
echo -e "${RED}Conflict: Query port $query_port is used by both '${query_occurrences[$query_port]}' and '$instance_name'.${RESET}"
duplicates_found=true
else
query_occurrences[$query_port]="$instance_name"
fi
fi
fi
fi
done
if [ "$duplicates_found" = true ]; then
echo -e "${RED}Port duplicates were found. Please correct the ports in the instance_config.ini files.${RESET}"
return 1
else
echo -e "${GREEN}No duplicate ports found.${RESET}"
return 0
fi
}
ensure_base_image() {
setup_host_directory
echo -e "${YELLOW}=== Building base image: $IMAGE_NAME ===${RESET}"
docker build -t "$IMAGE_NAME" "$BASE_DIR"
echo -e "${GREEN}Done!${RESET}"
}
# Function to check if a container is running
is_server_running() {
local instance="$1"
# Check if an instance was provided
if [[ -z "$instance" ]]; then
echo "Error: No instance specified." >&2
return 1
fi
# Check if a container with the instance name is running
if docker ps --filter "name=${instance}" --format "{{.Names}}" | grep -qw "${instance}"; then
return 0
else
return 1
fi
}
# Function to populate an array with available instances from INSTANCES_DIR
get_available_instances() {
# Clear the array to avoid stale entries
available_instances=()
if [ -d "$INSTANCES_DIR" ]; then
# Read all directories (one per line) into the array
mapfile -t available_instances < <(ls -1 "$INSTANCES_DIR" 2>/dev/null)
fi
}
# Function to list all instances
list_instances() {
# Reuse the helper function
get_available_instances
if [ ${#available_instances[@]} -eq 0 ]; then
echo -e "${RED}No instances found in '$INSTANCES_DIR'.${RESET}"
return
fi
echo -e "${YELLOW}Available instances:${RESET}"
for inst in "${available_instances[@]}"; do
echo "$inst"
done
}
# Function to create or edit instance configuration
edit_instance_config() {
local instance=$1
local config_file="$INSTANCES_DIR/$instance/instance_config.ini"
local game_ini_file="$INSTANCES_DIR/$instance/Config/Game.ini"
# Create instance directory if it doesn't exist
if [ ! -d "$INSTANCES_DIR/$instance" ]; then
mkdir -p "$INSTANCES_DIR/$instance"
fi
# Create the Config directory if it doesn't exist
if [ ! -d "$INSTANCES_DIR/$instance/Config" ]; then
mkdir -p "$INSTANCES_DIR/$instance/Config"
fi
# Create config file if it doesn't exist
if [ ! -f "$config_file" ]; then
cat <<EOF > "$config_file"
[ServerSettings]
ServerName=ARK Server $instance
ServerPassword=
ServerAdminPassword=adminpassword
MaxPlayers=70
MapName=TheIsland_WP
RCONPort=27020
QueryPort=27015
Port=7777
ModIDs=
CustomStartParameters=-NoBattlEye -crossplay -NoHangDetection
#When changing SaveDir, make sure to give it a unique name, as this can otherwise affect the stop server function.
#Do not use umlauts, spaces, or special characters.
SaveDir=$instance
ClusterID=
EOF
chmod 600 "$config_file" # Set file permissions to be owner-readable and writable
fi
# Create an empty Game.ini, if it doesnt exist
if [ ! -f "$game_ini_file" ]; then
touch "$game_ini_file"
echo -e "${GREEN}Empty Game.ini for '$instance' Created. Optional: Edit it for your needs${RESET}"
fi
# Open the config file in the default text editor
if [ -n "$EDITOR" ]; then
"$EDITOR" "$config_file"
elif command -v nano >/dev/null 2>&1; then
nano "$config_file"
elif command -v vim >/dev/null 2>&1; then
vim "$config_file"
else
echo -e "${RED}No suitable text editor found. Please edit $config_file manually.${RESET}"
fi
}
# Function to load instance configuration
load_instance_config() {
local instance=$1
local config_file="$INSTANCES_DIR/$instance/instance_config.ini"
if [ ! -f "$config_file" ]; then
echo -e "${RED}Configuration file for instance $instance not found.${RESET}"
return 1
fi
# Read configuration into variables
SERVER_NAME=$(grep -E '^ServerName=' "$config_file" | cut -d= -f2- | xargs)
SERVER_PASSWORD=$(grep -E '^ServerPassword=' "$config_file" | cut -d= -f2- | xargs)
ADMIN_PASSWORD=$(grep -E '^ServerAdminPassword=' "$config_file" | cut -d= -f2- | xargs)
MAX_PLAYERS=$(grep -E '^MaxPlayers=' "$config_file" | cut -d= -f2- | xargs)
MAP_NAME=$(grep -E '^MapName=' "$config_file" | cut -d= -f2- | xargs)
RCON_PORT=$(grep -E '^RCONPort=' "$config_file" | cut -d= -f2- | xargs)
QUERY_PORT=$(grep -E '^QueryPort=' "$config_file" | cut -d= -f2- | xargs)
GAME_PORT=$(grep -E '^Port=' "$config_file" | cut -d= -f2- | xargs)
MOD_IDS=$(grep -E '^ModIDs=' "$config_file" | cut -d= -f2- | xargs)
SAVE_DIR=$(grep -E '^SaveDir=' "$config_file" | cut -d= -f2- | xargs)
CLUSTER_ID=$(grep -E '^ClusterID=' "$config_file" | cut -d= -f2- | xargs)
CUSTOM_START_PARAMETERS=$(grep -E '^CustomStartParameters=' "$config_file" | cut -d= -f2- | xargs)
return 0
}
# Function to create a new instance (using 'read' with validation)
create_instance() {
while true; do
echo -e "${CYAN}Enter the name for the new instance (or type 'cancel' to abort):${RESET}"
read -r instance_name
if [ "$instance_name" = "cancel" ]; then
echo -e "${YELLOW}Instance creation cancelled.${RESET}"
return
elif [ -z "$instance_name" ]; then
echo -e "${RED}Instance name cannot be empty.${RESET}"
elif [ -d "$INSTANCES_DIR/$instance_name" ]; then
echo -e "${RED}Instance already exists.${RESET}"
else
mkdir -p "$INSTANCES_DIR/$instance_name"
edit_instance_config "$instance_name"
initialize_proton_prefix "$instance_name"
echo -e "${GREEN}Instance $instance_name created and configured.${RESET}"
return
fi
done
}
# Function to select an instance using 'select'
select_instance() {
local instances=()
local i=1
# Populate the instances array
for dir in "$INSTANCES_DIR"/*; do
if [ -d "$dir" ]; then
instances+=("$(basename "$dir")")
fi
done
if [ ${#instances[@]} -eq 0 ]; then
echo -e "${RED}No instances found.${RESET}"
return 1
fi
echo -e "${YELLOW}Available instances:${RESET}"
PS3="Please select an instance: "
select selected_instance in "${instances[@]}" "Cancel"; do
if [ "$REPLY" -gt 0 ] && [ "$REPLY" -le "${#instances[@]}" ]; then
echo -e "${GREEN}You have selected: $selected_instance${RESET}"
return 0
elif [ "$REPLY" -eq $((${#instances[@]} + 1)) ]; then
echo -e "${YELLOW}Operation cancelled.${RESET}"
return 1
else
echo -e "${RED}Invalid selection.${RESET}"
fi
done
}
# Function to start the server
start_server() {
local instance=$1
local inst_dir="$INSTANCES_DIR/$instance"
local config_dir="$inst_dir/Config"
local container_name="ark_${instance}"
setup_host_directory
# Check for duplicate ports
if ! check_for_duplicate_ports; then
echo -e "${YELLOW}Port conflicts detected. Server start aborted.${RESET}"
return 1
fi
# Remove existing container with the same name if it exists
if docker ps -a --filter "name=${container_name}" --format "{{.Names}}" | grep -q "^${container_name}$"; then
echo -e "${YELLOW}A container with the name '${container_name}' already exists. Removing it...${RESET}"
docker rm -f "$container_name" || {
echo -e "${RED}Failed to remove existing container. Aborting server start.${RESET}"
return 1
}
fi
if is_server_running "ark_$instance"; then
echo -e "${YELLOW}Server for instance $instance is already running.${RESET}"
return 0
fi
load_instance_config "$instance" || return 1
echo -e "${CYAN}Starting server for instance: $instance${RESET}"
# Set cluster parameters if ClusterID is set
local cluster_params=""
if [ -n "$CLUSTER_ID" ]; then
local cluster_dir="$BASE_DIR/clusters/$CLUSTER_ID"
mkdir -p "$cluster_dir" || true
cluster_params="-ClusterDirOverride=\"$cluster_dir\" -ClusterId=\"$CLUSTER_ID\""
fi
# # Ensure configuration files exists
mkdir -p "$config_dir"
touch "$config_dir/Game.ini" "$config_dir/GameUserSettings.ini"
# Ensure configuration directory exists and contains necessary files
if [ ! -f "$config_dir/Game.ini" ] || [ ! -f "$config_dir/GameUserSettings.ini" ]; then
echo -e "${RED}Configuration files are missing for instance: $instance${RESET}"
echo -e "${YELLOW}Expected directory: $config_dir${RESET}"
return 1
fi
# Start the server using Docker
# Adding a trailing space to the ServerName to avoid conflicts if the ServerName is identical to the instance name.
# This ensures the server processes the name correctly, even though the space is invisible to users.
docker run -d \
--env UMASK=0007 \
--name "$container_name" \
-p "${GAME_PORT}:${GAME_PORT}/udp" \
-p "${QUERY_PORT}:${QUERY_PORT}/udp" \
-p "${RCON_PORT}:${RCON_PORT}/tcp" \
-v "$BINARIES_DIR:/ark/binaries" \
-v "$inst_dir:/ark/instance" \
-v "$config_dir/Game.ini:/ark/binaries/ShooterGame/Saved/Config/WindowsServer/Game.ini:rw" \
-v "$config_dir/GameUserSettings.ini:/ark/binaries/ShooterGame/Saved/Config/WindowsServer/GameUserSettings.ini:rw" \
"$IMAGE_NAME" run \
"$MAP_NAME?listen?SessionName=$SERVER_NAME ?ServerPassword=$SERVER_PASSWORD?RCONEnabled=True?ServerAdminPassword=$ADMIN_PASSWORD?AltSaveDirectoryName=$SAVE_DIR" \
$CUSTOM_START_PARAMETERS \
-WinLiveMaxPlayers=$MAX_PLAYERS \
-Port=$GAME_PORT \
-QueryPort=$QUERY_PORT \
-RCONPort=$RCON_PORT \
-game \
$cluster_params \
-server \
-log \
-mods="$MOD_IDS"
if [ $? -eq 0 ]; then
echo -e "${GREEN}Server started for instance: $instance. It should be fully operational in approximately 60 seconds.${RESET}"
else
echo -e "${RED}Failed to start the server for instance: $instance.${RESET}"
return 1
fi
}
# Function to stop the server
stop_server() {
local instance="$1"
if ! is_server_running "ark_$instance"; then
echo -e "${YELLOW}Server for instance $instance is not running.${RESET}"
return 0
fi
echo -e "${GREEN}Stopping instance '$instance' ...${RESET}"
send_rcon_command ${instance} "SaveWorld"
docker stop "ark_${instance}" 2>/dev/null || true
docker rm "ark_${instance}" 2>/dev/null || true
}
# Function to start RCON CLI
start_rcon_cli() {
local instance=$1
if ! is_server_running "ark_$instance"; then
echo -e "${YELLOW}Server for instance $instance is not running.${RESET}"
return 0
fi
load_instance_config "$instance" || return 1
echo -e "${CYAN}Starting RCON CLI for instance: $instance${RESET}"
# Use the new RCON-Client
docker exec -it "ark_$instance" python3 /opt/rcon/rcon.py \
"localhost:$RCON_PORT" \
-p "$ADMIN_PASSWORD" || {
echo -e "${RED}Failed to start RCON CLI for instance $instance.${RESET}"
return 1
}
return 0
}
# Function to change map
change_map() {
local instance=$1
load_instance_config "$instance" || return 1
echo -e "${CYAN}Current map: $MAP_NAME${RESET}"
echo -e "${CYAN}Enter the new map name (or type 'cancel' to abort):${RESET}"
read -r new_map_name
if [[ "$new_map_name" == "cancel" ]]; then
echo -e "${YELLOW}Map change aborted.${RESET}"
return 0
fi
sed -i "s/MapName=.*/MapName=$new_map_name/" "$INSTANCES_DIR/$instance/instance_config.ini"
echo -e "${GREEN}Map changed to $new_map_name. Restart the server for changes to take effect.${RESET}"
}
# Function to change mods
change_mods() {
local instance=$1
load_instance_config "$instance" || return 1
echo -e "${CYAN}Current mods: $MOD_IDS${RESET}"
echo -e "${CYAN}Enter the new mod IDs (comma-separated, or type 'cancel' to abort):${RESET}"
read -r new_mod_ids
if [[ "$new_mod_ids" == "cancel" ]]; then
echo -e "${YELLOW}Mod change aborted.${RESET}"
return 0
fi
sed -i "s/ModIDs=.*/ModIDs=$new_mod_ids/" "$INSTANCES_DIR/$instance/instance_config.ini"
echo -e "${GREEN}Mods changed to $new_mod_ids. Restart the server for changes to take effect.${RESET}"
}
# Function to check server status
check_server_status() {
local instance="$1"
# Check if an instance was provided
if [[ -z "$instance" ]]; then
echo -e "${RED}Error: No instance specified.${RESET}"
return 1
fi
# Load instance configuration
if ! load_instance_config "$instance"; then
echo -e "${RED}Error loading configuration for instance $instance.${RESET}"
return 1
fi
# Check Docker status
if docker ps --filter "name=ark_${instance}" --format "{{.Names}}" | grep -qw "ark_${instance}"; then
echo -e "${GREEN}Server for instance $instance is running.${RESET}"
else
echo -e "${RED}Server for instance $instance is not running.${RESET}"
fi
}
# Function to start all instances with a delay between each
start_all_instances() {
echo -e "${CYAN}Starting all server instances...${RESET}"
for instance in "$INSTANCES_DIR"/*; do
if [ -d "$instance" ]; then
instance_name=$(basename "$instance")
# Check if the server is already running
if is_server_running "ark_$instance_name"; then
echo -e "${YELLOW}Instance $instance_name is already running. Skipping...${RESET}"
continue
fi
# Attempt to start the server
if start_server "$instance_name"; then
# Only wait if the server started successfully
echo -e "${YELLOW}Waiting 5 seconds before starting the next instance...${RESET}"
sleep 5
else
echo -e "${RED}Server $instance_name could not be started due to conflicts or errors. Skipping wait time.${RESET}"
fi
fi
done
echo -e "${GREEN}All instances have been processed.${RESET}"
}
# Function to stop all instances
stop_all_instances() {
echo -e "${CYAN}Stopping all server instances...${RESET}"
for instance in "$INSTANCES_DIR"/*; do
if [ -d "$instance" ]; then
instance_name=$(basename "$instance")
if ! is_server_running "ark_$instance_name"; then
echo -e "${YELLOW}Instance $instance_name is not running. Skipping...${RESET}"
continue
fi
stop_server "$instance_name"
fi
done
echo -e "${GREEN}All instances have been stopped.${RESET}"
}
# Function to send RCON command
send_rcon_command() {
local instance=$1
local command=$2
if ! is_server_running "ark_$instance"; then
echo -e "${YELLOW}Server for instance $instance is not running. Cannot send RCON command.${RESET}"
return 1
fi
load_instance_config "$instance" || return 1
# Always use the silent mode of the RCON client
local response
response=$(docker exec -i "ark_$instance" python3 /opt/rcon/rcon.py \
"localhost:$RCON_PORT" \
-p "$ADMIN_PASSWORD" \
-c "$command" \
--silent 2>&1)
# Check if the RCON command was successful
if [ $? -ne 0 ]; then
echo -e "${RED}Failed to send RCON command to instance $instance.${RESET}"
return 1
fi
# Return the RCON response
echo "$response"
return 0
}
# Function to show running instances
show_running_instances() {
echo -e "${CYAN}Running instances (containers):${RESET}"
docker ps --filter "name=ark_" --format "{{.Names}}"
}
# Function to delete an instance
delete_instance() {
local instance="$1"
# Select instance if none was provided
if [[ -z "$instance" ]]; then
if ! select_instance; then
return
fi
instance="$selected_instance"
fi
# Check if the instance exists
if [[ -d "$INSTANCES_DIR/$instance" ]]; then
echo -e "${RED}Warning: This will permanently delete the instance '$instance' and all its data.${RESET}"
echo "Type CONFIRM to delete the instance '$instance', or cancel to abort"
read -p "> " response
if [[ $response == "CONFIRM" ]]; then
# Verify if the Docker container is running, and stop it
if docker ps --filter "name=ark_${instance}" --format "{{.Names}}" | grep -qw "ark_${instance}"; then
echo -e "${CYAN}Stopping instance '$instance'...${RESET}"
stop_server "$instance"
fi
# Remove the Docker container
if docker ps -a --filter "name=ark_${instance}" --format "{{.Names}}" | grep -qw "ark_${instance}"; then
echo -e "${CYAN}Removing Docker container for instance '$instance'...${RESET}"
docker rm "ark_${instance}" > /dev/null 2>&1 || echo -e "${RED}Failed to remove Docker container.${RESET}"
fi
# Delete the instance directory
echo -e "${CYAN}Deleting instance directory for '$instance'...${RESET}"
rm -rf "$INSTANCES_DIR/$instance" || echo -e "${RED}Failed to delete instance directory.${RESET}"
rm -rf "$BINARIES_DIR/ShooterGame/Saved/$instance" || true
rm -rf "$BINARIES_DIR/ShooterGame/Saved/SavedArks/$instance" || true
echo -e "${GREEN}Instance '$instance' has been deleted.${RESET}"
elif [[ $response == "cancel" ]]; then
echo -e "${YELLOW}Deletion cancelled.${RESET}"
else
echo -e "${YELLOW}Invalid response. Deletion cancelled.${RESET}"
fi
else
echo -e "${RED}Instance '$instance' does not exist.${RESET}"
fi
}
# Function to change instance name
change_instance_name() {
local instance=$1
load_instance_config "$instance" || return 1
echo -e "${CYAN}Enter the new name for instance '$instance' (or type 'cancel' to abort):${RESET}"
read -r new_instance_name
# Validation
if [ "$new_instance_name" = "cancel" ]; then
echo -e "${YELLOW}Instance renaming cancelled.${RESET}"
return
elif [ -z "$new_instance_name" ]; then
echo -e "${RED}Instance name cannot be empty.${RESET}"
return 1
elif [ -d "$INSTANCES_DIR/$new_instance_name" ]; then
echo -e "${RED}An instance with the name '$new_instance_name' already exists.${RESET}"
return 1
fi
# Stop the server if running
if is_server_running "ark_$instance"; then
echo -e "${CYAN}Stopping running server for instance '$instance' before renaming...${RESET}"
stop_server "$instance"
fi
# Rename instance directory
mv "$INSTANCES_DIR/$instance" "$INSTANCES_DIR/$new_instance_name" || {
echo -e "${RED}Failed to rename instance directory.${RESET}"
return 1
}
# Rename save directories if they exist
if [ -d "$BINARIES_DIR/ShooterGame/Saved/$instance" ]; then
mv "$BINARIES_DIR/ShooterGame/Saved/$instance" "$BINARIES_DIR/ShooterGame/Saved/$new_instance_name" || true
fi
if [ -d "$BINARIES_DIR/ShooterGame/Saved/SavedArks/$instance" ]; then
mv "$BINARIES_DIR/ShooterGame/Saved/SavedArks/$instance" "$BINARIES_DIR/ShooterGame/Saved/SavedArks/$new_instance_name" || true
fi
# Update SaveDir in the instance configuration
sed -i "s/^SaveDir=.*/SaveDir=$new_instance_name/" "$INSTANCES_DIR/$new_instance_name/instance_config.ini"
echo -e "${GREEN}Instance renamed from '$instance' to '$new_instance_name'.${RESET}"
}
# Function to edit GameUserSettins.ini
edit_gameusersettings() {
local instance=$1
local file_path="$INSTANCES_DIR/$instance/Config/GameUserSettings.ini"
#Check if server is running
if is_server_running "ark_$instance"; then
echo -e "${YELLOW}Server for instance $instance is running. Stop it to edit config${RESET}"
return 0
fi
if [ ! -f "$file_path" ]; then
echo -e "${RED}Error: No GameUserSettings.ini found. Start the server once to generate one or place your own in the instances/$instance/Config folder.${RESET}"
return
fi
select_editor "$file_path"
}
# Function to edit Game.ini
edit_game_ini() {
local instance=$1
local file_path="$INSTANCES_DIR/$instance/Config/Game.ini"
#Check if server is running
if is_server_running "ark_$instance"; then
echo -e "${YELLOW}Server for instance $instance is running. Stop it to edit config${RESET}"
return 0
fi
if [ ! -f "$file_path" ]; then
echo -e "${YELLOW}Game.ini not found for instance '$instance'. Creating a new one.${RESET}"
touch "$file_path"
fi
select_editor "$file_path"
}
# MENU ENTRY: Create a backup of an existing world
menu_backup_world() {
echo -e "${CYAN}Please select an instance to create a backup from:${RESET}"
if select_instance; then
backup_instance_world "$selected_instance"
fi
}
# MENU ENTRY: Restore an existing backup into an instance
menu_restore_world() {
echo -e "${CYAN}Please select the target instance to restore the backup to:${RESET}"
if select_instance; then
restore_backup_to_instance "$selected_instance"
fi
}
#Save a world's backup from an instance
backup_instance_world() {
local instance=$1
# Check if the server is running
if is_server_running "ark_$instance"; then
echo -e "${RED}The server for instance '$instance' is running. Stop it before creating a backup.${RESET}"
return 0
fi
# -- List all world folders in $BINARIES_DIR/ShooterGame/Saved/$instance --
local worlds=()
local instance_dir="$BINARIES_DIR/ShooterGame/Saved/$instance"
if [ ! -d "$instance_dir" ]; then
echo -e "${RED}Instance directory '$instance_dir' not found.${RESET}"
return 1
fi
# Collect folders typical for ARK worlds (e.g., TheIsland_WP, Ragnarok_WP, etc.)
for d in "$instance_dir"/*; do
[ -d "$d" ] && worlds+=("$(basename "$d")")
done
if [ ${#worlds[@]} -eq 0 ]; then
echo -e "${RED}No worlds found to backup (${instance_dir} is empty).${RESET}"
return 1
fi
echo -e "${CYAN}Select a world to back up:${RESET}"
PS3="Selection: "
select world_folder in "${worlds[@]}" "Cancel"; do
if [ "$REPLY" -gt 0 ] && [ "$REPLY" -le "${#worlds[@]}" ]; then
echo -e "${CYAN}Creating backup for world: $world_folder ...${RESET}"
elif [ "$REPLY" -eq $((${#worlds[@]} + 1)) ]; then
echo -e "${YELLOW}Operation canceled.${RESET}"
return 0
else
echo -e "${RED}Invalid selection.${RESET}"
continue
fi
# Create backup directory
local backups_dir="$BASE_DIR/backups"
mkdir -p "$backups_dir"
local timestamp=$(date +"%Y-%m-%d_%H-%M-%S")
local archive_name="${instance}_${world_folder}_${timestamp}.tar.gz"
local archive_path="$backups_dir/$archive_name"
tar -czf "$archive_path" -C "$instance_dir" "$world_folder"
if [ $? -eq 0 ]; then
echo -e "${GREEN}Backup successfully created: $archive_path${RESET}"
else
echo -e "${RED}Error creating the backup.${RESET}"
fi
break
done
}
#Load an existing backup (from the backups folder) into a target instance
restore_backup_to_instance() {
local target_instance=$1
# Check if the server is running
if is_server_running "ark_$target_instance"; then
echo -e "${RED}The server for instance '$target_instance' is running. Stop it before restoring a backup.${RESET}"
return 1
fi
local backups_dir="$BASE_DIR/backups"
set +e
if [ ! -d "$backups_dir" ]; then
echo -e "${RED}Backup directory '$backups_dir' does not exist.${RESET}"
return 1
fi
set -e
# Gather all *.tar.gz files in $backups_dir
local backup_files=()
while IFS= read -r -d $'\0' file; do
backup_files+=("$file")
done < <(find "$backups_dir" -maxdepth 1 -type f -name "*.tar.gz" -print0 | sort -z)
if [ ${#backup_files[@]} -eq 0 ]; then
echo -e "${RED}No backups found in '$backups_dir'.${RESET}"
return 1
fi
echo -e "${CYAN}Select a backup to load into instance '$target_instance':${RESET}"
PS3="Selection: "
select chosen_backup in "${backup_files[@]}" "Cancel"; do
if [ "$REPLY" -gt 0 ] && [ "$REPLY" -le "${#backup_files[@]}" ]; then
local backup_file="$chosen_backup"
echo -e "${CYAN}Selected backup: $backup_file${RESET}"
elif [ "$REPLY" -eq $((${#backup_files[@]} + 1)) ]; then
echo -e "${YELLOW}Operation canceled.${RESET}"
return 0
else
echo -e "${RED}Invalid selection.${RESET}"
continue
fi
# WARNING about overwriting
echo -e "${RED}WARNING: Restoring this backup may overwrite existing worlds.${RESET}"
echo -e "Type '${YELLOW}CONFIRM${RESET}' to proceed, or '${YELLOW}cancel${RESET}' to abort:"
read -r user_input
if [ "$user_input" != "CONFIRM" ]; then
echo -e "${YELLOW}Operation canceled.${RESET}"
return 0
fi
# Extract the backup into $BINARIES_DIR/ShooterGame/Saved/$target_instance/
mkdir -p "$BINARIES_DIR/ShooterGame/Saved/$target_instance"
echo -e "${CYAN}Extracting backup...${RESET}"
tar -xzf "$backup_file" -C "$BINARIES_DIR/ShooterGame/Saved/$target_instance/"
if [ $? -eq 0 ]; then
echo -e "${GREEN}Backup successfully loaded into instance '$target_instance'.${RESET}"
else
echo -e "${RED}Error extracting the backup.${RESET}"
fi
break
done
}
##Save a world's backup from an instance via CLI
backup_instance_world_cli() {
local instance=$1
local world_folder=$2
# Check if the server is running
if is_server_running "ark_$instance"; then
echo -e "${RED}The server for instance '$instance' is running. Please stop it first.${RESET}"
return 1
fi
local instance_dir="$BINARIES_DIR/ShooterGame/Saved/$instance"
if [ ! -d "$instance_dir" ]; then
echo -e "${RED}Instance directory '$instance_dir' not found.${RESET}"
return 1
fi
local src_path="$instance_dir/$world_folder"
if [ ! -d "$src_path" ]; then
echo -e "${RED}World folder '$world_folder' does not exist (${src_path}).${RESET}"
return 1
fi
local backups_dir="$BASE_DIR/backups"
mkdir -p "$backups_dir"
local timestamp=$(date +"%Y-%m-%d_%H-%M-%S")
local archive_name="${instance}_${world_folder}_${timestamp}.tar.gz"
local archive_path="$backups_dir/$archive_name"
echo -e "${CYAN}Creating backup for '$world_folder' in instance '$instance'...${RESET}"
tar -czf "$archive_path" -C "$instance_dir" "$world_folder"
if [ $? -eq 0 ]; then
echo -e "${GREEN}Backup successfully created: $archive_path${RESET}"
else
echo -e "${RED}Error creating the backup.${RESET}"
return 1
fi
}
#Function to select editor and open a file in editor
select_editor() {
local file_path="$1"
# Open the file in the default text editor
if [ -n "$EDITOR" ]; then
"$EDITOR" "$file_path"
elif command -v nano >/dev/null 2>&1; then
nano "$file_path"
elif command -v vim >/dev/null 2>&1; then
vim "$file_path"
else
echo -e "${RED}No suitable text editor found. Please edit $file_path manually.${RESET}"
fi
}
# Menu to edit configuration files
edit_configuration_menu() {
local instance=$1
echo -e "${CYAN}Choose configuration to edit:${RESET}"
options=(
"Instance Configuration"
"GameUserSettings.ini"
"Game.ini"
"Back"
)
PS3="Please select an option: "
select opt in "${options[@]}"; do
case "$REPLY" in
1)
edit_instance_config "$instance"
break
;;
2)
edit_gameusersettings "$instance"
break
;;
3)
edit_game_ini "$instance"
break
;;
4)
return
;;
*)
echo -e "${RED}Invalid option selected.${RESET}"
;;
esac
done
}
# Function to configure the restart_manager.sh
configure_companion_script() {
local companion_script="$BASE_DIR/ark_restart_manager.sh"
if [ ! -f "$companion_script" ]; then
echo -e "${RED}Error: Companion script not found at '$companion_script'.${RESET}"
return 1
fi
echo -e "${CYAN}-- Restart Manager Configuration --${RESET}"
# 1) Dynamically get all available instances
get_available_instances
if [ ${#available_instances[@]} -eq 0 ]; then
echo -e "${RED}No instances found in '$INSTANCES_DIR'. Returning to main menu.${RESET}"
return 0
fi