source: scripts/usr/local/lib/7dtd/common.sh@ 344

Last change on this file since 344 was 308, checked in by alloc, 7 years ago

Scripts 109

File size: 7.9 KB
RevLine 
[17]1#!/bin/bash
2
[258]3# Copyright 2016 Christian 'Alloc' Illy
4#
5# Licensed under the Apache License, Version 2.0 (the "License");
6# you may not use this file except in compliance with the License.
7# You may obtain a copy of the License at
8#
9# http://www.apache.org/licenses/LICENSE-2.0
10#
11# Unless required by applicable law or agreed to in writing, software
12# distributed under the License is distributed on an "AS IS" BASIS,
13# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14# See the License for the specific language governing permissions and
15# limitations under the License.
16
17
[17]18# Provides common functions for 7dtd-scripts. Not intended to be run directly.
19
20# Check if the script is run as root (exit otherwise) and load global config
21checkRootLoadConf() {
22 if [ `id -u` -ne 0 ]; then
23 echo "This script has to be run as root!"
24 exit 10
25 fi
26 . /etc/7dtd.conf
27}
28
29# Get the config path for the given instance
30# Params:
31# 1: Instance name
32# Returns:
33# Config path for instance
34getInstancePath() {
35 echo $SDTD_BASE/instances/$1
36}
37
[19]38# Check if the given instance name is valid (no blanks, no special chars,
39# only letters, digits, underscore, hyphen -> [A-Za-z0-9_\-])
40# Params:
41# 1: Instance name
42# Returns:
43# 0/1 instance not valid/valid
44isValidInstanceName() {
45 if [[ "$1" =~ ^[A-Za-z0-9_\-]+$ ]]; then
46 echo 1
47 else
48 echo 0
49 fi
50}
51
[17]52# Check if the given instance name is an existing instance
53# Params:
54# 1: Instance name
55# Returns:
56# 0/1 instance not valid/valid
57isValidInstance() {
[19]58 if [ ! -z "$1" ]; then
59 if [ $(isValidInstanceName "$1") -eq 1 ]; then
60 if [ -d $(getInstancePath "$1") ]; then
61 if [ -f $(getInstancePath "$1")/config.xml ]; then
62 echo 1
63 return
64 fi
[17]65 fi
66 fi
67 fi
[19]68 echo 0
[17]69}
70
71# Check if the given instance is currently running
72# Params:
73# 1: Instance name
74# Returns:
75# 0 = not running
76# 1 = running
77isRunning() {
[25]78 $SSD --status --pidfile $(getInstancePath $1)/7dtd.pid
[17]79 if [ $? -eq 0 ]; then
80 echo 1
81 else
82 echo 0
83 fi
84}
85
86# Get list of defined instances
87# Returns:
88# List of instances
89getInstanceList() {
[19]90 local IF
[17]91 for IF in $SDTD_BASE/instances/*; do
[19]92 local I=`basename $IF`
[17]93 if [ $(isValidInstance $I) -eq 1 ]; then
94 echo $I
95 fi
96 done
97}
98
99# Get the PID of the instance if it is running, 0 otherwise
100# Params:
101# 1: Instance name
102# Returns:
103# 0 if not running
104# PID otherwise
105getInstancePID() {
106 if [ $(isRunning $1) -eq 1 ]; then
107 cat $(getInstancePath $1)/7dtd.pid
108 else
109 echo 0
110 fi
111}
112
[308]113# Get the installed branch name
114# Returns:
115# "public" if no engine installed or no appmanifest found or buildid could not be read
116# Branch name
117getLocalBranch() {
118 local APPMANIFEST=$(find $SDTD_BASE/engine -type f -name "appmanifest_294420.acf")
119 local LOCAL="public"
120 if [ -f "$APPMANIFEST" ]; then
121 LOCAL=$(grep betakey "$APPMANIFEST" | tr '[:blank:]"' ' ' | tr -s ' ' | cut -d\ -f3)
122 if [[ -z $LOCAL ]]; then
123 LOCAL="public"
124 else
125 echo $LOCAL
126 return
127 fi
128 fi
129 echo $LOCAL
130}
131
[53]132# Get the local engine version number (i.e. build id)
133# Returns:
134# 0 if no engine installed or no appmanifest found or buildid could not be read
135# Build Id otherwise
136getLocalEngineVersion() {
[80]137 local APPMANIFEST=$(find $SDTD_BASE/engine -type f -name "appmanifest_294420.acf")
[53]138 local LOCAL=0
139 if [ -f "$APPMANIFEST" ]; then
140 LOCAL=$(grep buildid "$APPMANIFEST" | tr '[:blank:]"' ' ' | tr -s ' ' | cut -d\ -f3)
141 if [ $(isANumber "$LOCAL") -eq 0 ]; then
142 LOCAL=0
143 fi
144 fi
145 echo $LOCAL
146}
147
[308]148# Get the local engine update time
[53]149# Returns:
[308]150# 0 if no engine installed or no appmanifest found or buildid could not be read
151# Update time otherwise
152getLocalEngineUpdateTime() {
153 local APPMANIFEST=$(find $SDTD_BASE/engine -type f -name "appmanifest_294420.acf")
154 local LOCAL=0
155 if [ -f "$APPMANIFEST" ]; then
156 LOCAL=$(grep LastUpdated "$APPMANIFEST" | tr '[:blank:]"' ' ' | tr -s ' ' | cut -d\ -f3)
157 if [ $(isANumber "$LOCAL") -eq 0 ]; then
158 LOCAL=0
159 else
160 date --date="@${LOCAL}" "+%Y-%m-%d %H:%M:%S"
161 return
162 fi
[53]163 fi
[308]164 echo $LOCAL
[53]165}
166
[17]167# Check if a given port range (baseport, baseport+1, baseport+2 each udp)
[19]168# is already in use by any other instance
[17]169# Params:
170# 1: Baseport
[19]171# 2: Current instance (ignored)
[17]172# Returns:
173# 0/1 not in use/in use
174checkGamePortUsed() {
[19]175 local PORTMIN=$1
176 local PORTMAX=$(( $1 + 2 ))
177 local I
[17]178 for I in $(getInstanceList); do
[19]179 if [ "$2" != "$I" ]; then
180 local CURPORTMIN=$(getConfigValue $I "ServerPort")
181 local CURPORTMAX=$(( $CURPORTMIN + 2 ))
182 if [ $PORTMAX -ge $CURPORTMIN -a $PORTMIN -le $CURPORTMAX ]; then
183 echo 1
184 return
185 fi
[17]186 fi
187 done
188 echo 0
189}
190
[19]191# Check if a given TCP port is already in use by any instance (either by control
192# panel or telnet)
[17]193# Params:
194# 1: Port
195# Returns:
196# 0/1 not in use/in use
[19]197checkTCPPortUsed() {
198 local I
[17]199 for I in $(getInstanceList); do
[19]200 if [ "$2" != "$I" ]; then
201 local CURENABLED=$(getConfigValue $I "TelnetEnabled")
202 local CURPORT=$(getConfigValue $I "TelnetPort")
203 if [ "$CURENABLED" = "true" -a $CURPORT -eq $1 ]; then
204 echo 1
205 return
206 fi
207 CURENABLED=$(getConfigValue $I "ControlPanelEnabled")
208 CURPORT=$(getConfigValue $I "ControlPanelPort")
209 if [ "$CURENABLED" = "true" -a $CURPORT -eq $1 ]; then
210 echo 1
211 return
212 fi
[17]213 fi
214 done
215 echo 0
216}
217
218# Send a single command to the telnet port
219# Params:
220# 1: Instance name
221# 2: Command
[46]222# 3: (Optional) Timeout in sec, defaulting to 1
[17]223# Returns:
224# String of telnet output
225telnetCommand() {
[19]226 local TEL_ENABLED=$(getConfigValue $1 TelnetEnabled)
227 local TEL_PORT=$(getConfigValue $1 TelnetPort)
228 local TEL_PASS=$(getConfigValue $1 TelnetPassword)
[88]229 if [ "$TEL_ENABLED" = "true" ]; then
[46]230 local TEMPFILE=$(mktemp)
231 rm -f $TEMPFILE
232 mkfifo $TEMPFILE
233 exec 3<> $TEMPFILE
234 nc 127.0.0.1 $TEL_PORT <&3 &
235 local NCPID=$!
[304]236 disown
[88]237 if [ -n "$TEL_PASS" ]; then
238 printf "$TEL_PASS\n$2\n" >&3
239 else
240 printf "$2\n" >&3
241 fi
[46]242 sleep ${3:-1}
243 printf "exit\n" >&3
244 sleep 0.2
245 kill -9 $NCPID > /dev/null 2>&1
246 exec 3>&-
247 rm -f $TEMPFILE
[17]248 else
[88]249 echo "Telnet not enabled."
[17]250 fi
251}
252
253# Get all hook files for the given hook-name
254# Params:
255# 1: Hook name
[259]256# 2: Instance name
[17]257# Returns:
258# Names of hook files
259getHooksFor() {
[308]260 if [ -n "$2" ]; then
[260]261 if [ -d $(getInstancePath $2)/hooks/$1 ]; then
[259]262 local H
[260]263 for H in $(getInstancePath $2)/hooks/$1/*.sh; do
[259]264 echo "$H"
265 done
266 fi
267 fi
[17]268 if [ -d $SDTD_BASE/hooks/$1 ]; then
[19]269 local H
[17]270 for H in $SDTD_BASE/hooks/$1/*.sh; do
271 echo "$H"
272 done
273 fi
274}
275
276# Lowercase passed string
277# Params:
278# 1: String
279# Returns:
280# Lowercased string
281lowercase() {
282 echo "${1}" | tr "[:upper:]" "[:lower:]"
283}
284
285# Prepare passed string as part of camelcase, i.e. first char upper case, others
286# lowercase
287# Params:
288# 1: String
289# Returns:
290# Transformed string
291camelcasePrep() {
292 echo $(echo "${1:0:1}" | tr "[:lower:]" "[:upper:]")$(echo "${1:1}" | tr "[:upper:]" "[:lower:]")
293}
294
[19]295# Check if given value is a (integer) number
296# Params:
297# 1: Value
298# Returns:
299# 0/1 for NaN / is a number
300isANumber() {
301 if [[ $1 =~ ^[0-9]+$ ]] ; then
302 echo "1"
303 else
304 echo "0"
305 fi
306}
307
308# Check if given value is a boolean (true/false, yes/no, y/n)
309# Params:
310# 1: Value
311# Returns:
312# 0/1
313isABool() {
314 local LOW=$(lowercase "$1")
315 if [ "$LOW" = "false" -o "$LOW" = "true"\
316 -o "$LOW" = "yes" -o "$LOW" = "y"\
317 -o "$LOW" = "no" -o "$LOW" = "n" ]; then
318 echo 1
319 else
320 echo 0
321 fi
322}
323
324# Convert the given value to a boolean 0/1
325# Params:
326# 1: Value
327# Returns:
328# 0/1 as false/true
329getBool() {
330 if [ $(isABool "$1") -eq 0 ]; then
331 echo 0
332 else
333 local LOW=$(lowercase "$1")
334 if [ "$LOW" = "true" -o "$LOW" = "yes" -o "$LOW" = "y" ]; then
335 echo 1
336 else
337 echo 0
338 fi
339 fi
340}
341
[17]342listCommands() {
[19]343 local C
[17]344 for C in $(declare -F | cut -d\ -f3 | grep "^sdtdCommand"\
345 | grep -v "Help$"\
346 | grep -v "Description$"\
347 | grep -v "Expects$"); do
[19]348 local CMD=$(lowercase "${C#sdtdCommand}")
[17]349 printf "%s " "$CMD"
350 done
351}
352
353. /usr/local/lib/7dtd/help.sh
[19]354. /usr/local/lib/7dtd/serverconfig.sh
[17]355for M in /usr/local/lib/7dtd/commands/*.sh; do
356 . $M
357done
358
[64]359checkRootLoadConf
360
Note: See TracBrowser for help on using the repository browser.