-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjamexec
executable file
·465 lines (403 loc) · 12.1 KB
/
jamexec
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
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
#!/bin/bash
IDIR="${BASH_SOURCE%/*}"
if [[ ! -d "$IDIR" ]]; then IDIR="$PWD"; fi
source "$IDIR/inc/misc_tools.sh"
SHELLPID=$$
trap cleanup SIGTERM SIGINT
TMUX=`which tmux`
die() {
printf '%s\n' "$1" >&2
exit 1
}
show_usage() {
cat << EOF
Stripped down version of jamrun for use by the shell. Unlike jamrun, jamexec
assumes all resources except tmux terminals are available and reuses them.
Usage: jamexec file.jxe [--app=appl_name] [--fog|--cloud] [--num=num_c_devs]
[--data=data-url]
[--tags=quoted_list_of_tags]
[--bg]
[--iflow=flow_name]
[--oflow=flow_name]
[--log]
EOF
}
dojamout() {
dojamout_p1 $1 $2 $3
dojamout_p2 $1 $2 $3
}
dojamout_p1() {
local type=$1
local pnum=$2
local floc=$3
echo "$data" > $floc/$pnum/dataStore
echo "process" > $floc/$pnum/class
echo "$SHELLPID" > $floc/$pnum/shellpid
echo "new" > $floc/$pnum/processId
}
dojamout_p2() {
if [ -z $bg ]; then
dojamout_p2_fg $1 $2 $3
else
dojamout_p2_bg $1 $2 $3
fi
}
cleanup() {
reset
cleanuptmux
exit 1
}
check_jamhome() {
if [ -e jview.json ]; then
if [ -z `printenv JAMHOME` ]; then
die "JAMHOME not set. Point JAMHOME to JAMScript installation and restart."
fi
fi
}
dojamout_p2_fg() {
local type=$1
local pnum=$2
local floc=$3
buildjargs "--app=$jappid" "--port=$pnum" "--data=$data" "--tags=$tags" "--iflow=$iflow" "--oflow=$oflow"
local args=$results
case $type in
cloud)
node jamout.js $args
;;
fog)
node jamout.js $args
;;
device)
check_jamhome
(
if [ -e jview.json ]; then
node $JAMHOME/lib/jview/Compiler.js jview.json
cp -R $JAMHOME/lib/jview/templates/electron electron
cd app
node index.js &
sleep 2
cd ../electron
electron .
fi
) &
echo $! > $floc/$pnum/jviewshell
node jamout.js $args
vpid=`cat $floc/$pnum/jviewshell`
local pid=`ps -o pid= -p $vpid`
if [ ! -z $pid ]; then
kill $pid
fi
;;
esac
echo ">>> Started the J node at port $pnum"
}
dojamout_p2_bg() {
local type=$1
local pnum=$2
local floc=$3
buildjargs "--app=$jappid" "--port=$pnum" "--data=$data" "--tags=$tags" "--iflow=$iflow" "--oflow=$oflow"
node jamout.js $results &
jpid=$!
}
doaout() {
local counter=1
local num=$1
local port=$2
if [ -e a.out ]; then
chmod +x a.out
fi
while [ $counter -le $num ]; do
if [ -e a.out ]; then
# execute the program.. we are in the folder..
buildcargs "-a=$jappid" "-p=$port" "-n=$counter" "-t=$tags"
local cargs=$results
if [ -z $log ]; then
$TMUX new-session -s $tmuxapp-$counter -d "./a.out $cargs"
else
if [ `uname` == "Linux" ]; then
$TMUX new-session -s $tmuxapp-$counter -d script -a -c "./a.out $cargs" -f log
else
$TMUX new-session -s $tmuxapp-$counter -d "script -a -t 1 log ./a.out $cargs"
fi
fi
fi
((counter++))
done
}
setuptmux() {
if [ -z $tmuxid ]; then
tmuxapp="tg-$RANDOM"
else
tmuxapp=$tmuxid
fi
echo "$tmuxapp" > $1/tmuxid
save "$tmuxapp" $appfolder/tmuxid
trap cleanuptmux SIGINT
}
getappid() {
local mainf=$1
local localf=$2
local appid=$3
if [ $appid == "app-n" ]; then
inc_counter $mainf/counter
save "app-$result" $localf/appid
else
save "$appid" $localf/appid
fi
jappid=`cat $localf/appid`
}
cleanuptmux() {
if [ ! -z $tmuxapp ]; then
$TMUX kill-session -t $tmuxapp
fi
exit
}
# We are already in the correct foldler
# We should find MANIFEST if there is a previous version in the current folder
# We unzip to the current folder if needed.
unpack() {
local file=$1
if [ -z $old ]; then
if [ ! -e MANIFEST.txt ]; then
# there is no old file. unpack
unzip -oq $file
if [ $? -ne 0 ]; then
die "Problem reading file: $file"
fi
else
ntime=`zipgrep CREATE $file | awk '{split($0,a, " "); print a[3]}'`
otime=`grep CREATE MANIFEST.txt | awk '{split($0,a, " "); print a[3]}'`
if (( $ntime > $otime )); then
# newer file so unpack it
unzip -oq $file
if [ $? -ne 0 ]; then
die "Problem reading file: $file"
fi
fi
fi
fi
}
###
# Main script execution begins here...
#
# Initialize all the option variables.
app=app-n
type=device
data=127.0.0.1:6379
iport=1883
num=1
tags=
bg=
old=
tmuxid=
log=
iflow=
oflow=
file=$1
shift
fext="${file##*.}"
if [ -z $file ] || [ $file = "-h" ] || [ $file = "--help" ]; then
show_usage
exit 1
fi
if [ "$fext" != "jxe" ]; then
die "Extension on $file is not .jxe"
fi
while :; do
case $1 in
-h|-\?|--help)
show_usage # Display a usage synopsis.
exit
;;
-a|--app) # Takes an option argument; ensure it has been specified.
if [ "$2" ]; then
app=$2
shift
else
die 'ERROR: "--app" requires a non-empty option argument.'
fi
;;
--app=?*)
app=${1#*=} # Delete everything up to "=" and assign the remainder.
;;
--app=) # Handle the case of an empty
die 'ERROR: "--args" requires a non-empty option argument.'
;;
-p|--port) # Takes an option argument; ensure it has been specified.
if [ "$2" ]; then
iport=$2
shift
else
die 'ERROR: "--port" requires a non-empty option argument.'
fi
;;
--port=?*)
iport=${1#*=} # Delete everything up to "=" and assign the remainder.
;;
--port=) # Handle the case of an empty
die 'ERROR: "--port" requires a non-empty option argument.'
;;
-t|--tags) # Takes an option argument; ensure it has been specified.
if [ "$2" ]; then
tags=$2
shift
else
die 'ERROR: "--tags" requires a non-empty option argument.'
fi
;;
--tags=?*)
tags=${1#*=} # Delete everything up to "=" and assign the remainder.
;;
--tags=) # Handle the case of an empty
die 'ERROR: "--tags" requires a non-empty option argument.'
;;
-m|--tmux) # Takes an option argument; ensure it has been specified.
if [ "$2" ]; then
tmuxid=$2
shift
else
die 'ERROR: "--tmux" requires a non-empty option argument.'
fi
;;
--tmux=?*)
tmuxid=${1#*=} # Delete everything up to "=" and assign the remainder.
;;
--tmux=) # Handle the case of an empty
die 'ERROR: "--tmux" requires a non-empty option argument.'
;;
-n|--num) # Takes an option argument; ensure it has been specified.
if [ "$2" ]; then
num=$2
shift
else
die 'ERROR: "--num" requires a non-empty option argument.'
fi
;;
--num=?*)
num=${1#*=} # Delete everything up to "=" and assign the remainder.
;;
--num=) # Handle the case of an empty
die 'ERROR: "--num" requires a non-empty option argument.'
;;
-u|--oflow) # Takes an option argument; ensure it has been specified.
if [ "$2" ]; then
oflow=$2
shift
else
die 'ERROR: "--oflow" requires a non-empty option argument.'
fi
;;
--oflow=?*)
oflow=${1#*=} # Delete everything up to "=" and assign the remainder.
;;
--oflow=) # Handle the case of an empty
die 'ERROR: "--oflow" requires a non-empty option argument.'
;;
-i|--iflow) # Takes an option argument; ensure it has been specified.
if [ "$2" ]; then
iflow=$2
shift
else
die 'ERROR: "--iflow" requires a non-empty option argument.'
fi
;;
--iflow=?*)
iflow=${1#*=} # Delete everything up to "=" and assign the remainder.
;;
--iflow=) # Handle the case of an empty
die 'ERROR: "--iflow" requires a non-empty option argument.'
;;
-d|--data) # Takes an option argument; ensure it has been specified.
if [ "$2" ]; then
data=$2
shift
else
die 'ERROR: "--data" requires a non-empty option argument.'
fi
;;
--data=?*)
data=${1#*=} # Delete everything up to "=" and assign the remainder.
;;
--data=) # Handle the case of an empty
die 'ERROR: "--data" requires a non-empty option argument.'
;;
-f|--fog)
if [ "$type" != "device" ]; then
die 'ERROR: "type" cannot be reassigned.'
else
type="fog"
num=
fi
;;
-c|--cloud)
if [ "$type" != "device" ]; then
die 'ERROR: "type" cannot be reassigned.'
else
type="cloud"
num=
fi
;;
-b|--bg)
bg=1
;;
-l|--log)
log=1
;;
-o|--old)
old=1
;;
--) # End of all options.
shift
break
;;
-?*)
printf 'WARN: Unknown option (ignored): %s\n' "$1" >&2
;;
*) # Default case: No more options, so break out of the loop.
break
esac
shift
done
if [ -e "$file" ]; then
jamfolder=$HOME"/__jamruns"
appfolder=$jamfolder/apps
# Get the folder
filenoext="${file%.*}"
filenoext="${filenoext##*/}"
folder=$appfolder/$filenoext"_"$app
create_missingdir $folder
#save handle to file
ifile="$(cd "$(dirname "$file")"; pwd)/$(basename "$file")"
cd $folder
unpack $ifile # We are already in $folder
if [ -e jamout.js ]; then
# execute the program.. we are in the folder..
case $type in
cloud)
create_missingdir $folder/$iport
getappid $jamfolder $folder/$iport $app
dojamout $type $iport $folder
;;
fog)
create_missingdir $folder/$iport
getappid $jamfolder $folder/$iport $app
dojamout $type $iport $folder
;;
device)
create_missingdir $folder/$iport
getappid $jamfolder $folder/$iport $app
dojamout_p1 $type $iport $folder
setuptmux $folder/$iport
doaout $num $iport
dojamout_p2 $type $iport $folder
if [ -z $bg ]; then
cleanuptmux
fi
;;
esac
else
die "File: $file is not a valid JAMScript executable"
fi
else
die "File: $file not found"
fi