-
Notifications
You must be signed in to change notification settings - Fork 38
/
run.sh
executable file
·419 lines (374 loc) · 11.4 KB
/
run.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
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
#!/bin/bash
function help() {
echo "Usage: ./run.sh [centos|ubuntu|ports|ssh-config|deploy-ear] [1|3] [ndb|hopsworks|hops|jim|antonios||etc] [no-random-ports] [udp-hack]"
echo ""
echo "Create a custom cluster definition from an existing one:"
echo "cp cluster-defns/1.hopsworks.yml cluster-defns/1.jim.yml"
echo ""
echo "For a hopsworks cluster on ubuntu or centos, run:"
echo "./run.sh ubuntu 1 jim"
echo "./run.sh centos 1 centos"
exit 1
}
port=
forwarded_port=
ports=
VBOX_MANAGE=/usr/bin/VBoxManage
OCTETS="192.168."
ORIGINAL_OCTETS=${OCTETS}"56"
GB=1024
DISK_SOFT_LIMIT=$(( 80 * GB ))
DISK_HARD_LIMIT=$(( 30 * GB ))
RED='\033[0;31m'
YELLOW='\033[1;33m'
NC='\033[0m'
function check_available_disk_space() {
available_mb_str=$(df . --output=avail -B MB | grep -v "\W")
available_mb=${available_mb_str//[^0-9]*/}
echo "Available hard disk space on device: $available_mb_str HARD limit : ${DISK_HARD_LIMIT}MB SOFT limit: ${DISK_SOFT_LIMIT}MB"
if [ $available_mb -le $DISK_HARD_LIMIT ]
then
echo -e "${RED}*****************************************************${NC}"
echo -e "${RED}* *${NC}"
echo -e "${RED}* Error: Not enough disk space left on device :( *${NC}"
echo -e "${RED}* *${NC}"
echo -e "${RED}*****************************************************${NC}"
exit 3
elif [ $available_mb -le $DISK_SOFT_LIMIT ]
then
echo -e "${YELLOW}**************************************************************${NC}"
echo -e "${YELLOW}* *${NC}"
echo -e "${YELLOW}* Warning: Available disk space on device is getting low *${NC}"
echo -e "${YELLOW}* *${NC}"
echo -e "${YELLOW}**************************************************************${NC}"
sleep 2
fi
}
HEAD_VM_MEMORY_MB=$(( 27 * GB ))
WORKER_VM_MEMORY_MB=$(( 18 * GB ))
MEM_SOFT_LIMIT=$(( 10 * GB ))
MEM_HARD_LIMIT=$(( 2 * GB ))
function check_available_memory() {
available_memory_mb=$(free --mega | grep -e "^Mem" | awk '{ print $7 }')
num_vms=$1
mem_cluster_total_mb=$HEAD_VM_MEMORY_MB
if [ $num_vms -gt 1 ]
then
mem_cluster_total_mb=$(( HEAD_VM_MEMORY_MB + (num_vms - 1) * WORKER_VM_MEMORY_MB ))
fi
memory_left_mb=$(( available_memory_mb - mem_cluster_total_mb ))
echo "Available memory on device ${available_memory_mb}MB Required: ${mem_cluster_total_mb}MB HARD limit: ${MEM_HARD_LIMIT}MB SOFT limit: ${MEM_SOFT_LIMIT}"
if [ $memory_left_mb -le $MEM_HARD_LIMIT ]
then
echo -e "${RED}*************************************************${NC}"
echo -e "${RED}* *${NC}"
echo -e "${RED}* Error: Not enough memory left on device :( *${NC}"
echo -e "${RED}* *${NC}"
echo -e "${RED}*************************************************${NC}"
exit 3
elif [ $memory_left_mb -le $MEM_SOFT_LIMIT ]
then
echo -e "${YELLOW}**********************************************************${NC}"
echo -e "${YELLOW}* *${NC}"
echo -e "${YELLOW}* Warning: Available memory on device is getting low *${NC}"
echo -e "${YELLOW}* *${NC}"
echo -e "${YELLOW}**********************************************************${NC}"
sleep 2
fi
}
function replace_port() {
res=0
p=
while [ $res -eq 0 ] ; do
p=`shuf -i 20000-65000 -n 1`
# If the port is already in the file, try again
grep $p Vagrantfile
r1=$?
$(ss -lptn | grep $p 2>&1 > /dev/null)
r2=$?
if [ $r1 -eq 0 ] || [ $r2 -eq 0 ] ; then
p=`shuf -i 2000-65000 -n 1`
else
res=1
fi
done
if [ "$forwarded_port" != "22" ] ; then
if [ "$forwarded_port" == "42011" ] ; then
echo "dela port 42011 - leave it alone"
elif [ "$forwarded_port" == "42012" ] ; then
echo "dela port 42012 - leave it alone"
elif [ "$forwarded_port" == "42013" ] ; then
echo "dela port 42013 - leave it alone"
# if [ "$forwarded_port" == "9090" ] ; then
# echo "9090 - leave it alone"
# else if [ "$forwarded_port" == "8080" ] ; then
# perl -pi -e "s/$forwarded_port/$p/g" Vagrantfile
# perl -pi -e "s/$forwarded_port/$p/" cluster.yml
# http_port=$p
# echo "http_port -> $p"
else
perl -pi -e "s/$forwarded_port/$p/g" Vagrantfile
perl -pi -e "s/$p/$forwarded_port/" Vagrantfile
echo "$port -> $p"
fi
else
echo "New port is: $p"
sed "0,/10022/s/10022/$p/" Vagrantfile > Vagrantfile.new
sed "0,/10023/s/10023/$(expr $p + 1)/" Vagrantfile.new > Vagrantfile
sed "0,/10024/s/10024/$(expr $p + 2)/" Vagrantfile > Vagrantfile.new
mv Vagrantfile.new Vagrantfile
fi
}
function parse_ports() {
SAVEIFS=$IFS
# Change IFS to new line.
IFS=$'\n'
ports=$(grep forward Vagrantfile | grep -Eo '[0-9]{2,5}'|xargs)
count=0
ports=($ports)
# Restore IFS
IFS=$SAVEIFS
for i in $ports ; do
odd=$(($count % 2))
if [ $odd -eq 1 ] ; then
echo "$i"
else
echo -n "$i -> "
fi
count=$(($count + 1))
done
}
function machine_name() {
machine_ips=$(hostname -I)
for i in dev0 dev1 dev2 dev3 dev4 dev5 dev6; do
if echo $machine_ips | grep -q $(dig +short $i.hops.works | grep -v '\.$'); then
echo $i
break
fi
done
}
function deploy_ear() {
if [ -f ${HOME}/.ssh/id_rsa.pub ] ; then
ssh_key=$(cat ${HOME}/.ssh/id_rsa.pub)
grep "$ssh_key" ~/.ssh/authorized_keys >/dev/null 2>&1
if [ $? -ne 0 ] ; then
cat ~/.ssh/id_rsa.pub >> ~/.ssh/authorized_keys
fi
else
echo "----------------------------------------------------------"
echo "WARNING: the deploy-ear.sh script will not work as you do not have a public-private key"
echo "Creating a public/private ssh key with no password with this command:"
echo "cat /dev/zero | ssh-keygen -m PEM -q -N > /dev/null "
echo "----------------------------------------------------------"
cat /dev/zero | ssh-keygen -m PEM -q -N > /dev/null
fi
name=$(machine_name)
echo ""
echo ""
echo "You can redeploy your ear on Vagrant after you follow these instructions: "
echo "On $name.hops.works:"
echo ""
echo "mkdir Projects"
echo "cd Projects"
echo "# Change this from jimdowling to your private github repo"
echo "git clone [email protected]:jimdowling/hopsworks-ee.git"
echo "cd Projects/hopsworks-ee"
echo "./scripts/allow-cors.sh"
echo "mvn clean install -Pkube,web,jupyter-git -DskipTests"
echo ""
echo "cd ~/karamel-chef"
echo "vagrant ssh"
echo "#now on vagrant, copies the hopsworks-ear.ear file from $name, and deploys it to glassfish"
echo "./deploy-ear.sh"
echo ""
}
function ssh_config() {
name=$(machine_name)
echo "=========================================="
echo ""
echo "Copy and paste the following into ~/.ssh/config"
echo "Make sure permissions are correct:"
echo "chmod 600 ~/.ssh/config"
echo ""
echo "=========================================="
echo ""
echo ""
echo "Host $name"
echo " Hostname $name.hops.works"
echo " User $USER"
echo "Host vagrant"
echo " Hostname $name.hops.works"
echo " User $USER"
echo " ProxyJump $name"
DEBUG_PORT=0
HTTPS_PORT=0
GLASSFISH_PORT=0
KARAMEL_PORT=0
SSH_PORT=0
SAVEIFS=$IFS
# Change IFS to new line.
IFS=$'\n'
ports=$(grep forward Vagrantfile | grep -Eo '[0-9]{2,5}'|xargs)
count=0
ports=($ports)
# Restore IFS
IFS=$SAVEIFS
for i in $ports ; do
odd=$(($count % 2))
if [ $odd -eq 1 ] ; then
if [ $DEBUG_PORT -eq 9009 ] ; then
DEBUG_PORT=$i
fi
if [ $HTTPS_PORT -eq 8181 ] ; then
HTTPS_PORT=$i
fi
if [ $GLASSFISH_PORT -eq 4848 ] ; then
GLASSFISH_PORT=$i
fi
if [ $KARAMEL_PORT -eq 9090 ] ; then
KARAMEL_PORT=$i
fi
if [ $SSH_PORT -eq 22 ] ; then
SSH_PORT=$i
fi
else
if [ $i -eq 9009 ] ; then
DEBUG_PORT=9009
fi
if [ $i -eq 8181 ] ; then
HTTPS_PORT=8181
fi
if [ $i -eq 4848 ] ; then
GLASSFISH_PORT=4848
fi
if [ $i -eq 9090 ] ; then
KARAMEL_PORT=9090
fi
if [ $i -eq 22 ] ; then
SSH_PORT=22
fi
fi
count=$(($count + 1))
done
echo " LocalForward 9009 localhost:$DEBUG_PORT"
echo " LocalForward 8181 localhost:$HTTPS_PORT"
echo " LocalForward 4848 localhost:$GLASSFISH_PORT"
echo " LocalForward 9090 localhost:$KARAMEL_PORT"
echo ""
echo "Host ${name}_vagrant"
echo " HostName $name.hops.works"
echo " ProxyJump $name"
echo " User vagrant"
echo " Port $SSH_PORT"
echo " IdentityFile ~/.vagrant.d/insecure_private_key"
echo ""
echo ""
}
function change_subnet() {
priv_subnets=($($VBOX_MANAGE list hostonlyifs | grep "IPAddress:" | awk -F' ' '{print $2}' | awk -F'.' '{print $3}'))
if [ ${#priv_subnets[@]} -gt 0 ]; then
new_subnet=-1
while [ $new_subnet -eq -1 ]
do
tentative=$(($RANDOM % 255))
present=0
for i in "${priv_subnets[@]}"
do
if [ "$i" == "$tentative" ]; then
present=1
break
fi
done
if [ $present -eq 0 ]; then
new_subnet=$tentative
fi
done
new_octets=${OCTETS}${new_subnet}
sed -i "s/${ORIGINAL_OCTETS}/${new_octets}/g" Vagrantfile
sed -i "s/${ORIGINAL_OCTETS}/${new_octets}/g" cluster.yml
fi
}
if [ "$1" == "ports" ] ; then
parse_ports
exit 0
fi
if [ "$1" == "ssh-config" ] ; then
ssh_config
exit 0
fi
if [ "$1" == "deploy-ear" ] ; then
deploy_ear
exit 0
fi
if [ $# -lt 3 ] ; then
help
fi
check_available_disk_space
check_available_memory $2
PORTS=1
if [ $# -eq 4 ] ; then
if [ $4 != "no-random-ports" ] ; then
help
fi
PORTS=0
fi
UDP_HACK=0
if [ $# -eq 5 ] ; then
if [ $4 != "no-random-ports" ] ; then
help
fi
PORTS=0
if [ $5 != "udp-hack" ] ; then
help
fi
UDP_HACK=1
fi
#set -e
if [ ! -f vagrantfiles/Vagrantfile.$1.$2 ] ; then
echo "Couldn't find the Vagrantfile.$1.$2 for your cluster in the vagrantfiles directory"
exit 1
fi
if [ ! -f cluster-defns/$2.$3.yml ] ; then
echo "Couldn't find the $2.$3.yml for your cluster in the cluster-defns directory"
exit 1
fi
cp vagrantfiles/Vagrantfile.$1.$2 Vagrantfile
cp cluster-defns/$2.$3.yml cluster.yml
cp -f scripts/deploy-ear.sh .deploy.sh
sed -i "s/__USER__/$USER/g" .deploy.sh
if [ $PORTS -eq 1 ] ; then
parse_ports
count=0
for i in $ports ; do
odd=$(($count % 2))
if [ $odd -eq 1 ] ; then
port=$i
replace_port
else
forwarded_port=$i
fi
count=$(($count + 1))
done
fi
if [ $2 -gt 1 ]; then
echo "Changing VMs subnet"
change_subnet
fi
echo "Removing old vendored cookbooks"
rm -rf cookbooks > /dev/null 2>&1
rm -f Berksfile.lock nohup.out > /dev/null 2>&1
echo "Vendoring cookbooks using 'berks vendor cookbooks'"
berks vendor cookbooks
if [ $? -ne 0 ] ; then
echo "ERROR: 'berks vendor cookbooks' failed"
exit 3
fi
echo "Running the Vagrantfile using 'vagrant up'"
nohup vagrant up &
if [ $UDP_HACK -eq 1 ]; then
./udp_hacky_fix.sh
fi
parse_ports
deploy_ear
ssh_config
exit 0