aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/scripts/start-sim
diff options
context:
space:
mode:
authoronefang2020-09-08 21:31:56 +1000
committeronefang2020-09-08 21:31:56 +1000
commitf92e3e7c49fef4d136ddd7d2e7f817cb0b255f4b (patch)
treed78dfa6bf1f8aa4ae47423ddcd3c778be8811e57 /scripts/start-sim
parentAdd examples directory. (diff)
downloadopensim-SC-f92e3e7c49fef4d136ddd7d2e7f817cb0b255f4b.zip
opensim-SC-f92e3e7c49fef4d136ddd7d2e7f817cb0b255f4b.tar.gz
opensim-SC-f92e3e7c49fef4d136ddd7d2e7f817cb0b255f4b.tar.bz2
opensim-SC-f92e3e7c49fef4d136ddd7d2e7f817cb0b255f4b.tar.xz
Add the old bash scripts.
Most of these will eventually be rewritten as C + Lua.
Diffstat (limited to 'scripts/start-sim')
-rwxr-xr-xscripts/start-sim218
1 files changed, 218 insertions, 0 deletions
diff --git a/scripts/start-sim b/scripts/start-sim
new file mode 100755
index 0000000..0a58294
--- /dev/null
+++ b/scripts/start-sim
@@ -0,0 +1,218 @@
1#!/bin/bash
2
3source common.sh
4getPrgDir
5
6USER=$(whoami)
7console_name="SledjChisl"
8tmux_command="tmux -S ${PRGDIR}/../../var/run/opensim-tmux.socket"
9tmux_session=${console_name}
10tmux_window="${tmux_command} select-window -t ${tmux_session}"
11tmux_send="${tmux_command} send-keys -t ${tmux_session}"
12bindir="${PRGDIR}/../bin"
13date=$(date '+%F_%T')
14all=""
15delay=45
16quiet=""
17waiting=""
18inventory=""
19
20function wait_for_text()
21{
22 while :; do
23 sleep 10
24 ${tmux_command} capture-pane -t ${tmux_session}:"${1}" -p | grep -E "${2}" 2>&1 > /dev/null && return
25 done
26}
27
28
29if [ $USER = "${OS_USER}" ]
30then
31 SUDO=""
32else
33 SUDO="sudo -Hu ${OS_USER}"
34fi
35
36if [ "$1" = "-a" ]
37then
38 all="-a"
39 shift 1
40fi
41
42if [ "$1" = "-d" ]
43then
44 delay=$2
45 shift 2
46fi
47
48if [ "$1" = "-q" ]
49then
50 quiet="-q"
51 shift 1
52fi
53
54if [ "$1" = "-w" ]
55then
56 waiting="-w"
57 shift 1
58fi
59
60if [ "x$1" = "x" ]; then
61 tgt=$(basename $(pwd))
62 # These checks are kludgy.
63 if [ "$tgt" = "scripts" ]; then
64 all="-a"
65 cd ${PRGDIR}/../../config/ROBUST
66 tgt="ROBUST"
67 fi
68 if [ "$tgt" = "bin" ]; then
69 uptime
70 wait_for_text "ROBUST" "INITIALIZATION COMPLETE FOR ROBUST"
71 if [ ! "x$all" = "x" ]
72 then
73 echo "Sleeping for $((${delay} / 2)) seconds."
74 sleep $((${delay} / 2))
75 for i in $(seq -w 1 99)
76 do
77 if [ -e "${PRGDIR}/../../config/sim${i}" ]
78 then
79 uptime
80 echo "Starting sim${i}."
81 cd ${PRGDIR}/../../config/sim${i}; ./start-sim -w
82 uptime
83 # Sleep for a while, so that there is plenty of time to finish starting scripts and such,
84 # and we are not keeping the computer very busy if there are lots of sims.
85 echo "Sleeping for ${delay} seconds."
86 sleep ${delay}
87 fi
88 done
89 uptime
90 fi
91 exit 0
92 fi
93 num="$(echo ${tgt} | cut -c 4-)"
94elif [ -d "$1" ]; then
95 tgt=$1
96elif [ -d "sim$1" ]; then
97 num=$1
98 tgt="sim${num}"
99else
100 tgt=$1
101 inventory=$1
102fi
103
104if [ "x$tgt" = "x" ]; then
105 echo "usage:"
106 echo " $ $(basename $0) <sim>"
107 echo "where <sim> is one of: " robust sim[0-9][0-9]
108 exit 1
109fi
110
111cd ${bindir}
112
113if ( ${tmux_command} -q list-sessions 2>/dev/null | grep -q ${console_name}: ); then
114 true
115else
116 # The sudo is only so that the session is owned by opensim, otherwise it's owned by whoever ran this script, which is a likely security hole.
117 # After the session is created, we rely on the ${PRGDIR}/../../caches/ directory to be group sticky, so that anyone in the opensim group can attach to the tmux socket.
118 $SUDO ${tmux_command} new-session -d -s ${console_name} -n "ROBUST"
119fi
120
121
122if [ "x$tgt" = "xROBUST" ]; then
123 exe="Robust"
124 title="ROBUST"
125elif [ "x$inventory" = "x" ]; then
126 exe="OpenSim"
127 # Grab the first RegionName line of the sims .ini file, cut it down to the name.
128 title=$(getSimName ${num})
129fi
130
131case $(basename $0) in
132 "start-sim")
133 cmd="mono ${exe}.exe -inidirectory=../../config/${tgt} "
134
135 # Check if it's already running.
136 if [ -e ${PRGDIR}/../../caches/${tgt}.pid ]
137 then
138 # Double check if it's REALLY running.
139 if [ "x$(ps -p $(cat ${PRGDIR}/../../caches/${tgt}.pid) --no-headers -o comm)" = "x" ]; then
140 $SUDO rm -f ${PRGDIR}/../../caches/${tgt}.pid
141 fi
142 fi
143 # Now see if it's really really running. lol
144 if [ ! -e ${PRGDIR}/../../caches/${tgt}.pid ]
145 then
146 if [ "x$tgt" = "xROBUST" ]; then
147 ${tmux_command} split-window -hp 50 -t "${tmux_session}:" "${cmd}"
148 else
149 ${tmux_command} new-window -dn "${title}" -t "${tmux_session}:${num}" "${cmd}"
150 fi
151 fi
152
153 if [ ! "x$all" = "x" ]
154 then
155 ${tmux_send}:ROBUST.{left} "../scripts/start-sim -a -d ${delay}" Enter
156 fi
157
158 if [ "x$quiet" = "x" ]
159 then
160 if [ "x$tgt" = "xROBUST" ]; then
161 ${tmux_window} \; attach-session -t "${tmux_session}"
162 fi
163 fi
164
165 if [ ! "x$waiting" = "x" ]
166 then
167 wait_for_text "${title}" "INITIALIZATION COMPLETE FOR ${title}"
168 fi
169 ;;
170
171 "backup-inventory")
172 cd ${PRGDIR}
173 $SUDO ${PRGDIR}/gitAR.sh i "${inventory}"
174 user=$(sanitize "${inventory}")
175 # Add the full date and time to create the IAR file name.
176 cmd="save iar -c ${inventory} / password ${PRGDIR}/../../backups/${user}-${date}.iar"
177 # Do it in the highest numbered window.
178 ${tmux_send}:"$" "${cmd}" Enter
179 wait_for_text "$" "Saved archive with [[:digit:]]+ items for ${user/_/ }"
180 ${tmux_send}:"$" "force gc" Enter Enter Enter
181 ;;
182
183 "backup-sim")
184 if [ -e ${PRGDIR}/../../caches/${tgt}.pid ]; then
185 cd ${PRGDIR}
186 $SUDO ${PRGDIR}/gitAR.sh o "${title}"
187 sim=$(sanitize "${title}")
188 # Add the full date and time to create the OAR file name.
189 cmd="save oar --all ${PRGDIR}/../../backups/${sim}-${date}.oar"
190 ${tmux_send}:"${title}" "${cmd}" Enter
191 wait_for_text "${title}" "Finished writing out OAR for ${title}"
192 # Generate the map tiles, coz the good generator leaks memory badly if you leave it turned on.
193 ${tmux_send}:"${title}" "generate map" Enter
194 ${tmux_send}:"${title}" "force gc" Enter Enter Enter
195 else
196 echo "No OAR created for ${title}, it's not running."
197 fi
198 ;;
199
200 "stop-sim")
201 if [ -e ${PRGDIR}/../../caches/${tgt}.pid ]; then
202 ${tmux_send}:"${title}" "alert WARNING - Emergency shutdown in one minute!" Enter
203 ${tmux_send}:"${title}" "alert WARNING - Emergency shutdown in one minute!" Enter
204 sleep 30
205 ${tmux_send}:"${title}" "alert WARNING - Emergency shutdown in thirty seconds!" Enter
206 ${tmux_send}:"${title}" "alert WARNING - Emergency shutdown in thirty seconds!" Enter
207 sleep 30
208 ${tmux_send}:"${title}" "shutdown" Enter
209 sleep 30
210 if [ -e ${PRGDIR}/../../caches/${tgt}.pid ]; then
211 echo "Killing it with extreme prejudice!"
212 kill -TERM `cat ${PRGDIR}/../../caches/${tgt}.pid`
213 fi
214 else
215 echo "Sim ${title} is not running, so not stopping."
216 fi
217 ;;
218esac