aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/scripts/start-sim
diff options
context:
space:
mode:
Diffstat (limited to '')
-rwxr-xr-xscripts/start-sim152
1 files changed, 152 insertions, 0 deletions
diff --git a/scripts/start-sim b/scripts/start-sim
new file mode 100755
index 0000000..cd28b43
--- /dev/null
+++ b/scripts/start-sim
@@ -0,0 +1,152 @@
1#!/bin/bash
2
3# Figure out where we are, most of this mess is to troll through soft links.
4PRG="$0"
5while [ -h "${PRG}" ] ; do
6 ls=$(ls -ld "${PRG}")
7 link=`expr "${ls}" : '.*-> \(.*\)$'`
8 if expr "${link}" : '.*/.*' > /dev/null; then
9 PRG="${link}"
10 else
11 PRG=$(dirname "${PRG}")/"${link}"
12 fi
13done
14PRGDIR=$(dirname "${PRG}")
15pushd ${PRGDIR} >/dev/null
16PRGDIR=$(pwd)
17popd >/dev/null
18
19
20USER=$(whoami)
21console_name="OpenSim_console"
22tmux_command="tmux -S /var/run/opensim/opensim-tmux.socket"
23tmux_session=${console_name}
24tmux_window="${tmux_command} select-window -t ${tmux_session}"
25tmux_send="${tmux_command} send-keys -t ${tmux_session}"
26bindir="${PRGDIR}/../bin"
27date=$(date '+%F_%T')
28quiet=""
29inventory=""
30
31if [ $USER = "opensim" ]
32then
33 SUDO=""
34else
35 SUDO="sudo -Hu opensim"
36fi
37
38if [ "$1" = "-q" ]
39then
40 quiet="true"
41 shift 1
42fi
43
44if [ "x$1" = "x" ]; then
45 tgt=$(basename $(pwd))
46 num=$(echo ${tgt} | cut -c 4-)
47elif [ -d "./$1" ]; then
48 tgt=$1
49elif [ -d "./sim$1" ]; then
50 num=$1
51 tgt="./sim${num}"
52else
53 tgt=$1
54 inventory=$1
55fi
56
57if [ "x$tgt" = "x" ]; then
58 echo "usage:"
59 echo " $ $(basename $0) <sim>"
60 echo "where <sim> is one of: " robust sim[0-9][0-9]
61 exit 1
62fi
63
64cd ${bindir}
65
66if ( ${tmux_command} -q list-sessions 2>/dev/null | grep -q ${console_name}: ); then
67 true
68else
69 # 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.
70 # After the session is created, we rely on the /var/run/opensim directory to be group sticky, so that anyone in the opensim group can attach to the tmux socket.
71 $SUDO ${tmux_command} new-session -d -s ${console_name} -n "Server"
72fi
73
74
75if [ "x$tgt" = "xROBUST" ]; then
76 exe="Robust"
77 title="ROBUST"
78elif [ "x$inventory" = "x" ]; then
79 exe="OpenSim"
80 # Grab the first Section line of the sims .xml file, cut it down to the name.
81 title=$(grep "<Section " ../config/${tgt}/*.xml | head -n 1 | cut -d '"' -f 2 )
82fi
83
84case $(basename $0) in
85 "start-sim")
86 cmd="mono ${exe}.exe -inidirectory=${tgt} "
87
88 # Check if it's already running.
89 if [ -e /var/run/opensim/${tgt}.pid ]
90 then
91 # Double check if it's REALLY running.
92 if [ "x$(ps -p $(cat /var/run/opensim/${tgt}.pid) --no-headers -o comm)" = "x" ]; then
93 $SUDO rm -f /var/run/opensim/${tgt}.pid
94 fi
95 fi
96 # Now see if it's really really running. lol
97 if [ ! -e /var/run/opensim/${tgt}.pid ]
98 then
99 if [ "x$tgt" = "xROBUST" ]; then
100 $SUDO cat ../config/config.ini ../config/Robust.ini.template >../config/Robust.ini
101 ${tmux_command} split-window -vp 30 -t "${tmux_session}:" "${cmd}"
102 else
103 ${tmux_command} new-window -dn "[${title}]" -t "${tmux_session}:${num}" "${cmd}"
104 fi
105 fi
106
107 if [ "x$quiet" = "x" ]
108 then
109 if [ "x$tgt" = "xROBUST" ]; then
110 ${tmux_window} \; attach-session -t "${tmux_session}"
111 fi
112 fi
113 ;;
114
115 "backup-inventory")
116 user=$($SUDO ${PRGDIR}/gitAR.sh i "${inventory}")
117 # Add the full date and time to create the IAR file name.
118 cmd="save iar -c ${inventory} / password ${PRGDIR}/../../backups/${user}-${date}.iar"
119 # Do it in the highest numbered window.
120 ${tmux_send}:"$" "${cmd}" Enter
121 ${tmux_send}:"$" "force gc" Enter
122 ;;
123
124 "backup-sim")
125 sim=$($SUDO ${PRGDIR}/gitAR.sh o "${title}")
126 # Add the full date and time to create the OAR file name.
127 cmd="save oar --all ${PRGDIR}/../../backups/${sim}-${date}.oar"
128 if [ -e /var/run/opensim/${tgt}.pid ]; then
129 ${tmux_send}:"[${title}]" "${cmd}" Enter
130 # Wait a bit, then generate the map tiles, coz the good generator leaks memory badly if you leave it turned on.
131 sleep 30
132 ${tmux_send}:"[${title}]" "generate map" Enter
133 ${tmux_send}:"[${title}]" "force gc" Enter
134 else
135 echo "No OAR created for ${title}, it's not running."
136 fi
137 ;;
138
139 "stop-sim")
140 if [ -e /var/run/opensim/${tgt}.pid ]; then
141 ${tmux_send}:"[${title}]" "alert WARNING - Emergency shutdown in one minute!" Enter
142 ${tmux_send}:"[${title}]" "alert WARNING - Emergency shutdown in one minute!" Enter
143 sleep 30
144 ${tmux_send}:"[${title}]" "alert WARNING - Emergency shutdown in thirty seconds!" Enter
145 ${tmux_send}:"[${title}]" "alert WARNING - Emergency shutdown in thirty seconds!" Enter
146 sleep 30
147 ${tmux_send}:"[${title}]" "shutdown" Enter
148 else
149 echo "Sim ${title} is not running, so not stopping."
150 fi
151 ;;
152esac