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

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

Version 7: Added 'instances print_config <instance>'

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