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

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

Scripts 109

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