exercises/floating_ips.sh
b019151c
 #!/usr/bin/env bash
 
27e32699
 # **floating_ips.sh** - using the cloud can be fun
b019151c
 
da85cdad
 # Test instance connectivity with the ``nova`` command from ``python-novaclient``
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
 
5db5bfa2
 # Import quantum functions if needed
 if is_service_enabled quantum; then
     source $TOP_DIR/lib/quantum
 fi
 
51fb454f
 # Import exercise configuration
 source $TOP_DIR/exerciserc
751c1524
 
 # Instance type to create
 DEFAULT_INSTANCE_TYPE=${DEFAULT_INSTANCE_TYPE:-m1.tiny}
696ad331
 
da85cdad
 # Boot this image, use first AMI image if unset
751c1524
 DEFAULT_IMAGE_NAME=${DEFAULT_IMAGE_NAME:-ami}
 
 # Security group name
 SECGROUP=${SECGROUP:-test_secgroup}
 
 # Default floating IP pool name
640f1e4c
 DEFAULT_FLOATING_POOL=${DEFAULT_FLOATING_POOL:-public}
751c1524
 
 # Additional floating IP pool and range
696ad331
 TEST_FLOATING_POOL=${TEST_FLOATING_POOL:-test}
 
da85cdad
 # Instance name
 VM_NAME="ex-float"
 
27e32699
 
593828d4
 # Launching a server
 # ==================
b19424fb
 
593828d4
 # List servers for tenant:
b019151c
 nova list
593828d4
 
 # Images
 # ------
 
da85cdad
 # List the images available
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)
07115eb5
 die_if_not_set $LINENO IMAGE "Failure getting image $DEFAULT_IMAGE_NAME"
d888e1cd
 
20a2caec
 # Security Groups
 # ---------------
 
da85cdad
 # List security groups
20a2caec
 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
07115eb5
         die $LINENO "Security group not created"
751c1524
     fi
 fi
d888e1cd
 
da85cdad
 # Configure Security Group Rules
 if ! nova secgroup-list-rules $SECGROUP | grep -q icmp; then
     nova secgroup-add-rule $SECGROUP icmp -1 -1 0.0.0.0/0
 fi
 if ! nova secgroup-list-rules $SECGROUP | grep -q " tcp .* 22 "; then
     nova secgroup-add-rule $SECGROUP tcp 22 22 0.0.0.0/0
 fi
d888e1cd
 
da85cdad
 # List secgroup rules
 nova secgroup-list-rules $SECGROUP
 
 # Set up instance
 # ---------------
 
 # List flavors
d888e1cd
 nova flavor-list
 
da85cdad
 # Select a flavor
 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
da85cdad
    INSTANCE_TYPE=$(nova flavor-list | head -n 4 | tail -n 1 | get_field 1)
1d6e0e19
 fi
d888e1cd
 
da85cdad
 # Clean-up from previous runs
 nova delete $VM_NAME || true
 if ! timeout $ACTIVE_TIMEOUT sh -c "while nova show $VM_NAME; do sleep 1; done"; then
07115eb5
     die $LINENO "server didn't terminate!"
da85cdad
     exit 1
 fi
6fc71019
 
da85cdad
 # Boot instance
 # -------------
6fc71019
 
da85cdad
 VM_UUID=$(nova boot --flavor $INSTANCE_TYPE --image $IMAGE --security_groups=$SECGROUP $VM_NAME | grep ' id ' | get_field 2)
07115eb5
 die_if_not_set $LINENO VM_UUID "Failure launching $VM_NAME"
6fc71019
 
da85cdad
 # 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
07115eb5
     die $LINENO "server didn't become active!"
5a774839
 fi
d888e1cd
 
da85cdad
 # Get the instance IP
 IP=$(nova show $VM_UUID | grep "$PRIVATE_NETWORK_NAME" | get_field 2)
07115eb5
 die_if_not_set $LINENO IP "Failure retrieving IP address"
