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

Last change on this file since 111 was 90, checked in by alloc, 10 years ago

v.32 hotfix: Add new commands to admins-template

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