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

Last change on this file since 480 was 480, checked in by alloc, 12 months ago

Scripts

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