1 | #!/bin/bash
|
---|
2 |
|
---|
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 |
|
---|
18 |
|
---|
19 | sdtdSubcommandInstancesList() {
|
---|
20 | printf "%-*s | %-*s | %-*s | %-*s | %-*s | %-*s\n" 20 "Instance name" 8 "Running" 7 "Players" 9 "Game Port" 11 "Dashb. Port" 6 "Telnet"
|
---|
21 | printf -v line "%*s-+-%*s-+-%*s-+-%*s-+-%*s-+-%*s\n" 20 " " 8 " " 7 " " 9 " " 11 " " 6 " "
|
---|
22 | echo ${line// /-}
|
---|
23 | for I in $(getInstanceList); do
|
---|
24 | if [ $(isRunning $I) -eq 1 ]; then
|
---|
25 | run="yes"
|
---|
26 | cur=$(telnetCommand $I lp | grep -aE "^\s?Total of " | cut -d\ -f 3)
|
---|
27 | else
|
---|
28 | run="no"
|
---|
29 | cur="-"
|
---|
30 | fi
|
---|
31 |
|
---|
32 | if [ -z $cur ]; then
|
---|
33 | cur="?"
|
---|
34 | fi
|
---|
35 | max=$(getConfigValue $I ServerMaxPlayerCount)
|
---|
36 | port=$(getConfigValue $I ServerPort)
|
---|
37 | cpState="disabled"
|
---|
38 | if [ "$(getConfigValue $I WebDashboardEnabled)" = "true" ]; then
|
---|
39 | cpState="$(getConfigValue $I WebDashboardPort)"
|
---|
40 | fi
|
---|
41 | telnet=$(getConfigValue $I TelnetPort)
|
---|
42 |
|
---|
43 | printf "%-*s | %*s | %2s/%2d | %9d | %*s | %*s\n" 20 "$I" 8 "$run" $cur $max $port 11 "$cpState" 6 "$telnet"
|
---|
44 | done
|
---|
45 | }
|
---|
46 |
|
---|
47 | sdtdSubcommandInstancesCreate() {
|
---|
48 | while : ; do
|
---|
49 | readInstanceName
|
---|
50 | [ $(isValidInstance "$INSTANCE") -eq 0 ] && break
|
---|
51 | echo "Instance name already in use."
|
---|
52 | INSTANCE=
|
---|
53 | done
|
---|
54 | echo
|
---|
55 |
|
---|
56 | local IPATH=$(getInstancePath "$INSTANCE")
|
---|
57 | mkdir -p "$IPATH" 2>/dev/null
|
---|
58 |
|
---|
59 | if [ $(configTemplateExists) -eq 1 ]; then
|
---|
60 | local USETEMPLATE
|
---|
61 | while : ; do
|
---|
62 | read -p "Use the config template? [Yn] " USETEMPLATE
|
---|
63 | USETEMPLATE=${USETEMPLATE:-Y}
|
---|
64 | case $USETEMPLATE in
|
---|
65 | y|Y)
|
---|
66 | cp $SDTD_BASE/templates/config.xml $IPATH/config.xml
|
---|
67 | break
|
---|
68 | ;;
|
---|
69 | n|N)
|
---|
70 | break
|
---|
71 | ;;
|
---|
72 | esac
|
---|
73 | done
|
---|
74 | echo
|
---|
75 | fi
|
---|
76 |
|
---|
77 | if [ ! -f $IPATH/config.xml ]; then
|
---|
78 | cp $SDTD_BASE/engine/serverconfig.xml $IPATH/config.xml
|
---|
79 | fi
|
---|
80 |
|
---|
81 | $EDITOR $IPATH/config.xml
|
---|
82 |
|
---|
83 | if [ -f "$SDTD_BASE/templates/admins.xml" ]; then
|
---|
84 | cp "$SDTD_BASE/templates/admins.xml" "$IPATH/"
|
---|
85 | fi
|
---|
86 | chown -R $SDTD_USER:$SDTD_GROUP $IPATH
|
---|
87 | echo "Done"
|
---|
88 | }
|
---|
89 |
|
---|
90 | sdtdSubcommandInstancesEdit() {
|
---|
91 | if [ $(isValidInstance "$1") -eq 0 ]; then
|
---|
92 | echo "No instance given or not a valid instance!"
|
---|
93 | return
|
---|
94 | fi
|
---|
95 |
|
---|
96 | if [ $(isRunning "$1") -eq 0 ]; then
|
---|
97 | local INSTANCE=$1
|
---|
98 | local IPATH=$(getInstancePath "$INSTANCE")
|
---|
99 |
|
---|
100 | $EDITOR $IPATH/config.xml
|
---|
101 | else
|
---|
102 | echo "Instance $1 is currently running. Please stop it first."
|
---|
103 | fi
|
---|
104 | }
|
---|
105 |
|
---|
106 | sdtdSubcommandInstancesDelete() {
|
---|
107 | if [ $(isValidInstance "$1") -eq 0 ]; then
|
---|
108 | echo "No instance given or not a valid instance!"
|
---|
109 | return
|
---|
110 | fi
|
---|
111 |
|
---|
112 | if [ $(isRunning "$1") -eq 0 ]; then
|
---|
113 | local SECCODE=$(dd if=/dev/urandom bs=1 count=100 2>/dev/null \
|
---|
114 | | tr -cd '[:alnum:]' | head -c5)
|
---|
115 | local SECCODEIN
|
---|
116 | echo
|
---|
117 | echo "WARNING: Do you really want to delete the following instance?"
|
---|
118 | echo " $1"
|
---|
119 | echo "This will delete all of its configuration and save data."
|
---|
120 | echo "If you REALLY want to continue enter the following security code:"
|
---|
121 | echo " $SECCODE"
|
---|
122 | echo
|
---|
123 | read -p "Security code: " -e SECCODEIN
|
---|
124 | if [ "$SECCODE" = "$SECCODEIN" ]; then
|
---|
125 | rm -R "$(getInstancePath "$1")"
|
---|
126 | echo "Done"
|
---|
127 | else
|
---|
128 | echo "Security code did not match, aborting."
|
---|
129 | fi
|
---|
130 | else
|
---|
131 | echo "Instance $1 is currently running. Please stop it first."
|
---|
132 | fi
|
---|
133 | }
|
---|
134 |
|
---|
135 | sdtdSubcommandInstancesPrintConfig() {
|
---|
136 | if [ $(isValidInstance "$1") -eq 0 ]; then
|
---|
137 | echo "No instance given or not a valid instance!"
|
---|
138 | return
|
---|
139 | fi
|
---|
140 |
|
---|
141 | local INSTANCE=$1
|
---|
142 | local IPATH=$(getInstancePath "$INSTANCE")
|
---|
143 | xmlstarlet sel -t -m "//property" -v "@name" -o " = " -v "@value" -nl $IPATH/config.xml | sort
|
---|
144 | }
|
---|
145 |
|
---|
146 | sdtdSubcommandInstancesPrintXml() {
|
---|
147 | if [ $(isValidInstance "$1") -eq 0 ]; then
|
---|
148 | echo "No instance given or not a valid instance!"
|
---|
149 | return
|
---|
150 | fi
|
---|
151 |
|
---|
152 | local INSTANCE=$1
|
---|
153 | local IPATH=$(getInstancePath "$INSTANCE")
|
---|
154 |
|
---|
155 | which pygmentize > /dev/null 2>&1
|
---|
156 | if [ $? -eq 0 ]; then
|
---|
157 | cat $IPATH/config.xml | pygmentize -l xml
|
---|
158 | else
|
---|
159 | cat $IPATH/config.xml
|
---|
160 | fi
|
---|
161 | }
|
---|
162 |
|
---|
163 | sdtdCommandInstances() {
|
---|
164 | SUBCMD=$1
|
---|
165 | shift
|
---|
166 | case $SUBCMD in
|
---|
167 | list)
|
---|
168 | sdtdSubcommandInstancesList "$@"
|
---|
169 | ;;
|
---|
170 | create)
|
---|
171 | sdtdSubcommandInstancesCreate "$@"
|
---|
172 | ;;
|
---|
173 | edit)
|
---|
174 | sdtdSubcommandInstancesEdit "$@"
|
---|
175 | ;;
|
---|
176 | delete)
|
---|
177 | sdtdSubcommandInstancesDelete "$@"
|
---|
178 | ;;
|
---|
179 | print_config)
|
---|
180 | sdtdSubcommandInstancesPrintConfig "$@"
|
---|
181 | ;;
|
---|
182 | print_xml)
|
---|
183 | sdtdSubcommandInstancesPrintXml "$@"
|
---|
184 | ;;
|
---|
185 | *)
|
---|
186 | sdtdCommandInstancesHelp
|
---|
187 | ;;
|
---|
188 | esac
|
---|
189 | }
|
---|
190 |
|
---|
191 | sdtdCommandInstancesHelp() {
|
---|
192 | line() {
|
---|
193 | printf " %-*s %s\n" 19 "$1" "$2"
|
---|
194 | }
|
---|
195 |
|
---|
196 | echo "Usage: $(basename $0) instances <subcommand>"
|
---|
197 | echo "Subcommands are:"
|
---|
198 | line "list" "List all defined instances and their status."
|
---|
199 | line "create" "Create a new instance"
|
---|
200 | line "edit <instance>" "Edit an existing instance"
|
---|
201 | line "delete <instance>" "Delete an existing instance"
|
---|
202 | line "print_config <instance>" "Print the current config of the instance to the console"
|
---|
203 | line "print_xml <instance>" "Print the config XML as is"
|
---|
204 | }
|
---|
205 |
|
---|
206 | sdtdCommandInstancesDescription() {
|
---|
207 | echo "List all defined instances"
|
---|
208 | }
|
---|
209 |
|
---|
210 | sdtdCommandInstancesExpects() {
|
---|
211 | case $1 in
|
---|
212 | 2)
|
---|
213 | echo "list create edit delete print_config print_xml"
|
---|
214 | ;;
|
---|
215 | 3)
|
---|
216 | case $2 in
|
---|
217 | edit|delete|print_config|print_xml)
|
---|
218 | echo "$(getInstanceList)"
|
---|
219 | ;;
|
---|
220 | esac
|
---|
221 | ;;
|
---|
222 | esac
|
---|
223 | }
|
---|
224 |
|
---|