Browse code

move from exercise.sh to exercises/..

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