| 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 | %-*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 " "
|
|---|
| 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 | cpState="disabled"
|
|---|
| 38 | if [ "$(getConfigValue $I WebDashboardEnabled)" = "true" ]; then
|
|---|
| 39 | cpState="$(getConfigValue $I WebDashboardPort)"
|
|---|
| 40 | fi
|
|---|
| 41 | telnet=$(getConfigValue $I TelnetPort)
|
|---|
| 42 |
|
|---|
| 43 | printf "%-*s | %*s | %2s/%2d | %9d | %*s | %*s\n" 20 "$I" 8 "$run" $cur $max $port 11 "$cpState" 6 "$telnet"
|
|---|
| 44 | done
|
|---|
| 45 | }
|
|---|
| 46 |
|
|---|
| 47 | sdtdSubcommandInstancesCreate() {
|
|---|
| 48 | while : ; do
|
|---|
| 49 | readInstanceName
|
|---|
| 50 | [ $(isValidInstance "$INSTANCE") -eq 0 ] && break
|
|---|
| 51 | echo "Instance name already in use."
|
|---|
| 52 | INSTANCE=
|
|---|
| 53 | done
|
|---|
| 54 | echo
|
|---|
| 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
|
|---|
| 74 | echo
|
|---|
| 75 | fi
|
|---|
| 76 |
|
|---|
| 77 | if [ ! -f $IPATH/config.xml ]; then
|
|---|
| 78 | cp $SDTD_BASE/engine/serverconfig.xml $IPATH/config.xml
|
|---|
| 79 | fi
|
|---|
| 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 |
|
|---|
| 92 | if [ -f "$SDTD_BASE/templates/admins.xml" ]; then
|
|---|
| 93 | cp "$SDTD_BASE/templates/admins.xml" "$IPATH/"
|
|---|
| 94 | fi
|
|---|
| 95 | chown -R $SDTD_USER:$SDTD_GROUP $IPATH
|
|---|
| 96 | echo "Done"
|
|---|
| 97 | }
|
|---|
| 98 |
|
|---|
| 99 | sdtdSubcommandInstancesEdit() {
|
|---|
| 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 |
|
|---|
| 108 | if [ $(isValidInstance "$1") -eq 0 ]; then
|
|---|
| 109 | echo "No instance given or not a valid instance!"
|
|---|
| 110 | return
|
|---|
| 111 | fi
|
|---|
| 112 |
|
|---|
| 113 | if [ $(isRunning "$1") -eq 0 ]; then
|
|---|
| 114 | local INSTANCE=$1
|
|---|
| 115 | local IPATH=$(getInstancePath "$INSTANCE")
|
|---|
| 116 |
|
|---|
| 117 | $EDITOR $IPATH/config.xml
|
|---|
| 118 | else
|
|---|
| 119 | echo "Instance $1 is currently running. Please stop it first."
|
|---|
| 120 | fi
|
|---|
| 121 | }
|
|---|
| 122 |
|
|---|
| 123 | sdtdSubcommandInstancesDelete() {
|
|---|
| 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 |
|
|---|
| 152 | sdtdSubcommandInstancesPrintConfig() {
|
|---|
| 153 | if [ $(isValidInstance "$1") -eq 0 ]; then
|
|---|
| 154 | echo "No instance given or not a valid instance!"
|
|---|
| 155 | return
|
|---|
| 156 | fi
|
|---|
| 157 |
|
|---|
| 158 | local INSTANCE=$1
|
|---|
| 159 | local IPATH=$(getInstancePath "$INSTANCE")
|
|---|
| 160 | xmlstarlet sel -t -m "//property" -v "@name" -o " = " -v "@value" -nl $IPATH/config.xml | sort
|
|---|
| 161 | }
|
|---|
| 162 |
|
|---|
| 163 | sdtdSubcommandInstancesPrintXml() {
|
|---|
| 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 |
|
|---|
| 180 | sdtdCommandInstances() {
|
|---|
| 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 | ;;
|
|---|
| 196 | print_config)
|
|---|
| 197 | sdtdSubcommandInstancesPrintConfig "$@"
|
|---|
| 198 | ;;
|
|---|
| 199 | print_xml)
|
|---|
| 200 | sdtdSubcommandInstancesPrintXml "$@"
|
|---|
| 201 | ;;
|
|---|
| 202 | *)
|
|---|
| 203 | sdtdCommandInstancesHelp
|
|---|
| 204 | ;;
|
|---|
| 205 | esac
|
|---|
| 206 | }
|
|---|
| 207 |
|
|---|
| 208 | sdtdCommandInstancesHelp() {
|
|---|
| 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"
|
|---|
| 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"
|
|---|
| 221 | }
|
|---|
| 222 |
|
|---|
| 223 | sdtdCommandInstancesDescription() {
|
|---|
| 224 | echo "List all defined instances"
|
|---|
| 225 | }
|
|---|
| 226 |
|
|---|
| 227 | sdtdCommandInstancesExpects() {
|
|---|
| 228 | case $1 in
|
|---|
| 229 | 2)
|
|---|
| 230 | echo "list create edit delete print_config print_xml"
|
|---|
| 231 | ;;
|
|---|
| 232 | 3)
|
|---|
| 233 | case $2 in
|
|---|
| 234 | edit|delete|print_config|print_xml)
|
|---|
| 235 | echo "$(getInstanceList)"
|
|---|
| 236 | ;;
|
|---|
| 237 | esac
|
|---|
| 238 | ;;
|
|---|
| 239 | esac
|
|---|
| 240 | }
|
|---|
| 241 |
|
|---|