source: scripts/usr/local/bin/7dtd-common.sh@ 14

Last change on this file since 14 was 14, checked in by alloc, 11 years ago

More instance validation, switch to XMLStarlet, config.xml instead of serverconfig.xml, admins.xml instead of serveradmin.xml

File size: 2.5 KB
Line 
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
7checkRootLoadConf() {
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
20getInstancePath() {
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
29isValidInstance() {
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
48checkInstanceValid() {
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
65isRunning() {
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
77getInstanceList() {
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
92getInstancePID() {
93 run=$(isRunning $1)
94 if [ $run -eq 1 ]; then
95 cat $(getInstancePath $1)/7dtd.pid
96 else
97 echo 0
98 fi
99}
100
101# Get a single value from a serverconfig
102# Params:
103# 1: Instance name
104# 2: Property name
105# Returns:
106# Property value
107getConfigValue() {
108 CONF=$(getInstancePath $1)/config.xml
109 xmlstarlet sel -t -v "/ServerSettings/property[@name='$2']/@value" $CONF
110}
111
112# Send a single command to the telnet port
113# Params:
114# 1: Instance name
115# 2: Command
116# Returns:
117# String of telnet output
118telnetCommand() {
119 TEL_ENABLED=$(getConfigValue $1 TelnetEnabled)
120 TEL_PORT=$(getConfigValue $1 TelnetPort)
121 TEL_PASS=$(getConfigValue $1 TelnetPassword)
122 if [ "$TEL_ENABLED" = "true" ] && [ -n "$TEL_PASS" ]; then
123 echo -e "$TEL_PASS\n$2\nexit" | nc -q 2 127.0.0.1 $TEL_PORT
124 else
125 echo "Telnet not enabled or no password set."
126 fi
127}
Note: See TracBrowser for help on using the repository browser.