Changeset 19
- Timestamp:
- May 23, 2014, 6:39:58 PM (11 years ago)
- Location:
- scripts
- Files:
-
- 3 added
- 5 edited
Legend:
- Unmodified
- Added
- Removed
-
scripts/etc/bash_completion.d/7dtd
r18 r19 1 . /usr/local/lib/7dtd/common.sh 2 checkRootLoadConf 1 if [ $(id -u) -eq 0 ]; then 2 _sdtd() { 3 . /usr/local/lib/7dtd/common.sh 4 checkRootLoadConf 3 5 4 _sdtd() { 5 local cur prev opts 6 COMPREPLY=() 7 cur="${COMP_WORDS[COMP_CWORD]}" 8 prev="${COMP_WORDS[COMP_CWORD-1]}" 6 local cur prev opts 7 COMPREPLY=() 8 cur="${COMP_WORDS[COMP_CWORD]}" 9 prev="${COMP_WORDS[COMP_CWORD-1]}" 9 10 10 opts="help $(listCommands)" 11 12 case "${COMP_CWORD}" in 13 1) 14 COMPREPLY=( $(compgen -W "${opts}" -- ${cur}) ) 15 return 0 16 ;; 17 *) 18 if [ "$(type -t sdtdCommand$(camelcasePrep ${COMP_WORDS[1]})Expects)" = "function" ]; then 19 COMPREPLY=( $(compgen -W "$(sdtdCommand$(camelcasePrep ${COMP_WORDS[1]})Expects $COMP_CWORD)" -- ${cur}) ) 11 opts="help $(listCommands)" 12 13 case "${COMP_CWORD}" in 14 1) 15 COMPREPLY=( $(compgen -W "${opts}" -- ${cur}) ) 20 16 return 0 21 fi 22 ;; 23 esac 24 } 25 complete -F _sdtd 7dtd.sh 17 ;; 18 *) 19 if [ "$(type -t sdtdCommand$(camelcasePrep ${COMP_WORDS[1]})Expects)" = "function" ]; then 20 local words="$(sdtdCommand$(camelcasePrep ${COMP_WORDS[1]})Expects $COMP_CWORD $prev)" 21 COMPREPLY=( $(compgen -W "$words" -- ${cur}) ) 22 return 0 23 fi 24 ;; 25 esac 26 } 27 complete -F _sdtd 7dtd.sh 28 fi -
scripts/usr/local/lib/7dtd/commands/instances.sh
r17 r19 3 3 4 4 5 sdtd CommandInstances() {5 sdtdSubcommandInstancesList() { 6 6 printf "%-*s | %-*s | %-*s | %-*s\n" 20 "Instance name" 8 "Running" 7 "Players" 5 "Port" 7 7 printf -v line "%*s-+-%*s-+-%*s-+-%*s\n" 20 " " 8 " " 7 " " 5 " " … … 22 22 printf "%-*s | %*s | %2s/%2d | %5d\n" 20 "$I" 8 "$run" $cur $max $port 23 23 done 24 exit 0 24 } 25 26 sdtdSubcommandInstancesCreate() { 27 while : ; do 28 readInstanceName 29 [ $(isValidInstance "$INSTANCE") -eq 0 ] && break 30 echo "Instance name already in use." 31 INSTANCE= 32 done 33 echo 34 35 local IPATH=$(getInstancePath "$INSTANCE") 36 mkdir -p "$IPATH" 2>/dev/null 37 38 if [ $(configTemplateExists) -eq 1 ]; then 39 local USETEMPLATE 40 while : ; do 41 read -p "Use the config template? [Yn] " USETEMPLATE 42 USETEMPLATE=${USETEMPLATE:-Y} 43 case $USETEMPLATE in 44 y|Y) 45 cp $SDTD_BASE/templates/config.xml $IPATH/config.xml 46 loadCurrentConfigValues "$INSTANCE" 47 break 48 ;; 49 n|N) 50 break 51 ;; 52 esac 53 done 54 fi 55 configEditAll 56 echo 57 configSetAutoParameters "$INSTANCE" 58 echo 59 echo "Saving" 60 61 if [ ! -f $IPATH/config.xml ]; then 62 echo "<ServerSettings/>" > $IPATH/config.xml 63 fi 64 saveCurrentConfigValues "$INSTANCE" 65 if [ -f "$SDTD_BASE/templates/admins.xml" ]; then 66 cp "$SDTD_BASE/templates/admins.xml" "$IPATH/" 67 fi 68 chown -R $SDTD_USER.$SDTD_GROUP $IPATH 69 echo "Done" 70 } 71 72 sdtdSubcommandInstancesEdit() { 73 if [ $(isValidInstance "$1") -eq 0 ]; then 74 echo "No instance given or not a valid instance!" 75 return 76 fi 77 78 if [ $(isRunning "$1") -eq 0 ]; then 79 INSTANCE=$1 80 loadCurrentConfigValues "$1" 81 configEditAll 82 echo 83 configSetAutoParameters "$INSTANCE" 84 echo 85 echo "Saving" 86 saveCurrentConfigValues "$1" 87 echo "Done" 88 else 89 echo "Instance $1 is currently running. Please stop it first." 90 fi 91 } 92 93 sdtdSubcommandInstancesDelete() { 94 if [ $(isValidInstance "$1") -eq 0 ]; then 95 echo "No instance given or not a valid instance!" 96 return 97 fi 98 99 if [ $(isRunning "$1") -eq 0 ]; then 100 local SECCODE=$(dd if=/dev/urandom bs=1 count=100 2>/dev/null \ 101 | tr -cd '[:alnum:]' | head -c5) 102 local SECCODEIN 103 echo 104 echo "WARNING: Do you really want to delete the following instance?" 105 echo " $1" 106 echo "This will delete all of its configuration and save data." 107 echo "If you REALLY want to continue enter the following security code:" 108 echo " $SECCODE" 109 echo 110 read -p "Security code: " -e SECCODEIN 111 if [ "$SECCODE" = "$SECCODEIN" ]; then 112 rm -R "$(getInstancePath "$1")" 113 echo "Done" 114 else 115 echo "Security code did not match, aborting." 116 fi 117 else 118 echo "Instance $1 is currently running. Please stop it first." 119 fi 120 } 121 122 sdtdCommandInstances() { 123 SUBCMD=$1 124 shift 125 case $SUBCMD in 126 list) 127 sdtdSubcommandInstancesList "$@" 128 ;; 129 create) 130 sdtdSubcommandInstancesCreate "$@" 131 ;; 132 edit) 133 sdtdSubcommandInstancesEdit "$@" 134 ;; 135 delete) 136 sdtdSubcommandInstancesDelete "$@" 137 ;; 138 *) 139 sdtdCommandInstancesHelp 140 ;; 141 esac 25 142 } 26 143 27 144 sdtdCommandInstancesHelp() { 28 echo "Usage: $(basename $0) instances" 29 echo 30 echo "List all defined instances and their status." 145 line() { 146 printf " %-*s %s\n" 19 "$1" "$2" 147 } 148 149 echo "Usage: $(basename $0) instances <subcommand>" 150 echo "Subcommands are:" 151 line "list" "List all defined instances and their status." 152 line "create" "Create a new instance" 153 line "edit <instance>" "Edit an existing instance" 154 line "delete <instance>" "Delete an existing instance" 31 155 } 32 156 … … 34 158 echo "List all defined instances" 35 159 } 160 161 sdtdCommandInstancesExpects() { 162 case $1 in 163 2) 164 echo "list create edit delete" 165 ;; 166 3) 167 case $2 in 168 edit|delete) 169 echo "$(getInstanceList)" 170 ;; 171 esac 172 ;; 173 esac 174 } 175 -
scripts/usr/local/lib/7dtd/commands/status.sh
r17 r19 53 53 54 54 echo 55 exit 056 55 } 57 56 -
scripts/usr/local/lib/7dtd/commands/updateengine.sh
r17 r19 7 7 for I in $(getInstanceList); do 8 8 if [ $(isRunning $I) -eq 1 ]; then 9 echo "At least one instance is still running ."9 echo "At least one instance is still running (\"$I\")." 10 10 echo "Before updating the engine please stop all instances!" 11 11 return -
scripts/usr/local/lib/7dtd/common.sh
r17 r19 22 22 } 23 23 24 # Check if the given instance name is valid (no blanks, no special chars, 25 # only letters, digits, underscore, hyphen -> [A-Za-z0-9_\-]) 26 # Params: 27 # 1: Instance name 28 # Returns: 29 # 0/1 instance not valid/valid 30 isValidInstanceName() { 31 if [[ "$1" =~ ^[A-Za-z0-9_\-]+$ ]]; then 32 echo 1 33 else 34 echo 0 35 fi 36 } 37 24 38 # Check if the given instance name is an existing instance 25 39 # Params: … … 28 42 # 0/1 instance not valid/valid 29 43 isValidInstance() { 30 if [ -z $1 ]; then 31 echo 0 32 else 33 if [ ! -d $(getInstancePath $1) ]; then 34 echo 0 35 else 36 if [ ! -f $(getInstancePath $1)/config.xml ]; then 37 echo 0 38 else 39 echo 1 40 fi 41 fi 42 fi 44 if [ ! -z "$1" ]; then 45 if [ $(isValidInstanceName "$1") -eq 1 ]; then 46 if [ -d $(getInstancePath "$1") ]; then 47 if [ -f $(getInstancePath "$1")/config.xml ]; then 48 echo 1 49 return 50 fi 51 fi 52 fi 53 fi 54 echo 0 43 55 } 44 56 … … 62 74 # List of instances 63 75 getInstanceList() { 76 local IF 64 77 for IF in $SDTD_BASE/instances/*; do 65 I=`basename $IF`78 local I=`basename $IF` 66 79 if [ $(isValidInstance $I) -eq 1 ]; then 67 80 echo $I … … 84 97 } 85 98 86 # Get a single value from a serverconfig87 # Params:88 # 1: Instance name89 # 2: Property name90 # Returns:91 # Property value92 getConfigValue() {93 CONF=$(getInstancePath $1)/config.xml94 $XMLSTARLET sel -t -v "/ServerSettings/property[@name='$2']/@value" $CONF95 }96 97 # Update a single value in a serverconfig98 # Params:99 # 1: Instance name100 # 2: Property name101 # 3: New value102 setConfigValue() {103 CONF=$(getInstancePath $1)/config.xml104 $XMLSTARLET ed -L -u "/ServerSettings/property[@name='$2']/@value" -v "$3" $CONF105 }106 107 99 # Check if a given port range (baseport, baseport+1, baseport+2 each udp) 108 # is already in use by any instance100 # is already in use by any other instance 109 101 # Params: 110 102 # 1: Baseport 103 # 2: Current instance (ignored) 111 104 # Returns: 112 105 # 0/1 not in use/in use 113 106 checkGamePortUsed() { 114 PORTMIN=$1 115 PORTMAX=$(( $1 + 2 )) 107 local PORTMIN=$1 108 local PORTMAX=$(( $1 + 2 )) 109 local I 116 110 for I in $(getInstanceList); do 117 CURPORTMIN=$(getConfigValue $I "ServerPort") 118 CURPORTMAX=$(( $CURPORTMIN + 2 )) 119 if [ $PORTMAX -ge $CURPORTMIN -a $PORTMIN -le $CURPORTMAX ]; then 120 echo 1 121 return 111 if [ "$2" != "$I" ]; then 112 local CURPORTMIN=$(getConfigValue $I "ServerPort") 113 local CURPORTMAX=$(( $CURPORTMIN + 2 )) 114 if [ $PORTMAX -ge $CURPORTMIN -a $PORTMIN -le $CURPORTMAX ]; then 115 echo 1 116 return 117 fi 122 118 fi 123 119 done … … 125 121 } 126 122 127 # Check if a given telnet port is already in use by any instance 123 # Check if a given TCP port is already in use by any instance (either by control 124 # panel or telnet) 128 125 # Params: 129 126 # 1: Port 130 127 # Returns: 131 128 # 0/1 not in use/in use 132 checkTelnetPortUsed() { 129 checkTCPPortUsed() { 130 local I 133 131 for I in $(getInstanceList); do 134 CURENABLED=$(getConfigValue $I "TelnetEnabled") 135 CURPORT=$(getConfigValue $I "TelnetPort") 136 if [ "$CURENABLED" = "true" -a $CURPORT -eq $1 ]; then 137 echo 1 138 return 132 if [ "$2" != "$I" ]; then 133 local CURENABLED=$(getConfigValue $I "TelnetEnabled") 134 local CURPORT=$(getConfigValue $I "TelnetPort") 135 if [ "$CURENABLED" = "true" -a $CURPORT -eq $1 ]; then 136 echo 1 137 return 138 fi 139 CURENABLED=$(getConfigValue $I "ControlPanelEnabled") 140 CURPORT=$(getConfigValue $I "ControlPanelPort") 141 if [ "$CURENABLED" = "true" -a $CURPORT -eq $1 ]; then 142 echo 1 143 return 144 fi 139 145 fi 140 146 done … … 149 155 # String of telnet output 150 156 telnetCommand() { 151 TEL_ENABLED=$(getConfigValue $1 TelnetEnabled)152 TEL_PORT=$(getConfigValue $1 TelnetPort)153 TEL_PASS=$(getConfigValue $1 TelnetPassword)157 local TEL_ENABLED=$(getConfigValue $1 TelnetEnabled) 158 local TEL_PORT=$(getConfigValue $1 TelnetPort) 159 local TEL_PASS=$(getConfigValue $1 TelnetPassword) 154 160 if [ "$TEL_ENABLED" = "true" ] && [ -n "$TEL_PASS" ]; then 155 161 echo -e "$TEL_PASS\n$2\nexit" | nc -q 2 127.0.0.1 $TEL_PORT … … 166 172 getHooksFor() { 167 173 if [ -d $SDTD_BASE/hooks/$1 ]; then 174 local H 168 175 for H in $SDTD_BASE/hooks/$1/*.sh; do 169 176 echo "$H" … … 191 198 } 192 199 200 # Check if given value is a (integer) number 201 # Params: 202 # 1: Value 203 # Returns: 204 # 0/1 for NaN / is a number 205 isANumber() { 206 if [[ $1 =~ ^[0-9]+$ ]] ; then 207 echo "1" 208 else 209 echo "0" 210 fi 211 } 212 213 # Check if given value is a boolean (true/false, yes/no, y/n) 214 # Params: 215 # 1: Value 216 # Returns: 217 # 0/1 218 isABool() { 219 local LOW=$(lowercase "$1") 220 if [ "$LOW" = "false" -o "$LOW" = "true"\ 221 -o "$LOW" = "yes" -o "$LOW" = "y"\ 222 -o "$LOW" = "no" -o "$LOW" = "n" ]; then 223 echo 1 224 else 225 echo 0 226 fi 227 } 228 229 # Convert the given value to a boolean 0/1 230 # Params: 231 # 1: Value 232 # Returns: 233 # 0/1 as false/true 234 getBool() { 235 if [ $(isABool "$1") -eq 0 ]; then 236 echo 0 237 else 238 local LOW=$(lowercase "$1") 239 if [ "$LOW" = "true" -o "$LOW" = "yes" -o "$LOW" = "y" ]; then 240 echo 1 241 else 242 echo 0 243 fi 244 fi 245 } 246 193 247 listCommands() { 248 local C 194 249 for C in $(declare -F | cut -d\ -f3 | grep "^sdtdCommand"\ 195 250 | grep -v "Help$"\ 196 251 | grep -v "Description$"\ 197 252 | grep -v "Expects$"); do 198 CMD=$(lowercase "${C#sdtdCommand}")253 local CMD=$(lowercase "${C#sdtdCommand}") 199 254 printf "%s " "$CMD" 200 255 done … … 203 258 . /usr/local/lib/7dtd/help.sh 204 259 . /usr/local/lib/7dtd/playerlog.sh 260 . /usr/local/lib/7dtd/serverconfig.sh 205 261 for M in /usr/local/lib/7dtd/commands/*.sh; do 206 262 . $M
Note:
See TracChangeset
for help on using the changeset viewer.