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 | |
| 10 | # This script exits on an error so that errors don't compound and you see |
| 11 | # only the first error that occured. |
| 12 | set -o errexit |
| 13 | |
| 14 | # Print the commands being run so that we can see the command that triggers |
| 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 | |
| 22 | HOST=${HOST:-localhost} |
Jesse Andrews | b019151 | 2011-09-14 19:37:10 -0700 | [diff] [blame] | 23 | |
Jesse Andrews | b19424f | 2011-09-14 22:03:04 -0700 | [diff] [blame] | 24 | # Nova original used project_id as the *account* that owned resources (servers, |
| 25 | # ip address, ...) With the addition of Keystone we have standardized on the |
| 26 | # term **tenant** as the entity that owns the resources. **novaclient** still |
Anthony Young | 0edde7d | 2011-10-06 07:10:24 -0700 | [diff] [blame] | 27 | # uses the old deprecated terms project_id. Note that this field should now be |
| 28 | # set to tenant_name, not tenant_id. |
| 29 | export NOVA_PROJECT_ID=${TENANT:-demo} |
Jesse Andrews | b19424f | 2011-09-14 22:03:04 -0700 | [diff] [blame] | 30 | |
| 31 | # In addition to the owning entity (tenant), nova stores the entity performing |
| 32 | # the action as the **user**. |
| 33 | export NOVA_USERNAME=${USERNAME:-demo} |
| 34 | |
| 35 | # With Keystone you pass the keystone password instead of an api key. |
| 36 | export NOVA_API_KEY=${PASSWORD:-secrete} |
| 37 | |
| 38 | # With the addition of Keystone, to use an openstack cloud you should |
| 39 | # authenticate against keystone, which returns a **Token** and **Service |
| 40 | # Catalog**. The catalog contains the endpoint for all services the user/tenant |
| 41 | # has access to - including nova, glance, keystone, swift, ... We currently |
| 42 | # recommend using the 2.0 *auth api*. |
| 43 | # |
| 44 | # *NOTE*: Using the 2.0 *auth api* does mean that compute api is 2.0. We will |
| 45 | # use the 1.1 *compute api* |
| 46 | export NOVA_URL=${NOVA_URL:-http://$HOST:5000/v2.0/} |
| 47 | |
| 48 | # Currently novaclient needs you to specify the *compute api* version. This |
| 49 | # needs to match the config of your catalog returned by Keystone. |
Jesse Andrews | b019151 | 2011-09-14 19:37:10 -0700 | [diff] [blame] | 50 | export NOVA_VERSION=1.1 |
| 51 | |
Anthony Young | 4387690 | 2011-09-27 00:29:28 -0700 | [diff] [blame] | 52 | # FIXME - why does this need to be specified? |
| 53 | export NOVA_REGION_NAME=RegionOne |
| 54 | |
Jesse Andrews | b9c77d6 | 2011-10-15 18:37:25 -0700 | [diff] [blame] | 55 | # set log level to DEBUG (helps debug issues) |
| 56 | export NOVACLIENT_DEBUG=1 |
Jesse Andrews | b019151 | 2011-09-14 19:37:10 -0700 | [diff] [blame] | 57 | |
Anthony Young | 94c889a | 2011-10-11 18:07:48 +0000 | [diff] [blame] | 58 | # Get a token for clients that don't support service catalog |
| 59 | # ========================================================== |
Jesse Andrews | b9c77d6 | 2011-10-15 18:37:25 -0700 | [diff] [blame] | 60 | |
| 61 | # manually create a token by querying keystone (sending JSON data). Keystone |
| 62 | # returns a token and catalog of endpoints. We use python to parse the token |
| 63 | # and save it. |
| 64 | |
| 65 | TOKEN=`curl -s -d "{\"auth\":{\"passwordCredentials\": {\"username\": \"$NOVA_USERNAME\", \"password\": \"$NOVA_API_KEY\"}}}" -H "Content-type: application/json" http://$HOST: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] | 66 | |
Jesse Andrews | 593828d | 2011-09-14 22:44:50 -0700 | [diff] [blame] | 67 | # Launching a server |
| 68 | # ================== |
Jesse Andrews | b19424f | 2011-09-14 22:03:04 -0700 | [diff] [blame] | 69 | |
Jesse Andrews | 593828d | 2011-09-14 22:44:50 -0700 | [diff] [blame] | 70 | # List servers for tenant: |
Jesse Andrews | b019151 | 2011-09-14 19:37:10 -0700 | [diff] [blame] | 71 | nova list |
Jesse Andrews | 593828d | 2011-09-14 22:44:50 -0700 | [diff] [blame] | 72 | |
Jesse Andrews | 593828d | 2011-09-14 22:44:50 -0700 | [diff] [blame] | 73 | # Images |
| 74 | # ------ |
| 75 | |
| 76 | # Nova has a **deprecated** way of listing images. |
| 77 | nova image-list |
| 78 | |
| 79 | # But we recommend using glance directly |
Jesse Andrews | 4e8847c | 2011-10-15 19:29:55 -0700 | [diff] [blame] | 80 | glance -A $TOKEN index |
Jesse Andrews | 593828d | 2011-09-14 22:44:50 -0700 | [diff] [blame] | 81 | |
Jesse Andrews | d888e1c | 2011-10-15 20:01:12 -0700 | [diff] [blame] | 82 | # Let's grab the id of the first AMI image to launch |
| 83 | IMAGE=`glance -A $TOKEN index | egrep ami | cut -d" " -f1` |
| 84 | |
Anthony Young | 20a2cae | 2011-10-17 16:02:24 -0700 | [diff] [blame^] | 85 | # Security Groups |
| 86 | # --------------- |
| 87 | SECGROUP=test_secgroup |
| 88 | |
| 89 | # List of secgroups: |
| 90 | nova secgroup-list |
| 91 | |
| 92 | # Create a secgroup |
| 93 | nova secgroup-create $SECGROUP "test_secgroup description" |
Jesse Andrews | d888e1c | 2011-10-15 20:01:12 -0700 | [diff] [blame] | 94 | |
| 95 | # Flavors |
| 96 | # ------- |
| 97 | |
| 98 | # List of flavors: |
| 99 | nova flavor-list |
| 100 | |
| 101 | # and grab the first flavor in the list to launch |
| 102 | FLAVOR=`nova flavor-list | head -n 4 | tail -n 1 | cut -d"|" -f2` |
| 103 | |
Anthony Young | 20a2cae | 2011-10-17 16:02:24 -0700 | [diff] [blame^] | 104 | NAME="myserver" |
Jesse Andrews | d888e1c | 2011-10-15 20:01:12 -0700 | [diff] [blame] | 105 | |
Anthony Young | 20a2cae | 2011-10-17 16:02:24 -0700 | [diff] [blame^] | 106 | nova boot --flavor $FLAVOR --image $IMAGE $NAME --security_groups=$SECGROUP |
Jesse Andrews | d888e1c | 2011-10-15 20:01:12 -0700 | [diff] [blame] | 107 | |
| 108 | # let's give it 10 seconds to launch |
| 109 | sleep 10 |
| 110 | |
| 111 | # check that the status is active |
| 112 | nova show $NAME | grep status | grep -q ACTIVE |
| 113 | |
| 114 | # get the IP of the server |
| 115 | IP=`nova show $NAME | grep "private network" | cut -d"|" -f3` |
| 116 | |
| 117 | # ping it once (timeout of a second) |
Jesse Andrews | da89268 | 2011-10-15 20:14:07 -0700 | [diff] [blame] | 118 | ping -c1 -w1 $IP || true |
| 119 | |
| 120 | # sometimes the first ping fails (10 seconds isn't enough time for the VM's |
| 121 | # network to respond?), so let's wait 5 seconds and really test ping |
| 122 | sleep 5 |
| 123 | |
| 124 | ping -c1 -w1 $IP |
Anthony Young | 20a2cae | 2011-10-17 16:02:24 -0700 | [diff] [blame^] | 125 | # allow icmp traffic |
| 126 | nova secgroup-add-rule $SECGROUP icmp -1 -1 0.0.0.0/0 |
| 127 | |
| 128 | # List rules for a secgroup |
| 129 | nova secgroup-list-rules $SECGROUP |
| 130 | |
| 131 | # allocate a floating ip |
| 132 | nova floating-ip-create |
| 133 | |
| 134 | # store floating address |
| 135 | FIP=`nova floating-ip-list | grep None | head -1 | cut -d '|' -f2 | sed 's/ //g'` |
| 136 | |
| 137 | # add floating ip to our server |
| 138 | nova add-floating-ip $NAME $FIP |
| 139 | |
| 140 | # sleep for a smidge |
| 141 | sleep 1 |
| 142 | |
| 143 | # ping our fip |
| 144 | ping -c1 -w1 $FIP |
| 145 | |
| 146 | # dis-allow icmp traffic |
| 147 | nova secgroup-delete-rule $SECGROUP icmp -1 -1 0.0.0.0/0 |
| 148 | |
| 149 | # sleep for a smidge |
| 150 | sleep 1 |
| 151 | |
| 152 | # ping our fip |
| 153 | if ( ping -c1 -w1 $FIP); then |
| 154 | print "Security group failure - ping should not be allowed!" |
| 155 | exit 1 |
| 156 | fi |
| 157 | |
| 158 | # de-allocate the floating ip |
| 159 | nova floating-ip-delete $FIP |
Jesse Andrews | d888e1c | 2011-10-15 20:01:12 -0700 | [diff] [blame] | 160 | |
| 161 | # shutdown the server |
| 162 | nova delete $NAME |
| 163 | |
Anthony Young | 20a2cae | 2011-10-17 16:02:24 -0700 | [diff] [blame^] | 164 | # Delete a secgroup |
| 165 | nova secgroup-delete $SECGROUP |
| 166 | |
Jesse Andrews | d888e1c | 2011-10-15 20:01:12 -0700 | [diff] [blame] | 167 | # FIXME: validate shutdown within 5 seconds |
| 168 | # (nova show $NAME returns 1 or status != ACTIVE)? |