-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjetstream
More file actions
executable file
·595 lines (536 loc) · 15.9 KB
/
jetstream
File metadata and controls
executable file
·595 lines (536 loc) · 15.9 KB
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
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
#!/usr/bin/env bash
set -e
# Script for managing VM instances on Jetstream with the OpenStack command
# line tools.
#if [[ $OS_PROJECT_ID != 715* ]] ; then
# echo "Reloading openrc.sh"
# source ~/bin/openrc.sh
#fi
# Key installed to instances to enable SSH connections.
KEY=lappsgrid-shared-key
#KEY=deiis-shared-key
#KEY=lappsgrid-suderman-key
# Set PEM to the location of the key. Possession of this key allows the user to
# manage instances on Jetstream.
if [[ -e ./$KEY.pem ]] ; then
PEM=./$KEY.pem
elif [[ -e $HOME/.ssh/$KEY.pem ]] ; then
PEM=$HOME/.ssh/$KEY.pem
else
echo "Please install $KEY.pem to $HOME/.ssh"
exit 1
fi
# This will never need to be changed... until it needs to be changed.
# This is the IP used for several *.lappsgrid.org addresses and only needs
# to be changed when the DNS entries are changed.
PROXY=149.165.168.244
if [ -z "$OS_USERNAME" ] ; then
echo "The OpenStack API has not been configured. Please source the openrc.sh"
echo "script and try again. One of the following might work:"
echo "source ~/openrc.sh"
echo "source ~/bin/openrc.sh"
echo
echo "See http://wiki.lappsgrid/org/technical/jetstream for instructions"
echo "for generating the openrc.sh script if you don't have one."
exit 1
fi
trap "echo 'Aborting due to an error (likely a missing parameter).'; exit 1" TERM
export TOP_PID=$$
# Use the abort function inside other functions when the entire jetstream script
# should exit when an error is encountered. That is:
# function foo() {
# exit 1
# }
# causes the foo() function to return the error code 1, it will not terminate
# the script.
# function foo() {
# abort "Something bad happened."
# }
# causes the script to terminate.
function abort() {
ERROR_MESSAGE=$1
kill -s TERM $TOP_PID
}
# ANSI color codes for the console.
reset="\033[0m"
bold="\033[1m"
ital="\033[3m" # does not work on OS X
# Function used to highlight text.
function hi() {
echo -e "$bold$@$reset"
}
function em() {
# Italics does not work on my terminal... need to test on others.
#echo -e "$ital$1$reset"
echo -e "$bold$1$reset"
}
# The Lappsgrid images that are available on Jetstream.
#CENTOS_IMAGE=93905450-0714-4336-a9c1-5e0f50aa9209
#CENTOS_IMAGE=217a24b4-2338-4802-8221-fff3bf3fc260
CENTOS_IMAGE=f2c3fa0b-0ed3-4e98-b256-e33efa5b658d
UBUNTU_IMAGE=d7fe3289-943f-4417-90e8-8a6b171677ca
DOCKER_IMAGE=f3847d59-6a90-43da-8666-b58699398b9a
MASTER_IMAGE=f9e0fa21-6cad-49fc-97db-efd3812fc199
UBUNTU_JAVA_POSTGRES=9b96fac5-38a7-4052-b2f9-e6b29dff7e45
PROXY_IMAGE=90b43fe0-7ece-460e-85bf-92d939964f7f
# Default values used when launching an image.
IMAGE=$UBUNTU_IMAGE
FLAVOR="m1.medium"
GROUP="lappsgrid-secgroup"
NETWORK="lappsgrid-network"
#
# Prints the help screen for the user.
#
function usage {
if [[ -n $1 ]] ; then
echo "$1" | cat -
echo | cat -
fi
cat << EOF
$(hi NAME)
$(hi jetstream) -- Manage instances on Jetstream via the OpenStack API
$(hi SYNOPSIS)
$(hi jetstream) COMMAND [$(hi options)]
$(hi DESCRIPTION)
The $(em jetstream) script uses the OpenStack API ($(em nova), $(em glance), $(em neutron), etc.)
to manage instances running on Jetstream (http://jblb.jetstream-cloud.org/dashboard).
$(hi COMMANDS)
$(hi launch) [options] <instance_name>
Launches a new instance using the default network ($(em lappsgrid-network)),
security group ($(em lappsgrid-secgroup)), and SSH key ($(em lappsgrid-shared-key))
$(hi list)
Lists all instances currently allocated, including suspended and stopped instances.
$(hi "ip list|free")
$(hi list) : lists all floating ip addresses allocated.
$(hi free) : lists floating ip not assigned to instances.
$(hi suspend) <name>
Suspends an instance. (Sleep)
$(hi resume) <name>
Resumes an suspended instance.
$(hi stop) <name>
Stops an instances. (Shutdown)
$(hi delete) <name>
Deletes an instance. All data in memory and on disk will be lost.
$(hi visit) [<instance_name>|<ip address>]
Opens the instance, or IP address, in the default web browser.
Only tested on Mac OS X, but it may work on Linux and/or Windows.
$(hi offline)
Disables the Jetstream proxy. When users attempt to access
http://jetstream.lappsgrid.org the proxy will display a static html page
stating that the Jetstream instance is offline to preserve resources.
$(hi online)
Causes the proxy server to resume forwarding http://jetstream.lappsgrid.org
traffic to the configured address.
$(hi proxy) <ip address> [port]
Reconfigures the proxy to forward http://jetstream.lappsgrid.org traffic to
the specified IP address. The address does not have to be a public ip
address, that is, the proxy can forward to 10.x.x.x, 192.168.x.x, etc.
provided the proxy and server are on the same subnet.
$(hi pull "scripts|manager")
Does a $(hi git pull) from with the /var/lib/downloads/scripts directory or
/var/lib/downloads/service-manager on the proxy server. Use this command
to remotely update the scripts available.
$(hi ssh) <ip address | name>
Connects to the Jetstream instance via ssh. The name or IP address of the
instance can be specified. If the name is provided the script will attempt
to parse IP address from the output of the $(hi nova list) command.
$(hi scp) <local_path> <remote_path>
Copies a local file to the proxy server (i.e. downloads.lappsgrid.org)
$(hi help)
Displays this help message.
$(hi LAUNCH OPTIONS)
$(hi -t)|$(hi --tiny)
Launches a tiny instance (1x2G)
$(hi -s)|$(hi --small)
Launches a small instance (2x4G)
$(hi -m)|$(hi --medium)
Launches a medium instance (6x16G)
$(hi -l)|$(hi --large)
Launches a large instance (10x30G)
$(hi -x)|$(hi --xlarge)
Launches an extra large instace (24x60G)
$(hi -n)|$(hi --name) <name>
Specifies the name for the instance. The name will be prefixed with $(em lappsgrid-)
if it is not already.
$(hi --ip) [free|alloc|<ip address>]
Associates a floating ip address to the new instance. Users may specify
the ip address, allocate a new floating ip address, or use an ip address
that has been allocated by not assigned to an instance. If an ip address
is provided then it must have been allocated via the $(hi --ip alloc)
command already.
$(hi -i)|$(hi --image) [centos|ubuntu|docker|manager]
The VM image to be used for the instance. Valid options are:
$(hi centos) CentOS 7
$(hi ubuntu) Default Ubuntu 14.04.3 Development image.
$(hi docker) Ubuntu image with Docker and Docker Compose.
$(hi master) Ubuntu image with Docker, Compose, and Lappsgrid tools.
$(hi ubuntu-java-postgres) Ubuntu 14.04.3 with Java and Postgres.
$(hi EXAMPLES)
$(hi '$>') jetstream launch -t --ip assign test-instance
Creates a new tiny instance named $(hi lappsgrid-test-instance) and allocates a
public floating IP address.
$(hi '$>') jetstream launch lappsgrid-test
Create a medium instance (the default) named $(hi lappsgrid-test). The instance
will not have a public IP address.
$(hi '$>') jetstream suspend test
Suspends the instance named $(hi lappsgrid-test)
$(hi '$>') jetstream scp my_file.tgz /var/lib/downloads
Press $(hi Q) to exit help.
EOF
}
# The `help` function pipes the output from the usage function through `less`
# for proper pagination.
function help {
usage "$1" | less -R
}
# Launch an instance on Jetstream.
function launch() {
NAME=
IP=
while [ -n "$1" ] ; do
case $1 in
-t|--tiny) FLAVOR="m1.tiny" ;;
-s|--small) FLAVOR="m1.small" ;;
-m|--medium) FLAVOR="m1.medium" ;;
-l|--large) FLAVOR="m1.large" ;;
-x|--xlarge) FLAVOR="m1.xlarge" ;;
-n|--name)
if [ -z "$2" ] ; then
help "ERROR: No name specified!"
exit 1
fi
NAME=lappsgrid-$2
shift
;;
--ip)
if [ -z "$2" ] ; then
help "ERROR: No IP address provided."
exit 1
fi
IP=$2
shift
if (is_ip_address $IP) ; then
echo "Assignin ip address $IP"
elif [[ $IP = alloc ]] ; then
echo "Getting a floating IP address."
IP=`nova floating-ip-create public | tail -2 | head -1 | awk '{print $4}'`
echo $IP
elif [[ $IP = assign ]] ; then
echo "$(hi WARNING) the $(hi assign) option has been deprecated and"
echo "will be removed in a future update of the jetstream script."
echo "Please use $(hi --ip alloc) instead."
echo
echo "Getting a floating IP address."
IP=`nova floating-ip-create public | tail -2 | head -1 | awk '{print $4}'`
echo $IP
elif [[ $IP = free ]] ; then
IP=$(get_free_ip)
if [[ -z $IP ]] ; then
echo "There are no unassigned floating ip addresses available."
exit 1
fi
else
echo "Invalid option or address: $IP"
exit 1
fi
;;
-i|--image)
if [ -z "$2" ] ; then
echo "ERROR: No image name provided."
exit 1
fi
case $2 in
docker) IMAGE=$DOCKER_IMAGE ;;
ubuntu) IMAGE=$UBUNTU_IMAGE ;;
master) IMAGE=$MASTER_IMAGE ;;
centos) IMAGE=$CENTOS_IMAGE ;;
ubuntu-java-postgres) IMAGE=$UBUNTU_JAVA_POSTGRES ;;
proxy) IMAGE=$PROXY_IMAGE ;;
*)
#echo "Invalid image name $2"
#echo "Expected one of ubuntu, docker, or master"
#exit 1
IMAGE=$2
;;
esac
shift
;;
*)
if [ -n "$NAME" ] ; then
#echo "ERROR: Unknown option $1"
echo "The name has already been set: $NAME"
exit 1
fi
NAME=$(get_name $1)
;;
esac
shift
done
if [ -z $NAME ] ; then
echo "ERROR: No name specified for the instance."
exit 1
fi
#echo "Launching instance $NAME ($FLAVOR) with key $KEY"
#NETWORK=genapp-net
#GROUP=default
echo "Launching instance"
echo "NAME : $NAME"
echo "FLAVOR : $FLAVOR"
echo "IMAGE : $IMAGE"
echo "KEY : $KEY"
echo "GROUP : $GROUP"
echo "NETWORK : $NETWORK"
nova boot $NAME \
--flavor $FLAVOR \
--image $IMAGE \
--key-name $KEY \
--security-groups $GROUP \
--nic net-name=$NETWORK
#ip=`nova floating-ip-create public | tail -2 | head -1 | awk '{print $2}'`
#nova floating-ip-associate $NAME $ip
if [ -n "$IP" ] ; then
echo "Assigning $IP to $NAME"
sleep 5
nova floating-ip-associate $NAME $IP
#echo "To enable ssh from this instance to other instances run the"
#echo "following command once the instance is up and responding:"
#echo "scp -i $PEM $PEM root@$IP:/root/.ssh/id_rsa"
fi
}
function get_name() {
if [ -z "$1" ] ; then
#echo "ERROR: No instance name provided."
#exit 1
ERROR="ERROR: No instance name provided."
abort
fi
if [[ $1 = lappsgrid* ]] ; then
echo $1
else
echo lappsgrid-$1
fi
}
function run_command() {
local CMD=$1
local ACTION=$2
local NAME=$(get_name $3)
echo "$ACTION instance: $NAME"
nova $CMD $NAME
}
function is_ip_address() {
local pat='[1-9][0-9]{0,2}'
if [[ $1 =~ ^$pat\.$pat\.$pat\.$pat$ ]] ; then
exit 0
fi
exit 1
}
function list_ip() {
if [[ `nova list | grep "$1" | wc -l` -ne 1 ]] ; then
echo "Ambiguous image name $1" >&2
exit 1
fi
echo `nova list | grep "$1" | rev | awk '{print $2}' | rev`
}
function floating_ip {
echo `neutron floatingip-list -F fixed_ip_address -F floating_ip_address | grep -v 10.0 | grep 149 | awk '{print $3}'`
}
function get_free_ip {
echo `neutron floatingip-list -F fixed_ip_address -F floating_ip_address | grep -v 10.0 | grep 149 | awk '{print $3}' | head -1`
}
# Expects out put from `[cinder|nova] list --name <name>`
function get_id {
echo $1
echo $1 | tail -2 | head -1 | awk '{print $2}'
}
# Main program starts here.
case $1 in
test)
echo $(list_ip $2)
;;
list)
nova list
;;
launch)
shift
launch $@
;;
suspend)
run_command suspend 'Suspending' $2 || exit 1
;;
resume)
run_command resume 'Resuming' $2
;;
stop)
run_command stop 'Stopping' $2
;;
start)
run_command start 'Starting' $2
;;
delete)
if [[ $2 == *proxy* ]] ; then
echo "ERROR: Can not delete a proxy with this program."
exit 1
fi
run_command delete 'Deleting' $2
;;
offline)
ssh -i $PEM root@$PROXY proxy offline
;;
online)
ssh -i $PEM root@$PROXY proxy online
;;
proxy)
if [ -z $2 ] ; then
echo "ERROR: No proxy address specified"
exit 1
fi
if $(is_ip_address "$2") ; then
IP=$2
else
echo "Getting ip address for $2"
IP=$(list_ip $2)
fi
ssh -i $PEM root@$PROXY proxy server $IP $3
echo "If the proxy is enabled traffic to http://jetstream.lappsgrid.org will redirect to $IP"
;;
forget)
#known_hosts=$HOME/.ssh/known_hosts
#if [ ! -e "$known_hosts" ] ; then
# echo "Can not find the known_hosts file."
# exit 1
#fi
IP=
if $(is_ip_address "$2") ; then
echo "$2 is an ip address."
IP=$2
else
echo "Getting ip address for $2"
IP=$(list_ip $2)
fi
echo "Removing $IP from $known_hosts"
ssh-keygen -R $IP
#grep -v $IP $known_hosts > /tmp/known_hosts
#mv /tmp/known_hosts $known_hosts
;;
ssh)
IP=
if $(is_ip_address "$2") ; then
IP=$2
else
# If the user didn't specify the ip address we will
# have to grep it out of the `nova list` output.
# Parsing the 2nd to last element is much easier if
# we reverse the input and just find the 2nd element.
IP=$(list_ip $2)
fi
echo "Connecting to $IP"
ssh -i $PEM root@$IP
;;
scp)
echo "Uploading $2 to root@downloads.lappsgrid.org:$3"
scp -i $PEM $2 root@downloads.lappsgrid.org:$3
;;
pull)
case $2 in
scripts)
ssh -i $PEM root@$PROXY "cd /var/lib/downloads/scripts && pwd && git pull origin master"
;;
manager)
ssh -i $PEM root@$PROXY "cd /var/lib/downloads/service-manager && pwd && git pull origin master"
;;
keith)
ssh -i $PEM root@$PROXY "cd /var/lib/downloads/keith && pwd && git pull origin keith"
;;
*)
echo "Unknown option: $2"
;;
esac
;;
ip)
case $2 in
free)
neutron floatingip-list -F fixed_ip_address -F floating_ip_address | grep -v 10.0 | grep 149 | awk '{print $3}'
;;
list)
nova floating-ip-list
;;
assign)
NAME=$(get_name $3)
if [[ $4 = free ]] ; then
IP=$(get_free_ip)
else
IP=$4
fi
nova floating-ip-associate $NAME $IP
echo "Assigned $IP to $NAME"
;;
*)
echo "Unrecognized option. Use list or list-all"
exit 1
;;
esac
;;
visit)
if [[ $(uname) != Darwin ]] ; then
echo "This command has only been tested on OS X and"
echo "may not work on Windows or Linux distributions."
echo -e "Press $(hi ENTER) to continue."
read
fi
IP=
if $(is_ip_address "$2") ; then
IP=$2
else
IP=$(list_ip $2)
fi
port=
if [ -n "$3" ] ; then
port=":$3"
fi
echo "Opening $IP in a web browser"
case "$OSTYPE" in
cygwin*)
open="cmd /c start"
;;
linux*)
open="xdg-open"
;;
darwin*)
open="open"
;;
esac
$open http://$IP$port
;;
attach)
server=$2
volume=$3
iid=`nova list --name $server | tail -2 | head -1 | awk '{print \$2}'`
#iid=$(get_id `nova list --name $server`)
#echo "Instance ID: $iid"
vid=`cinder list --name $volume | tail -2 | head -1 | awk '{print \$2}'`
#echo "Volume: $vid"
nova volume-attach $iid $vid auto
;;
detach)
server=$2
volume=$3
iid=`nova list --name $server | tail -2 | head -1 | awk '{print \$2}'`
#iid=$(get_id `nova list --name $server`)
echo "Instance ID: $iid"
vid=`cinder list --name $volume | tail -2 | head -1 | awk '{print \$2}'`
echo "Volume: $vid"
nova volume-detach $iid $vid
;;
help)
help
;;
*)
help "ERROR: Unknown command"
;;
esac
#nova boot ${OS_PROJECT_NAME}-api-U-1 \
#--flavor m1.medium \
#--image d7fe3289-943f-4417-90e8-8a6b171677ca \
#--key-name ${OS_PROJECT_NAME}-api-key \
#--security-groups global-ssh \
#--nic net-name=${OS_PROJECT_NAME}-api-net