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 |
|
---|
19 | sdtdSubcommandInstancesList() {
|
---|
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 |
|
---|
42 | sdtdSubcommandInstancesCreate() {
|
---|
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 |
|
---|
78 | if [ -f "$SDTD_BASE/templates/admins.xml" ]; then
|
---|
79 | cp "$SDTD_BASE/templates/admins.xml" "$IPATH/"
|
---|
80 | fi
|
---|
81 | chown -R $SDTD_USER:$SDTD_GROUP $IPATH
|
---|
82 | echo "Done"
|
---|
83 | }
|
---|
84 |
|
---|
85 | sdtdSubcommandInstancesEdit() {
|
---|
86 | if [ $(isValidInstance "$1") -eq 0 ]; then
|
---|
87 | echo "No instance given or not a valid instance!"
|
---|
88 | return
|
---|
89 | fi
|
---|
90 |
|
---|
91 | if [ $(isRunning "$1") -eq 0 ]; then
|
---|
92 | local INSTANCE=$1
|
---|
93 | local IPATH=$(getInstancePath "$INSTANCE")
|
---|
94 |
|
---|
95 | $EDITOR $IPATH/config.xml
|
---|
96 | else
|
---|
97 | echo "Instance $1 is currently running. Please stop it first."
|
---|
98 | fi
|
---|
99 | }
|
---|
100 |
|
---|
101 | sdtdSubcommandInstancesDelete() {
|
---|
102 | if [ $(isValidInstance "$1") -eq 0 ]; then
|
---|
103 | echo "No instance given or not a valid instance!"
|
---|
104 | return
|
---|
105 | fi
|
---|
106 |
|
---|
107 | if [ $(isRunning "$1") -eq 0 ]; then
|
---|
108 | local SECCODE=$(dd if=/dev/urandom bs=1 count=100 2>/dev/null \
|
---|
109 | | tr -cd '[:alnum:]' | head -c5)
|
---|
110 | local SECCODEIN
|
---|
111 | echo
|
---|
112 | echo "WARNING: Do you really want to delete the following instance?"
|
---|
113 | echo " $1"
|
---|
114 | echo "This will delete all of its configuration and save data."
|
---|
115 | echo "If you REALLY want to continue enter the following security code:"
|
---|
116 | echo " $SECCODE"
|
---|
117 | echo
|
---|
118 | read -p "Security code: " -e SECCODEIN
|
---|
119 | if [ "$SECCODE" = "$SECCODEIN" ]; then
|
---|
120 | rm -R "$(getInstancePath "$1")"
|
---|
121 | echo "Done"
|
---|
122 | else
|
---|
123 | echo "Security code did not match, aborting."
|
---|
124 | fi
|
---|
125 | else
|
---|
126 | echo "Instance $1 is currently running. Please stop it first."
|
---|
127 | fi
|
---|
128 | }
|
---|
129 |
|
---|
130 | sdtdSubcommandInstancesPrintConfig() {
|
---|
131 | if [ $(isValidInstance "$1") -eq 0 ]; then
|
---|
132 | echo "No instance given or not a valid instance!"
|
---|
133 | return
|
---|
134 | fi
|
---|
135 |
|
---|
136 | local INSTANCE=$1
|
---|
137 | local IPATH=$(getInstancePath "$INSTANCE")
|
---|
138 | cat $IPATH/config.xml
|
---|
139 | }
|
---|
140 |
|
---|
141 | sdtdCommandInstances() {
|
---|
142 | SUBCMD=$1
|
---|
143 | shift
|
---|
144 | case $SUBCMD in
|
---|
145 | list)
|
---|
146 | sdtdSubcommandInstancesList "$@"
|
---|
147 | ;;
|
---|
148 | create)
|
---|
149 | sdtdSubcommandInstancesCreate "$@"
|
---|
150 | ;;
|
---|
151 | edit)
|
---|
152 | sdtdSubcommandInstancesEdit "$@"
|
---|
153 | ;;
|
---|
154 | delete)
|
---|
155 | sdtdSubcommandInstancesDelete "$@"
|
---|
156 | ;;
|
---|
157 | print_config)
|
---|
158 | sdtdSubcommandInstancesPrintConfig "$@"
|
---|
159 | ;;
|
---|
160 | *)
|
---|
161 | sdtdCommandInstancesHelp
|
---|
162 | ;;
|
---|
163 | esac
|
---|
164 | }
|
---|
165 |
|
---|
166 | sdtdCommandInstancesHelp() {
|
---|
167 | line() {
|
---|
168 | printf " %-*s %s\n" 19 "$1" "$2"
|
---|
169 | }
|
---|
170 |
|
---|
171 | echo "Usage: $(basename $0) instances <subcommand>"
|
---|
172 | echo "Subcommands are:"
|
---|
173 | line "list" "List all defined instances and their status."
|
---|
174 | line "create" "Create a new instance"
|
---|
175 | line "edit <instance>" "Edit an existing instance"
|
---|
176 | line "delete <instance>" "Delete an existing instance"
|
---|
177 | }
|
---|
178 |
|
---|
179 | sdtdCommandInstancesDescription() {
|
---|
180 | echo "List all defined instances"
|
---|
181 | }
|
---|
182 |
|
---|
183 | sdtdCommandInstancesExpects() {
|
---|
184 | case $1 in
|
---|
185 | 2)
|
---|
186 | echo "list create edit delete print_config"
|
---|
187 | ;;
|
---|
188 | 3)
|
---|
189 | case $2 in
|
---|
190 | edit|delete|print_config)
|
---|
191 | echo "$(getInstanceList)"
|
---|
192 | ;;
|
---|
193 | esac
|
---|
194 | ;;
|
---|
195 | esac
|
---|
196 | }
|
---|
197 |
|
---|