d888e1cd
 
da85cdad
 # Private IPs can be pinged in single node deployments
fda946e3
 ping_check "$PRIVATE_NETWORK_NAME" $IP $BOOT_TIMEOUT
6fc71019
 
da85cdad
 # Floating IPs
 # ------------
20a2caec
 
da85cdad
 # Allocate a floating IP from the default pool
 FLOATING_IP=$(nova floating-ip-create | grep $DEFAULT_FLOATING_POOL | get_field 1)
07115eb5
 die_if_not_set $LINENO FLOATING_IP "Failure creating floating IP from pool $DEFAULT_FLOATING_POOL"
20a2caec
 
da85cdad
 # List floating addresses
696ad331
 if ! timeout $ASSOCIATE_TIMEOUT sh -c "while ! nova floating-ip-list | grep -q $FLOATING_IP; do sleep 1; done"; then
07115eb5
     die $LINENO "Floating IP not allocated"
696ad331
 fi
20a2caec
 
da85cdad
 # Add floating IP to our server
27e32699
 nova add-floating-ip $VM_UUID $FLOATING_IP || \
07115eb5
     die $LINENO "Failure adding floating IP $FLOATING_IP to $VM_NAME"
20a2caec
 
da85cdad
 # Test we can ping our floating IP within ASSOCIATE_TIMEOUT seconds
fda946e3
 ping_check "$PUBLIC_NETWORK_NAME" $FLOATING_IP $ASSOCIATE_TIMEOUT
20a2caec
 
5db5bfa2
 if ! is_service_enabled quantum; then
     # Allocate an IP from second floating pool
da85cdad
     TEST_FLOATING_IP=$(nova floating-ip-create $TEST_FLOATING_POOL | grep $TEST_FLOATING_POOL | get_field 1)
07115eb5
     die_if_not_set $LINENO TEST_FLOATING_IP "Failure creating floating IP in $TEST_FLOATING_POOL"
696ad331
 
5db5bfa2
     # 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
07115eb5
         die $LINENO "Floating IP not allocated"
5db5bfa2
      fi
696ad331
 fi
 
da85cdad
 # Dis-allow icmp traffic (ping)
 nova secgroup-delete-rule $SECGROUP icmp -1 -1 0.0.0.0/0 || \
07115eb5
     die $LINENO "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
da85cdad
     # Test we can aren't able to ping our floating ip within ASSOCIATE_TIMEOUT seconds
5db5bfa2
     ping_check "$PUBLIC_NETWORK_NAME" $FLOATING_IP $ASSOCIATE_TIMEOUT Fail
20a2caec
 fi
 
da85cdad
 # Clean up
 # --------
 
5db5bfa2
 if ! is_service_enabled quantum; then
     # Delete second floating IP
da85cdad
     nova floating-ip-delete $TEST_FLOATING_IP || \
07115eb5
         die $LINENO "Failure deleting floating IP $TEST_FLOATING_IP"
5db5bfa2
 fi
fda946e3
 
da85cdad
 # Delete the floating ip
 nova floating-ip-delete $FLOATING_IP || \
07115eb5
     die $LINENO "Failure deleting floating IP $FLOATING_IP"
d888e1cd
 
da85cdad
 # Delete instance
07115eb5
 nova delete $VM_UUID || die $LINENO "Failure deleting instance $VM_NAME"
96288ba9
 # Wait for termination
 if ! timeout $TERMINATE_TIMEOUT sh -c "while nova list | grep -q $VM_UUID; do sleep 1; done"; then
07115eb5
     die $LINENO "Server $VM_NAME not deleted"
5836b153
 fi
 
da85cdad
 # Delete secgroup
 nova secgroup-delete $SECGROUP || \
07115eb5
     die $LINENO "Failure deleting security group $SECGROUP"
489bd2a6
 
 set +o xtrace
27e32699
 echo "*********************************************************************"
 echo "SUCCESS: End DevStack Exercise: $0"
 echo "*********************************************************************"