source: scripts/usr/local/lib/7dtd/commands/instances.sh@ 296

Last change on this file since 296 was 258, checked in by alloc, 9 years ago

Scripts: License

File size: 5.4 KB
Line 
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
19sdtdSubcommandInstancesList() {
20 printf "%-*s | %-*s | %-*s | %-*s\n" 20 "Instance name" 8 "Running" 7 "Players" 5 "Port"
21 printf -v line "%*s-+-%*s-+-%*s-+-%*s\n" 20 " " 8 " " 7 " " 5 " "
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 max=$(getConfigValue $I ServerMaxPlayerCount)
33 port=$(getConfigValue $I ServerPort)
34
35 printf "%-*s | %*s | %2s/%2d | %5d\n" 20 "$I" 8 "$run" $cur $max $port
36 done
37}
38
39sdtdSubcommandInstancesCreate() {
40 while : ; do
41 readInstanceName
42 [ $(isValidInstance "$INSTANCE") -eq 0 ] && break
43 echo "Instance name already in use."
44 INSTANCE=
45 done
46 echo
47
48 local IPATH=$(getInstancePath "$INSTANCE")
49 mkdir -p "$IPATH" 2>/dev/null
50
51 if [ $(configTemplateExists) -eq 1 ]; then
52 local USETEMPLATE
53 while : ; do
54 read -p "Use the config template? [Yn] " USETEMPLATE
55 USETEMPLATE=${USETEMPLATE:-Y}
56 case $USETEMPLATE in
57 y|Y)
58 cp $SDTD_BASE/templates/config.xml $IPATH/config.xml
59 loadCurrentConfigValues "$INSTANCE"
60 break
61 ;;
62 n|N)
63 break
64 ;;
65 esac
66 done
67 echo
68 fi
69 configEditAll configQueryValue
70 echo
71 configSetAutoParameters "$INSTANCE"
72 echo
73 echo "Saving"
74
75 if [ ! -f $IPATH/config.xml ]; then
76 echo "<ServerSettings/>" > $IPATH/config.xml
77 fi
78 saveCurrentConfigValues "$INSTANCE"
79 if [ -f "$SDTD_BASE/templates/admins.xml" ]; then
80 cp "$SDTD_BASE/templates/admins.xml" "$IPATH/"
81 fi
82 chown -R $SDTD_USER.$SDTD_GROUP $IPATH
83 echo "Done"
84}
85
86sdtdSubcommandInstancesEdit() {
87 if [ $(isValidInstance "$1") -eq 0 ]; then
88 echo "No instance given or not a valid instance!"
89 return
90 fi
91
92 if [ $(isRunning "$1") -eq 0 ]; then
93 INSTANCE=$1
94 loadCurrentConfigValues "$1"
95
96 while : ; do
97 echo "What section of the config do you want to edit?"
98 local i=0
99 local sects=()
100 for S in $(listConfigEditFuncs); do
101 (( i++ ))
102 sects[$i]=$S
103 printf " %2d: %s\n" $i "$S"
104 done
105 echo
106 echo " W: Save and exit"
107 echo " Q: Exit WITHOUT saving"
108
109 local SEC
110 while : ; do
111 read -p "Section number: " SEC
112 SEC=$(lowercase $SEC)
113 if [ $(isANumber $SEC) -eq 1 ]; then
114 if [ $SEC -ge 1 -a $SEC -le $i ]; then
115 break
116 fi
117 else
118 if [ "$SEC" = "q" -o "$SEC" = "w" ]; then
119 break
120 fi
121 fi
122 echo "Not a valid section number!"
123 done
124 echo
125
126 case "$SEC" in
127 q)
128 echo "Not saving"
129 break
130 ;;
131 w)
132 configSetAutoParameters "$INSTANCE"
133 echo "Saving"
134 saveCurrentConfigValues "$1"
135 echo "Done"
136 break
137 ;;
138 *)
139 configEdit${sects[$SEC]} configQueryValue
140 echo
141 esac
142 done
143 else
144 echo "Instance $1 is currently running. Please stop it first."
145 fi
146}
147
148sdtdSubcommandInstancesDelete() {
149 if [ $(isValidInstance "$1") -eq 0 ]; then
150 echo "No instance given or not a valid instance!"
151 return
152 fi
153
154 if [ $(isRunning "$1") -eq 0 ]; then
155 local SECCODE=$(dd if=/dev/urandom bs=1 count=100 2>/dev/null \
156 | tr -cd '[:alnum:]' | head -c5)
157 local SECCODEIN
158 echo
159 echo "WARNING: Do you really want to delete the following instance?"
160 echo " $1"
161 echo "This will delete all of its configuration and save data."
162 echo "If you REALLY want to continue enter the following security code:"
163 echo " $SECCODE"
164 echo
165 read -p "Security code: " -e SECCODEIN
166 if [ "$SECCODE" = "$SECCODEIN" ]; then
167 rm -R "$(getInstancePath "$1")"
168 echo "Done"
169 else
170 echo "Security code did not match, aborting."
171 fi
172 else
173 echo "Instance $1 is currently running. Please stop it first."
174 fi
175}
176
177sdtdSubcommandInstancesPrintConfig() {
178 if [ $(isValidInstance "$1") -eq 0 ]; then
179 echo "No instance given or not a valid instance!"
180 return
181 fi
182
183 INSTANCE=$1
184 loadCurrentConfigValues "$1"
185
186 configEditAll printConfigValue
187}
188
189sdtdCommandInstances() {
190 SUBCMD=$1
191 shift
192 case $SUBCMD in
193 list)
194 sdtdSubcommandInstancesList "$@"
195 ;;
196 create)
197 sdtdSubcommandInstancesCreate "$@"
198 ;;
199 edit)
200 sdtdSubcommandInstancesEdit "$@"
201 ;;
202 delete)
203 sdtdSubcommandInstancesDelete "$@"
204 ;;
205 print_config)
206 sdtdSubcommandInstancesPrintConfig "$@"
207 ;;
208 *)
209 sdtdCommandInstancesHelp
210 ;;
211 esac
212}
213
214sdtdCommandInstancesHelp() {
215 line() {
216 printf " %-*s %s\n" 19 "$1" "$2"
217 }
218
219 echo "Usage: $(basename $0) instances <subcommand>"
220 echo "Subcommands are:"
221 line "list" "List all defined instances and their status."
222 line "create" "Create a new instance"
223 line "edit <instance>" "Edit an existing instance"
224 line "delete <instance>" "Delete an existing instance"
225}
226
227sdtdCommandInstancesDescription() {
228 echo "List all defined instances"
229}
230
231sdtdCommandInstancesExpects() {
232 case $1 in
233 2)
234 echo "list create edit delete print_config"
235 ;;
236 3)
237 case $2 in
238 edit|delete|print_config)
239 echo "$(getInstanceList)"
240 ;;
241 esac
242 ;;
243 esac
244}
245
Note: See TracBrowser for help on using the repository browser.