-
Notifications
You must be signed in to change notification settings - Fork 0
/
devstack-exercise-1.sh
executable file
·446 lines (369 loc) · 11.8 KB
/
devstack-exercise-1.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
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
#!/usr/bin/env bash
#
# **contrail-cw.sh**
# Perform integration testing of Nova and other components with Neutron.
echo "*********************************************************************"
echo "Begin DevStack Exercise: $0"
echo "*********************************************************************"
# This script exits on an error so that errors don't compound and you see
# only the first error that occurred.
set -o errtrace
trap failed ERR
failed() {
local r=$?
set +o errtrace
set +o xtrace
echo "Failed to execute"
echo "Starting cleanup..."
delete_all
echo "Finished cleanup"
exit $r
}
# Print the commands being run so that we can see the command that triggers
# an error. It is also useful for following allowing as the install occurs.
set -o xtrace
# Environment
# -----------
# Keep track of the current directory
EXERCISE_DIR=$(cd $(dirname "$0") && pwd)
TOP_DIR=$(cd $EXERCISE_DIR/..; pwd)
# Import common functions
source $TOP_DIR/functions
# Import configuration
source $TOP_DIR/openrc
# Import neutron functions
source $TOP_DIR/lib/neutron
# If neutron is not enabled we exit with exitcode 55, which means exercise is skipped.
neutron_plugin_check_adv_test_requirements || exit 55
# Import exercise configuration
source $TOP_DIR/exerciserc
# Neutron Settings
# ----------------
TENANTS="DEMO1"
# TODO (nati)_Test public network
#TENANTS="DEMO1,DEMO2"
PUBLIC_NAME="admin"
DEMO1_NAME="demo1"
DEMO2_NAME="demo2"
PUBLIC_NUM_NET=1
DEMO1_NUM_NET=2
PUBLIC_NET1_CIDR="200.0.0.0/24"
DEMO1_NET1_CIDR="10.10.0.0/24"
DEMO1_NET2_CIDR="10.20.0.0/24"
PUBLIC_NET1_GATEWAY="200.0.0.1"
DEMO1_NET1_GATEWAY="10.10.0.1"
DEMO1_NET2_GATEWAY="10.20.0.1"
PUBLIC_NUM_VM=1
DEMO1_NUM_VM=2
PUBLIC_VM1_NET='admin-net1'
DEMO1_VM1_NET='demo1-net1'
DEMO1_VM2_NET='demo1-net1,demo1-net2'
# Multinic settings. But this is fail without nic setting in OS image
PUBLIC_NUM_ROUTER=1
PUBLIC_ROUTER1_NET="admin-net1"
KEYSTONE="keystone"
# Manually create a token by querying keystone (sending JSON data). Keystone
# returns a token and catalog of endpoints. We use python to parse the token
# and save it.
TOKEN=`keystone token-get | grep ' id ' | awk '{print $4}'`
# Various functions
# -----------------
function foreach_tenant {
COMMAND=$1
for TENANT in ${TENANTS//,/ };do
eval ${COMMAND//%TENANT%/$TENANT}
done
}
function foreach_tenant_resource {
COMMAND=$1
RESOURCE=$2
for TENANT in ${TENANTS//,/ };do
eval 'NUM=$'"${TENANT}_NUM_$RESOURCE"
for i in `seq $NUM`;do
local COMMAND_LOCAL=${COMMAND//%TENANT%/$TENANT}
COMMAND_LOCAL=${COMMAND_LOCAL//%NUM%/$i}
eval $COMMAND_LOCAL
done
done
}
function foreach_tenant_vm {
COMMAND=$1
foreach_tenant_resource "$COMMAND" 'VM'
}
function foreach_tenant_net {
COMMAND=$1
foreach_tenant_resource "$COMMAND" 'NET'
}
function get_image_id {
local IMAGE_ID=$(glance image-list | egrep " $DEFAULT_IMAGE_NAME " | get_field 1)
echo "$IMAGE_ID"
}
function get_tenant_id {
local TENANT_NAME=$1
local TENANT_ID=`keystone tenant-list | grep " $TENANT_NAME " | head -n 1 | get_field 1`
echo "$TENANT_ID"
}
function get_user_id {
local USER_NAME=$1
local USER_ID=`keystone user-list | grep $USER_NAME | awk '{print $2}'`
echo "$USER_ID"
}
function get_role_id {
local ROLE_NAME=$1
local ROLE_ID=`keystone role-list | grep $ROLE_NAME | awk '{print $2}'`
echo "$ROLE_ID"
}
function get_network_id {
local NETWORK_NAME="$1"
local NETWORK_ID=$(neutron net-list -F id -F name | awk -v net=${NETWORK_NAME} '$4 ~ net {print $2}')
echo $NETWORK_ID
}
function get_flavor_id {
local INSTANCE_TYPE=$1
local FLAVOR_ID=`nova flavor-list | grep $INSTANCE_TYPE | awk '{print $2}'`
echo "$FLAVOR_ID"
}
function confirm_server_active {
local VM_UUID=$1
if ! timeout $ACTIVE_TIMEOUT sh -c "while ! nova show $VM_UUID | grep status | grep -q ACTIVE; do sleep 1; done"; then
echo "server '$VM_UUID' did not become active!"
false
fi
}
function add_tenant {
local TENANT=$1
local USER=$2
$KEYSTONE tenant-create --name=$TENANT
$KEYSTONE user-create --name=$USER --pass=${ADMIN_PASSWORD}
local USER_ID=$(get_user_id $USER)
local TENANT_ID=$(get_tenant_id $TENANT)
$KEYSTONE user-role-add --user-id $USER_ID --role-id $(get_role_id Member) --tenant-id $TENANT_ID
}
function remove_tenant {
local TENANT=$1
local TENANT_ID=$(get_tenant_id $TENANT)
$KEYSTONE tenant-delete $TENANT_ID
}
function remove_user {
local USER=$1
local USER_ID=$(get_user_id $USER)
$KEYSTONE user-delete $USER_ID
}
function create_tenants {
source $TOP_DIR/openrc admin admin
add_tenant demo1 demo1 demo1
source $TOP_DIR/openrc demo demo
}
function delete_tenants_and_users {
source $TOP_DIR/openrc admin admin
remove_user demo1
remove_tenant demo1
echo "removed all tenants"
source $TOP_DIR/openrc demo demo
}
function create_network {
local TENANT=$1
local GATEWAY=$2
local CIDR=$3
local NUM=$4
local EXTRA=$5
local NET_NAME="${TENANT}-net$NUM"
local ROUTER_NAME="${TENANT}-router${NUM}"
source $TOP_DIR/openrc admin admin
local TENANT_ID=$(get_tenant_id $TENANT)
source $TOP_DIR/openrc $TENANT $TENANT
local NET_ID=$(neutron net-create --tenant_id $TENANT_ID $NET_NAME $EXTRA| grep ' id ' | awk '{print $4}' )
neutron subnet-create --ip_version 4 --tenant_id $TENANT_ID --gateway $GATEWAY $NET_ID $CIDR
# neutron-debug probe-create --device-owner compute $NET_ID
source $TOP_DIR/openrc demo demo
}
function create_networks {
foreach_tenant_net 'create_network ${%TENANT%_NAME} ${%TENANT%_NET%NUM%_GATEWAY} ${%TENANT%_NET%NUM%_CIDR} %NUM% ${%TENANT%_NET%NUM%_EXTRA}'
#TODO(nati) test security group function
# allow ICMP for both tenant's security groups
#source $TOP_DIR/openrc demo1 demo1
#$NOVA secgroup-add-rule default icmp -1 -1 0.0.0.0/0
#source $TOP_DIR/openrc demo2 demo2
#$NOVA secgroup-add-rule default icmp -1 -1 0.0.0.0/0
}
function create_vm {
local TENANT=$1
local NUM=$2
local NET_NAMES=$3
source $TOP_DIR/openrc $TENANT $TENANT
local NIC=""
for NET_NAME in ${NET_NAMES//,/ };do
NIC="$NIC --nic net-id="`get_network_id $NET_NAME`
done
#TODO (nati) Add multi-nic test
#TODO (nati) Add public-net test
local VM_UUID=`nova boot --flavor $(get_flavor_id m1.tiny) \
--image $(get_image_id) \
$NIC \
$TENANT-server$NUM | grep ' id ' | cut -d"|" -f3 | sed 's/ //g'`
die_if_not_set $LINENO VM_UUID "Failure launching $TENANT-server$NUM"
confirm_server_active $VM_UUID
}
function create_vms {
foreach_tenant_vm 'create_vm ${%TENANT%_NAME} %NUM% ${%TENANT%_VM%NUM%_NET}'
}
function ping_ip {
# Test agent connection. Assumes namespaces are disabled, and
# that DHCP is in use, but not L3
local VM_NAME=$1
local NET_NAME=$2
IP=$(get_instance_ip $VM_NAME $NET_NAME)
ping_check $NET_NAME $IP $BOOT_TIMEOUT
}
function check_vm {
local TENANT=$1
local NUM=$2
local VM_NAME="$TENANT-server$NUM"
local NET_NAME=$3
source $TOP_DIR/openrc $TENANT $TENANT
ping_ip $VM_NAME $NET_NAME
# TODO (nati) test ssh connection
# TODO (nati) test inter connection between vm
# TODO (nati) test dhcp host routes
# TODO (nati) test multi-nic
}
function check_vms {
foreach_tenant_vm 'check_vm ${%TENANT%_NAME} %NUM% ${%TENANT%_VM%NUM%_NET}'
}
function shutdown_vm {
local TENANT=$1
local NUM=$2
source $TOP_DIR/openrc $TENANT $TENANT
VM_NAME=${TENANT}-server$NUM
nova delete $VM_NAME
}
function shutdown_vms {
foreach_tenant_vm 'shutdown_vm ${%TENANT%_NAME} %NUM%'
if ! timeout $TERMINATE_TIMEOUT sh -c "while nova list | grep -q ACTIVE; do sleep 1; done"; then
die $LINENO "Some VMs failed to shutdown"
fi
}
function delete_network {
local TENANT=$1
local NUM=$2
local NET_NAME="${TENANT}-net$NUM"
source $TOP_DIR/openrc admin admin
local TENANT_ID=$(get_tenant_id $TENANT)
#TODO(nati) comment out until l3-agent merged
#for res in port subnet net router;do
for net_id in `neutron net-list -c id -c name | grep $NET_NAME | awk '{print $2}'`;do
# delete_probe $net_id
neutron subnet-list | grep $net_id | awk '{print $2}' | xargs -I% neutron subnet-delete %
neutron net-delete $net_id
done
source $TOP_DIR/openrc demo demo
}
function delete_networks {
foreach_tenant_net 'delete_network ${%TENANT%_NAME} %NUM%'
#TODO(nati) add secuirty group check after it is implemented
# source $TOP_DIR/openrc demo1 demo1
# nova secgroup-delete-rule default icmp -1 -1 0.0.0.0/0
# source $TOP_DIR/openrc demo2 demo2
# nova secgroup-delete-rule default icmp -1 -1 0.0.0.0/0
}
function create_all {
create_tenants
create_networks
create_vms
}
function delete_all {
shutdown_vms
delete_networks
delete_tenants_and_users
}
function all {
create_all
check_vms
delete_all
}
# Test functions
# --------------
function test_functions {
IMAGE=$(get_image_id)
echo $IMAGE
TENANT_ID=$(get_tenant_id demo)
echo $TENANT_ID
FLAVOR_ID=$(get_flavor_id m1.tiny)
echo $FLAVOR_ID
NETWORK_ID=$(get_network_id admin)
echo $NETWORK_ID
}
# Usage and main
# --------------
usage() {
echo "$0: [-h]"
echo " -h, --help Display help message"
echo " -t, --tenant Create tenants"
echo " -n, --net Create networks"
echo " -v, --vm Create vms"
echo " -c, --check Check connection"
echo " -x, --delete-tenants Delete tenants"
echo " -y, --delete-nets Delete networks"
echo " -z, --delete-vms Delete vms"
echo " -T, --test Test functions"
}
main() {
echo Description
echo
echo Copyright 2012, Cisco Systems
echo Copyright 2012, Nicira Networks, Inc.
echo Copyright 2012, NTT MCL, Inc.
echo
echo Please direct any questions to dedutta@cisco.com, dan@nicira.com, nachi@nttmcl.com
echo
if [ $# -eq 0 ] ; then
# if no args are provided, run all tests
all
else
while [ "$1" != "" ]; do
case $1 in
-h | --help ) usage
exit
;;
-n | --net ) create_networks
exit
;;
-v | --vm ) create_vms
exit
;;
-t | --tenant ) create_tenants
exit
;;
-c | --check ) check_vms
exit
;;
-T | --test ) test_functions
exit
;;
-x | --delete-tenants ) delete_tenants_and_users
exit
;;
-y | --delete-nets ) delete_networks
exit
;;
-z | --delete-vms ) shutdown_vms
exit
;;
-a | --all ) all
exit
;;
* ) usage
exit 1
esac
shift
done
fi
}
# Kick off script
# ---------------
echo $*
main $*
set +o xtrace
echo "*********************************************************************"
echo "SUCCESS: End DevStack Exercise: $0"
echo "*********************************************************************"