diff options
Diffstat (limited to '')
-rwxr-xr-x | scripts/start-sim | 152 |
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. | ||
4 | PRG="$0" | ||
5 | while [ -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 | ||
13 | done | ||
14 | PRGDIR=$(dirname "${PRG}") | ||
15 | pushd ${PRGDIR} >/dev/null | ||
16 | PRGDIR=$(pwd) | ||
17 | popd >/dev/null | ||
18 | |||
19 | |||
20 | USER=$(whoami) | ||
21 | console_name="OpenSim_console" | ||
22 | tmux_command="tmux -S /var/run/opensim/opensim-tmux.socket" | ||
23 | tmux_session=${console_name} | ||
24 | tmux_window="${tmux_command} select-window -t ${tmux_session}" | ||
25 | tmux_send="${tmux_command} send-keys -t ${tmux_session}" | ||
26 | bindir="${PRGDIR}/../bin" | ||
27 | date=$(date '+%F_%T') | ||
28 | quiet="" | ||
29 | inventory="" | ||
30 | |||
31 | if [ $USER = "opensim" ] | ||
32 | then | ||
33 | SUDO="" | ||
34 | else | ||
35 | SUDO="sudo -Hu opensim" | ||
36 | fi | ||
37 | |||
38 | if [ "$1" = "-q" ] | ||
39 | then | ||
40 | quiet="true" | ||
41 | shift 1 | ||
42 | fi | ||
43 | |||
44 | if [ "x$1" = "x" ]; then | ||
45 | tgt=$(basename $(pwd)) | ||
46 | num=$(echo ${tgt} | cut -c 4-) | ||
47 | elif [ -d "./$1" ]; then | ||
48 | tgt=$1 | ||
49 | elif [ -d "./sim$1" ]; then | ||
50 | num=$1 | ||
51 | tgt="./sim${num}" | ||
52 | else | ||
53 | tgt=$1 | ||
54 | inventory=$1 | ||
55 | fi | ||
56 | |||
57 | if [ "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 | ||
62 | fi | ||
63 | |||
64 | cd ${bindir} | ||
65 | |||
66 | if ( ${tmux_command} -q list-sessions 2>/dev/null | grep -q ${console_name}: ); then | ||
67 | true | ||
68 | else | ||
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" | ||
72 | fi | ||
73 | |||
74 | |||
75 | if [ "x$tgt" = "xROBUST" ]; then | ||
76 | exe="Robust" | ||
77 | title="ROBUST" | ||
78 | elif [ "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 ) | ||
82 | fi | ||
83 | |||
84 | case $(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 | ;; | ||
152 | esac | ||