-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy patharma3srv-script.bash
More file actions
1921 lines (1709 loc) · 90.4 KB
/
arma3srv-script.bash
File metadata and controls
1921 lines (1709 loc) · 90.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
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
# Copyright (C) 2022 7thCore
# This file is part of Arma3Srv-Script.
#
# Arma3Srv-Script is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# Arma3Srv-Script is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#Arma 3 server script by 7thCore
#If you do not know what any of these settings are you are better off leaving them alone. One thing might brake the other if you fiddle around with it.
#Static script variables
export NAME="Arma3Srv" #Name of the tmux session
export VERSION="1.1-3" #Package and script version
export SERVICE_NAME="arma3srv" #Name of the service files, user, script and script log
export LOG_DIR="/srv/$SERVICE_NAME/logs" #Location of the script's log files.
export LOG_STRUCTURE="$LOG_DIR/$(date +"%Y")/$(date +"%m")/$(date +"%d")" #Folder structure of the script's log files.
export LOG_SCRIPT="$LOG_STRUCTURE/$SERVICE_NAME-script.log" #Script log.
SRV_DIR="$SRV_DIR" #Location of the server located on your hdd/ssd.
CONFIG_DIR="/srv/$SERVICE_NAME/config" #Location of this script's configuration.
UPDATE_DIR="$UPDATE_DIR" #Location of update information for the script's automatic update feature.
BCKP_DIR="/srv/$SERVICE_NAME/backups" #Location of stored backups
BCKP_STRUCTURE="$(date +"%Y")/$(date +"%m")/$(date +"%d")" #How backups are sorted, by default it's sorted in folders by month and day.
#Static game variables
APPID="233780" #Steam app id for the server
WORKSHOP_APPID="107410" #Steam app id for the workshop
#Script config file variables
BCKP_DELOLD=$(cat $CONFIG_DIR/$SERVICE_NAME-script.conf 2> /dev/null | grep script_bckp_delold= | cut -d = -f2) #Delete old backups.
LOG_DELOLD=$(cat $CONFIG_DIR/$SERVICE_NAME-script.conf 2> /dev/null | grep script_log_delold= | cut -d = -f2) #Delete old logs.
UPDATE_IGNORE_FAILED_ACTIVATIONS=$(cat $CONFIG_DIR/$SERVICE_NAME-script.conf 2> /dev/null | grep script_update_ignore_failed_startups= | cut -d = -f2) #Ignore failed startups during update configuration
#Script config variables if config doesn't exist
BCKP_DELOLD=${BCKP_DELOLD:="7"} #If the variable for old backup deletion is not defined, assign a default value.
LOG_DELOLD=${LOG_DELOLD:="7"} #If the variable for old log deletion is not defined, assign a default value.
UPDATE_IGNORE_FAILED_ACTIVATIONS=$(cat $CONFIG_DIR/$SERVICE_NAME-script.conf 2> /dev/null | grep script_update_ignore_failed_startups= | cut -d = -f2) #Defines if errors during startup after updates should be ignored.
#Steamcmd config file variables
STEAMCMD_UID=$(cat $CONFIG_DIR/$SERVICE_NAME-steam.conf 2> /dev/null | grep steamcmd_username= | cut -d = -f2) #Defines your steam username.
STEAMCMD_PSW=$(cat $CONFIG_DIR/$SERVICE_NAME-steam.conf 2> /dev/null | grep steamcmd_password= | cut -d = -f2) #Defines your steam password.
STEAMCMD_BETA_BRANCH=$(cat $CONFIG_DIR/$SERVICE_NAME-steam.conf 2> /dev/null | grep steamcmd_beta_branch= | cut -d = -f2) #Defines if the beta branch is enabled.
STEAMCMD_BETA_BRANCH_NAME=$(cat $CONFIG_DIR/$SERVICE_NAME-steam.conf 2> /dev/null | grep steamcmd_beta_branch_name= | cut -d = -f2) #Defines the beta branch name.
STEAMGUARD_CLI=$(cat $CONFIG_DIR/$SERVICE_NAME-steam.conf 2> /dev/null | grep steamguard_cli= | cut -d = -f2) #Defines usage of steamguard_cli.
#Steamcmd config variables if config doesn't exist
STEAMCMD_UID=${STEAMCMD_UID:="disabled"} #If the variable for steam username is not defined, assign a default value.
STEAMCMD_PSW=${STEAMCMD_PSW:="disabled"} #If the variable for steam password is not defined, assign a default value.
STEAMCMD_BETA_BRANCH=${STEAMCMD_BETA_BRANCH:="0"} #If the variable for steam beta branch selection is not defined, assign a default value.
STEAMCMD_BETA_BRANCH_NAME=${STEAMCMD_BETA_BRANCH_NAME:="none"} #If the variable for steam beta branch name is not defined, assign a default value.
STEAMGUARD_CLI=${STEAMGUARD_CLI:="0"} #If the variable for steam guard is not defined, assign a default value.
#Mod config file variables
MODS_ENABLED=$(cat $SCRIPT_DIR/$SERVICE_NAME-mods.conf 2> /dev/null | grep mods_enabled= | cut -d = -f2) #Are mods enabled?
MODS=$(cat $SCRIPT_DIR/$SERVICE_NAME-mods.conf 2> /dev/null | grep mod_list | cut -d = -f2) #Get mod list
#Mod config variables if config doesn't exist
MODS_ENABLED=${MODS_ENABLED:="0"} #If the variable for steam username is not defined, assign a default value.
MODS=${MODS:="0"} #If the variable for steam username is not defined, assign a default value.
#Discord config file variables
DISCORD_UPDATE=$(cat $CONFIG_DIR/$SERVICE_NAME-discord.conf 2> /dev/null | grep discord_update= | cut -d = -f2) #Send notification when the server updates.
DISCORD_START=$(cat $CONFIG_DIR/$SERVICE_NAME-discord.conf 2> /dev/null | grep discord_start= | cut -d = -f2) #Send notifications when the server starts.
DISCORD_STOP=$(cat $CONFIG_DIR/$SERVICE_NAME-discord.conf 2> /dev/null | grep discord_stop= | cut -d = -f2) #Send notifications when the server stops.
DISCORD_CRASH=$(cat $CONFIG_DIR/$SERVICE_NAME-discord.conf 2> /dev/null | grep discord_crash= | cut -d = -f2) #Send notifications when the server crashes.
DISCORD_TMPFS_SPACE=$(cat $CONFIG_DIR/$SERVICE_NAME-discord.conf 2> /dev/null | grep discord_tmpfs_space= | cut -d = -f2) #Send notifications if tmpfs space is over the designated percentage.
DISCORD_COLOR_PRESTART=$(cat $CONFIG_DIR/$SERVICE_NAME-discord.conf 2> /dev/null | grep discord_color_prestart= | cut -d = -f2) #Discord embed color for prestart.
DISCORD_COLOR_POSTSTART=$(cat $CONFIG_DIR/$SERVICE_NAME-discord.conf 2> /dev/null | grep discord_color_poststart= | cut -d = -f2) #Discord embed color for poststart.
DISCORD_COLOR_PRESTOP=$(cat $CONFIG_DIR/$SERVICE_NAME-discord.conf 2> /dev/null | grep discord_color_prestop= | cut -d = -f2) #Discord embed color for prestop.
DISCORD_COLOR_POSTSTOP=$(cat $CONFIG_DIR/$SERVICE_NAME-discord.conf 2> /dev/null | grep discord_color_poststop= | cut -d = -f2) #Discord embed color for poststop.
DISCORD_COLOR_UPDATE=$(cat $CONFIG_DIR/$SERVICE_NAME-discord.conf 2> /dev/null | grep discord_color_update= | cut -d = -f2) #Discord embed color for update.
DISCORD_COLOR_CRASH=$(cat $CONFIG_DIR/$SERVICE_NAME-discord.conf 2> /dev/null | grep discord_color_crash= | cut -d = -f2) #Discord embed color for crash.
#Discord config variables if config doesn't exist
DISCORD_UPDATE=${DISCORD_UPDATE:="0"} #If the variable for discord update is not defined, assign a default value.
DISCORD_START=${DISCORD_START:="0"} #If the variable for discord start is not defined, assign a default value.
DISCORD_STOP=${DISCORD_STOP:="0"} #If the variable for discord stop is not defined, assign a default value.
DISCORD_CRASH=${DISCORD_CRASH:="0"} #If the variable for discord crash is not defined, assign a default value.
DISCORD_TMPFS_SPACE=${DISCORD_TMPFS_SPACE:="0"} #If the variable for discord tmpfs space is not defined, assign a default value.
DISCORD_COLOR_PRESTART=${DISCORD_COLOR_PRESTART:="16776960"} #If the variable discord pre-start color is not defined, assign a default value.
DISCORD_COLOR_POSTSTART=${DISCORD_COLOR_POSTSTART:="65280"} #If the variable discord post-start color is not defined, assign a default value.
DISCORD_COLOR_PRESTOP=${DISCORD_COLOR_PRESTOP:="16776960"} #If the variable discord pre-stop color is not defined, assign a default value.
DISCORD_COLOR_POSTSTOP=${DISCORD_COLOR_POSTSTOP:="65280"} #If the variable discord post-stop color is not defined, assign a default value.
DISCORD_COLOR_UPDATE=${DISCORD_COLOR_UPDATE:="47083"} #If the variable discord update color is not defined, assign a default value.
DISCORD_COLOR_CRASH=${DISCORD_COLOR_CRASH:="16711680"} #If the variable for discord crash color is not defined, assign a default value.
#Email config file variables
EMAIL_SENDER=$(cat $CONFIG_DIR/$SERVICE_NAME-email.conf 2> /dev/null | grep email_sender= | cut -d = -f2) #Send emails from this address.
EMAIL_RECIPIENT=$(cat $CONFIG_DIR/$SERVICE_NAME-email.conf 2> /dev/null | grep email_recipient= | cut -d = -f2) #Send emails to this address.
EMAIL_UPDATE=$(cat $CONFIG_DIR/$SERVICE_NAME-email.conf 2> /dev/null | grep email_update= | cut -d = -f2) #Send emails when server updates.
EMAIL_START=$(cat $CONFIG_DIR/$SERVICE_NAME-email.conf 2> /dev/null | grep email_start= | cut -d = -f2) #Send emails when the server starts up.
EMAIL_STOP=$(cat $CONFIG_DIR/$SERVICE_NAME-email.conf 2> /dev/null | grep email_stop= | cut -d = -f2) #Send emails when the server shuts down.
EMAIL_CRASH=$(cat $CONFIG_DIR/$SERVICE_NAME-email.conf 2> /dev/null | grep email_crash= | cut -d = -f2) #Send emails when the server crashes.
#Email config variables if config doesn't exist
EMAIL_SENDER=${EMAIL_SENDER:="none"} #If the variable for email sender is not defined, assign a default value.
EMAIL_RECIPIENT=${EMAIL_RECIPIENT:="none"} #If the variable for email recipient is not defined, assign a default value.
EMAIL_UPDATE=${EMAIL_UPDATE:="0"} #If the variable for email update is not defined, assign a default value.
EMAIL_START=${EMAIL_START:="0"} #If the variable for email start is not defined, assign a default value.
EMAIL_STOP=${EMAIL_STOP:="0"} #If the variable for email stop is not defined, assign a default value.
EMAIL_CRASH=${EMAIL_CRASH:="0"} #If the variable for email crash is not defined, assign a default value.
#Console collors
RED='\033[0;31m'
GREEN='\033[0;32m'
CYAN='\033[0;36m'
LIGHTRED='\033[1;31m'
NC='\033[0m'
#-------Do not edit anything beyond this line-------
#Generate log folder structure
script_logs() {
#If there is not a folder for today, create one
if [ ! -d "$LOG_STRUCTURE" ]; then
mkdir -p $LOG_STRUCTURE
fi
}
#---------------------------
#Discord webhook message send
script_discord_message() {
while IFS="" read -r DISCORD_WEBHOOK || [ -n "$DISCORD_WEBHOOK" ]; do
curl -H "Content-Type: application/json" -X POST -d "{\"embeds\": [{ \"author\": { \"name\": \"$NAME Script\", \"url\": \"https://github.com/7thCore/$SERVICE_NAME-script\" }, \"color\": \"$1\", \"description\": \"$2\", \"footer\": {\"text\": \"Version $VERSION\"}, \"timestamp\": \"$(date -u --iso-8601=seconds)\"}] }" "$DISCORD_WEBHOOK"
done < $CONFIG_DIR/discord_webhooks.txt
}
#--------------------------
#Send email message
script_email_message() {
mail -r "$EMAIL_SENDER ($1)" -s "$2" $EMAIL_RECIPIENT <<- EOF
$3
EOF
}
#--------------------------
#Attaches to the server tmux session
script_attach() {
script_logs
tmux -L $SERVICE_NAME-tmux.sock has-session -t $NAME 2>/dev/null
if [ $? == 0 ]; then
echo "$(date +"%Y-%m-%d %H:%M:%S") [$VERSION] [$NAME] (Attach) User attached to server session with ID: $1" | tee -a "$LOG_SCRIPT"
tmux -L $SERVICE_NAME-tmux.sock attach -t $NAME
echo "$(date +"%Y-%m-%d %H:%M:%S") [$VERSION] [$NAME] (Attach) User deattached from server session with ID: $1" | tee -a "$LOG_SCRIPT"
else
echo "$(date +"%Y-%m-%d %H:%M:%S") [$VERSION] [$NAME] (Attach) Failed to attach to server session with ID: $1" | tee -a "$LOG_SCRIPT"
fi
}
#--------------------------
#Deletes old files
script_remove_old_files() {
script_logs
echo "$(date +"%Y-%m-%d %H:%M:%S") [$VERSION] [$NAME] (Remove old files) Beginning removal of old files." | tee -a "$LOG_SCRIPT"
#Delete old logs
echo "$(date +"%Y-%m-%d %H:%M:%S") [$VERSION] [$NAME] (Remove old files) Removing old script logs: $LOG_DELOLD days old." | tee -a "$LOG_SCRIPT"
find $LOG_DIR/* -mtime +$LOG_DELOLD -delete
#Delete empty folders
echo "$(date +"%Y-%m-%d %H:%M:%S") [$VERSION] [$NAME] (Remove old files) Removing empty script log folders." | tee -a "$LOG_SCRIPT"
find $LOG_DIR/ -type d -empty -delete
echo "$(date +"%Y-%m-%d %H:%M:%S") [$VERSION] [$NAME] (Remove old files) Removal of old files complete." | tee -a "$LOG_SCRIPT"
}
#---------------------------
#Prints out if the server is running
script_status() {
script_logs
if [[ "$(systemctl --user show -p ActiveState --value $SERVICE)" == "inactive" ]]; then
echo "$(date +"%Y-%m-%d %H:%M:%S") [$VERSION] [$NAME] (Status) Server is not running." | tee -a "$LOG_SCRIPT"
elif [[ "$(systemctl --user show -p ActiveState --value $SERVICE)" == "active" ]]; then
echo "$(date +"%Y-%m-%d %H:%M:%S") [$VERSION] [$NAME] (Status) Server running." | tee -a "$LOG_SCRIPT"
elif [[ "$(systemctl --user show -p ActiveState --value $SERVICE)" == "failed" ]]; then
echo "$(date +"%Y-%m-%d %H:%M:%S") [$VERSION] [$NAME] (Status) Server is in failed state. Please check logs." | tee -a "$LOG_SCRIPT"
elif [[ "$(systemctl --user show -p ActiveState --value $SERVICE)" == "activating" ]]; then
echo "$(date +"%Y-%m-%d %H:%M:%S") [$VERSION] [$NAME] (Status) Server is activating. Please wait." | tee -a "$LOG_SCRIPT"
elif [[ "$(systemctl --user show -p ActiveState --value $SERVICE)" == "deactivating" ]]; then
echo "$(date +"%Y-%m-%d %H:%M:%S") [$VERSION] [$NAME] (Status) Server is in deactivating. Please wait." | tee -a "$LOG_SCRIPT"
fi
}
#---------------------------
#Disable all script services
script_disable_services() {
script_logs
if [[ "$(systemctl --user show -p UnitFileState --value $SERVICE_NAME.service)" == "enabled" ]]; then
systemctl --user disable $SERVICE_NAME.service
fi
if [[ "$(systemctl --user show -p UnitFileState --value $SERVICE_NAME-timer-1.timer)" == "enabled" ]]; then
systemctl --user disable $SERVICE_NAME-timer-1.timer
fi
if [[ "$(systemctl --user show -p UnitFileState --value $SERVICE_NAME-timer-2.timer)" == "enabled" ]]; then
systemctl --user disable $SERVICE_NAME-timer-2.timer
fi
echo "$(date +"%Y-%m-%d %H:%M:%S") [$VERSION] [$NAME] (Disable services) Services successfully disabled." | tee -a "$LOG_SCRIPT"
}
#---------------------------
#Disables all script services, available to the user
script_disable_services_manual() {
script_logs
echo "$(date +"%Y-%m-%d %H:%M:%S") [$VERSION] [$NAME] (Disable services) WARNING: This will disable all script services. The server will be disabled." | tee -a "$LOG_SCRIPT"
read -p "Are you sure you want to disable all services? (y/n): " DISABLE_SCRIPT_SERVICES
if [[ "$DISABLE_SCRIPT_SERVICES" =~ ^([yY][eE][sS]|[yY])$ ]]; then
script_disable_services
elif [[ "$DISABLE_SCRIPT_SERVICES" =~ ^([nN][oO]|[nN])$ ]]; then
echo "$(date +"%Y-%m-%d %H:%M:%S") [$VERSION] [$NAME] (Disable services) Disable services canceled." | tee -a "$LOG_SCRIPT"
fi
}
#---------------------------
# Enable script services by reading the configuration file
script_enable_services() {
script_logs
if [[ "$(systemctl --user show -p UnitFileState --value $SERVICE_NAME.service)" == "disabled" ]]; then
systemctl --user enable $SERVICE_NAME.service
fi
if [[ "$(systemctl --user show -p UnitFileState --value $SERVICE_NAME-timer-1.timer)" == "disabled" ]]; then
systemctl --user enable $SERVICE_NAME-timer-1.timer
fi
if [[ "$(systemctl --user show -p UnitFileState --value $SERVICE_NAME-timer-2.timer)" == "disabled" ]]; then
systemctl --user enable $SERVICE_NAME-timer-2.timer
fi
echo "$(date +"%Y-%m-%d %H:%M:%S") [$VERSION] [$NAME] (Enable services) Services successfully Enabled." | tee -a "$LOG_SCRIPT"
}
#---------------------------
# Enable script services by reading the configuration file, available to the user
script_enable_services_manual() {
script_logs
echo "$(date +"%Y-%m-%d %H:%M:%S") [$VERSION] [$NAME] (Enable services) This will enable all script services. The server will be enabled." | tee -a "$LOG_SCRIPT"
read -p "Are you sure you want to enable all services? (y/n): " ENABLE_SCRIPT_SERVICES
if [[ "$ENABLE_SCRIPT_SERVICES" =~ ^([yY][eE][sS]|[yY])$ ]]; then
script_enable_services
elif [[ "$ENABLE_SCRIPT_SERVICES" =~ ^([nN][oO]|[nN])$ ]]; then
echo "$(date +"%Y-%m-%d %H:%M:%S") [$VERSION] [$NAME] (Enable services) Enable services canceled." | tee -a "$LOG_SCRIPT"
fi
}
#---------------------------
#Disables all script services an re-enables them by reading the configuration file
script_reload_services() {
script_logs
echo "$(date +"%Y-%m-%d %H:%M:%S") [$VERSION] [$NAME] (Reload services) This will reload all script services." | tee -a "$LOG_SCRIPT"
read -p "Are you sure you want to reload all services? (y/n): " RELOAD_SCRIPT_SERVICES
if [[ "$RELOAD_SCRIPT_SERVICES" =~ ^([yY][eE][sS]|[yY])$ ]]; then
script_disable_services
systemctl --user daemon-reload
script_enable_services
echo "$(date +"%Y-%m-%d %H:%M:%S") [$VERSION] [$NAME] (Reload services) Reload services complete." | tee -a "$LOG_SCRIPT"
elif [[ "$RELOAD_SCRIPT_SERVICES" =~ ^([nN][oO]|[nN])$ ]]; then
echo "$(date +"%Y-%m-%d %H:%M:%S") [$VERSION] [$NAME] (Reload services) Reload services canceled." | tee -a "$LOG_SCRIPT"
fi
}
#---------------------------
#Pre-start functions to be called by the systemd service
script_prestart() {
script_logs
if [[ "$DISCORD_START" == "1" ]]; then
script_discord_message "$DISCORD_COLOR_PRESTART" "Server startup for $1 was initialized."
fi
if [[ "$EMAIL_START" == "1" ]]; then
script_email_message "$NAME-$1" "Notification: Server startup $1" "Server startup for $1 was initialized at $(date +"%d.%m.%Y %H:%M:%S")"
fi
echo "$(date +"%Y-%m-%d %H:%M:%S") [$VERSION] [$NAME] (Start) Server startup was initialized." | tee -a "$LOG_SCRIPT"
}
#---------------------------
#Post-start functions to be called by the systemd service
script_poststart() {
script_logs
if [[ "$DISCORD_START" == "1" ]]; then
script_discord_message "$DISCORD_COLOR_POSTSTART" "Server startup for $1 complete."
fi
if [[ "$EMAIL_START" == "1" ]]; then
script_email_message "$NAME-$1" "Notification: Server startup $1" "Server startup for $1 was completed at $(date +"%d.%m.%Y %H:%M:%S")"
fi
echo "$(date +"%Y-%m-%d %H:%M:%S") [$VERSION] [$NAME] (Start) Server startup complete." | tee -a "$LOG_SCRIPT"
}
#---------------------------
#Pre-stop functions to be called by the systemd service
script_prestop() {
script_logs
if [[ "$DISCORD_STOP" == "1" ]]; then
script_discord_message "$DISCORD_COLOR_PRESTOP" "Server shutdown for $1 was initialized."
fi
if [[ "$EMAIL_STOP" == "1" ]]; then
script_email_message "$NAME-$1" "Notification: Server shutdown $1" "Server shutdown was initiated at $(date +"%d.%m.%Y %H:%M:%S")"
fi
echo "$(date +"%Y-%m-%d %H:%M:%S") [$VERSION] [$NAME] (Stop) Server shutdown was initialized." | tee -a "$LOG_SCRIPT"
}
#---------------------------
#Post-stop functions to be called by the systemd service
script_poststop() {
script_logs
#Check if the server is still running, if it is wait for it to stop.
while true; do
tmux -L $SERVICE_NAME-tmux.sock has-session -t $NAME 2>/dev/null
if [ $? -eq 1 ]; then
break
fi
sleep 1
done
if [ -f "/tmp/$SERVICE_NAME-tmux.log" ]; then
rm /tmp/$SERVICE_NAME-tmux.log
fi
if [ -f "/tmp/$SERVICE_NAME-tmux.conf" ]; then
rm /tmp/$SERVICE_NAME-tmux.conf
fi
if [[ "$DISCORD_STOP" == "1" ]]; then
script_discord_message "$DISCORD_COLOR_POSTSTOP" "Server shutdown for $1 complete."
fi
if [[ "$EMAIL_STOP" == "1" ]]; then
script_email_message "$NAME-$1" "Notification: Server shutdown $1" "Server shutdown was complete at $(date +"%d.%m.%Y %H:%M:%S")"
fi
echo "$(date +"%Y-%m-%d %H:%M:%S") [$VERSION] [$NAME] (Stop) Server shutdown complete." | tee -a "$LOG_SCRIPT"
}
#---------------------------
#Start the server
script_start() {
script_logs
#Loop until the server is active and output the state of it
script_start_loop() {
while [[ "$(systemctl --user show -p ActiveState --value $SERVICE)" == "activating" ]]; do
echo "$(date +"%Y-%m-%d %H:%M:%S") [$VERSION] [$NAME] (Start) Server is activating. Please wait..." | tee -a "$LOG_SCRIPT"
sleep 1
done
if [[ "$(systemctl --user show -p ActiveState --value $SERVICE)" == "active" ]]; then
echo "$(date +"%Y-%m-%d %H:%M:%S") [$VERSION] [$NAME] (Start) Server has been successfully activated." | tee -a "$LOG_SCRIPT"
sleep 1
elif [[ "$(systemctl --user show -p ActiveState --value $SERVICE)" == "failed" ]]; then
echo "$(date +"%Y-%m-%d %H:%M:%S") [$VERSION] [$NAME] (Start) Server failed to activate. See systemctl --user status $SERVICE for details." | tee -a "$LOG_SCRIPT"
sleep 1
fi
}
if [[ "$(systemctl --user show -p ActiveState --value $SERVICE)" == "inactive" ]]; then
echo "$(date +"%Y-%m-%d %H:%M:%S") [$VERSION] [$NAME] (Start) Server start initialized." | tee -a "$LOG_SCRIPT"
systemctl --user start $SERVICE
script_start_loop
elif [[ "$(systemctl --user show -p ActiveState --value $SERVICE)" == "active" ]]; then
echo "$(date +"%Y-%m-%d %H:%M:%S") [$VERSION] [$NAME] (Start) Server is already running." | tee -a "$LOG_SCRIPT"
sleep 1
elif [[ "$(systemctl --user show -p ActiveState --value $SERVICE)" == "failed" ]]; then
echo "$(date +"%Y-%m-%d %H:%M:%S") [$VERSION] [$NAME] (Start) Server is in failed state. See systemctl --user status $SERVICE for details." | tee -a "$LOG_SCRIPT"
if [[ "$1" == "ignore" ]]; then
systemctl --user start $SERVER_SERVICE
script_start_loop $SERVER_SERVICE
else
read -p "Do you still want to start the server? (y/n): " FORCE_START
if [[ "$FORCE_START" =~ ^([yY][eE][sS]|[yY])$ ]]; then
systemctl --user start $SERVER_SERVICE
script_start_loop $SERVER_SERVICE
fi
fi
fi
}
#---------------------------
#Stop the server
script_stop() {
script_logs
if [[ "$(systemctl --user show -p ActiveState --value $SERVICE)" == "inactive" ]]; then
echo "$(date +"%Y-%m-%d %H:%M:%S") [$VERSION] [$NAME] (Stop) Server is not running." | tee -a "$LOG_SCRIPT"
sleep 1
elif [[ "$(systemctl --user show -p ActiveState --value $SERVICE)" == "active" ]]; then
echo "$(date +"%Y-%m-%d %H:%M:%S") [$VERSION] [$NAME] (Stop) Server shutdown in progress." | tee -a "$LOG_SCRIPT"
systemctl --user stop $SERVICE
sleep 1
while [[ "$(systemctl --user show -p ActiveState --value $SERVICE)" == "deactivating" ]]; do
echo "$(date +"%Y-%m-%d %H:%M:%S") [$VERSION] [$NAME] (Stop) Server is deactivating. Please wait..." | tee -a "$LOG_SCRIPT"
sleep 1
done
echo "$(date +"%Y-%m-%d %H:%M:%S") [$VERSION] [$NAME] (Stop) Server is deactivated." | tee -a "$LOG_SCRIPT"
fi
}
#---------------------------
#Restart the server
script_restart() {
script_logs
if [[ "$(systemctl --user show -p ActiveState --value $SERVICE)" == "inactive" ]]; then
echo "$(date +"%Y-%m-%d %H:%M:%S") [$VERSION] [$NAME] (Restart) Server is not running. Use -start to start the server." | tee -a "$LOG_SCRIPT"
elif [[ "$(systemctl --user show -p ActiveState --value $SERVICE)" == "activating" ]]; then
echo "$(date +"%Y-%m-%d %H:%M:%S") [$VERSION] [$NAME] (Restart) Server is activating. Aborting restart." | tee -a "$LOG_SCRIPT"
elif [[ "$(systemctl --user show -p ActiveState --value $SERVICE)" == "deactivating" ]]; then
echo "$(date +"%Y-%m-%d %H:%M:%S") [$VERSION] [$NAME] (Restart) Server is in deactivating. Aborting restart." | tee -a "$LOG_SCRIPT"
elif [[ "$(systemctl --user show -p ActiveState --value $SERVICE)" == "active" ]]; then
echo "$(date +"%Y-%m-%d %H:%M:%S") [$VERSION] [$NAME] (Restart) Server is going to restart in 15-30 seconds, please wait..." | tee -a "$LOG_SCRIPT"
sleep 1
script_stop
sleep 1
script_start
sleep 1
fi
}
#---------------------------
#Systemd service sends email if email notifications for crashes enabled
script_send_notification_crash() {
script_logs
if [ ! -d "$CRASH_DIR" ]; then
mkdir -p "$CRASH_DIR"
fi
systemctl --user status $SERVICE > $CRASH_DIR/service_log.txt
zip -j $CRASH_DIR/service_logs.zip $CRASH_DIR/service_log.txt
zip -j $CRASH_DIR/script_logs.zip $LOG_SCRIPT
rm $CRASH_DIR/service_log.txt
if [[ "$DISCORD_CRASH" == "1" ]]; then
script_discord_message "$DISCORD_COLOR_CRASH" "Server crashed 3 times in the last 5 minutes.\nAutomatic restart is disabled and the server is inactive.\n\nPlease review your logs located in $LOG_STRUCTURE/Server-crash_$CRASH_TIME."
fi
if [[ "$EMAIL_CRASH" == "1" ]]; then
mail -a $CRASH_DIR/service_logs.zip -a $CRASH_DIR/script_logs.zip -r "$EMAIL_SENDER ($NAME-$SERVICE_NAME)" -s "Notification: Crash" $EMAIL_RECIPIENT <<- EOF
The server crashed 3 times in the last 5 minutes. Automatic restart is disabled and the server is inactive. Please check the logs for more information.
Attachment contents:
service_logs.zip - Logs from the systemd service
script_logs.zip - Logs from the script
DO NOT SEND ANY OF THESE TO THE DEVS!
EOF
fi
echo "$(date +"%Y-%m-%d %H:%M:%S") [$VERSION] [$NAME] (Crash) Server crashed. Please review your logs located in $CRASH_DIR." | tee -a "$LOG_SCRIPT"
}
#--------------------------
#Creates a backup of the server
script_backup() {
script_logs
#If there is not a folder for today, create one
script_backup_create_folder() {
if [ ! -d "$BCKP_DIR/$BCKP_STRUCTURE" ]; then
mkdir -p "$BCKP_DIR/$BCKP_STRUCTURE"
fi
}
#Backup source to destination
echo "$(date +"%Y-%m-%d %H:%M:%S") [$VERSION] [$NAME] (Backup) Backup has been initiated." | tee -a "$LOG_SCRIPT"
if [[ "$(systemctl --user show -p ActiveState --value $SERVER_SERVICE)" != "active" ]] && [[ "$(systemctl --user show -p UnitFileState --value $SERVER_SERVICE)" == "enabled" ]]; then
echo "$(date +"%Y-%m-%d %H:%M:%S") [$VERSION] [$NAME] (Autobackup) Server is not running." | tee -a "$LOG_SCRIPT"
elif [[ "$(systemctl --user show -p ActiveState --value $SERVER_SERVICE)" == "active" ]] && [[ "$(systemctl --user show -p UnitFileState --value $SERVER_SERVICE)" == "enabled" ]]; then
script_backup_create_folder
cd "/srv/$SERVICE_NAME/.local/share"
tar -cpvzf $BCKP_DIR/$BCKP_STRUCTURE/$(date +"%Y%m%d%H%M").tar.gz /srv/$SERVICE_NAME/.local/share/Arma*
fi
echo "$(date +"%Y-%m-%d %H:%M:%S") [$VERSION] [$NAME] (Backup) Backup complete." | tee -a "$LOG_SCRIPT"
}
#--------------------------
#Get steam credentials for script operations
script_steamcmd_credentials() {
while [[ "$STEAMCMDSUCCESS" != "0" ]]; do
read -p "Enter your Steam username: " STEAMCMD_UID
echo ""
read -p "Enter your Steam password: " STEAMCMD_PSW
steamcmd +login $STEAMCMD_UID $STEAMCMD_PSW +quit
STEAMCMDSUCCESS=$?
if [[ "$STEAMCMDSUCCESS" == "0" ]]; then
echo "Steam login for $STEAMCMD_UID: SUCCEDED!"
elif [[ "$STEAMCMDSUCCESS" != "0" ]]; then
echo "Steam login for $STEAMCMD_UID: FAILED!"
echo "Please try again."
fi
done
}
#--------------------------
#Change the steam branch of the app
script_change_branch() {
script_logs
echo "$(date +"%Y-%m-%d %H:%M:%S") [$VERSION] [$NAME] (Change branch) Server branch change initiated. Waiting on user configuration." | tee -a "$LOG_SCRIPT"
if [[ "$STEAMCMD_UID" == "disabled" ]] && [[ "$STEAMCMD_PSW" == "disabled" ]]; then
script_steamcmd_credentials
fi
read -p "Are you sure you want to change the server branch? (y/n): " CHANGE_SERVER_BRANCH
if [[ "$CHANGE_SERVER_BRANCH" =~ ^([yY][eE][sS]|[yY])$ ]]; then
echo "$(date +"%Y-%m-%d %H:%M:%S") [$VERSION] [$NAME] (Change branch) Clearing game installation." | tee -a "$LOG_SCRIPT"
rm -rf $SRV_DIR/*
if [[ "$STEAMCMD_BETA_BRANCH" == "1" ]]; then
PUBLIC_BRANCH="0"
elif [[ "$STEAMCMD_BETA_BRANCH" == "0" ]]; then
PUBLIC_BRANCH="1"
fi
echo "Current configuration:"
echo 'Public branch: '"$PUBLIC_BRANCH"
echo 'Beta branch enabled: '"$STEAMCMD_BETA_BRANCH"
echo 'Beta branch name: '"$STEAMCMD_BETA_BRANCH_NAME"
echo ""
read -p "Public branch or beta branch? (public/beta): " SET_BRANCH_STATE
echo ""
if [[ "$SET_BRANCH_STATE" =~ ^([bB][eE][tT][aA]|[bB])$ ]]; then
STEAMCMD_BETA_BRANCH="1"
echo "Look up beta branch names at https://steamdb.info/app/$APPID/depots/"
echo "Name example: experimental"
read -p "Enter beta branch name: " STEAMCMD_BETA_BRANCH_NAME
elif [[ "$SET_BRANCH_STATE" =~ ^([pP][uU][bB][lL][iI][cC]|[pP])$ ]]; then
STEAMCMD_BETA_BRANCH="0"
STEAMCMD_BETA_BRANCH_NAME="none"
fi
sed -i '/beta_branch_enabled/d' $SCRIPT_DIR/$SERVICE_NAME-steam.conf
sed -i '/beta_branch_name/d' $SCRIPT_DIR/$SERVICE_NAME-steam.conf
echo 'beta_branch_enabled='"$STEAMCMD_BETA_BRANCH" >> $SCRIPT_DIR/$SERVICE_NAME-steam.conf
echo 'beta_branch_name='"$STEAMCMD_BETA_BRANCH_NAME" >> $SCRIPT_DIR/$SERVICE_NAME-steam.conf
steamcmd +login anonymous +app_info_update 1 +app_info_print $APPID +quit > $UPDATE_DIR/steam_app_data.txt
if [[ "$(systemctl --user show -p ActiveState --value $SERVICE_NAME)" == "active" ]]; then
script_stop
WAS_ACTIVE="1"
fi
if [[ "$STEAMCMD_BETA_BRANCH" == "0" ]]; then
INSTALLED_BUILDID=$(cat $UPDATE_DIR/steam_app_data.txt | grep -EA 1000 "^\s+\"branches\"$" | grep -EA 5 "^\s+\"public\"$" | grep -m 1 -EB 10 "^\s+}$" | grep -E "^\s+\"buildid\"\s+" | tr '[:blank:]"' ' ' | tr -s ' ' | cut -d' ' -f3)
echo "$INSTALLED_BUILDID" > $UPDATE_DIR/installed.buildid
INSTALLED_TIME=$(cat $UPDATE_DIR/steam_app_data.txt | grep -EA 1000 "^\s+\"branches\"$" | grep -EA 5 "^\s+\"public\"$" | grep -m 1 -EB 10 "^\s+}$" | grep -E "^\s+\"timeupdated\"\s+" | tr '[:blank:]"' ' ' | tr -s ' ' | cut -d' ' -f3)
echo "$INSTALLED_TIME" > $UPDATE_DIR/installed.timeupdated
if [[ "$STEAMGUARD_CLI" == "1" ]]; then
steamcmd +@sSteamCmdForcePlatformType windows +force_install_dir $SRV_DIR/$WINE_PREFIX_GAME_DIR +login $STEAMCMD_UID $STEAMCMD_PSW $(steamguard) +app_update $APPID validate +quit
else
steamcmd +@sSteamCmdForcePlatformType windows +force_install_dir $SRV_DIR/$WINE_PREFIX_GAME_DIR +login $STEAMCMD_UID $STEAMCMD_PSW +app_update $APPID validate +quit
fi
elif [[ "$STEAMCMD_BETA_BRANCH" == "1" ]]; then
INSTALLED_BUILDID=$(cat $UPDATE_DIR/steam_app_data.txt | grep -EA 1000 "^\s+\"branches\"$" | grep -EA 5 "^\s+\"$STEAMCMD_BETA_BRANCH_NAME\"$" | grep -m 1 -EB 10 "^\s+}$" | grep -E "^\s+\"buildid\"\s+" | tr '[:blank:]"' ' ' | tr -s ' ' | cut -d' ' -f3)
echo "$INSTALLED_BUILDID" > $UPDATE_DIR/installed.buildid
INSTALLED_TIME=$(cat $UPDATE_DIR/steam_app_data.txt | grep -EA 1000 "^\s+\"branches\"$" | grep -EA 5 "^\s+\"$STEAMCMD_BETA_BRANCH_NAME\"$" | grep -m 1 -EB 10 "^\s+}$" | grep -E "^\s+\"timeupdated\"\s+" | tr '[:blank:]"' ' ' | tr -s ' ' | cut -d' ' -f3)
echo "$INSTALLED_TIME" > $UPDATE_DIR/installed.timeupdated
if [[ "$STEAMGUARD_CLI" == "1" ]]; then
steamcmd +@sSteamCmdForcePlatformType windows +force_install_dir $SRV_DIR/$WINE_PREFIX_GAME_DIR +login $STEAMCMD_UID $STEAMCMD_PSW $(steamguard) +app_update $APPID -beta $INSTALL_STEAMCMD_BETA_BRANCH_NAME validate +quit
else
steamcmd +@sSteamCmdForcePlatformType windows +force_install_dir $SRV_DIR/$WINE_PREFIX_GAME_DIR +login $STEAMCMD_UID $STEAMCMD_PSW +app_update $APPID -beta $STEAMCMD_BETA_BRANCH_NAME validate +quit
fi
fi
if [[ "$WAS_ACTIVE" == "1" ]]; then
script_start
fi
echo "$(date +"%Y-%m-%d %H:%M:%S") [$VERSION] [$NAME] (Change branch) Server branch change complete." | tee -a "$LOG_SCRIPT"
elif [[ "$CHANGE_SERVER_BRANCH" =~ ^([nN][oO]|[nN])$ ]]; then
echo "$(date +"%Y-%m-%d %H:%M:%S") [$VERSION] [$NAME] (Change branch) Server branch change canceled." | tee -a "$LOG_SCRIPT"
fi
}
#---------------------------
#Check for updates. If there are updates available, shut down the server, update it and restart it.
script_update() {
script_logs
echo "$(date +"%Y-%m-%d %H:%M:%S") [$VERSION] [$NAME] (Update) Initializing update check." | tee -a "$LOG_SCRIPT"
if [[ "$STEAMCMD_UID" == "disabled" ]] && [[ "$STEAMCMD_PSW" == "disabled" ]]; then
script_steamcmd_credentials
fi
if [[ "$STEAMCMD_BETA_BRANCH" == "1" ]]; then
echo "$(date +"%Y-%m-%d %H:%M:%S") [$VERSION] [$NAME] (Update) Beta branch enabled. Branch name: $STEAMCMD_BETA_BRANCH_NAME" | tee -a "$LOG_SCRIPT"
fi
if [ ! -f $UPDATE_DIR/installed.buildid ] ; then
touch $UPDATE_DIR/installed.buildid
echo "0" > $UPDATE_DIR/installed.buildid
fi
if [ ! -f $UPDATE_DIR/installed.timeupdated ] ; then
touch $UPDATE_DIR/installed.timeupdated
echo "0" > $UPDATE_DIR/installed.timeupdated
fi
echo "$(date +"%Y-%m-%d %H:%M:%S") [$VERSION] [$NAME] (Update) Removing Steam/appcache/appinfo.vdf" | tee -a "$LOG_SCRIPT"
rm -rf "/srv/$SERVICE_NAME/.steam/appcache/appinfo.vdf"
echo "$(date +"%Y-%m-%d %H:%M:%S") [$VERSION] [$NAME] (Update) Connecting to steam servers." | tee -a "$LOG_SCRIPT"
steamcmd +login anonymous +app_info_update 1 +app_info_print $APPID +quit > $UPDATE_DIR/steam_app_data.txt
if [[ "$STEAMCMD_BETA_BRANCH" == "0" ]]; then
AVAILABLE_BUILDID=$(cat $UPDATE_DIR/steam_app_data.txt | grep -EA 1000 "^\s+\"branches\"$" | grep -EA 5 "^\s+\"public\"$" | grep -m 1 -EB 10 "^\s+}$" | grep -E "^\s+\"buildid\"\s+" | tr '[:blank:]"' ' ' | tr -s ' ' | cut -d' ' -f3)
AVAILABLE_TIME=$(cat $UPDATE_DIR/steam_app_data.txt | grep -EA 1000 "^\s+\"branches\"$" | grep -EA 5 "^\s+\"public\"$" | grep -m 1 -EB 10 "^\s+}$" | grep -E "^\s+\"timeupdated\"\s+" | tr '[:blank:]"' ' ' | tr -s ' ' | cut -d' ' -f3)
elif [[ "$STEAMCMD_BETA_BRANCH" == "1" ]]; then
AVAILABLE_BUILDID=$(cat $UPDATE_DIR/steam_app_data.txt | grep -EA 1000 "^\s+\"branches\"$" | grep -EA 5 "^\s+\"$STEAMCMD_BETA_BRANCH_NAME\"$" | grep -m 1 -EB 10 "^\s+}$" | grep -E "^\s+\"buildid\"\s+" | tr '[:blank:]"' ' ' | tr -s ' ' | cut -d' ' -f3)
AVAILABLE_TIME=$(cat $UPDATE_DIR/steam_app_data.txt | grep -EA 1000 "^\s+\"branches\"$" | grep -EA 5 "^\s+\"$STEAMCMD_BETA_BRANCH_NAME\"$" | grep -m 1 -EB 10 "^\s+}$" | grep -E "^\s+\"timeupdated\"\s+" | tr '[:blank:]"' ' ' | tr -s ' ' | cut -d' ' -f3)
fi
echo "$(date +"%Y-%m-%d %H:%M:%S") [$VERSION] [$NAME] (Update) Received application info data." | tee -a "$LOG_SCRIPT"
INSTALLED_BUILDID=$(cat $UPDATE_DIR/installed.buildid)
INSTALLED_TIME=$(cat $UPDATE_DIR/installed.timeupdated)
if [ "$AVAILABLE_TIME" -gt "$INSTALLED_TIME" ]; then
echo "$(date +"%Y-%m-%d %H:%M:%S") [$VERSION] [$NAME] (Update) New update detected." | tee -a "$LOG_SCRIPT"
echo "$(date +"%Y-%m-%d %H:%M:%S") [$VERSION] [$NAME] (Update) Installed: BuildID: $INSTALLED_BUILDID, TimeUpdated: $INSTALLED_TIME" | tee -a "$LOG_SCRIPT"
echo "$(date +"%Y-%m-%d %H:%M:%S") [$VERSION] [$NAME] (Update) Available: BuildID: $AVAILABLE_BUILDID, TimeUpdated: $AVAILABLE_TIME" | tee -a "$LOG_SCRIPT"
if [[ "$DISCORD_UPDATE" == "1" ]]; then
script_discord_message "$DISCORD_COLOR_UPDATE" "New update detected. Installing update."
fi
if [[ "$(systemctl --user show -p ActiveState --value $SERVICE)" == "active" ]]; then
sleep 1
WAS_ACTIVE="1"
script_stop
sleep 1
fi
echo "$(date +"%Y-%m-%d %H:%M:%S") [$VERSION] [$NAME] (Update) Updating..." | tee -a "$LOG_SCRIPT"
if [[ "$STEAMGUARD_CLI" == "1" ]]; then
if [[ "$STEAMCMD_BETA_BRANCH" == "0" ]]; then
steamcmd +force_install_dir $SRV_DIR/ +login $STEAMCMD_UID $STEAMCMD_PSW $(steamguard) +app_update $APPID validate +quit
elif [[ "$STEAMCMD_BETA_BRANCH" == "1" ]]; then
steamcmd +force_install_dir $SRV_DIR/ +login $STEAMCMD_UID $STEAMCMD_PSW $(steamguard) +app_update +app_update $APPID -beta $STEAMCMD_BETA_BRANCH_NAME validate +quit
fi
else
if [[ "$STEAMCMD_BETA_BRANCH" == "0" ]]; then
steamcmd +force_install_dir $SRV_DIR/ +login $STEAMCMD_UID $STEAMCMD_PSW +app_update $APPID validate +quit
elif [[ "$STEAMCMD_BETA_BRANCH" == "1" ]]; then
steamcmd +force_install_dir $SRV_DIR/ +login $STEAMCMD_UID $STEAMCMD_PSW +app_update $APPID -beta $STEAMCMD_BETA_BRANCH_NAME validate +quit
fi
fi
echo "$(date +"%Y-%m-%d %H:%M:%S") [$VERSION] [$NAME] (Update) Update completed." | tee -a "$LOG_SCRIPT"
echo "$AVAILABLE_BUILDID" > $UPDATE_DIR/installed.buildid
echo "$AVAILABLE_TIME" > $UPDATE_DIR/installed.timeupdated
if [ "$WAS_ACTIVE" == "1" ]; then
sleep 1
if [[ "$UPDATE_IGNORE_FAILED_ACTIVATIONS" == "1" ]]; then
script_start "ignore"
else
script_start
fi
fi
if [[ "$DISCORD_UPDATE" == "1" ]]; then
script_discord_message "$DISCORD_COLOR_UPDATE" "Server update complete."
fi
if [[ "$EMAIL_UPDATE" == "1" ]]; then
script_email_message "$NAME" "Notification: Update" "Server was updated. Please check the update notes if there are any additional steps to take."
fi
elif [ "$AVAILABLE_TIME" -eq "$INSTALLED_TIME" ]; then
echo "$(date +"%Y-%m-%d %H:%M:%S") [$VERSION] [$NAME] (Update) No new updates detected." | tee -a "$LOG_SCRIPT"
echo "$(date +"%Y-%m-%d %H:%M:%S") [$VERSION] [$NAME] (Update) Installed: BuildID: $INSTALLED_BUILDID, TimeUpdated: $INSTALLED_TIME" | tee -a "$LOG_SCRIPT"
fi
}
#---------------------------
#Check for updates of mods. If there are updates available, shut down the server, update it and restart it.
script_update_mods() {
script_logs
if [[ "$MODS_ENABLED" == "1" ]]; then
script_logs
echo "$(date +"%Y-%m-%d %H:%M:%S") [$VERSION] [$NAME] (Mod Update) Initializing update check." | tee -a "$LOG_SCRIPT"
if [[ "$STEAMCMD_UID" == "disabled" ]] && [[ "$STEAMCMD_PSW" == "disabled" ]]; then
script_steamcmd_credentials
fi
MODS_TO_UPDATE=""
echo "$(date +"%Y-%m-%d %H:%M:%S") [$VERSION] [$NAME] (Mod Update) Checking for mod updates." | tee -a "$LOG_SCRIPT"
IFS=","
for MOD_NAME_ID in $MODS; do
MOD_ID=$(echo $MOD_NAME_ID | cut -d - -f2)
MOD_NAME=$(echo $MOD_NAME_ID | cut -d - -f1)
AVAILABLE_DATE_MOD=$(curl -s https://steamcommunity.com/sharedfiles/filedetails/changelog/$MOD_ID | grep "Update:" | head -n1 |awk -F 'Update: ' '{print $2}' | tr -d '\t' | awk -F '</div>' '{print $1}' | awk -F ' @ ' '{print $1}')
AVAILABLE_TIME_MOD=$(curl -s https://steamcommunity.com/sharedfiles/filedetails/changelog/$MOD_ID | grep "Update:" | head -n1 |awk -F 'Update: ' '{print $2}' | tr -d '\t' | awk -F '</div>' '{print $1}' | awk -F ' @ ' '{print $2}')
AVAILABLE_VERSION_MOD=$(date --date="$(printf "%s" $AVAILABLE_DATE_MOD)" +"%Y%m%d")$(date --date="$(printf "%s" $AVAILABLE_TIME_MOD)" +"%H%M")
INSTALLED_VERSION_MOD=$(cat $UPDATE_DIR/mods/$MOD_NAME.mod_version)
if [[ ! "$INSTALLED_VERSION_MOD" ]]; then
INSTALLED_VERSION_MOD="0"
fi
if [ "$AVAILABLE_VERSION_MOD" -gt "$INSTALLED_VERSION_MOD" ]; then
MODS_TO_UPDATE="$MODS_TO_UPDATE$MOD_NAME_ID,"
fi
done
if [ ! -z "$MODS_TO_UPDATE" ]; then
echo "$(date +"%Y-%m-%d %H:%M:%S") [$VERSION] [$NAME] (Mod Update) New mod updates detected. Installing updates." | tee -a "$LOG_SCRIPT"
if [[ "$DISCORD_UPDATE_MOD" == "1" ]]; then
script_discord_message "$DISCORD_COLOR_UPDATE" "New updates for mods detected. Installing updates."
fi
if [[ "$(systemctl --user show -p ActiveState --value $SERVICE)" == "active" ]]; then
sleep 1
WAS_ACTIVE="1"
script_stop
sleep 1
fi
IFS=","
for MOD_NAME_ID in $MODS_TO_UPDATE; do
MOD_ID=$(echo $MOD_NAME_ID | cut -d - -f2)
MOD_NAME=$(echo $MOD_NAME_ID | cut -d - -f1)
AVAILABLE_DATE_MOD=$(curl -s https://steamcommunity.com/sharedfiles/filedetails/changelog/$MOD_ID | grep "Update:" | head -n1 |awk -F 'Update: ' '{print $2}' | tr -d '\t' | awk -F '</div>' '{print $1}' | awk -F ' @ ' '{print $1}')
AVAILABLE_TIME_MOD=$(curl -s https://steamcommunity.com/sharedfiles/filedetails/changelog/$MOD_ID | grep "Update:" | head -n1 |awk -F 'Update: ' '{print $2}' | tr -d '\t' | awk -F '</div>' '{print $1}' | awk -F ' @ ' '{print $2}')
AVAILABLE_VERSION_MOD=$(date --date="$(printf "%s" $AVAILABLE_DATE_MOD)" +"%Y%m%d")$(date --date="$(printf "%s" $AVAILABLE_TIME_MOD)" +"%H%M")
echo "$(date +"%Y-%m-%d %H:%M:%S") [$VERSION] [$NAME] (Mod Update) New update detected for mod $MOD_NAME." | tee -a "$LOG_SCRIPT"
echo "$(date +"%Y-%m-%d %H:%M:%S") [$VERSION] [$NAME] (Mod Update) Installed: $INSTALLED_VERSION_MOD" | tee -a "$LOG_SCRIPT"
echo "$(date +"%Y-%m-%d %H:%M:%S") [$VERSION] [$NAME] (Mod Update) Available: $AVAILABLE_VERSION_MOD" | tee -a "$LOG_SCRIPT"
if [[ "$STEAMGUARD_CLI" == "1" ]]; then
steamcmd +force_install_dir $SRV_DIR/ +login $STEAMCMD_UID $STEAMCMD_PSW $(steamguard) +workshop_download_item $WORKSHOP_APPID $MOD_ID +quit
else
steamcmd +force_install_dir $SRV_DIR/ +login $STEAMCMD_UID $STEAMCMD_PSW +workshop_download_item $WORKSHOP_APPID $MOD_ID +quit
fi
ln -s $SRV_DIR/steamapps/workshop/content/$WORKSHOP_APPID/$MOD_ID $SRV_DIR/@${MOD_NAME}
if [ -f "$SRV_DIR/steamapps/workshop/content/$WORKSHOP_APPID/keys/*.bikey" ]; then
cp -rf $SRV_DIR/steamapps/workshop/content/$WORKSHOP_APPID/keys/*.bikey $SRV_DIR/keys/
fi
if [ -f "$SRV_DIR/steamapps/workshop/content/$WORKSHOP_APPID/key/*.bikey" ]; then
cp -rf $SRV_DIR/steamapps/workshop/content/$WORKSHOP_APPID/key/*.bikey $SRV_DIR/keys/
fi
echo "$AVAILABLE_VERSION_MOD" > $UPDATE_DIR/mods/$MOD_NAME.mod_version
echo "$(date +"%Y-%m-%d %H:%M:%S") [$VERSION] [$NAME] (Mod Update) Update for mod $MOD_NAME complete." | tee -a "$LOG_SCRIPT"
done
if [ "$WAS_ACTIVE" == "1" ]; then
sleep 1
if [[ "$UPDATE_IGNORE_FAILED_ACTIVATIONS" == "1" ]]; then
script_start "ignore"
else
script_start
fi
fi
if [[ "$EMAIL_UPDATE_MOD" == "1" ]]; then
mail -r "$EMAIL_SENDER ($NAME-$SERVICE_NAME)" -s "Notification: Mod Update" $EMAIL_RECIPIENT <<- EOF
Mods ware updated. Please check the update notes if there are any additional steps to take.
Updated mods: $MODS_TO_UPDATE
EOF
fi
if [[ "$DISCORD_UPDATE_MOD" == "1" ]]; then
while IFS="" read -r DISCORD_WEBHOOK || [ -n "$DISCORD_WEBHOOK" ]; do
curl -H "Content-Type: application/json" -X POST -d "{\"content\": \"$(date +"%Y-%m-%d %H:%M:%S") [$VERSION] [$NAME] (Mod Update) Update complete.\nUpdated mods: $MODS_TO_UPDATE\"}" "$DISCORD_WEBHOOK"
done < $SCRIPT_DIR/discord_webhooks.txt
fi
if [[ "$DISCORD_UPDATE" == "1" ]]; then
script_discord_message "$DISCORD_COLOR_UPDATE" "Mods update complete.\n\nUpdated mods: $MODS_TO_UPDATE"
fi
if [[ "$EMAIL_UPDATE" == "1" ]]; then
script_email_message "$NAME" "Notification: Mod Update" "Mods ware updated. Please check the update notes if there are any additional steps to take.\nUpdated mods: $MODS_TO_UPDATE"
fi
echo "$(date +"%Y-%m-%d %H:%M:%S") [$VERSION] [$NAME] (Mod Update) Update for all mods complete." | tee -a "$LOG_SCRIPT"
else
echo "$(date +"%Y-%m-%d %H:%M:%S") [$VERSION] [$NAME] (Mod Update) All mods are up to date." | tee -a "$LOG_SCRIPT"
fi
fi
}
#---------------------------
#Shutdown any active servers, check the integrity of the server files and restart the servers.
script_verify_game_integrity() {
script_logs
if [[ "$STEAMCMD_UID" == "disabled" ]] && [[ "$STEAMCMD_PSW" == "disabled" ]]; then
script_steamcmd_credentials
fi
echo "$(date +"%Y-%m-%d %H:%M:%S") [$VERSION] [$NAME] (Integrity check) Initializing integrity check." | tee -a "$LOG_SCRIPT"
if [[ "$STEAMCMD_BETA_BRANCH" == "1" ]]; then
echo "$(date +"%Y-%m-%d %H:%M:%S") [$VERSION] [$NAME] (Integrity check) Beta branch enabled. Branch name: $STEAMCMD_BETA_BRANCH_NAME" | tee -a "$LOG_SCRIPT"
fi
echo "$(date +"%Y-%m-%d %H:%M:%S") [$VERSION] [$NAME] (Integrity check) Removing Steam/appcache/appinfo.vdf" | tee -a "$LOG_SCRIPT"
rm -rf "/srv/$SERVICE_NAME/.steam/appcache/appinfo.vdf"
if [[ "$(systemctl --user show -p ActiveState --value $SERVICE)" == "active" ]]; then
sleep 1
WAS_ACTIVE="1"
script_stop
sleep 1
fi
if [[ "$STEAMGUARD_CLI" == "1" ]]; then
if [[ "$STEAMCMD_BETA_BRANCH" == "0" ]]; then
steamcmd +force_install_dir $SRV_DIR/$WINE_PREFIX_GAME_DIR +login $STEAMCMD_UID $STEAMCMD_PSW $(steamguard) +app_update $APPID validate +quit
elif [[ "$STEAMCMD_BETA_BRANCH" == "1" ]]; then
steamcmd +force_install_dir $SRV_DIR/$WINE_PREFIX_GAME_DIR +login $STEAMCMD_UID $STEAMCMD_PSW $(steamguard) +app_update $APPID -beta $STEAMCMD_BETA_BRANCH_NAME validate +quit
fi
else
if [[ "$STEAMCMD_BETA_BRANCH" == "0" ]]; then
steamcmd +force_install_dir $SRV_DIR/$WINE_PREFIX_GAME_DIR +login $STEAMCMD_UID $STEAMCMD_PSW +app_update $APPID validate +quit
elif [[ "$STEAMCMD_BETA_BRANCH" == "1" ]]; then
steamcmd +force_install_dir $SRV_DIR/$WINE_PREFIX_GAME_DIR +login $STEAMCMD_UID $STEAMCMD_PSW +app_update $APPID -beta $STEAMCMD_BETA_BRANCH_NAME validate +quit
fi
fi
echo "$(date +"%Y-%m-%d %H:%M:%S") [$VERSION] [$NAME] (Integrity check) Integrity check completed." | tee -a "$LOG_SCRIPT"
if [ "$WAS_ACTIVE" == "1" ]; then
sleep 1
if [[ "$UPDATE_IGNORE_FAILED_ACTIVATIONS" == "1" ]]; then
script_start "ignore"
else
script_start
fi
fi
}
#---------------------------
#First timer function for systemd timers to execute parts of the script in order without interfering with each other
script_timer_one() {
if [[ "$(systemctl --user show -p ActiveState --value $SERVICE)" == "inactive" ]]; then
echo "$(date +"%Y-%m-%d %H:%M:%S") [$VERSION] [$NAME] (Status) Server is not running." | tee -a "$LOG_SCRIPT"
elif [[ "$(systemctl --user show -p ActiveState --value $SERVICE)" == "failed" ]]; then
echo "$(date +"%Y-%m-%d %H:%M:%S") [$VERSION] [$NAME] (Status) Server is in failed state. Please check logs." | tee -a "$LOG_SCRIPT"
elif [[ "$(systemctl --user show -p ActiveState --value $SERVICE)" == "activating" ]]; then
echo "$(date +"%Y-%m-%d %H:%M:%S") [$VERSION] [$NAME] (Status) Server is activating. Please wait." | tee -a "$LOG_SCRIPT"
elif [[ "$(systemctl --user show -p ActiveState --value $SERVICE)" == "deactivating" ]]; then
echo "$(date +"%Y-%m-%d %H:%M:%S") [$VERSION] [$NAME] (Status) Server is in deactivating. Please wait." | tee -a "$LOG_SCRIPT"
elif [[ "$(systemctl --user show -p ActiveState --value $SERVICE)" == "active" ]]; then
echo "$(date +"%Y-%m-%d %H:%M:%S") [$VERSION] [$NAME] (Status) Server running." | tee -a "$LOG_SCRIPT"
script_remove_old_files
script_backup
if [[ "$STEAMCMDUID" != "disabled" ]] && [[ "$STEAMCMDPSW" != "disabled" ]]; then
script_update
script_update_mods
fi
fi
}
#---------------------------
#Second timer function for systemd timers to execute parts of the script in order without interfering with each other
script_timer_two() {
if [[ "$(systemctl --user show -p ActiveState --value $SERVICE)" == "inactive" ]]; then
echo "$(date +"%Y-%m-%d %H:%M:%S") [$VERSION] [$NAME] (Status) Server is not running." | tee -a "$LOG_SCRIPT"
elif [[ "$(systemctl --user show -p ActiveState --value $SERVICE)" == "failed" ]]; then
echo "$(date +"%Y-%m-%d %H:%M:%S") [$VERSION] [$NAME] (Status) Server is in failed state. Please check logs." | tee -a "$LOG_SCRIPT"
elif [[ "$(systemctl --user show -p ActiveState --value $SERVICE)" == "activating" ]]; then
echo "$(date +"%Y-%m-%d %H:%M:%S") [$VERSION] [$NAME] (Status) Server is activating. Please wait." | tee -a "$LOG_SCRIPT"
elif [[ "$(systemctl --user show -p ActiveState --value $SERVICE)" == "deactivating" ]]; then
echo "$(date +"%Y-%m-%d %H:%M:%S") [$VERSION] [$NAME] (Status) Server is in deactivating. Please wait." | tee -a "$LOG_SCRIPT"
elif [[ "$(systemctl --user show -p ActiveState --value $SERVICE)" == "active" ]]; then
echo "$(date +"%Y-%m-%d %H:%M:%S") [$VERSION] [$NAME] (Status) Server running." | tee -a "$LOG_SCRIPT"
script_remove_old_files
if [[ "$STEAMCMDUID" != "disabled" ]] && [[ "$STEAMCMDPSW" != "disabled" ]]; then
script_update
script_update_mods
fi
fi
}
#---------------------------
#Runs the diagnostics
script_diagnostics() {
echo "Initializing diagnostics. Please wait..."
echo ""
sleep 3
#Check package versions
echo "Checkign package versions:"
if [ -f "/usr/bin/pacman" ]; then
echo "bash version:$(pacman -Qi bash | grep "^Version" | cut -d : -f2)"
echo "coreutils version:$(pacman -Qi coreutils | grep "^Version" | cut -d : -f2)"
echo "sudo version:$(pacman -Qi sudo | grep "^Version" | cut -d : -f2)"
echo "grep version:$(pacman -Qi grep | grep "^Version" | cut -d : -f2)"
echo "sed version:$(pacman -Qi sed | grep "^Version" | cut -d : -f2)"
echo "awk version:$(pacman -Qi awk | grep "^Version" | cut -d : -f2)"
echo "curl version:$(pacman -Qi curl | grep "^Version" | cut -d : -f2)"
echo "rsync version:$(pacman -Qi rsync | grep "^Version" | cut -d : -f2)"
echo "wget version:$(pacman -Qi wget | grep "^Version" | cut -d : -f2)"
echo "findutils version:$(pacman -Qi findutils | grep "^Version" | cut -d : -f2)"
echo "tmux version:$(pacman -Qi tmux | grep "^Version" | cut -d : -f2)"
echo "jq version:$(pacman -Qi jq | grep "^Version" | cut -d : -f2)"
echo "zip version:$(pacman -Qi zip | grep "^Version" | cut -d : -f2)"
echo "unzip version:$(pacman -Qi unzip | grep "^Version" | cut -d : -f2)"
echo "p7zip version:$(pacman -Qi p7zip | grep "^Version" | cut -d : -f2)"
echo "postfix version:$(pacman -Qi postfix | grep "^Version" | cut -d : -f2)"
echo "samba version:$(pacman -Qi samba | grep "^Version" | cut -d : -f2)"
elif [ -f "/usr/bin/dpkg" ]; then
echo "bash version:$(dpkg -s bash | grep "^Version" | cut -d : -f2)"
echo "coreutils version:$(dpkg -s coreutils | grep "^Version" | cut -d : -f2)"
echo "sudo version:$(dpkg -s sudo | grep "^Version" | cut -d : -f2)"
echo "libpam-systemd version:$(dpkg -s libpam-systemd | grep "^Version" | cut -d : -f2)"
echo "grep version:$(dpkg -s grep | grep "^Version" | cut -d : -f2)"
echo "sed version:$(dpkg -s sed | grep "^Version" | cut -d : -f2)"
echo "gawk version:$(dpkg -s gawk | grep "^Version" | cut -d : -f2)"
echo "curl version:$(dpkg -s curl | grep "^Version" | cut -d : -f2)"
echo "rsync version:$(dpkg -s rsync | grep "^Version" | cut -d : -f2)"
echo "wget version:$(dpkg -s wget | grep "^Version" | cut -d : -f2)"
echo "findutils version:$(dpkg -s findutils | grep "^Version" | cut -d : -f2)"
echo "tmux version:$(dpkg -s tmux | grep "^Version" | cut -d : -f2)"
echo "jq version:$(dpkg -s jq | grep "^Version" | cut -d : -f2)"
echo "zip version:$(dpkg -s zip | grep "^Version" | cut -d : -f2)"
echo "unzip version:$(dpkg -s unzip | grep "^Version" | cut -d : -f2)"
echo "p7zip version:$(dpkg -s p7zip | grep "^Version" | cut -d : -f2)"
echo "postfix version:$(dpkg -s postfix | grep "^Version" | cut -d : -f2)"
fi
echo ""
echo "Checking if files and folders present:"
#Check if files/folders present
if [ -f "$SCRIPT_DIR/$SCRIPT_NAME" ] ; then
echo "Script installed: Yes"
else
echo "Script installed: No"
fi
if [ -f "$SCRIPT_DIR/$SERVICE_NAME-config.conf" ] ; then
echo "Configuration file present: Yes"
else
echo "Configuration file present: No"
fi
if [ -d "$BCKP_DIR" ]; then
echo "Backups folder present: Yes"
else
echo "Backups folder present: No"
fi
if [ -d "/srv/$SERVICE_NAME/logs" ]; then
echo "Logs folder present: Yes"
else
echo "Logs folder present: No"
fi
if [ -d "/srv/$SERVICE_NAME/scripts" ]; then
echo "Scripts folder present: Yes"
else
echo "Scripts folder present: No"
fi
if [ -d "$SRV_DIR" ]; then
echo "Server folder present: Yes"
else
echo "Server folder present: No"
fi
if [ -d "$UPDATE_DIR" ]; then
echo "Updates folder present: Yes"
else
echo "Updates folder present: No"
fi
if [ -f "/srv/$SERVICE_NAME/.config/systemd/user/$SERVICE_NAME.service" ]; then
echo "Basic service present: Yes"
else
echo "Basic service present: No"
fi
if [ -f "/srv/$SERVICE_NAME/.config/systemd/user/$SERVICE_NAME-timer-1.timer" ]; then
echo "Timer 1 timer present: Yes"
else
echo "Timer 1 timer present: No"