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

Last change on this file since 20 was 20, checked in by alloc, 10 years ago

Version 5. Fixes #9, #11

File size: 3.8 KB
RevLine 
[17]1#!/bin/bash
[20]2# Version 5
[17]3
4
[19]5sdtdSubcommandInstancesList() {
[17]6 printf "%-*s | %-*s | %-*s | %-*s\n" 20 "Instance name" 8 "Running" 7 "Players" 5 "Port"
7 printf -v line "%*s-+-%*s-+-%*s-+-%*s\n" 20 " " 8 " " 7 " " 5 " "
8 echo ${line// /-}
9 for I in $(getInstanceList); do
10 if [ $(isRunning $I) -eq 1 ]; then
11 run="yes"
12 tel=$(telnetCommand $I lp)
13 cur=`echo $tel | sed "s/\r/\n/g" | sed "s/^ //g" | grep "Total of " | cut -d\ -f 3`
14 else
15 run="no"
16 cur="-"
17 fi
18
19 max=$(getConfigValue $I ServerMaxPlayerCount)
20 port=$(getConfigValue $I ServerPort)
21
22 printf "%-*s | %*s | %2s/%2d | %5d\n" 20 "$I" 8 "$run" $cur $max $port
23 done
24}
25
[19]26sdtdSubcommandInstancesCreate() {
27 while : ; do
28 readInstanceName
29 [ $(isValidInstance "$INSTANCE") -eq 0 ] && break
30 echo "Instance name already in use."
31 INSTANCE=
32 done
[17]33 echo
[19]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"
[17]70}
71
[19]72sdtdSubcommandInstancesEdit() {
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
93sdtdSubcommandInstancesDelete() {
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
122sdtdCommandInstances() {
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
142}
143
144sdtdCommandInstancesHelp() {
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"
155}
156
[17]157sdtdCommandInstancesDescription() {
158 echo "List all defined instances"
159}
[19]160
161sdtdCommandInstancesExpects() {
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
Note: See TracBrowser for help on using the repository browser.