-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjrun
executable file
·388 lines (328 loc) · 9.93 KB
/
jrun
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
#!/bin/bash
IDIR="${BASH_SOURCE%/*}"
if [[ ! -d "$IDIR" ]]; then IDIR="$PWD"; fi
source "$IDIR/inc/misc_tools.sh"
# Edit the following locations to provide the
MOSQUITTO=`which mosquitto`
if [ -z $MOSQUITTO ]; then
MOSQUITTO=/usr/local/sbin/mosquitto
fi
MOSQUITTO_PUB=`which mosquitto_pub`
if [ -z $MOSQUITTO_PUB ]; then
MOSQUITTO_PUB=/usr/local/bin/mosquitto_pub
fi
# No need to edit below this line unless you find a bug!
die() {
printf '%s\n' "$1" >&2
exit 1
}
show_usage() {
cat << EOF
jrun program.jxe
Runs only the J node of a device with program.jxe
under the application name 'testapp'. To run under a different
app name X, use the --app=X option. Use the --fog option to run at the
J node at the fog level or --cloud option to run at the cloud level.
By default, jrun uses a Redis server running at 127.0.0.1:6379 as the
data store. The Redis server needs to be started before launching the
application. To use a different Redis server use the --data option like
the following command using a Redis at local port 7000.
jrun program.jxe --data=127.0.0.1:7000
To provide a set of tags to the program, use the following command.
jrun program.jxe --tag="param1, param2"
Usage: runj file.jxe [--app=appl_name] [--fog|--cloud]
[--data=data-url]
[--tags=quoted_list_of_tags]
[--group=group_name]
The jrun command creates a run state in the $HOME/__jamrun folder.
EOF
}
writegroup() {
local port=$1
local gname=$2
if [ ! -e $port ]; then
mkdir $port
fi
echo $gname > $port/group
}
checkmosquitto() {
# Check the required commands..
if [ ! -e $MOSQUITTO ]; then
die "Mosquitto MQTT broker $MOSQUITTO not found. Set new location."
fi
if [ ! -e $MOSQUITTO_PUB ]; then
die "Mosquitto tools $MOSQUITTO_PUB not found. Set new location."
fi
if [ ! -e $TMUX ]; then
die "terminal multiplexor $TMUX not found. Set new location."
fi
}
startmqtt() {
local port=$1
# Check whether the MQTT server is running.. if not start it
$MOSQUITTO_PUB -p $port -t "test" -m "hello"
if [ $? -ne 0 ]; then
echo "MQTT is not running at $port"
echo "Attempting to start MQTT at $port"
$MOSQUITTO -p $port &
fi
}
startredis() {
# Start redis with no protected-mode and at the port
echo -e "port $1\nprotected-mode no" | redis-server - &
}
waitforredis() {
local port=$1
while : ; do
local present=`ps ax | grep redis-server | grep $port | wc -l`
if [ $present == "1" ]; then
break
fi
sleep 1
done
}
resolvedocker() {
local name=$1
local host="${name%:*}"
local port="${name##*:}"
startredis $port
waitforredis $port
if [ $host == "docker" ]; then
local ipaddr=`hostname -I`
name=$ipaddr:$port
fi
# trim space left behind by hostname -I
data=$(echo $name | tr -d '[:space:]')
}
dojamout() {
dojamout_p1 $1 $2 $3
dojamout_p2 $1 $2 $3
}
dojamout_p1() {
local type=$1
local pnum=$2
local floc=$3
startmqtt $pnum
echo $data > $floc/$pnum/dataStore
echo "process" > $floc/$pnum/class
echo $SHELLPID > $floc/$pnum/shellpid
echo "new" > $floc/$pnum/processId
}
dojamout_p2() {
local type=$1
local pnum=$2
local floc=$3
case $type in
cloud)
if [ -z "$tags" ]; then
node jamout.js --app=$app --cloud --port=$pnum --data=$data
else
node jamout.js --app=$app --cloud --port=$pnum --data=$data --tags=$tags
fi
;;
fog)
if [ -z "$tags" ]; then
node jamout.js --app=$app --fog --port=$pnum --data=$data
else
node jamout.js --app=$app --fog --port=$pnum --data=$data --tags=$tags
fi
;;
device)
if [ -z "$tags" ]; then
node jamout.js --app=$app --port=$pnum --data=$data
else
node jamout.js --app=$app --port=$pnum --data=$data --tags=$tags
fi
;;
esac
}
# This is always scanning for a command with 'node'
portavailable() {
local folder=$1
local port=$2
if [ -d $folder/$port ]; then
if [ -e $folder/$port/processId ]; then
local pid=`cat $folder/$port/processId`
if [ ! -z $pid ]; then
porttaken=`ps -p $pid | grep node | wc -l`
else
porttaken=0
fi
else
porttaken=0
fi
else
porttaken=0
fi
}
# Initialize all the option variables.
app=testapp
type=device
data=127.0.0.1:6379
tags=
group="default"
file=$1
shift
fext="${file##*.}"
if [ -z $file ] || [ $file = "-h" ]; then
show_usage
exit 1
fi
if [ "$fext" != "jxe" ]; then
die "Extension on $file is not .jxe"
fi
# Check whether mosquitto tools are there
checkmosquitto
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: "--app" 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.'
;;
-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.'
;;
-g|--group) # Takes an option argument; ensure it has been specified.
if [ "$2" ]; then
group=$2
shift
else
die 'ERROR: "--group" requires a non-empty option argument.'
fi
;;
--group=?*)
group=${1#*=} # Delete everything up to "=" and assign the remainder.
;;
--group=) # Handle the case of an empty
die 'ERROR: "--group" requires a non-empty option argument.'
;;
-f|--fog)
if [ "$type" != "device" ]; then
die 'ERROR: "type" cannot be reassigned.'
else
type="fog"
fi
;;
-c|--cloud)
if [ "$type" != "device" ]; then
die 'ERROR: "type" cannot be reassigned.'
else
type="cloud"
fi
;;
--) # 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
echo "Don't use jrun and crun! Use jamrun instead of them."
echo "Exiting."
exit
if [ "$type" != "device" ] && [ -n "$num" ]; then
die "number of devices can't be speciied for fog/cloud"
fi
resolvedocker $data
if [ -e "$file" ]; then
# Check whether the global __jamruns folder is there
jamfolder=$HOME"/__jamruns"
create_missingdir $jamfolder
appfolder=$jamfolder/apps
create_missingdir $appfolder
# Get the folder
filenoext="${file%.*}"
folder=$appfolder/$filenoext"_"$app
create_missingdir $folder
# unzip the executable in the folder and run it..
# We are unzipping the copying the files no matter what!
unzip -oq $file -d $folder
if [ $? -ne 0 ]; then
die "Problem reading file: $file"
fi
cd $folder
if [ -e jamout.js ]; then
# execute the program.. we are in the folder..
case $type in
cloud)
iport=9883
while [ : ]; do
portavailable $folder $iport
[[ $porttaken == 1 ]] || break
((iport++))
done
create_missingdir $folder/$iport
dojamout $type $iport $folder
;;
fog)
iport=5883
while [ : ]; do
portavailable $folder $iport
[[ $porttaken == 1 ]] || break
((iport++))
done
create_missingdir $folder/$iport
dojamout $type $iport $folder
;;
device)
iport=1883
while [ : ]; do
portavailable $folder $iport
[[ $porttaken == 1 ]] || break
((iport++))
done
create_missingdir $folder/$iport
writegroup $iport $group
dojamout $type $iport $folder
;;
esac
else
die "Not a valid .jxe file"
fi
else
die ".jxe file not found"
fi