1 | #!/bin/bash
|
---|
2 |
|
---|
3 |
|
---|
4 | sdtdSubcommandInstancesList() {
|
---|
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 |
|
---|
25 | sdtdSubcommandInstancesCreate() {
|
---|
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
|
---|
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 |
|
---|
72 | sdtdSubcommandInstancesEdit() {
|
---|
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]}
|
---|
126 | echo
|
---|
127 | esac
|
---|
128 | done
|
---|
129 | else
|
---|
130 | echo "Instance $1 is currently running. Please stop it first."
|
---|
131 | fi
|
---|
132 | }
|
---|
133 |
|
---|
134 | sdtdSubcommandInstancesDelete() {
|
---|
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 |
|
---|
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 | *)
|
---|
180 | sdtdCommandInstancesHelp
|
---|
181 | ;;
|
---|
182 | esac
|
---|
183 | }
|
---|
184 |
|
---|
185 | sdtdCommandInstancesHelp() {
|
---|
186 | line() {
|
---|
187 | printf " %-*s %s\n" 19 "$1" "$2"
|
---|
188 | }
|
---|
189 |
|
---|
190 | echo "Usage: $(basename $0) instances <subcommand>"
|
---|
191 | echo "Subcommands are:"
|
---|
192 | line "list" "List all defined instances and their status."
|
---|
193 | line "create" "Create a new instance"
|
---|
194 | line "edit <instance>" "Edit an existing instance"
|
---|
195 | line "delete <instance>" "Delete an existing instance"
|
---|
196 | }
|
---|
197 |
|
---|
198 | sdtdCommandInstancesDescription() {
|
---|
199 | echo "List all defined instances"
|
---|
200 | }
|
---|
201 |
|
---|
202 | sdtdCommandInstancesExpects() {
|
---|
203 | case $1 in
|
---|
204 | 2)
|
---|
205 | echo "list create edit delete"
|
---|
206 | ;;
|
---|
207 | 3)
|
---|
208 | case $2 in
|
---|
209 | edit|delete)
|
---|
210 | echo "$(getInstanceList)"
|
---|
211 | ;;
|
---|
212 | esac
|
---|
213 | ;;
|
---|
214 | esac
|
---|
215 | }
|
---|
216 |
|
---|