blob: 01d548d1f25470fb61fe7099ceb2c819ac19ef90 [file] [log] [blame]
John Garbuttfd1c87e2012-02-24 14:52:54 +00001#!/usr/bin/env bash
2
3# **aggregates.sh**
4
5# This script demonstrates how to use host aggregates:
Adam Spiers6a5aa7c2013-10-24 11:27:02 +01006#
7# * Create an Aggregate
8# * Updating Aggregate details
9# * Testing Aggregate metadata
10# * Testing Aggregate delete
11# * Testing General Aggregates (https://blueprints.launchpad.net/nova/+spec/general-host-aggregates)
12# * Testing add/remove hosts (with one host)
John Garbuttfd1c87e2012-02-24 14:52:54 +000013
14echo "**************************************************"
15echo "Begin DevStack Exercise: $0"
16echo "**************************************************"
17
18# This script exits on an error so that errors don't compound and you see
Joe Gordonb7ef5392012-08-01 16:13:42 -070019# only the first error that occurred.
John Garbuttfd1c87e2012-02-24 14:52:54 +000020set -o errexit
21
22# Print the commands being run so that we can see the command that triggers
23# an error. It is also useful for following allowing as the install occurs.
24set -o xtrace
25
26
27# Settings
28# ========
29
30# Keep track of the current directory
31EXERCISE_DIR=$(cd $(dirname "$0") && pwd)
32TOP_DIR=$(cd $EXERCISE_DIR/..; pwd)
33
34# Import common functions
35source $TOP_DIR/functions
36
37# Import configuration
38source $TOP_DIR/openrc
39
40# Import exercise configuration
41source $TOP_DIR/exerciserc
42
Dean Troyerda85cda2013-02-15 11:07:14 -060043# Test as the admin user
Mate Lakatc497a052013-02-21 15:25:51 +000044. $TOP_DIR/openrc admin admin
John Garbuttfd1c87e2012-02-24 14:52:54 +000045
Kiall Mac Innesa16c8212014-01-12 19:35:43 +000046# If nova api is not enabled we exit with exitcode 55 so that
47# the exercise is skipped
48is_service_enabled n-api || exit 55
49
Chris Behrensc62c2b92013-07-24 03:56:13 -070050# Cells does not support aggregates.
51is_service_enabled n-cell && exit 55
John Garbuttfd1c87e2012-02-24 14:52:54 +000052
53# Create an aggregate
54# ===================
55
56AGGREGATE_NAME=test_aggregate_$RANDOM
Joe Gordonb7ef5392012-08-01 16:13:42 -070057AGGREGATE2_NAME=test_aggregate_$RANDOM
John Garbuttfd1c87e2012-02-24 14:52:54 +000058AGGREGATE_A_ZONE=nova
59
Ian Wienandaee18c72014-02-21 15:35:08 +110060function exit_if_aggregate_present {
John Garbuttfd1c87e2012-02-24 14:52:54 +000061 aggregate_name=$1
62
Dean Troyerda85cda2013-02-15 11:07:14 -060063 if [ $(nova aggregate-list | grep -c " $aggregate_name ") == 0 ]; then
John Garbuttfd1c87e2012-02-24 14:52:54 +000064 echo "SUCCESS $aggregate_name not present"
65 else
Nachi Ueno07115eb2013-02-26 12:38:18 -080066 die $LINENO "found aggregate: $aggregate_name"
John Garbuttfd1c87e2012-02-24 14:52:54 +000067 exit -1
68 fi
69}
70
71exit_if_aggregate_present $AGGREGATE_NAME
72
Dean Troyerda85cda2013-02-15 11:07:14 -060073AGGREGATE_ID=$(nova aggregate-create $AGGREGATE_NAME $AGGREGATE_A_ZONE | grep " $AGGREGATE_NAME " | get_field 1)
DennyZhang23178a92013-10-22 17:07:32 -050074die_if_not_set $LINENO AGGREGATE_ID "Failure creating AGGREGATE_ID for $AGGREGATE_NAME $AGGREGATE_A_ZONE"
75
Dean Troyerda85cda2013-02-15 11:07:14 -060076AGGREGATE2_ID=$(nova aggregate-create $AGGREGATE2_NAME $AGGREGATE_A_ZONE | grep " $AGGREGATE2_NAME " | get_field 1)
DennyZhang23178a92013-10-22 17:07:32 -050077die_if_not_set $LINENO AGGREGATE2_ID "Fail creating AGGREGATE2_ID for $AGGREGATE2_NAME $AGGREGATE_A_ZONE"
John Garbuttfd1c87e2012-02-24 14:52:54 +000078
79# check aggregate created
Nachi Ueno07115eb2013-02-26 12:38:18 -080080nova aggregate-list | grep -q " $AGGREGATE_NAME " || die $LINENO "Aggregate $AGGREGATE_NAME not created"
John Garbuttfd1c87e2012-02-24 14:52:54 +000081
82
83# Ensure creating a duplicate fails
84# =================================
85
86if nova aggregate-create $AGGREGATE_NAME $AGGREGATE_A_ZONE; then
Nachi Ueno07115eb2013-02-26 12:38:18 -080087 die $LINENO "could create duplicate aggregate"
John Garbuttfd1c87e2012-02-24 14:52:54 +000088fi
89
90
91# Test aggregate-update (and aggregate-details)
92# =============================================
93AGGREGATE_NEW_NAME=test_aggregate_$RANDOM
94
95nova aggregate-update $AGGREGATE_ID $AGGREGATE_NEW_NAME
96nova aggregate-details $AGGREGATE_ID | grep $AGGREGATE_NEW_NAME
97nova aggregate-details $AGGREGATE_ID | grep $AGGREGATE_A_ZONE
98
99nova aggregate-update $AGGREGATE_ID $AGGREGATE_NAME $AGGREGATE_A_ZONE
100nova aggregate-details $AGGREGATE_ID | grep $AGGREGATE_NAME
101nova aggregate-details $AGGREGATE_ID | grep $AGGREGATE_A_ZONE
102
103
104# Test aggregate-set-metadata
105# ===========================
106META_DATA_1_KEY=asdf
107META_DATA_2_KEY=foo
108META_DATA_3_KEY=bar
109
Joe Gordon5c1bedd2012-12-12 12:03:19 +0000110#ensure no additional metadata is set
Sahid Orentino Ferdjaoui75e851a2013-10-16 08:34:05 +0000111nova aggregate-details $AGGREGATE_ID | egrep "\|[{u ]*'availability_zone.+$AGGREGATE_A_ZONE'[ }]*\|"
John Garbuttfd1c87e2012-02-24 14:52:54 +0000112
113nova aggregate-set-metadata $AGGREGATE_ID ${META_DATA_1_KEY}=123
114nova aggregate-details $AGGREGATE_ID | grep $META_DATA_1_KEY
115nova aggregate-details $AGGREGATE_ID | grep 123
116
117nova aggregate-set-metadata $AGGREGATE_ID ${META_DATA_2_KEY}=456
118nova aggregate-details $AGGREGATE_ID | grep $META_DATA_1_KEY
119nova aggregate-details $AGGREGATE_ID | grep $META_DATA_2_KEY
120
121nova aggregate-set-metadata $AGGREGATE_ID $META_DATA_2_KEY ${META_DATA_3_KEY}=789
122nova aggregate-details $AGGREGATE_ID | grep $META_DATA_1_KEY
123nova aggregate-details $AGGREGATE_ID | grep $META_DATA_3_KEY
124
Nachi Ueno07115eb2013-02-26 12:38:18 -0800125nova aggregate-details $AGGREGATE_ID | grep $META_DATA_2_KEY && die $LINENO "ERROR metadata was not cleared"
John Garbuttfd1c87e2012-02-24 14:52:54 +0000126
127nova aggregate-set-metadata $AGGREGATE_ID $META_DATA_3_KEY $META_DATA_1_KEY
Sahid Orentino Ferdjaoui75e851a2013-10-16 08:34:05 +0000128nova aggregate-details $AGGREGATE_ID | egrep "\|[{u ]*'availability_zone.+$AGGREGATE_A_ZONE'[ }]*\|"
John Garbuttfd1c87e2012-02-24 14:52:54 +0000129
130
131# Test aggregate-add/remove-host
132# ==============================
133if [ "$VIRT_DRIVER" == "xenserver" ]; then
Joe Gordonb7ef5392012-08-01 16:13:42 -0700134 echo "TODO(johngarbutt) add tests for add/remove host from pool aggregate"
John Garbuttfd1c87e2012-02-24 14:52:54 +0000135fi
Dean Troyerda85cda2013-02-15 11:07:14 -0600136FIRST_HOST=$(nova host-list | grep compute | get_field 1 | head -1)
Joe Gordonb7ef5392012-08-01 16:13:42 -0700137# Make sure can add two aggregates to same host
Mate Lakat178b8402012-09-05 10:42:10 +0100138nova aggregate-add-host $AGGREGATE_ID $FIRST_HOST
139nova aggregate-add-host $AGGREGATE2_ID $FIRST_HOST
140if nova aggregate-add-host $AGGREGATE2_ID $FIRST_HOST; then
Nachi Ueno07115eb2013-02-26 12:38:18 -0800141 die $LINENO "could add duplicate host to single aggregate"
Joe Gordonb7ef5392012-08-01 16:13:42 -0700142fi
Mate Lakat178b8402012-09-05 10:42:10 +0100143nova aggregate-remove-host $AGGREGATE2_ID $FIRST_HOST
144nova aggregate-remove-host $AGGREGATE_ID $FIRST_HOST
John Garbuttfd1c87e2012-02-24 14:52:54 +0000145
146# Test aggregate-delete
147# =====================
148nova aggregate-delete $AGGREGATE_ID
Joe Gordonb7ef5392012-08-01 16:13:42 -0700149nova aggregate-delete $AGGREGATE2_ID
John Garbuttfd1c87e2012-02-24 14:52:54 +0000150exit_if_aggregate_present $AGGREGATE_NAME
151
John Garbuttfd1c87e2012-02-24 14:52:54 +0000152set +o xtrace
153echo "**************************************************"
154echo "End DevStack Exercise: $0"
155echo "**************************************************"