Jesse Andrews | b019151 | 2011-09-14 19:37:10 -0700 | [diff] [blame] | 1 | #!/usr/bin/env bash |
| 2 | |
| 3 | # **exercise.sh** - using the cloud can be fun |
| 4 | |
| 5 | # we will use the ``nova`` cli tool provided by the ``python-novaclient`` |
| 6 | # package |
Jesse Andrews | b19424f | 2011-09-14 22:03:04 -0700 | [diff] [blame] | 7 | # |
Jesse Andrews | b019151 | 2011-09-14 19:37:10 -0700 | [diff] [blame] | 8 | |
Jesse Andrews | b19424f | 2011-09-14 22:03:04 -0700 | [diff] [blame] | 9 | |
Vishvananda Ishaya | 9b35367 | 2011-10-20 10:07:10 -0700 | [diff] [blame] | 10 | # This script exits on an error so that errors don't compound and you see |
Jesse Andrews | b19424f | 2011-09-14 22:03:04 -0700 | [diff] [blame] | 11 | # only the first error that occured. |
| 12 | set -o errexit |
| 13 | |
Vishvananda Ishaya | 9b35367 | 2011-10-20 10:07:10 -0700 | [diff] [blame] | 14 | # Print the commands being run so that we can see the command that triggers |
Jesse Andrews | b19424f | 2011-09-14 22:03:04 -0700 | [diff] [blame] | 15 | # an error. It is also useful for following allowing as the install occurs. |
| 16 | set -o xtrace |
| 17 | |
| 18 | |
| 19 | # Settings |
| 20 | # ======== |
Jesse Andrews | b019151 | 2011-09-14 19:37:10 -0700 | [diff] [blame] | 21 | |
Anthony Young | 6ab10d4 | 2011-10-20 10:24:50 -0700 | [diff] [blame] | 22 | # Use openrc + stackrc + localrc for settings |
Jesse Andrews | 787af01 | 2011-11-01 16:44:19 -0700 | [diff] [blame] | 23 | pushd $(cd $(dirname "$0")/.. && pwd) |
Anthony Young | 6ab10d4 | 2011-10-20 10:24:50 -0700 | [diff] [blame] | 24 | source ./openrc |
Jesse Andrews | 787af01 | 2011-11-01 16:44:19 -0700 | [diff] [blame] | 25 | popd |
Jesse Andrews | b019151 | 2011-09-14 19:37:10 -0700 | [diff] [blame] | 26 | |
Anthony Young | 94c889a | 2011-10-11 18:07:48 +0000 | [diff] [blame] | 27 | # Get a token for clients that don't support service catalog |
| 28 | # ========================================================== |
Jesse Andrews | b9c77d6 | 2011-10-15 18:37:25 -0700 | [diff] [blame] | 29 | |
Vishvananda Ishaya | 9b35367 | 2011-10-20 10:07:10 -0700 | [diff] [blame] | 30 | # manually create a token by querying keystone (sending JSON data). Keystone |
Jesse Andrews | b9c77d6 | 2011-10-15 18:37:25 -0700 | [diff] [blame] | 31 | # returns a token and catalog of endpoints. We use python to parse the token |
| 32 | # and save it. |
| 33 | |
Jesse Andrews | e61f318 | 2011-10-24 13:43:04 -0700 | [diff] [blame] | 34 | TOKEN=`curl -s -d "{\"auth\":{\"passwordCredentials\": {\"username\": \"$NOVA_USERNAME\", \"password\": \"$NOVA_API_KEY\"}}}" -H "Content-type: application/json" http://$HOST_IP:5000/v2.0/tokens | python -c "import sys; import json; tok = json.loads(sys.stdin.read()); print tok['access']['token']['id'];"` |
Anthony Young | 94c889a | 2011-10-11 18:07:48 +0000 | [diff] [blame] | 35 | |
Jesse Andrews | 593828d | 2011-09-14 22:44:50 -0700 | [diff] [blame] | 36 | # Launching a server |
| 37 | # ================== |
Jesse Andrews | b19424f | 2011-09-14 22:03:04 -0700 | [diff] [blame] | 38 | |
Jesse Andrews | 593828d | 2011-09-14 22:44:50 -0700 | [diff] [blame] | 39 | # List servers for tenant: |
Jesse Andrews | b019151 | 2011-09-14 19:37:10 -0700 | [diff] [blame] | 40 | nova list |
Jesse Andrews | 593828d | 2011-09-14 22:44:50 -0700 | [diff] [blame] | 41 | |
Jesse Andrews | 593828d | 2011-09-14 22:44:50 -0700 | [diff] [blame] | 42 | # Images |
| 43 | # ------ |
| 44 | |
| 45 | # Nova has a **deprecated** way of listing images. |
| 46 | nova image-list |
| 47 | |
| 48 | # But we recommend using glance directly |
Jesse Andrews | 4e8847c | 2011-10-15 19:29:55 -0700 | [diff] [blame] | 49 | glance -A $TOKEN index |
Jesse Andrews | 593828d | 2011-09-14 22:44:50 -0700 | [diff] [blame] | 50 | |
Jesse Andrews | d888e1c | 2011-10-15 20:01:12 -0700 | [diff] [blame] | 51 | # Let's grab the id of the first AMI image to launch |
| 52 | IMAGE=`glance -A $TOKEN index | egrep ami | cut -d" " -f1` |
| 53 | |
Anthony Young | 20a2cae | 2011-10-17 16:02:24 -0700 | [diff] [blame] | 54 | # Security Groups |
| 55 | # --------------- |
| 56 | SECGROUP=test_secgroup |
| 57 | |
| 58 | # List of secgroups: |
| 59 | nova secgroup-list |
| 60 | |
| 61 | # Create a secgroup |
| 62 | nova secgroup-create $SECGROUP "test_secgroup description" |
Jesse Andrews | d888e1c | 2011-10-15 20:01:12 -0700 | [diff] [blame] | 63 | |
Jesse Andrews | 6fc7101 | 2011-10-24 11:29:08 -0700 | [diff] [blame] | 64 | # determine flavor |
| 65 | # ---------------- |
Jesse Andrews | d888e1c | 2011-10-15 20:01:12 -0700 | [diff] [blame] | 66 | |
| 67 | # List of flavors: |
| 68 | nova flavor-list |
| 69 | |
| 70 | # and grab the first flavor in the list to launch |
| 71 | FLAVOR=`nova flavor-list | head -n 4 | tail -n 1 | cut -d"|" -f2` |
| 72 | |
Anthony Young | 20a2cae | 2011-10-17 16:02:24 -0700 | [diff] [blame] | 73 | NAME="myserver" |
Jesse Andrews | d888e1c | 2011-10-15 20:01:12 -0700 | [diff] [blame] | 74 | |
Anthony Young | 20a2cae | 2011-10-17 16:02:24 -0700 | [diff] [blame] | 75 | nova boot --flavor $FLAVOR --image $IMAGE $NAME --security_groups=$SECGROUP |
Jesse Andrews | d888e1c | 2011-10-15 20:01:12 -0700 | [diff] [blame] | 76 | |
Jesse Andrews | 6fc7101 | 2011-10-24 11:29:08 -0700 | [diff] [blame] | 77 | # Testing |
| 78 | # ======= |
| 79 | |
| 80 | # First check if it spins up (becomes active and responds to ping on |
| 81 | # internal ip). If you run this script from a nova node, you should |
| 82 | # bypass security groups and have direct access to the server. |
| 83 | |
| 84 | # Waiting for boot |
| 85 | # ---------------- |
| 86 | |
Jesse Andrews | 16b6efa | 2011-11-10 11:46:18 -0800 | [diff] [blame^] | 87 | # Max time to wait while vm goes from build to active state |
| 88 | ACTIVE_TIMEOUT=${ACTIVE_TIMEOUT:-10} |
| 89 | |
| 90 | # Max time till the vm is bootable |
| 91 | BOOT_TIMEOUT=${BOOT_TIMEOUT:-15} |
| 92 | |
| 93 | # Max time to wait for proper association and dis-association. |
| 94 | ASSOCIATE_TIMEOUT=${ASSOCIATE_TIMEOUT:-10} |
| 95 | |
Anthony Young | 79e807a | 2011-10-31 11:16:44 -0700 | [diff] [blame] | 96 | # check that the status is active within ACTIVE_TIMEOUT seconds |
| 97 | if ! timeout $ACTIVE_TIMEOUT sh -c "while ! nova show $NAME | grep status | grep -q ACTIVE; do sleep 1; done"; then |
Jesse Andrews | 5a77483 | 2011-10-26 21:30:02 -0700 | [diff] [blame] | 98 | echo "server didn't become active!" |
| 99 | exit 1 |
| 100 | fi |
Jesse Andrews | d888e1c | 2011-10-15 20:01:12 -0700 | [diff] [blame] | 101 | |
| 102 | # get the IP of the server |
| 103 | IP=`nova show $NAME | grep "private network" | cut -d"|" -f3` |
| 104 | |
Anthony Young | 8ecd294 | 2011-10-24 22:58:14 -0700 | [diff] [blame] | 105 | # for single node deployments, we can ping private ips |
| 106 | MULTI_HOST=${MULTI_HOST:-0} |
Justin Shepherd | 56a505f | 2011-10-26 10:45:02 -0500 | [diff] [blame] | 107 | if [ "$MULTI_HOST" = "0" ]; then |
Anthony Young | 8ecd294 | 2011-10-24 22:58:14 -0700 | [diff] [blame] | 108 | # sometimes the first ping fails (10 seconds isn't enough time for the VM's |
Anthony Young | 79e807a | 2011-10-31 11:16:44 -0700 | [diff] [blame] | 109 | # network to respond?), so let's ping for a default of 15 seconds with a |
| 110 | # timeout of a second for each ping. |
| 111 | if ! timeout $BOOT_TIMEOUT sh -c "while ! ping -c1 -w1 $IP; do sleep 1; done"; then |
Jesse Andrews | ab8dbce | 2011-10-26 21:23:20 -0700 | [diff] [blame] | 112 | echo "Couldn't ping server" |
| 113 | exit 1 |
| 114 | fi |
Anthony Young | 79e807a | 2011-10-31 11:16:44 -0700 | [diff] [blame] | 115 | else |
| 116 | # On a multi-host system, without vm net access, do a sleep to wait for the boot |
| 117 | sleep $BOOT_TIMEOUT |
Anthony Young | 8ecd294 | 2011-10-24 22:58:14 -0700 | [diff] [blame] | 118 | fi |
Jesse Andrews | 6fc7101 | 2011-10-24 11:29:08 -0700 | [diff] [blame] | 119 | |
| 120 | # Security Groups & Floating IPs |
| 121 | # ------------------------------ |
| 122 | |
| 123 | # allow icmp traffic (ping) |
Anthony Young | 20a2cae | 2011-10-17 16:02:24 -0700 | [diff] [blame] | 124 | nova secgroup-add-rule $SECGROUP icmp -1 -1 0.0.0.0/0 |
| 125 | |
| 126 | # List rules for a secgroup |
| 127 | nova secgroup-list-rules $SECGROUP |
| 128 | |
| 129 | # allocate a floating ip |
| 130 | nova floating-ip-create |
| 131 | |
| 132 | # store floating address |
Jesse Andrews | 6fc7101 | 2011-10-24 11:29:08 -0700 | [diff] [blame] | 133 | FLOATING_IP=`nova floating-ip-list | grep None | head -1 | cut -d '|' -f2 | sed 's/ //g'` |
Anthony Young | 20a2cae | 2011-10-17 16:02:24 -0700 | [diff] [blame] | 134 | |
| 135 | # add floating ip to our server |
Jesse Andrews | 6fc7101 | 2011-10-24 11:29:08 -0700 | [diff] [blame] | 136 | nova add-floating-ip $NAME $FLOATING_IP |
Anthony Young | 20a2cae | 2011-10-17 16:02:24 -0700 | [diff] [blame] | 137 | |
Anthony Young | 79e807a | 2011-10-31 11:16:44 -0700 | [diff] [blame] | 138 | # test we can ping our floating ip within ASSOCIATE_TIMEOUT seconds |
| 139 | if ! timeout $ASSOCIATE_TIMEOUT sh -c "while ! ping -c1 -w1 $FLOATING_IP; do sleep 1; done"; then |
Jesse Andrews | 5a77483 | 2011-10-26 21:30:02 -0700 | [diff] [blame] | 140 | echo "Couldn't ping server with floating ip" |
| 141 | exit 1 |
| 142 | fi |
Anthony Young | 20a2cae | 2011-10-17 16:02:24 -0700 | [diff] [blame] | 143 | |
Jesse Andrews | b7cc5bc | 2011-10-26 22:11:31 -0700 | [diff] [blame] | 144 | # pause the VM and verify we can't ping it anymore |
| 145 | nova pause $NAME |
| 146 | |
Jesse Andrews | 467135e | 2011-10-27 14:06:33 -0700 | [diff] [blame] | 147 | sleep 2 |
Jesse Andrews | b7cc5bc | 2011-10-26 22:11:31 -0700 | [diff] [blame] | 148 | |
Jesse Andrews | 467135e | 2011-10-27 14:06:33 -0700 | [diff] [blame] | 149 | if ( ping -c1 -w1 $IP); then |
Jesse Andrews | b7cc5bc | 2011-10-26 22:11:31 -0700 | [diff] [blame] | 150 | echo "Pause failure - ping shouldn't work" |
| 151 | exit 1 |
| 152 | fi |
| 153 | |
Jesse Andrews | 0c484fe | 2011-10-27 14:10:47 -0700 | [diff] [blame] | 154 | if ( ping -c1 -w1 $FLOATING_IP); then |
| 155 | echo "Pause failure - ping floating ips shouldn't work" |
| 156 | exit 1 |
| 157 | fi |
| 158 | |
Jesse Andrews | b7cc5bc | 2011-10-26 22:11:31 -0700 | [diff] [blame] | 159 | # unpause the VM and verify we can ping it again |
| 160 | nova unpause $NAME |
| 161 | |
Jesse Andrews | 467135e | 2011-10-27 14:06:33 -0700 | [diff] [blame] | 162 | sleep 2 |
Jesse Andrews | b7cc5bc | 2011-10-26 22:11:31 -0700 | [diff] [blame] | 163 | |
Jesse Andrews | 467135e | 2011-10-27 14:06:33 -0700 | [diff] [blame] | 164 | ping -c1 -w1 $IP |
Jesse Andrews | b7cc5bc | 2011-10-26 22:11:31 -0700 | [diff] [blame] | 165 | |
Jesse Andrews | 6fc7101 | 2011-10-24 11:29:08 -0700 | [diff] [blame] | 166 | # dis-allow icmp traffic (ping) |
Anthony Young | 20a2cae | 2011-10-17 16:02:24 -0700 | [diff] [blame] | 167 | nova secgroup-delete-rule $SECGROUP icmp -1 -1 0.0.0.0/0 |
| 168 | |
Anthony Young | 1de18c6 | 2011-11-01 14:19:18 -0500 | [diff] [blame] | 169 | # FIXME (anthony): make xs support security groups |
Jesse Andrews | 16b6efa | 2011-11-10 11:46:18 -0800 | [diff] [blame^] | 170 | if [ "$VIRT_DRIVER" != "xenserver" ]; then |
Anthony Young | 1de18c6 | 2011-11-01 14:19:18 -0500 | [diff] [blame] | 171 | # test we can aren't able to ping our floating ip within ASSOCIATE_TIMEOUT seconds |
| 172 | if ! timeout $ASSOCIATE_TIMEOUT sh -c "while ping -c1 -w1 $FLOATING_IP; do sleep 1; done"; then |
| 173 | print "Security group failure - ping should not be allowed!" |
| 174 | echo "Couldn't ping server with floating ip" |
| 175 | exit 1 |
| 176 | fi |
Anthony Young | 20a2cae | 2011-10-17 16:02:24 -0700 | [diff] [blame] | 177 | fi |
| 178 | |
| 179 | # de-allocate the floating ip |
Jesse Andrews | 6fc7101 | 2011-10-24 11:29:08 -0700 | [diff] [blame] | 180 | nova floating-ip-delete $FLOATING_IP |
Jesse Andrews | d888e1c | 2011-10-15 20:01:12 -0700 | [diff] [blame] | 181 | |
| 182 | # shutdown the server |
| 183 | nova delete $NAME |
| 184 | |
Anthony Young | 20a2cae | 2011-10-17 16:02:24 -0700 | [diff] [blame] | 185 | # Delete a secgroup |
| 186 | nova secgroup-delete $SECGROUP |
| 187 | |
Vishvananda Ishaya | 9b35367 | 2011-10-20 10:07:10 -0700 | [diff] [blame] | 188 | # FIXME: validate shutdown within 5 seconds |
Jesse Andrews | d888e1c | 2011-10-15 20:01:12 -0700 | [diff] [blame] | 189 | # (nova show $NAME returns 1 or status != ACTIVE)? |
Vishvananda Ishaya | f56e395 | 2011-10-24 16:05:57 -0700 | [diff] [blame] | 190 | |