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

Last change on this file since 488 was 483, checked in by alloc, 13 months ago

Scripts

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() {
[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
[478]80
81 $EDITOR $IPATH/config.xml
82
[19]83 if [ -f "$SDTD_BASE/templates/admins.xml" ]; then
84 cp "$SDTD_BASE/templates/admins.xml" "$IPATH/"
85 fi
[480]86 chown -R $SDTD_USER:$SDTD_GROUP $IPATH
[19]87 echo "Done"
[17]88}
89
[19]90sdtdSubcommandInstancesEdit() {
91 if [ $(isValidInstance "$1") -eq 0 ]; then
92 echo "No instance given or not a valid instance!"
93 return
94 fi
[23]95
[19]96 if [ $(isRunning "$1") -eq 0 ]; then
[478]97 local INSTANCE=$1
98 local IPATH=$(getInstancePath "$INSTANCE")
[23]99
[478]100 $EDITOR $IPATH/config.xml
[19]101 else
102 echo "Instance $1 is currently running. Please stop it first."
103 fi
104}
105
106sdtdSubcommandInstancesDelete() {
107 if [ $(isValidInstance "$1") -eq 0 ]; then
108 echo "No instance given or not a valid instance!"
109 return
110 fi
111
112 if [ $(isRunning "$1") -eq 0 ]; then
113 local SECCODE=$(dd if=/dev/urandom bs=1 count=100 2>/dev/null \
114 | tr -cd '[:alnum:]' | head -c5)
115 local SECCODEIN
116 echo
117 echo "WARNING: Do you really want to delete the following instance?"
118 echo " $1"
119 echo "This will delete all of its configuration and save data."
120 echo "If you REALLY want to continue enter the following security code:"
121 echo " $SECCODE"
122 echo
123 read -p "Security code: " -e SECCODEIN
124 if [ "$SECCODE" = "$SECCODEIN" ]; then
125 rm -R "$(getInstancePath "$1")"
126 echo "Done"
127 else
128 echo "Security code did not match, aborting."
129 fi
130 else
131 echo "Instance $1 is currently running. Please stop it first."
132 fi
133}
134
[24]135sdtdSubcommandInstancesPrintConfig() {
136 if [ $(isValidInstance "$1") -eq 0 ]; then
137 echo "No instance given or not a valid instance!"
138 return
139 fi
140
[478]141 local INSTANCE=$1
142 local IPATH=$(getInstancePath "$INSTANCE")
[483]143 xmlstarlet sel -t -m "//property" -v "@name" -o " = " -v "@value" -nl $IPATH/config.xml | sort
[24]144}
145
[483]146sdtdSubcommandInstancesPrintXml() {
147 if [ $(isValidInstance "$1") -eq 0 ]; then
148 echo "No instance given or not a valid instance!"
149 return
150 fi
151
152 local INSTANCE=$1
153 local IPATH=$(getInstancePath "$INSTANCE")
154
155 which pygmentize > /dev/null 2>&1
156 if [ $? -eq 0 ]; then
157 cat $IPATH/config.xml | pygmentize -l xml
158 else
159 cat $IPATH/config.xml
160 fi
161}
162
[19]163sdtdCommandInstances() {
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 ;;
[24]179 print_config)
180 sdtdSubcommandInstancesPrintConfig "$@"
181 ;;
[483]182 print_xml)
183 sdtdSubcommandInstancesPrintXml "$@"
184 ;;
[19]185 *)
186 sdtdCommandInstancesHelp
187 ;;
188 esac
189}
190
191sdtdCommandInstancesHelp() {
192 line() {
193 printf " %-*s %s\n" 19 "$1" "$2"
194 }
195
196 echo "Usage: $(basename $0) instances <subcommand>"
197 echo "Subcommands are:"
198 line "list" "List all defined instances and their status."
199 line "create" "Create a new instance"
200 line "edit <instance>" "Edit an existing instance"
201 line "delete <instance>" "Delete an existing instance"
[483]202 line "print_config <instance>" "Print the current config of the instance to the console"
203 line "print_xml <instance>" "Print the config XML as is"
[19]204}
205
[17]206sdtdCommandInstancesDescription() {
207 echo "List all defined instances"
208}
[19]209
210sdtdCommandInstancesExpects() {
211 case $1 in
212 2)
[483]213 echo "list create edit delete print_config print_xml"
[19]214 ;;
215 3)
216 case $2 in
[483]217 edit|delete|print_config|print_xml)
[19]218 echo "$(getInstanceList)"
219 ;;
220 esac
221 ;;
222 esac
223}
224
Note: See TracBrowser for help on using the repository browser.