exercises/floating_ips.sh
b019151c
 #!/usr/bin/env bash
 
27e32699
 # **floating_ips.sh** - using the cloud can be fun
b019151c
 
 # we will use the ``nova`` cli tool provided by the ``python-novaclient``
27e32699
 # package to work out the instance connectivity
b019151c
 
27e32699
 echo "*********************************************************************"
489bd2a6
 echo "Begin DevStack Exercise: $0"
27e32699
 echo "*********************************************************************"
489bd2a6
 
9b353671
 # This script exits on an error so that errors don't compound and you see
b19424fb
 # only the first error that occured.
 set -o errexit
 
9b353671
 # Print the commands being run so that we can see the command that triggers
b19424fb
 # an error.  It is also useful for following allowing as the install occurs.
 set -o xtrace
 
 
 # Settings
 # ========
b019151c
 
51fb454f
 # Keep track of the current directory
 EXERCISE_DIR=$(cd $(dirname "$0") && pwd)
 TOP_DIR=$(cd $EXERCISE_DIR/..; pwd)
489bd2a6
 
 # Import common functions
51fb454f
 source $TOP_DIR/functions
489bd2a6
 
 # Import configuration
51fb454f
 source $TOP_DIR/openrc
b019151c
 
51fb454f
 # Import exercise configuration
 source $TOP_DIR/exerciserc
751c1524
 
 # Instance type to create
 DEFAULT_INSTANCE_TYPE=${DEFAULT_INSTANCE_TYPE:-m1.tiny}
696ad331
 
751c1524
 # Boot this image, use first AMi image if unset
 DEFAULT_IMAGE_NAME=${DEFAULT_IMAGE_NAME:-ami}
 
 # Security group name
 SECGROUP=${SECGROUP:-test_secgroup}
 
 # Default floating IP pool name
696ad331
 DEFAULT_FLOATING_POOL=${DEFAULT_FLOATING_POOL:-nova}
751c1524
 
 # Additional floating IP pool and range
696ad331
 TEST_FLOATING_POOL=${TEST_FLOATING_POOL:-test}
 
27e32699
 
593828d4
 # Launching a server
 # ==================
b19424fb
 
593828d4
 # List servers for tenant:
b019151c
 nova list
593828d4
 
 # Images
 # ------
 
 # Nova has a **deprecated** way of listing images.
 nova image-list
 
 # But we recommend using glance directly
45495258
 glance image-list
593828d4
 
751c1524
 # Grab the id of the image to launch
45495258
 IMAGE=$(glance image-list | egrep " $DEFAULT_IMAGE_NAME " | get_field 1)
d888e1cd
 
20a2caec
 # Security Groups
 # ---------------
 
 # List of secgroups:
 nova secgroup-list
 
 # Create a secgroup
751c1524
 if ! nova secgroup-list | grep -q $SECGROUP; then
     nova secgroup-create $SECGROUP "$SECGROUP description"
     if ! timeout $ASSOCIATE_TIMEOUT sh -c "while ! nova secgroup-list | grep -q $SECGROUP; do sleep 1; done"; then
         echo "Security group not created"
         exit 1
     fi
 fi
d888e1cd
 
ad101767
 # Determinine instance type
751c1524
 # -------------------------
d888e1cd
 
751c1524
 # List of instance types:
d888e1cd
 nova flavor-list
 
489bd2a6
 INSTANCE_TYPE=`nova flavor-list | grep $DEFAULT_INSTANCE_TYPE | get_field 1`
1d6e0e19
 if [[ -z "$INSTANCE_TYPE" ]]; then
     # grab the first flavor in the list to launch if default doesn't exist
489bd2a6
    INSTANCE_TYPE=`nova flavor-list | head -n 4 | tail -n 1 | get_field 1`
1d6e0e19
 fi
d888e1cd
 
489bd2a6
 NAME="ex-float"
d888e1cd
 
489bd2a6
 VM_UUID=`nova boot --flavor $INSTANCE_TYPE --image $IMAGE $NAME --security_groups=$SECGROUP | grep ' id ' | get_field 2`
 die_if_not_set VM_UUID "Failure launching $NAME"
d888e1cd
 
ad101767
 
6fc71019
 # Testing
 # =======
 
 # First check if it spins up (becomes active and responds to ping on
 # internal ip).  If you run this script from a nova node, you should
 # bypass security groups and have direct access to the server.
 
 # Waiting for boot
 # ----------------
 
79e807a1
 # check that the status is active within ACTIVE_TIMEOUT seconds
751c1524
 if ! timeout $ACTIVE_TIMEOUT sh -c "while ! nova show $VM_UUID | grep status | grep -q ACTIVE; do sleep 1; done"; then
