Browse code

add assertions for blind grep

blind grep is error-prone.
Add assertions for errors we can not handle

Change-Id: Ibe19085545ecc848498506e8b8ee14e71825b273

DennyZhang authored on 2013/10/23 07:07:32
Showing 11 changed files
... ...
@@ -66,7 +66,10 @@ exit_if_aggregate_present() {
66 66
 exit_if_aggregate_present $AGGREGATE_NAME
67 67
 
68 68
 AGGREGATE_ID=$(nova aggregate-create $AGGREGATE_NAME $AGGREGATE_A_ZONE | grep " $AGGREGATE_NAME " | get_field 1)
69
+die_if_not_set $LINENO AGGREGATE_ID "Failure creating AGGREGATE_ID for $AGGREGATE_NAME $AGGREGATE_A_ZONE"
70
+
69 71
 AGGREGATE2_ID=$(nova aggregate-create $AGGREGATE2_NAME $AGGREGATE_A_ZONE | grep " $AGGREGATE2_NAME " | get_field 1)
72
+die_if_not_set $LINENO AGGREGATE2_ID "Fail creating AGGREGATE2_ID for $AGGREGATE2_NAME $AGGREGATE_A_ZONE"
70 73
 
71 74
 # check aggregate created
72 75
 nova aggregate-list | grep -q " $AGGREGATE_NAME " || die $LINENO "Aggregate $AGGREGATE_NAME not created"
... ...
@@ -114,6 +114,7 @@ INSTANCE_TYPE=$(nova flavor-list | grep $DEFAULT_INSTANCE_TYPE | get_field 1)
114 114
 if [[ -z "$INSTANCE_TYPE" ]]; then
115 115
     # grab the first flavor in the list to launch if default doesn't exist
116 116
     INSTANCE_TYPE=$(nova flavor-list | head -n 4 | tail -n 1 | get_field 1)
117
+    die_if_not_set $LINENO INSTANCE_TYPE "Failure retrieving INSTANCE_TYPE"
117 118
 fi
118 119
 
119 120
 # Clean-up from previous runs
... ...
@@ -139,24 +139,28 @@ function foreach_tenant_net {
139 139
 
140 140
 function get_image_id {
141 141
     local IMAGE_ID=$(glance image-list | egrep " $DEFAULT_IMAGE_NAME " | get_field 1)
142
+    die_if_not_set $LINENO IMAGE_ID "Failure retrieving IMAGE_ID"
142 143
     echo "$IMAGE_ID"
143 144
 }
144 145
 
145 146
 function get_tenant_id {
146 147
     local TENANT_NAME=$1
147 148
     local TENANT_ID=`keystone tenant-list | grep " $TENANT_NAME " | head -n 1 | get_field 1`
149
+    die_if_not_set $LINENO TENANT_ID "Failure retrieving TENANT_ID for $TENANT_NAME"
148 150
     echo "$TENANT_ID"
149 151
 }
150 152
 
151 153
 function get_user_id {
152 154
     local USER_NAME=$1
153 155
     local USER_ID=`keystone user-list | grep $USER_NAME | awk '{print $2}'`
156
+    die_if_not_set $LINENO USER_ID "Failure retrieving USER_ID for $USER_NAME"
154 157
     echo "$USER_ID"
155 158
 }
156 159
 
157 160
 function get_role_id {
158 161
     local ROLE_NAME=$1
159 162
     local ROLE_ID=`keystone role-list | grep $ROLE_NAME | awk '{print $2}'`
163
+    die_if_not_set $LINENO ROLE_ID "Failure retrieving ROLE_ID for $ROLE_NAME"
160 164
     echo "$ROLE_ID"
161 165
 }
162 166
 
... ...
@@ -169,6 +173,7 @@ function get_network_id {
169 169
 function get_flavor_id {
170 170
     local INSTANCE_TYPE=$1
171 171
     local FLAVOR_ID=`nova flavor-list | grep $INSTANCE_TYPE | awk '{print $2}'`
172
+    die_if_not_set $LINENO FLAVOR_ID "Failure retrieving FLAVOR_ID for $INSTANCE_TYPE"
172 173
     echo "$FLAVOR_ID"
173 174
 }
174 175
 
... ...
@@ -234,6 +239,7 @@ function create_network {
234 234
     local TENANT_ID=$(get_tenant_id $TENANT)
235 235
     source $TOP_DIR/openrc $TENANT $TENANT
236 236
     local NET_ID=$(neutron net-create --tenant_id $TENANT_ID $NET_NAME $EXTRA| grep ' id ' | awk '{print $4}' )
237
+    die_if_not_set $LINENO NET_ID "Failure creating NET_ID for $TENANT_ID $NET_NAME $EXTRA"
237 238
     neutron subnet-create --ip_version 4 --tenant_id $TENANT_ID --gateway $GATEWAY $NET_ID $CIDR
238 239
     neutron-debug probe-create --device-owner compute $NET_ID
239 240
     source $TOP_DIR/openrc demo demo
... ...
@@ -56,6 +56,7 @@ done
56 56
 
57 57
 # Check to make sure rules were added
58 58
 SEC_GROUP_RULES=( $(nova secgroup-list-rules $SEC_GROUP_NAME | grep -v \- | grep -v 'Source Group' | cut -d '|' -f3 | tr -d ' ') )
59
+die_if_not_set $LINENO SEC_GROUP_RULES "Failure retrieving SEC_GROUP_RULES for $SEC_GROUP_NAME"
59 60
 for i in "${RULES_TO_ADD[@]}"; do
60 61
     skip=
61 62
     for j in "${SEC_GROUP_RULES[@]}"; do
... ...
@@ -118,6 +118,7 @@ INSTANCE_TYPE=$(nova flavor-list | grep $DEFAULT_INSTANCE_TYPE | get_field 1)
118 118
 if [[ -z "$INSTANCE_TYPE" ]]; then
119 119
     # grab the first flavor in the list to launch if default doesn't exist
120 120
     INSTANCE_TYPE=$(nova flavor-list | head -n 4 | tail -n 1 | get_field 1)
121
+    die_if_not_set $LINENO INSTANCE_TYPE "Failure retrieving INSTANCE_TYPE"
121 122
 fi
122 123
 
123 124
 # Clean-up from previous runs
... ...
@@ -322,6 +322,7 @@ function create_neutron_accounts() {
322 322
 
323 323
 function create_neutron_initial_network() {
324 324
     TENANT_ID=$(keystone tenant-list | grep " demo " | get_field 1)
325
+    die_if_not_set $LINENO TENANT_ID "Failure retrieving TENANT_ID for demo"
325 326
 
326 327
     # Create a small network
327 328
     # Since neutron command is executed in admin context at this point,
... ...
@@ -336,12 +337,16 @@ function create_neutron_initial_network() {
336 336
             sudo ip addr add $IP dev $OVS_PHYSICAL_BRIDGE
337 337
         done
338 338
         NET_ID=$(neutron net-create $PHYSICAL_NETWORK --tenant_id $TENANT_ID --provider:network_type flat --provider:physical_network "$PHYSICAL_NETWORK" | grep ' id ' | get_field 2)
339
+        die_if_not_set $LINENO NET_ID "Failure creating NET_ID for $PHYSICAL_NETWORK $TENANT_ID"
339 340
         SUBNET_ID=$(neutron subnet-create --tenant_id $TENANT_ID --ip_version 4 ${ALLOCATION_POOL:+--allocation-pool $ALLOCATION_POOL} --gateway $NETWORK_GATEWAY --name $PRIVATE_SUBNET_NAME $NET_ID $FIXED_RANGE | grep ' id ' | get_field 2)
341
+        die_if_not_set $LINENO SUBNET_ID "Failure creating SUBNET_ID for $TENANT_ID"
340 342
         sudo ifconfig $OVS_PHYSICAL_BRIDGE up
341 343
         sudo route add default gw $NETWORK_GATEWAY dev $OVS_PHYSICAL_BRIDGE
342 344
     else
343 345
         NET_ID=$(neutron net-create --tenant_id $TENANT_ID "$PRIVATE_NETWORK_NAME" | grep ' id ' | get_field 2)
346
+        die_if_not_set $LINENO NET_ID "Failure creating NET_ID for $PHYSICAL_NETWORK $TENANT_ID"
344 347
         SUBNET_ID=$(neutron subnet-create --tenant_id $TENANT_ID --ip_version 4 --gateway $NETWORK_GATEWAY --name $PRIVATE_SUBNET_NAME $NET_ID $FIXED_RANGE | grep ' id ' | get_field 2)
348
+        die_if_not_set $LINENO SUBNET_ID "Failure creating SUBNET_ID for $TENANT_ID"
345 349
     fi
346 350
 
347 351
     if [[ "$Q_L3_ENABLED" == "True" ]]; then
... ...
@@ -349,14 +354,18 @@ function create_neutron_initial_network() {
349 349
         if [[ "$Q_L3_ROUTER_PER_TENANT" == "True" ]]; then
350 350
             # create a tenant-owned router.
351 351
             ROUTER_ID=$(neutron router-create --tenant_id $TENANT_ID $Q_ROUTER_NAME | grep ' id ' | get_field 2)
352
+            die_if_not_set $LINENO ROUTER_ID "Failure creating ROUTER_ID for $TENANT_ID $Q_ROUTER_NAME"
352 353
         else
353 354
             # Plugin only supports creating a single router, which should be admin owned.
354 355
             ROUTER_ID=$(neutron router-create $Q_ROUTER_NAME | grep ' id ' | get_field 2)
356
+            die_if_not_set $LINENO ROUTER_ID "Failure creating ROUTER_ID for $Q_ROUTER_NAME"
355 357
         fi
356 358
         neutron router-interface-add $ROUTER_ID $SUBNET_ID
357 359
         # Create an external network, and a subnet. Configure the external network as router gw
358 360
         EXT_NET_ID=$(neutron net-create "$PUBLIC_NETWORK_NAME" -- --router:external=True | grep ' id ' | get_field 2)
361
+        die_if_not_set $LINENO EXT_NET_ID "Failure creating EXT_NET_ID for $PUBLIC_NETWORK_NAME"
359 362
         EXT_GW_IP=$(neutron subnet-create --ip_version 4 ${Q_FLOATING_ALLOCATION_POOL:+--allocation-pool $Q_FLOATING_ALLOCATION_POOL} --gateway $PUBLIC_NETWORK_GATEWAY --name $PUBLIC_SUBNET_NAME $EXT_NET_ID $FLOATING_RANGE -- --enable_dhcp=False | grep 'gateway_ip' | get_field 2)
363
+        die_if_not_set $LINENO EXT_GW_IP "Failure creating EXT_GW_IP"
360 364
         neutron router-gateway-set $ROUTER_ID $EXT_NET_ID
361 365
 
362 366
         if is_service_enabled q-l3; then
... ...
@@ -366,6 +375,7 @@ function create_neutron_initial_network() {
366 366
                 sudo ip addr add $EXT_GW_IP/$CIDR_LEN dev $PUBLIC_BRIDGE
367 367
                 sudo ip link set $PUBLIC_BRIDGE up
368 368
                 ROUTER_GW_IP=`neutron port-list -c fixed_ips -c device_owner | grep router_gateway | awk -F '"' '{ print $8; }'`
369
+                die_if_not_set $LINENO ROUTER_GW_IP "Failure retrieving ROUTER_GW_IP"
369 370
                 sudo route add -net $FIXED_RANGE gw $ROUTER_GW_IP
370 371
             fi
371 372
             if [[ "$Q_USE_NAMESPACE" == "False" ]]; then
... ...
@@ -492,14 +492,19 @@ function create_swift_accounts() {
492 492
     fi
493 493
 
494 494
     SWIFT_TENANT_TEST1=$(keystone tenant-create --name=swifttenanttest1 | grep " id " | get_field 2)
495
+    die_if_not_set $LINENO SWIFT_TENANT_TEST1 "Failure creating SWIFT_TENANT_TEST1"
495 496
     SWIFT_USER_TEST1=$(keystone user-create --name=swiftusertest1 --pass=testing --email=test@example.com | grep " id " | get_field 2)
497
+    die_if_not_set $LINENO SWIFT_USER_TEST1 "Failure creating SWIFT_USER_TEST1"
496 498
     keystone user-role-add --user-id $SWIFT_USER_TEST1 --role-id $ADMIN_ROLE --tenant-id $SWIFT_TENANT_TEST1
497 499
 
498 500
     SWIFT_USER_TEST3=$(keystone user-create --name=swiftusertest3 --pass=testing3 --email=test3@example.com | grep " id " | get_field 2)
501
+    die_if_not_set $LINENO SWIFT_USER_TEST3 "Failure creating SWIFT_USER_TEST3"
499 502
     keystone user-role-add --user-id $SWIFT_USER_TEST3 --role-id $ANOTHER_ROLE --tenant-id $SWIFT_TENANT_TEST1
500 503
 
501 504
     SWIFT_TENANT_TEST2=$(keystone tenant-create --name=swifttenanttest2 | grep " id " | get_field 2)
505
+    die_if_not_set $LINENO SWIFT_TENANT_TEST2 "Failure creating SWIFT_TENANT_TEST2"
502 506
     SWIFT_USER_TEST2=$(keystone user-create --name=swiftusertest2 --pass=testing2 --email=test2@example.com | grep " id " | get_field 2)
507
+    die_if_not_set $LINENO SWIFT_USER_TEST2 "Failure creating SWIFT_USER_TEST2"
503 508
     keystone user-role-add --user-id $SWIFT_USER_TEST2 --role-id $ADMIN_ROLE --tenant-id $SWIFT_TENANT_TEST2
504 509
 }
505 510
 
... ...
@@ -1068,7 +1068,9 @@ fi
1068 1068
 # Create an access key and secret key for nova ec2 register image
1069 1069
 if is_service_enabled key && is_service_enabled swift3 && is_service_enabled nova; then
1070 1070
     NOVA_USER_ID=$(keystone user-list | grep ' nova ' | get_field 1)
1071
+    die_if_not_set $LINENO NOVA_USER_ID "Failure retrieving NOVA_USER_ID for nova"
1071 1072
     NOVA_TENANT_ID=$(keystone tenant-list | grep " $SERVICE_TENANT_NAME " | get_field 1)
1073
+    die_if_not_set $LINENO NOVA_TENANT_ID "Failure retrieving NOVA_TENANT_ID for $SERVICE_TENANT_NAME"
1072 1074
     CREDS=$(keystone ec2-credentials-create --user_id $NOVA_USER_ID --tenant_id $NOVA_TENANT_ID)
1073 1075
     ACCESS_KEY=$(echo "$CREDS" | awk '/ access / { print $4 }')
1074 1076
     SECRET_KEY=$(echo "$CREDS" | awk '/ secret / { print $4 }')
... ...
@@ -5,4 +5,5 @@ set -o errexit
5 5
 
6 6
 TOP_DIR=$(cd ../../.. && pwd)
7 7
 HEAD_IP=`cat $TOP_DIR/addresses | grep HEAD | cut -d "=" -f2`
8
+die_if_not_set $LINENO HEAD_IP "Failure retrieving HEAD_IP"
8 9
 ssh stack@$HEAD_IP 'cd devstack && source openrc && cd exercises &&  ./euca.sh'
... ...
@@ -5,4 +5,5 @@ set -o errexit
5 5
 
6 6
 TOP_DIR=$(cd ../../.. && pwd)
7 7
 HEAD_IP=`cat $TOP_DIR/addresses | grep HEAD | cut -d "=" -f2`
8
+die_if_not_set $LINENO HEAD_IP "Failure retrieving HEAD_IP"
8 9
 ssh stack@$HEAD_IP 'cd devstack && source openrc && cd exercises &&  ./floating_ips.sh'
... ...
@@ -5,4 +5,5 @@ set -o errexit
5 5
 
6 6
 TOP_DIR=$(cd ../../.. && pwd)
7 7
 HEAD_IP=`cat $TOP_DIR/addresses | grep HEAD | cut -d "=" -f2`
8
+die_if_not_set $LINENO HEAD_IP "Failure retrieving HEAD_IP"
8 9
 ssh stack@$HEAD_IP 'cd devstack && source openrc && cd exercises &&  ./volumes.sh'