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 |
| 23 | source ./openrc |
Jesse Andrews | b019151 | 2011-09-14 19:37:10 -0700 | [diff] [blame] | 24 | |
Anthony Young | 94c889a | 2011-10-11 18:07:48 +0000 | [diff] [blame] | 25 | # Get a token for clients that don't support service catalog |
| 26 | # ========================================================== |
Jesse Andrews | b9c77d6 | 2011-10-15 18:37:25 -0700 | [diff] [blame] | 27 | |
Vishvananda Ishaya | 9b35367 | 2011-10-20 10:07:10 -0700 | [diff] [blame] | 28 | # manually create a token by querying keystone (sending JSON data). Keystone |
Jesse Andrews | b9c77d6 | 2011-10-15 18:37:25 -0700 | [diff] [blame] | 29 | # returns a token and catalog of endpoints. We use python to parse the token |
| 30 | # and save it. |
| 31 | |
Jesse Andrews | e61f318 | 2011-10-24 13:43:04 -0700 | [diff] [blame] | 32 | 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] | 33 | |
Jesse Andrews | 593828d | 2011-09-14 22:44:50 -0700 | [diff] [blame] | 34 | # Launching a server |
| 35 | # ================== |
Jesse Andrews | b19424f | 2011-09-14 22:03:04 -0700 | [diff] [blame] | 36 | |
Jesse Andrews | 593828d | 2011-09-14 22:44:50 -0700 | [diff] [blame] | 37 | # List servers for tenant: |
Jesse Andrews | b019151 | 2011-09-14 19:37:10 -0700 | [diff] [blame] | 38 | nova list |
Jesse Andrews | 593828d | 2011-09-14 22:44:50 -0700 | [diff] [blame] | 39 | |
Jesse Andrews | 593828d | 2011-09-14 22:44:50 -0700 | [diff] [blame] | 40 | # Images |
| 41 | # ------ |
| 42 | |
| 43 | # Nova has a **deprecated** way of listing images. |
| 44 | nova image-list |
| 45 | |
| 46 | # But we recommend using glance directly |
Jesse Andrews | 4e8847c | 2011-10-15 19:29:55 -0700 | [diff] [blame] | 47 | glance -A $TOKEN index |
Jesse Andrews | 593828d | 2011-09-14 22:44:50 -0700 | [diff] [blame] | 48 | |
Jesse Andrews | d888e1c | 2011-10-15 20:01:12 -0700 | [diff] [blame] | 49 | # Let's grab the id of the first AMI image to launch |
| 50 | IMAGE=`glance -A $TOKEN index | egrep ami | cut -d" " -f1` |
| 51 | |
Anthony Young | 20a2cae | 2011-10-17 16:02:24 -0700 | [diff] [blame] | 52 | # Security Groups |
| 53 | # --------------- |
| 54 | SECGROUP=test_secgroup |
| 55 | |
| 56 | # List of secgroups: |
| 57 | nova secgroup-list |
| 58 | |
| 59 | # Create a secgroup |
| 60 | nova secgroup-create $SECGROUP "test_secgroup description" |
Jesse Andrews | d888e1c | 2011-10-15 20:01:12 -0700 | [diff] [blame] | 61 | |
Jesse Andrews | 6fc7101 | 2011-10-24 11:29:08 -0700 | [diff] [blame] | 62 | # determine flavor |
| 63 | # ---------------- |
Jesse Andrews | d888e1c | 2011-10-15 20:01:12 -0700 | [diff] [blame] | 64 | |
| 65 | # List of flavors: |
| 66 | nova flavor-list |
| 67 | |
| 68 | # and grab the first flavor in the list to launch |
| 69 | FLAVOR=`nova flavor-list | head -n 4 | tail -n 1 | cut -d"|" -f2` |
| 70 | |
Anthony Young | 20a2cae | 2011-10-17 16:02:24 -0700 | [diff] [blame] | 71 | NAME="myserver" |
Jesse Andrews | d888e1c | 2011-10-15 20:01:12 -0700 | [diff] [blame] | 72 | |
Anthony Young | 20a2cae | 2011-10-17 16:02:24 -0700 | [diff] [blame] | 73 | nova boot --flavor $FLAVOR --image $IMAGE $NAME --security_groups=$SECGROUP |
Jesse Andrews | d888e1c | 2011-10-15 20:01:12 -0700 | [diff] [blame] | 74 | |
Jesse Andrews | 6fc7101 | 2011-10-24 11:29:08 -0700 | [diff] [blame] | 75 | # Testing |
| 76 | # ======= |
| 77 | |
| 78 | # First check if it spins up (becomes active and responds to ping on |
| 79 | # internal ip). If you run this script from a nova node, you should |
| 80 | # bypass security groups and have direct access to the server. |
| 81 | |
| 82 | # Waiting for boot |
| 83 | # ---------------- |
| 84 | |
Jesse Andrews | 5a77483 | 2011-10-26 21:30:02 -0700 | [diff] [blame] | 85 | # check that the status is active within 10 seconds |
| 86 | if ! timeout 10 sh -c "while ! nova show $NAME | grep status | grep -q ACTIVE; do sleep 1; done"; then |
| 87 | echo "server didn't become active!" |
| 88 | exit 1 |
| 89 | fi |
Jesse Andrews | d888e1c | 2011-10-15 20:01:12 -0700 | [diff] [blame] | 90 | |
| 91 | # get the IP of the server |
| 92 | IP=`nova show $NAME | grep "private network" | cut -d"|" -f3` |
| 93 | |
Anthony Young | 8ecd294 | 2011-10-24 22:58:14 -0700 | [diff] [blame] | 94 | # for single node deployments, we can ping private ips |
| 95 | MULTI_HOST=${MULTI_HOST:-0} |
Justin Shepherd | 56a505f | 2011-10-26 10:45:02 -0500 | [diff] [blame] | 96 | if [ "$MULTI_HOST" = "0" ]; then |
Anthony Young | 8ecd294 | 2011-10-24 22:58:14 -0700 | [diff] [blame] | 97 | # sometimes the first ping fails (10 seconds isn't enough time for the VM's |
Jesse Andrews | ac2e166 | 2011-10-26 21:39:56 -0700 | [diff] [blame] | 98 | # network to respond?), so let's ping for 15 seconds with a timeout |
Jesse Andrews | ab8dbce | 2011-10-26 21:23:20 -0700 | [diff] [blame] | 99 | # of a second. |
| 100 | if ! timeout 15 sh -c "while ! ping -c1 -w1 $IP; do sleep 1; done"; then |
| 101 | echo "Couldn't ping server" |
| 102 | exit 1 |
| 103 | fi |
Anthony Young | 8ecd294 | 2011-10-24 22:58:14 -0700 | [diff] [blame] | 104 | fi |
Jesse Andrews | 6fc7101 | 2011-10-24 11:29:08 -0700 | [diff] [blame] | 105 | |
| 106 | # Security Groups & Floating IPs |
| 107 | # ------------------------------ |
| 108 | |
| 109 | # allow icmp traffic (ping) |
Anthony Young | 20a2cae | 2011-10-17 16:02:24 -0700 | [diff] [blame] | 110 | nova secgroup-add-rule $SECGROUP icmp -1 -1 0.0.0.0/0 |
| 111 | |
| 112 | # List rules for a secgroup |
| 113 | nova secgroup-list-rules $SECGROUP |
| 114 | |
| 115 | # allocate a floating ip |
| 116 | nova floating-ip-create |
| 117 | |
| 118 | # store floating address |
Jesse Andrews | 6fc7101 | 2011-10-24 11:29:08 -0700 | [diff] [blame] | 119 | 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] | 120 | |
| 121 | # add floating ip to our server |
Jesse Andrews | 6fc7101 | 2011-10-24 11:29:08 -0700 | [diff] [blame] | 122 | nova add-floating-ip $NAME $FLOATING_IP |
Anthony Young | 20a2cae | 2011-10-17 16:02:24 -0700 | [diff] [blame] | 123 | |
Jesse Andrews | 5a77483 | 2011-10-26 21:30:02 -0700 | [diff] [blame] | 124 | # test we can ping our floating ip within 10 seconds |
| 125 | if ! timeout 10 sh -c "while ! ping -c1 -w1 $FLOATING_IP; do sleep 1; done"; then |
| 126 | echo "Couldn't ping server with floating ip" |
| 127 | exit 1 |
| 128 | fi |
Anthony Young | 20a2cae | 2011-10-17 16:02:24 -0700 | [diff] [blame] | 129 | |
Jesse Andrews | b7cc5bc | 2011-10-26 22:11:31 -0700 | [diff] [blame] | 130 | # pause the VM and verify we can't ping it anymore |
| 131 | nova pause $NAME |
| 132 | |
Jesse Andrews | 467135e | 2011-10-27 14:06:33 -0700 | [diff] [blame] | 133 | sleep 2 |
Jesse Andrews | b7cc5bc | 2011-10-26 22:11:31 -0700 | [diff] [blame] | 134 | |
Jesse Andrews | 467135e | 2011-10-27 14:06:33 -0700 | [diff] [blame] | 135 | if ( ping -c1 -w1 $IP); then |
Jesse Andrews | b7cc5bc | 2011-10-26 22:11:31 -0700 | [diff] [blame] | 136 | echo "Pause failure - ping shouldn't work" |
| 137 | exit 1 |
| 138 | fi |
| 139 | |
Jesse Andrews | 0c484fe | 2011-10-27 14:10:47 -0700 | [diff] [blame] | 140 | if ( ping -c1 -w1 $FLOATING_IP); then |
| 141 | echo "Pause failure - ping floating ips shouldn't work" |
| 142 | exit 1 |
| 143 | fi |
| 144 | |
Jesse Andrews | b7cc5bc | 2011-10-26 22:11:31 -0700 | [diff] [blame] | 145 | # unpause the VM and verify we can ping it again |
| 146 | nova unpause $NAME |
| 147 | |
Jesse Andrews | 467135e | 2011-10-27 14:06:33 -0700 | [diff] [blame] | 148 | sleep 2 |
Jesse Andrews | b7cc5bc | 2011-10-26 22:11:31 -0700 | [diff] [blame] | 149 | |
Jesse Andrews | 467135e | 2011-10-27 14:06:33 -0700 | [diff] [blame] | 150 | ping -c1 -w1 $IP |
Jesse Andrews | b7cc5bc | 2011-10-26 22:11:31 -0700 | [diff] [blame] | 151 | |
Jesse Andrews | 6fc7101 | 2011-10-24 11:29:08 -0700 | [diff] [blame] | 152 | # dis-allow icmp traffic (ping) |
Anthony Young | 20a2cae | 2011-10-17 16:02:24 -0700 | [diff] [blame] | 153 | nova secgroup-delete-rule $SECGROUP icmp -1 -1 0.0.0.0/0 |
| 154 | |
Jesse Andrews | 5a77483 | 2011-10-26 21:30:02 -0700 | [diff] [blame] | 155 | # test we can aren't able to ping our floating ip within 10 seconds |
| 156 | if ! timeout 10 sh -c "while ping -c1 -w1 $FLOATING_IP; do sleep 1; done"; then |
Anthony Young | 20a2cae | 2011-10-17 16:02:24 -0700 | [diff] [blame] | 157 | print "Security group failure - ping should not be allowed!" |
Jesse Andrews | 5a77483 | 2011-10-26 21:30:02 -0700 | [diff] [blame] | 158 | echo "Couldn't ping server with floating ip" |
Anthony Young | 20a2cae | 2011-10-17 16:02:24 -0700 | [diff] [blame] | 159 | exit 1 |
| 160 | fi |
| 161 | |
| 162 | # de-allocate the floating ip |
Jesse Andrews | 6fc7101 | 2011-10-24 11:29:08 -0700 | [diff] [blame] | 163 | nova floating-ip-delete $FLOATING_IP |
Jesse Andrews | d888e1c | 2011-10-15 20:01:12 -0700 | [diff] [blame] | 164 | |
| 165 | # shutdown the server |
| 166 | nova delete $NAME |
| 167 | |
Anthony Young | 20a2cae | 2011-10-17 16:02:24 -0700 | [diff] [blame] | 168 | # Delete a secgroup |
| 169 | nova secgroup-delete $SECGROUP |
| 170 | |
Vishvananda Ishaya | 9b35367 | 2011-10-20 10:07:10 -0700 | [diff] [blame] | 171 | # FIXME: validate shutdown within 5 seconds |
Jesse Andrews | d888e1c | 2011-10-15 20:01:12 -0700 | [diff] [blame] | 172 | # (nova show $NAME returns 1 or status != ACTIVE)? |
Vishvananda Ishaya | f56e395 | 2011-10-24 16:05:57 -0700 | [diff] [blame] | 173 | |
| 174 | # Testing Euca2ools |
| 175 | # ================== |
| 176 | |
| 177 | # make sure that we can describe instances |
| 178 | euca-describe-instances |