5a774839
     echo "server didn't become active!"
     exit 1
 fi
d888e1cd
 
 # get the IP of the server
fda946e3
 IP=`nova show $VM_UUID | grep "$PRIVATE_NETWORK_NAME" | get_field 2`
489bd2a6
 die_if_not_set IP "Failure retrieving IP address"
d888e1cd
 
fda946e3
 ping_check "$PRIVATE_NETWORK_NAME" $IP $BOOT_TIMEOUT
6fc71019
 
 # Security Groups & Floating IPs
 # ------------------------------
 
751c1524
 if ! nova secgroup-list-rules $SECGROUP | grep -q icmp; then
     # allow icmp traffic (ping)
     nova secgroup-add-rule $SECGROUP icmp -1 -1 0.0.0.0/0
     if ! timeout $ASSOCIATE_TIMEOUT sh -c "while ! nova secgroup-list-rules $SECGROUP | grep -q icmp; do sleep 1; done"; then
         echo "Security group rule not created"
         exit 1
     fi
 fi
20a2caec
 
 # List rules for a secgroup
 nova secgroup-list-rules $SECGROUP
 
696ad331
 # allocate a floating ip from default pool
489bd2a6
 FLOATING_IP=`nova floating-ip-create | grep $DEFAULT_FLOATING_POOL | get_field 1`
 die_if_not_set FLOATING_IP "Failure creating floating IP"
20a2caec
 
696ad331
 # list floating addresses
 if ! timeout $ASSOCIATE_TIMEOUT sh -c "while ! nova floating-ip-list | grep -q $FLOATING_IP; do sleep 1; done"; then
     echo "Floating IP not allocated"
     exit 1
 fi
20a2caec
 
 # add floating ip to our server
27e32699
 nova add-floating-ip $VM_UUID $FLOATING_IP || \
     die "Failure adding floating IP $FLOATING_IP to $NAME"
20a2caec
 
79e807a1
 # test we can ping our floating ip within ASSOCIATE_TIMEOUT seconds
fda946e3
 ping_check "$PUBLIC_NETWORK_NAME" $FLOATING_IP $ASSOCIATE_TIMEOUT
20a2caec
 
751c1524
 # Allocate an IP from second floating pool
489bd2a6
 TEST_FLOATING_IP=`nova floating-ip-create $TEST_FLOATING_POOL | grep $TEST_FLOATING_POOL | get_field 1`
 die_if_not_set TEST_FLOATING_IP "Failure creating floating IP in $TEST_FLOATING_POOL"
696ad331
 
 # list floating addresses
 if ! timeout $ASSOCIATE_TIMEOUT sh -c "while ! nova floating-ip-list | grep $TEST_FLOATING_POOL | grep -q $TEST_FLOATING_IP; do sleep 1; done"; then
     echo "Floating IP not allocated"
     exit 1
 fi
 
6fc71019
 # dis-allow icmp traffic (ping)
27e32699
 nova secgroup-delete-rule $SECGROUP icmp -1 -1 0.0.0.0/0 || die "Failure deleting security group rule from $SECGROUP"
20a2caec
 
1de18c62
 # FIXME (anthony): make xs support security groups
c0c6f006
 if [ "$VIRT_DRIVER" != "xenserver" -a "$VIRT_DRIVER" != "openvz" ]; then
1de18c62
     # test we can aren't able to ping our floating ip within ASSOCIATE_TIMEOUT seconds
fda946e3
     ping_check "$PUBLIC_NETWORK_NAME" $FLOATING_IP $ASSOCIATE_TIMEOUT
20a2caec
 fi
 
696ad331
 # Delete second floating IP
27e32699
 nova floating-ip-delete $TEST_FLOATING_IP || die "Failure deleting floating IP $TEST_FLOATING_IP"
696ad331
 
fda946e3
 
 # de-allocate the floating ip
 nova floating-ip-delete $FLOATING_IP || die "Failure deleting floating IP $FLOATING_IP"
 
96288ba9
 # Shutdown the server
27e32699
 nova delete $VM_UUID || die "Failure deleting instance $NAME"
d888e1cd
 
96288ba9
 # Wait for termination
 if ! timeout $TERMINATE_TIMEOUT sh -c "while nova list | grep -q $VM_UUID; do sleep 1; done"; then
     echo "Server $NAME not deleted"
5836b153
     exit 1
 fi
 
20a2caec
 # Delete a secgroup
27e32699
 nova secgroup-delete $SECGROUP || die "Failure deleting security group $SECGROUP"
489bd2a6
 
 set +o xtrace
27e32699
 echo "*********************************************************************"
 echo "SUCCESS: End DevStack Exercise: $0"
 echo "*********************************************************************"