-
Notifications
You must be signed in to change notification settings - Fork 0
/
launchsditunnel.sh
executable file
·320 lines (279 loc) · 7.97 KB
/
launchsditunnel.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
#!/bin/bash
PREFIX=$(dirname $0)
if [ ! -e $PREFIX'/sdi.conf' ]; then
echo "ERROR: The $PREFIX/sdi.conf file does not exist or can not be accessed"
exit 1
fi
source $PREFIX'/sdi.conf'
#test if config is loaded
if test $? != 0; then
echo "ERROR: failed to load $PREFIX/sdi.conf file (launchsditunnel.sh)"
exit 1
elif ! source $PREFIX/misc.sh; then
echo "ERROR: failed to load $PREFIX/misc.sh file (launchsditunnel.sh)"
exit 1
elif ! source $PREFIX/parser.sh; then
echo "ERROR: failed to load $PREFIX/parser.sh file (launchsditunnel.sh)"
exit 1
elif ! source $PREFIX/sendfile.sh; then
echo "WARNING: failed to load $PREFIX/sendfile.sh file"
echo "WARNING: you will not be able to send files to hosts through SDI"
fi
# daemon must be set like this
${DAEMON:=false}
# Check if must use the fast data dir
if test "$USEFASTDATADIR" = "yes"; then
DATADIR="$FASTDATADIR"
fi
# define STATEDIR
STATEDIR=$WWWDIR/states
usage()
{
echo "Usage:"
echo " $0 [options] host1 [host2 [host3 [host... ]]]"
echo "Options:"
echo " --kill=HOST Close the SDI tunnel for HOST"
echo " --killall Close all SDI tunnels and stop SDI application"
echo " --reload-po Force a reload of parser objects file"
echo " --reload-states Force a reload of states files"
}
removecronconfig()
{
crontab -l | grep -v "launchscripts.sh" | crontab -
crontab -l | grep -v "sdictl --sync-data" | crontab -
}
configurecron()
{
# first the basic scripts proccess
script=$(realpath launchscripts.sh)
cron="* * * * * $script minutely"
cron="$cron\n0 * * * * $script hourly"
cron="$cron\n0 0 * * * $script daily"
cron="$cron\n0 0 1 * * $script montly"
cron="$cron\n0 0 * * 0 $script weekly"
# check if we must add the data sync
if test "$USEFASTDATADIR" = "yes"; then
script=$(realpath sdictl)
cron="$cron\n20 */$DATASYNCINTERVAL * * * $script --sync-data"
fi
# add old cron info
cron="$cron\n$(crontab -l| \
egrep -v '(sdictl --sync-data|launchscripts.sh)'| uniq)"
cron="$cron\n"
# update the crontab
printf "$cron" | crontab -
}
# function used to kill the childs of a process
killchilds()
{
PID=$1
CHILDS=$(ps --ppid $PID |awk 'NR>1{print $1}')
if test -d /proc/$PID; then
kill $PID
fi
for CHILD in $CHILDS; do
killchilds $CHILD
done
}
waitend()
{
iter=0
for pid in $*; do
if ps --ppid $pid 2> /dev/null | grep -q "sleep"; then
killchilds $pid
fi
while test -d /proc/$pid; do
if test $iter -ge $KILLTOUT; then
printf "Forced kill signal on pid $pid\n"
kill $pid
break
else
iter=$((iter+1))
fi
sleep 1
done
done
}
notunnelisopen()
{
for pid in $(find $PIDDIRHOSTS -type f -exec cat {} \; 2> /dev/null); do
test -d /proc/$pid && return 1
done
return 0
}
closesdiprocs()
{
printf "Removing cron configuration... "
removecronconfig
printf "done\n"
printf "Waiting savestate to finish... "
PIDFIFO=$(cat $PIDDIRSYS/statesdaemon.pid)
closefifo $PIDFIFO "states.fifo"
printf "done\n"
printf "Stopping SDI services... "
kill $(cat $PIDDIRSYS/*) &> /dev/null
printf "done\n"
}
closehost()
{
local HOST=$1
if test -f $PIDDIRHOSTS/$HOST.sditunnel; then
touch $TMPDIR/${HOST}_FINISH
echo 'killchilds $$' >> $CMDDIR/$HOST
echo "exit 0" >> $CMDDIR/$HOST
sleep 15
echo "exit 0" >> $CMDDIR/$HOST
printf "Waiting $HOST tunnel finish... "
waitend $(cat $PIDDIRHOSTS/$HOST.sditunnel)
printf "done\n"
printf "Blocking $HOST to receive files... "
sendfile -b $HOST
printf "done\n"
if notunnelisopen; then
printf "There are no more SDI tunnels open. "
printf "SDI will be closed now.\n"
closesdiprocs
fi
else
printf "Host $HOST not running.\n"
fi
}
closeallhosts()
{
printf "Waiting tunnels to finish... "
touch $TMPDIR/SDIFINISH
echo 'killchilds $$' >> $CMDGENERAL
echo "exit 0" >> $CMDGENERAL
sleep 15
echo "exit 0" >> $CMDGENERAL
waitend $(find $PIDDIRHOSTS -type f -exec cat {} \; 2> /dev/null)
printf "done\n"
closesdiprocs
}
SDITUNNEL()
{
HOST=$1
SENDCMD=$2
RECEIVECMD=$3
CMDFILE=$CMDDIR/$HOST
SELF=/proc/self/task/*
basename $SELF > $PIDDIRHOSTS/$HOST.sditunnel
SELF=$(cat $PIDDIRHOSTS/$HOST.sditunnel)
. "$SENDDIR/$SENDCMD"
. "$RECEIVEDIR/$RECEIVECMD"
while true; do
rm -f $CMDFILE
touch $CMDFILE
(printf "STATUS+OFFLINE\n";
(cat $HOOKS/onconnect.d/* 2>/dev/null;
tail -fq -n0 $CMDFILE $CMDGENERAL &\
echo $! > $PIDDIRHOSTS/$HOST.tail)| sdisend $HOST;
printf "STATUS+OFFLINE\n") | sdireceive $HOST | PARSE $HOST
#$PREFIX/socketclient $SOCKETPORT "release"
kill $(cat $PIDDIRHOSTS/$HOST.tail) 2> /dev/null &&
rm -f $PIDDIRHOSTS/$HOST.tail
(test -f $TMPDIR/SDIFINISH || test -f $TMPDIR/${HOST}_FINISH) && break
RANDOM=$(date +%N)
sleep $(echo "($RANDOM%600)+120" | bc)
done
rm -f $PIDDIRHOSTS/$HOST.sditunnel
}
LAUNCH ()
{
#If there are SDI tunnels opened, the execution should be stopped
unset HOSTSRUNNING
unset HOSTSTOOPEN
for HOST in $*; do
if test -f $PIDDIRHOSTS/$HOST.sditunnel; then
PID=$(cat $PIDDIRHOSTS/$HOST.sditunnel)
if test -d /proc/$PID; then
HOSTSRUNNING="$HOSTSRUNNING $HOST"
else
HOSTSTOOPEN="$HOSTSTOOPEN $HOST"
fi
else
HOSTSTOOPEN="$HOSTSTOOPEN $HOST"
fi
done
if ! test -z "$HOSTSRUNNING"; then
printf "\tSome SDI tunnels are already opened. Ignoring them.\n"
printf "\tOpened hosts:$HOSTSRUNNING\n"
if ! test -z "$HOSTSTOOPEN"; then
printf "\nThe following tunnels will be opened now.\n"
fi
fi
rm -f $TMPDIR/*FINISH
# Create file that will be used to send commands to all hosts
touch $CMDGENERAL
#Open a tunnel for each host
for HOST in $HOSTSTOOPEN; do
echo $HOST
SENDCMD=$(awk -F':' '$1 ~ /^'$HOST'$/ {print $2}'\
$CLASSESDIR/$CLASS)
test -z "$SENDCMD" && SENDCMD="$DEFAULTSEND"
RECEIVECMD=$(awk -F':' '$1 ~ /^'$HOST'$/ {print $3}'\
$CLASSESDIR/$CLASS)
test -z "$RECEIVECMD" && RECEIVECMD="$DEFAULTRECEIVE"
SDITUNNEL $HOST $SENDCMD $RECEIVECMD &
sleep $LAUNCHDELAY
done
}
if test $# -eq 0 ; then
usage
exit 1
fi
case $1 in
--kill=?*)
closehost $(echo $1| cut -d'=' -f2)
exit 0
;;
--killall)
closeallhosts
if $DOCKER_REGISTRY = 'true'; then
printf "Finalizando Docker Registry..."
docker stop registry
printf "Done...\n"
fi
exit 0
;;
--reload-po)
printf "Sending signal to parsers... "
for PARSERPID in $(cat $PIDDIRSYS/*.parserpid); do
kill -USR1 $PARSERPID 2> /dev/null
done
printf "done\nParser objects will be reloaded.\n"
exit 0
;;
--reload-states)
printf "Sending signal to states... "
kill -USR1 $(cat $PIDDIRSYS/statesdaemon.pid) 2> /dev/null
printf "done\nStates files will be reloaded.\n"
exit 0
;;
-h|--help)
usage
exit 0
;;
-*)
echo "Unknown option."
usage
exit 1
;;
esac
#Create directories
for dir in $TMPDIR $PIDDIR $PIDDIRHOSTS $PIDDIRSYS $CMDDIR $DATADIR \
$STATEDIR $HOOKS $SHOOKS $FIFODIR; do
SDIMKDIR $dir || exit 1
done
#Start launching SDI tunnels
LAUNCH $*
#Initiate crontab
configurecron
if test $DAEMON = true; then
exit 0
else
printf "Waiting SDI Tunnels to finish"
wait $(jobs -p)
printf ".\n"
exit 0
fi