1 | #!/bin/bash
|
---|
2 | # Version 3
|
---|
3 |
|
---|
4 | # Provides common functions for 7dtd-scripts. Not intended to be run directly.
|
---|
5 |
|
---|
6 | # Check if the script is run as root (exit otherwise) and load global config
|
---|
7 | checkRootLoadConf() {
|
---|
8 | if [ `id -u` -ne 0 ]; then
|
---|
9 | echo "This script has to be run as root!"
|
---|
10 | exit 10
|
---|
11 | fi
|
---|
12 | . /etc/7dtd.conf
|
---|
13 | }
|
---|
14 |
|
---|
15 | # Get the config path for the given instance
|
---|
16 | # Params:
|
---|
17 | # 1: Instance name
|
---|
18 | # Returns:
|
---|
19 | # Config path for instance
|
---|
20 | getInstancePath() {
|
---|
21 | echo $SDTD_BASE/instances/$1
|
---|
22 | }
|
---|
23 |
|
---|
24 | # Check if the given instance name is an existing instance
|
---|
25 | # Params:
|
---|
26 | # 1: Instance name
|
---|
27 | # Returns:
|
---|
28 | # 0/1 instance not valid/valid
|
---|
29 | isValidInstance() {
|
---|
30 | if [ -z $1 ]; then
|
---|
31 | echo 0
|
---|
32 | else
|
---|
33 | if [ ! -d $(getInstancePath $1) ]; then
|
---|
34 | echo 0
|
---|
35 | else
|
---|
36 | if [ ! -f $(getInstancePath $1)/config.xml ]; then
|
---|
37 | echo 0
|
---|
38 | else
|
---|
39 | echo 1
|
---|
40 | fi
|
---|
41 | fi
|
---|
42 | fi
|
---|
43 | }
|
---|
44 |
|
---|
45 | # Check if the given instance is valid, exit the script otherwise
|
---|
46 | # Params:
|
---|
47 | # 1: instance name
|
---|
48 | checkInstanceValid() {
|
---|
49 | if [ -z $1 ]; then
|
---|
50 | echo "Missing parameter <instance>"
|
---|
51 | exit 1
|
---|
52 | fi
|
---|
53 | if [ $(isValidInstance $1) -eq 0 ]; then
|
---|
54 | echo "'$1' is not a valid instance"
|
---|
55 | exit 1
|
---|
56 | fi
|
---|
57 | }
|
---|
58 |
|
---|
59 | # Check if the given instance is currently running
|
---|
60 | # Params:
|
---|
61 | # 1: Instance name
|
---|
62 | # Returns:
|
---|
63 | # 0 = not running
|
---|
64 | # 1 = running
|
---|
65 | isRunning() {
|
---|
66 | start-stop-daemon --status --pidfile $(getInstancePath $1)/7dtd.pid
|
---|
67 | if [ $? -eq 0 ]; then
|
---|
68 | echo 1
|
---|
69 | else
|
---|
70 | echo 0
|
---|
71 | fi
|
---|
72 | }
|
---|
73 |
|
---|
74 | # Get list of defined instances
|
---|
75 | # Returns:
|
---|
76 | # List of instances
|
---|
77 | getInstanceList() {
|
---|
78 | for IF in $SDTD_BASE/instances/*; do
|
---|
79 | I=`basename $IF`
|
---|
80 | if [ $(isValidInstance $I) -eq 1 ]; then
|
---|
81 | echo $I
|
---|
82 | fi
|
---|
83 | done
|
---|
84 | }
|
---|
85 |
|
---|
86 | # Get the PID of the instance if it is running, 0 otherwise
|
---|
87 | # Params:
|
---|
88 | # 1: Instance name
|
---|
89 | # Returns:
|
---|
90 | # 0 if not running
|
---|
91 | # PID otherwise
|
---|
92 | getInstancePID() {
|
---|
93 | if [ $(isRunning $1) -eq 1 ]; then
|
---|
94 | cat $(getInstancePath $1)/7dtd.pid
|
---|
95 | else
|
---|
96 | echo 0
|
---|
97 | fi
|
---|
98 | }
|
---|
99 |
|
---|
100 | # Get a single value from a serverconfig
|
---|
101 | # Params:
|
---|
102 | # 1: Instance name
|
---|
103 | # 2: Property name
|
---|
104 | # Returns:
|
---|
105 | # Property value
|
---|
106 | getConfigValue() {
|
---|
107 | CONF=$(getInstancePath $1)/config.xml
|
---|
108 | xmlstarlet sel -t -v "/ServerSettings/property[@name='$2']/@value" $CONF
|
---|
109 | }
|
---|
110 |
|
---|
111 | # Send a single command to the telnet port
|
---|
112 | # Params:
|
---|
113 | # 1: Instance name
|
---|
114 | # 2: Command
|
---|
115 | # Returns:
|
---|
116 | # String of telnet output
|
---|
117 | telnetCommand() {
|
---|
118 | TEL_ENABLED=$(getConfigValue $1 TelnetEnabled)
|
---|
119 | TEL_PORT=$(getConfigValue $1 TelnetPort)
|
---|
120 | TEL_PASS=$(getConfigValue $1 TelnetPassword)
|
---|
121 | if [ "$TEL_ENABLED" = "true" ] && [ -n "$TEL_PASS" ]; then
|
---|
122 | echo -e "$TEL_PASS\n$2\nexit" | nc -q 2 127.0.0.1 $TEL_PORT
|
---|
123 | else
|
---|
124 | echo "Telnet not enabled or no password set."
|
---|
125 | fi
|
---|
126 | }
|
---|