Skip to content

Commit 854d8c9

Browse files
committed
Add a helper method to volumes test to get fields
* The way we retrieve data from cli output is janky, this improves it slightly by creating a helper method. Change-Id: Ib0889fd56f6a78bed85dad4c5e9e6e34bac9fb0d
1 parent 995bfe0 commit 854d8c9

File tree

1 file changed

+22
-7
lines changed

1 file changed

+22
-7
lines changed

exercises/volumes.sh

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -55,18 +55,33 @@ IMAGE=`glance -f index | egrep $DEFAULT_IMAGE_NAME | head -1 | cut -d" " -f1`
5555
# determinine instance type
5656
# -------------------------
5757

58+
# Helper function to grab a numbered field from python novaclient cli result
59+
# Fields are numbered starting with 1
60+
# Reverse syntax is supported: -1 is the last field, -2 is second to last, etc.
61+
function get_field () {
62+
while read data
63+
do
64+
if [ "$1" -lt 0 ]; then
65+
field="(\$(NF$1))"
66+
else
67+
field="\$$(($1 + 1))"
68+
fi
69+
echo "$data" | awk -F'[ \t]*\\|[ \t]*' "{print $field}"
70+
done
71+
}
72+
5873
# List of instance types:
5974
nova flavor-list
6075

61-
INSTANCE_TYPE=`nova flavor-list | grep $DEFAULT_INSTANCE_TYPE | cut -d"|" -f2`
76+
INSTANCE_TYPE=`nova flavor-list | grep $DEFAULT_INSTANCE_TYPE | get_field 1`
6277
if [[ -z "$INSTANCE_TYPE" ]]; then
6378
# grab the first flavor in the list to launch if default doesn't exist
64-
INSTANCE_TYPE=`nova flavor-list | head -n 4 | tail -n 1 | cut -d"|" -f2`
79+
INSTANCE_TYPE=`nova flavor-list | head -n 4 | tail -n 1 | get_field 1`
6580
fi
6681

6782
NAME="myserver"
6883

69-
VM_UUID=`nova boot --flavor $INSTANCE_TYPE --image $IMAGE $NAME --security_groups=$SECGROUP | grep ' id ' | cut -d"|" -f3 | sed 's/ //g'`
84+
VM_UUID=`nova boot --flavor $INSTANCE_TYPE --image $IMAGE $NAME --security_groups=$SECGROUP | grep ' id ' | get_field 2`
7085

7186
# Testing
7287
# =======
@@ -85,7 +100,7 @@ if ! timeout $ACTIVE_TIMEOUT sh -c "while ! nova show $VM_UUID | grep status | g
85100
fi
86101

87102
# get the IP of the server
88-
IP=`nova show $VM_UUID | grep "private network" | cut -d"|" -f3`
103+
IP=`nova show $VM_UUID | grep "private network" | get_field 2`
89104

90105
# for single node deployments, we can ping private ips
91106
MULTI_HOST=${MULTI_HOST:-0}
@@ -108,7 +123,7 @@ fi
108123
VOL_NAME="myvol-$(openssl rand -hex 4)"
109124

110125
# Verify it doesn't exist
111-
if [[ -n "`nova volume-list | grep $VOL_NAME | head -1 | cut -d'|' -f3 | sed 's/ //g'`" ]]; then
126+
if [[ -n "`nova volume-list | grep $VOL_NAME | head -1 | get_field 2`" ]]; then
112127
echo "Volume $VOL_NAME already exists"
113128
exit 1
114129
fi
@@ -121,7 +136,7 @@ if ! timeout $ACTIVE_TIMEOUT sh -c "while ! nova volume-list | grep $VOL_NAME |
121136
fi
122137

123138
# Get volume ID
124-
VOL_ID=`nova volume-list | grep $VOL_NAME | head -1 | cut -d'|' -f2 | sed 's/ //g'`
139+
VOL_ID=`nova volume-list | grep $VOL_NAME | head -1 | get_field 1`
125140

126141
# Attach to server
127142
DEVICE=/dev/vdb
@@ -131,7 +146,7 @@ if ! timeout $ACTIVE_TIMEOUT sh -c "while ! nova volume-list | grep $VOL_NAME |
131146
exit 1
132147
fi
133148

134-
VOL_ATTACH=`nova volume-list | grep $VOL_NAME | head -1 | cut -d'|' -f6 | sed 's/ //g'`
149+
VOL_ATTACH=`nova volume-list | grep $VOL_NAME | head -1 | get_field -1`
135150
if [[ "$VOL_ATTACH" != $VM_UUID ]]; then
136151
echo "Volume not attached to correct instance"
137152
exit 1

0 commit comments

Comments
 (0)