-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdjamexec
executable file
·220 lines (190 loc) · 6.44 KB
/
djamexec
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
#!/bin/bash
IDIR="${BASH_SOURCE%/*}"
if [[ ! -d "$IDIR" ]]; then IDIR="$PWD"; fi
source "$IDIR/inc/misc_tools.sh"
die() {
printf '%s\n' "$1" >&2
exit 1
}
show_usage() {
cat << EOF
This command executes the given command in the docker containers that are
already executing the target program under the given application ID.
If the program and application ID are not specified, we use the containers
executing the last launched (active) program.
Usage: djamexec [--program=program --app=appl_name --type=node_type] command
NOTE: djamexec differs from the rest because the command is at the end.
The "command" could be JAMScript executable (file.jxe) or some
arbitrary Linux command (e.g., ls) with arguments.
If no running instance is found, an error message will be printed.
EOF
}
if [ -z $1 ]; then
show_usage
exit 0
fi
checkdocker() {
if [ -e $1/dockerId ]; then
local didp=`cat $1/dockerId`
local present=`docker ps --filter id=$didp | grep $didp | wc -l | tr -d '[:space:]'`
if [ $present == "1" ]; then
running="docker"
else
running="none"
fi
fi
}
app=
program=
type=device
xapp=
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.'
;;
-x|--xapp) # Takes an option argument; ensure it has been specified.
if [ "$2" ]; then
xapp=$2
shift
else
die 'ERROR: "--xapp" requires a non-empty option argument.'
fi
;;
--xapp=?*)
xapp=${1#*=} # Delete everything up to "=" and assign the remainder.
;;
--xapp=) # Handle the case of an empty
die 'ERROR: "--xapp" requires a non-empty option argument.'
;;
-p|--program) # Takes an option argument; ensure it has been specified.
if [ "$2" ]; then
program=$2
shift
else
die 'ERROR: "--program" requires a non-empty option argument.'
fi
;;
--program=?*)
program=${1#*=} # Delete everything up to "=" and assign the remainder.
;;
--program=) # Handle the case of an empty
die 'ERROR: "--program" requires a non-empty option argument.'
;;
-t|--type) # Takes an option argument; ensure it has been specified.
if [ "$2" ]; then
type=$2
shift
else
die 'ERROR: "--type" requires a non-empty option argument.'
fi
;;
--type=?*)
type=${1#*=} # Delete everything up to "=" and assign the remainder.
;;
--type=) # Handle the case of an empty
die 'ERROR: "--type" requires a non-empty option argument.'
;;
--) # 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
file=$1
shift
rest=$*
# Check folder
appsfolder=$HOME"/__jamruns/apps"
exit_missingdir $appsfolder "ERROR! No running instance found. "
if [ -z $program ]; then
if [ -e $appsfolder/program ]; then
program=`cat $appsfolder/program`
else
die "Cannot execute with empty program name"
fi
fi
if [ -z $app ]; then
if [ -e $appsfolder/app ]; then
app=`cat $appsfolder/app`
else
die "Cannot execute with empty application ID"
fi
fi
tname="${program%.*}"
tname=$tname"_"$app
fext="${file##*.}"
xyapp=$xapp
if [ ! -z $xapp ]; then
xapp="--app=$xapp"
fi
if [ "$(ls -A $appsfolder)" ]; then
olddir=`pwd`
cd $appsfolder
if [ ! -d $tname ]; then
die "No folder $tname found."
fi
for jruns in `ls $appsfolder/$tname`; do
dir=$appsfolder/$tname/$jruns
echo "Directory: " $dir
if [ -d $dir ]; then
checkdocker $dir
if [ $running == "docker" ]; then
mtype=`cat $dir/machType`
if [ -z $type ] || [ $type == $mtype ]; then
data=`cat $dir/dataStore`
runon=`cat $dir/dockerId`
present=`docker exec -it $runon jamlist | wc -l | tr -d '[:space:]'`
if [ $present != "0" ]; then
# go back to the old directory and run these command otherwise $file won't be found
cd $olddir
case $mtype in
cloud)
if [ $fext == "jxe" ]; then
djamrun $file "$xapp" --cloud --data=$data --runon=$runon --bg
else
docker exec -it $runon $file $rest
fi
;;
fog)
if [ $fext == "jxe" ]; then
djamrun $file "$xapp" --fog --data=$data --runon=$runon --bg
else
docker exec -it $runon $file $rest
fi
;;
device)
if [ $fext == "jxe" ]; then
djamrun $file "$xapp" --data=$data --runon=$runon --num=`cat $dir/cdevs` --bg
else
docker exec -it $runon $file $rest
fi
;;
esac
fi
fi
fi
fi
done
fi