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

Last change on this file since 502 was 502, checked in by alloc, 45 hours ago

118: Fixed error when trying to edit instances without EDITOR set

File size: 6.2 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() {
[483]20 printf "%-*s | %-*s | %-*s | %-*s | %-*s | %-*s\n" 20 "Instance name" 8 "Running" 7 "Players" 9 "Game Port" 11 "Dashb. Port" 6 "Telnet"
21 printf -v line "%*s-+-%*s-+-%*s-+-%*s-+-%*s-+-%*s\n" 20 " " 8 " " 7 " " 9 " " 11 " " 6 " "
[17]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)
[483]37 cpState="disabled"
38 if [ "$(getConfigValue $I WebDashboardEnabled)" = "true" ]; then
39 cpState="$(getConfigValue $I WebDashboardPort)"
40 fi
41 telnet=$(getConfigValue $I TelnetPort)
[17]42
[483]43 printf "%-*s | %*s | %2s/%2d | %9d | %*s | %*s\n" 20 "$I" 8 "$run" $cur $max $port 11 "$cpState" 6 "$telnet"
[17]44 done
45}
46
[19]47sdtdSubcommandInstancesCreate() {
48 while : ; do
49 readInstanceName
50 [ $(isValidInstance "$INSTANCE") -eq 0 ] && break
51 echo "Instance name already in use."
52 INSTANCE=
53 done
[17]54 echo
[19]55
56 local IPATH=$(getInstancePath "$INSTANCE")
57 mkdir -p "$IPATH" 2>/dev/null
58
59 if [ $(configTemplateExists) -eq 1 ]; then
60 local USETEMPLATE
61 while : ; do
62 read -p "Use the config template? [Yn] " USETEMPLATE
63 USETEMPLATE=${USETEMPLATE:-Y}
64 case $USETEMPLATE in
65 y|Y)
66 cp $SDTD_BASE/templates/config.xml $IPATH/config.xml
67 break
68 ;;
69 n|N)
70 break
71 ;;
72 esac
73 done
[23]74 echo
[19]75 fi
76
77 if [ ! -f $IPATH/config.xml ]; then
[478]78 cp $SDTD_BASE/engine/serverconfig.xml $IPATH/config.xml
[19]79 fi
[502]80
81 if [ -z "$EDITOR" ]; then
82 echo "No EDITOR specified in the global configuration file (7dtd.conf)!"
83 echo "Please add/edit the variable 'EDITOR' and set it to the text editor you would"
84 echo "like to use to edit the XML configs, preferably an editor that supports XML"
85 echo "syntax highlighting for easier editing."
86 echo "Run '7dtd.sh instances edit $INSTANCE' afterwards to edit the configuration."
87 echo
88 else
89 $EDITOR $IPATH/config.xml
90 fi
91
[19]92 if [ -f "$SDTD_BASE/templates/admins.xml" ]; then
93 cp "$SDTD_BASE/templates/admins.xml" "$IPATH/"
94 fi
[480]95 chown -R $SDTD_USER:$SDTD_GROUP $IPATH
[19]96 echo "Done"
[17]97}
98
[19]99sdtdSubcommandInstancesEdit() {
[502]100 if [ -z "$EDITOR" ]; then
101 echo "No EDITOR specified in the global configuration file (7dtd.conf)!"
102 echo "Please add/edit the variable 'EDITOR' and set it to the text editor you would"
103 echo "like to use to edit the XML configs, preferably an editor that supports XML"
104 echo "syntax highlighting for easier editing."
105 return
106 fi
107
[19]108 if [ $(isValidInstance "$1") -eq 0 ]; then
109 echo "No instance given or not a valid instance!"
110 return
111 fi
[23]112
[19]113 if [ $(isRunning "$1") -eq 0 ]; then
[478]114 local INSTANCE=$1
115 local IPATH=$(getInstancePath "$INSTANCE")
[23]116
[478]117 $EDITOR $IPATH/config.xml
[19]118 else
119 echo "Instance $1 is currently running. Please stop it first."
120 fi
121}
122
123sdtdSubcommandInstancesDelete() {
124 if [ $(isValidInstance "$1") -eq 0 ]; then
125 echo "No instance given or not a valid instance!"
126 return
127 fi
128
129 if [ $(isRunning "$1") -eq 0 ]; then
130 local SECCODE=$(dd if=/dev/urandom bs=1 count=100 2>/dev/null \
131 | tr -cd '[:alnum:]' | head -c5)
132 local SECCODEIN
133 echo
134 echo "WARNING: Do you really want to delete the following instance?"
135 echo " $1"
136 echo "This will delete all of its configuration and save data."
137 echo "If you REALLY want to continue enter the following security code:"
138 echo " $SECCODE"
139 echo
140 read -p "Security code: " -e SECCODEIN
141 if [ "$SECCODE" = "$SECCODEIN" ]; then
142 rm -R "$(getInstancePath "$1")"
143 echo "Done"
144 else
145 echo "Security code did not match, aborting."
146 fi
147 else
148 echo "Instance $1 is currently running. Please stop it first."
149 fi
150}
151
[24]152sdtdSubcommandInstancesPrintConfig() {
153 if [ $(isValidInstance "$1") -eq 0 ]; then
154 echo "No instance given or not a valid instance!"
155 return
156 fi
157
[478]158 local INSTANCE=$1
159 local IPATH=$(getInstancePath "$INSTANCE")
[483]160 xmlstarlet sel -t -m "//property" -v "@name" -o " = " -v "@value" -nl $IPATH/config.xml | sort
[24]161}
162
[483]163sdtdSubcommandInstancesPrintXml() {
164 if [ $(isValidInstance "$1") -eq 0 ]; then
165 echo "No instance given or not a valid instance!"
166 return
167 fi
168
169 local INSTANCE=$1
170 local IPATH=$(getInstancePath "$INSTANCE")
171
172 which pygmentize > /dev/null 2>&1
173 if [ $? -eq 0 ]; then
174 cat $IPATH/config.xml | pygmentize -l xml
175 else
176 cat $IPATH/config.xml
177 fi
178}
179
[19]180sdtdCommandInstances() {
181 SUBCMD=$1
182 shift
183 case $SUBCMD in
184 list)
185 sdtdSubcommandInstancesList "$@"
186 ;;
187 create)
188 sdtdSubcommandInstancesCreate "$@"
189 ;;
190 edit)
191 sdtdSubcommandInstancesEdit "$@"
192 ;;
193 delete)
194 sdtdSubcommandInstancesDelete "$@"
195 ;;
[24]196 print_config)
197 sdtdSubcommandInstancesPrintConfig "$@"
198 ;;
[483]199 print_xml)
200 sdtdSubcommandInstancesPrintXml "$@"
201 ;;
[19]202 *)
203 sdtdCommandInstancesHelp
204 ;;
205 esac
206}
207
208sdtdCommandInstancesHelp() {
209 line() {
210 printf " %-*s %s\n" 19 "$1" "$2"
211 }
212
213 echo "Usage: $(basename $0) instances <subcommand>"
214 echo "Subcommands are:"
215 line "list" "List all defined instances and their status."
216 line "create" "Create a new instance"
217 line "edit <instance>" "Edit an existing instance"
218 line "delete <instance>" "Delete an existing instance"
[483]219 line "print_config <instance>" "Print the current config of the instance to the console"
220 line "print_xml <instance>" "Print the config XML as is"
[19]221}
222
[17]223sdtdCommandInstancesDescription() {
224 echo "List all defined instances"
225}
[19]226
227sdtdCommandInstancesExpects() {
228 case $1 in
229 2)
[483]230 echo "list create edit delete print_config print_xml"
[19]231 ;;
232 3)
233 case $2 in
[483]234 edit|delete|print_config|print_xml)
[19]235 echo "$(getInstanceList)"
236 ;;
237 esac
238 ;;
239 esac
240}
241
Note: See TracBrowser for help on using the repository browser.