Sean Dague | 655e0af | 2014-05-29 09:00:22 -0400 | [diff] [blame] | 1 | #!/usr/bin/env python |
| 2 | # |
| 3 | # Licensed under the Apache License, Version 2.0 (the "License"); you may |
| 4 | # not use this file except in compliance with the License. You may obtain |
| 5 | # a copy of the License at |
| 6 | # |
| 7 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | # |
| 9 | # Unless required by applicable law or agreed to in writing, software |
| 10 | # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 11 | # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| 12 | # License for the specific language governing permissions and limitations |
| 13 | # under the License. |
| 14 | |
Joe H. Rahme | 61469fd | 2014-12-02 17:09:17 +0100 | [diff] [blame] | 15 | """Javelin is a tool for creating, verifying, and deleting a small set of |
Sean Dague | 655e0af | 2014-05-29 09:00:22 -0400 | [diff] [blame] | 16 | resources in a declarative way. |
| 17 | |
Joe H. Rahme | 61469fd | 2014-12-02 17:09:17 +0100 | [diff] [blame] | 18 | Javelin is meant to be used as a way to validate quickly that resources can |
| 19 | survive an upgrade process. |
| 20 | |
| 21 | Authentication |
| 22 | -------------- |
| 23 | |
| 24 | Javelin will be creating (and removing) users and tenants so it needs the admin |
| 25 | credentials of your cloud to operate properly. The corresponding info can be |
| 26 | given the usual way, either through CLI options or environment variables. |
| 27 | |
| 28 | You're probably familiar with these, but just in case:: |
| 29 | |
| 30 | +----------+------------------+----------------------+ |
| 31 | | Param | CLI | Environment Variable | |
| 32 | +----------+------------------+----------------------+ |
| 33 | | Username | --os-username | OS_USERNAME | |
| 34 | | Password | --os-password | OS_PASSWORD | |
| 35 | | Tenant | --os-tenant-name | OS_TENANT_NAME | |
| 36 | +----------+------------------+----------------------+ |
| 37 | |
| 38 | |
| 39 | Runtime Arguments |
| 40 | ----------------- |
| 41 | |
| 42 | **-m/--mode**: (Required) Has to be one of 'check', 'create' or 'destroy'. It |
| 43 | indicates which actions javelin is going to perform. |
| 44 | |
| 45 | **-r/--resources**: (Required) The path to a YAML file describing the resources |
| 46 | used by Javelin. |
| 47 | |
| 48 | **-d/--devstack-base**: (Required) The path to the devstack repo used to |
| 49 | retrieve artefacts (like images) that will be referenced in the resource files. |
| 50 | |
| 51 | **-c/--config-file**: (Optional) The path to a valid Tempest config file |
| 52 | describing your cloud. Javelin may use this to determine if certain services |
| 53 | are enabled and modify its behavior accordingly. |
| 54 | |
| 55 | |
| 56 | Resource file |
| 57 | ------------- |
| 58 | |
| 59 | The resource file is a valid YAML file describing the resources that will be |
| 60 | created, checked and destroyed by javelin. Here's a canonical example of a |
| 61 | resource file:: |
| 62 | |
| 63 | tenants: |
| 64 | - javelin |
| 65 | - discuss |
| 66 | |
| 67 | users: |
| 68 | - name: javelin |
| 69 | pass: gungnir |
| 70 | tenant: javelin |
| 71 | - name: javelin2 |
| 72 | pass: gungnir2 |
| 73 | tenant: discuss |
| 74 | |
| 75 | # resources that we want to create |
| 76 | images: |
| 77 | - name: javelin_cirros |
| 78 | owner: javelin |
| 79 | file: cirros-0.3.2-x86_64-blank.img |
Joe H. Rahme | 4352d5d | 2015-03-09 17:41:18 +0100 | [diff] [blame] | 80 | disk_format: ami |
| 81 | container_format: ami |
Joe H. Rahme | 61469fd | 2014-12-02 17:09:17 +0100 | [diff] [blame] | 82 | aki: cirros-0.3.2-x86_64-vmlinuz |
| 83 | ari: cirros-0.3.2-x86_64-initrd |
| 84 | |
| 85 | servers: |
| 86 | - name: peltast |
| 87 | owner: javelin |
| 88 | flavor: m1.small |
| 89 | image: javelin_cirros |
Joe H. Rahme | 9350a10 | 2015-03-29 17:39:20 +0200 | [diff] [blame] | 90 | floating_ip_pool: public |
Joe H. Rahme | 61469fd | 2014-12-02 17:09:17 +0100 | [diff] [blame] | 91 | - name: hoplite |
| 92 | owner: javelin |
| 93 | flavor: m1.medium |
| 94 | image: javelin_cirros |
| 95 | |
| 96 | |
| 97 | An important piece of the resource definition is the *owner* field, which is |
| 98 | the user (that we've created) that is the owner of that resource. All |
| 99 | operations on that resource will happen as that regular user to ensure that |
| 100 | admin level access does not mask issues. |
| 101 | |
| 102 | The check phase will act like a unit test, using well known assert methods to |
| 103 | verify that the correct resources exist. |
| 104 | |
Sean Dague | 655e0af | 2014-05-29 09:00:22 -0400 | [diff] [blame] | 105 | """ |
| 106 | |
Matthew Treinish | 96e9e88 | 2014-06-09 18:37:19 -0400 | [diff] [blame] | 107 | import argparse |
Chris Dent | 51e76de | 2014-10-01 12:07:14 +0100 | [diff] [blame] | 108 | import collections |
Chris Dent | 878f378 | 2014-06-30 17:04:15 +0100 | [diff] [blame] | 109 | import datetime |
Sean Dague | 655e0af | 2014-05-29 09:00:22 -0400 | [diff] [blame] | 110 | import os |
| 111 | import sys |
| 112 | import unittest |
Sean Dague | 655e0af | 2014-05-29 09:00:22 -0400 | [diff] [blame] | 113 | |
Emilien Macchi | 7a2348b | 2014-06-16 07:32:11 +0200 | [diff] [blame] | 114 | import netaddr |
Doug Hellmann | 583ce2c | 2015-03-11 14:55:46 +0000 | [diff] [blame] | 115 | from oslo_log import log as logging |
| 116 | from oslo_utils import timeutils |
Matthew Treinish | 7142668 | 2015-04-23 11:19:38 -0400 | [diff] [blame] | 117 | import six |
Matthew Treinish | 96e9e88 | 2014-06-09 18:37:19 -0400 | [diff] [blame] | 118 | import yaml |
Sean Dague | 655e0af | 2014-05-29 09:00:22 -0400 | [diff] [blame] | 119 | |
Ken'ichi Ohmichi | 6ea3f98 | 2015-11-09 12:41:13 +0000 | [diff] [blame] | 120 | from tempest.common import identity |
Ken'ichi Ohmichi | e91a0c6 | 2015-08-13 02:09:16 +0000 | [diff] [blame] | 121 | from tempest.common import waiters |
Joe Gordon | 28a84ae | 2014-07-17 15:38:28 +0000 | [diff] [blame] | 122 | from tempest import config |
Andrea Frittoli (andreaf) | db9672e | 2016-02-23 14:07:24 -0500 | [diff] [blame] | 123 | from tempest.lib import auth |
| 124 | from tempest.lib import exceptions as lib_exc |
| 125 | from tempest.lib.services.compute import flavors_client |
| 126 | from tempest.lib.services.compute import floating_ips_client |
| 127 | from tempest.lib.services.compute import security_group_rules_client |
| 128 | from tempest.lib.services.compute import security_groups_client |
| 129 | from tempest.lib.services.compute import servers_client |
Ken'ichi Ohmichi | 43e7fcf | 2016-04-04 11:59:13 -0700 | [diff] [blame] | 130 | from tempest.lib.services.network import networks_client |
Ken'ichi Ohmichi | f3110f0 | 2016-03-21 12:29:03 -0700 | [diff] [blame] | 131 | from tempest.lib.services.network import ports_client |
Andrea Frittoli (andreaf) | db9672e | 2016-02-23 14:07:24 -0500 | [diff] [blame] | 132 | from tempest.lib.services.network import subnets_client |
Jamie Lennox | c429e6a | 2015-02-24 10:42:42 +1100 | [diff] [blame] | 133 | from tempest.services.identity.v2.json import identity_client |
Daniel Mellado | 6b16b92 | 2015-12-07 12:43:08 +0000 | [diff] [blame] | 134 | from tempest.services.identity.v2.json import roles_client |
Daniel Mellado | b04da90 | 2015-11-20 17:43:12 +0100 | [diff] [blame] | 135 | from tempest.services.identity.v2.json import tenants_client |
Daniel Mellado | 82c83a5 | 2015-12-09 15:16:49 +0000 | [diff] [blame] | 136 | from tempest.services.identity.v2.json import users_client |
Ken'ichi Ohmichi | 69dcf44 | 2015-11-30 11:48:01 +0000 | [diff] [blame] | 137 | from tempest.services.image.v2.json import images_client |
Ken'ichi Ohmichi | e35f472 | 2015-12-22 04:57:11 +0000 | [diff] [blame] | 138 | from tempest.services.network.json import routers_client |
Sean Dague | 655e0af | 2014-05-29 09:00:22 -0400 | [diff] [blame] | 139 | from tempest.services.object_storage import container_client |
| 140 | from tempest.services.object_storage import object_client |
liu-sheng | 67b730e | 2015-07-16 15:19:33 +0800 | [diff] [blame] | 141 | from tempest.services.telemetry.json import alarming_client |
Chris Dent | 878f378 | 2014-06-30 17:04:15 +0100 | [diff] [blame] | 142 | from tempest.services.telemetry.json import telemetry_client |
Yaroslav Lobankov | db4a2e1 | 2015-11-28 20:04:54 +0300 | [diff] [blame] | 143 | from tempest.services.volume.v1.json import volumes_client |
Sean Dague | 655e0af | 2014-05-29 09:00:22 -0400 | [diff] [blame] | 144 | |
Emilien Macchi | 7a2348b | 2014-06-16 07:32:11 +0200 | [diff] [blame] | 145 | CONF = config.CONF |
Sean Dague | 655e0af | 2014-05-29 09:00:22 -0400 | [diff] [blame] | 146 | OPTS = {} |
| 147 | USERS = {} |
Chris Dent | 51e76de | 2014-10-01 12:07:14 +0100 | [diff] [blame] | 148 | RES = collections.defaultdict(list) |
Sean Dague | 655e0af | 2014-05-29 09:00:22 -0400 | [diff] [blame] | 149 | |
| 150 | LOG = None |
| 151 | |
Chris Dent | 878f378 | 2014-06-30 17:04:15 +0100 | [diff] [blame] | 152 | JAVELIN_START = datetime.datetime.utcnow() |
| 153 | |
Sean Dague | 655e0af | 2014-05-29 09:00:22 -0400 | [diff] [blame] | 154 | |
| 155 | class OSClient(object): |
| 156 | _creds = None |
| 157 | identity = None |
| 158 | servers = None |
| 159 | |
| 160 | def __init__(self, user, pw, tenant): |
Ken'ichi Ohmichi | 4771cbc | 2015-01-19 23:45:23 +0000 | [diff] [blame] | 161 | default_params = { |
| 162 | 'disable_ssl_certificate_validation': |
| 163 | CONF.identity.disable_ssl_certificate_validation, |
| 164 | 'ca_certs': CONF.identity.ca_certificates_file, |
| 165 | 'trace_requests': CONF.debug.trace_requests |
| 166 | } |
Ken'ichi Ohmichi | d5dba1c | 2015-01-23 02:23:22 +0000 | [diff] [blame] | 167 | default_params_with_timeout_values = { |
| 168 | 'build_interval': CONF.compute.build_interval, |
| 169 | 'build_timeout': CONF.compute.build_timeout |
| 170 | } |
| 171 | default_params_with_timeout_values.update(default_params) |
| 172 | |
Ken'ichi Ohmichi | 4771cbc | 2015-01-19 23:45:23 +0000 | [diff] [blame] | 173 | compute_params = { |
| 174 | 'service': CONF.compute.catalog_type, |
| 175 | 'region': CONF.compute.region or CONF.identity.region, |
| 176 | 'endpoint_type': CONF.compute.endpoint_type, |
| 177 | 'build_interval': CONF.compute.build_interval, |
| 178 | 'build_timeout': CONF.compute.build_timeout |
| 179 | } |
| 180 | compute_params.update(default_params) |
| 181 | |
Ken'ichi Ohmichi | 564b2ad | 2015-01-22 02:08:59 +0000 | [diff] [blame] | 182 | object_storage_params = { |
| 183 | 'service': CONF.object_storage.catalog_type, |
| 184 | 'region': CONF.object_storage.region or CONF.identity.region, |
| 185 | 'endpoint_type': CONF.object_storage.endpoint_type |
| 186 | } |
| 187 | object_storage_params.update(default_params) |
| 188 | |
andreaf | b8a5228 | 2015-03-19 22:21:54 +0000 | [diff] [blame] | 189 | _creds = auth.KeystoneV2Credentials( |
Sean Dague | 655e0af | 2014-05-29 09:00:22 -0400 | [diff] [blame] | 190 | username=user, |
| 191 | password=pw, |
| 192 | tenant_name=tenant) |
Andrea Frittoli | 9001235 | 2015-02-25 21:58:02 +0000 | [diff] [blame] | 193 | auth_provider_params = { |
| 194 | 'disable_ssl_certificate_validation': |
| 195 | CONF.identity.disable_ssl_certificate_validation, |
| 196 | 'ca_certs': CONF.identity.ca_certificates_file, |
| 197 | 'trace_requests': CONF.debug.trace_requests |
| 198 | } |
andreaf | b8a5228 | 2015-03-19 22:21:54 +0000 | [diff] [blame] | 199 | _auth = auth.KeystoneV2AuthProvider( |
Andrea Frittoli | 9001235 | 2015-02-25 21:58:02 +0000 | [diff] [blame] | 200 | _creds, CONF.identity.uri, **auth_provider_params) |
Ken'ichi Ohmichi | a628707 | 2015-07-02 02:43:15 +0000 | [diff] [blame] | 201 | self.identity = identity_client.IdentityClient( |
ghanshyam | d26b5cd | 2015-02-09 14:48:58 +0900 | [diff] [blame] | 202 | _auth, |
| 203 | CONF.identity.catalog_type, |
| 204 | CONF.identity.region, |
| 205 | endpoint_type='adminURL', |
| 206 | **default_params_with_timeout_values) |
Daniel Mellado | b04da90 | 2015-11-20 17:43:12 +0100 | [diff] [blame] | 207 | self.tenants = tenants_client.TenantsClient( |
| 208 | _auth, |
| 209 | CONF.identity.catalog_type, |
| 210 | CONF.identity.region, |
| 211 | endpoint_type='adminURL', |
| 212 | **default_params_with_timeout_values) |
Daniel Mellado | 6b16b92 | 2015-12-07 12:43:08 +0000 | [diff] [blame] | 213 | self.roles = roles_client.RolesClient( |
| 214 | _auth, |
| 215 | CONF.identity.catalog_type, |
| 216 | CONF.identity.region, |
| 217 | endpoint_type='adminURL', |
| 218 | **default_params_with_timeout_values) |
Daniel Mellado | 82c83a5 | 2015-12-09 15:16:49 +0000 | [diff] [blame] | 219 | self.users = users_client.UsersClient( |
| 220 | _auth, |
| 221 | CONF.identity.catalog_type, |
| 222 | CONF.identity.region, |
| 223 | endpoint_type='adminURL', |
| 224 | **default_params_with_timeout_values) |
Ken'ichi Ohmichi | a628707 | 2015-07-02 02:43:15 +0000 | [diff] [blame] | 225 | self.servers = servers_client.ServersClient(_auth, |
| 226 | **compute_params) |
| 227 | self.flavors = flavors_client.FlavorsClient(_auth, |
| 228 | **compute_params) |
| 229 | self.floating_ips = floating_ips_client.FloatingIPsClient( |
Emilien Macchi | c3e3e29 | 2015-03-11 17:42:08 -0400 | [diff] [blame] | 230 | _auth, **compute_params) |
Ken'ichi Ohmichi | a628707 | 2015-07-02 02:43:15 +0000 | [diff] [blame] | 231 | self.secgroups = security_groups_client.SecurityGroupsClient( |
Ken'ichi Ohmichi | 4771cbc | 2015-01-19 23:45:23 +0000 | [diff] [blame] | 232 | _auth, **compute_params) |
Ken'ichi Ohmichi | 685cd17 | 2015-07-13 01:29:57 +0000 | [diff] [blame] | 233 | self.secrules = security_group_rules_client.SecurityGroupRulesClient( |
| 234 | _auth, **compute_params) |
Ken'ichi Ohmichi | 564b2ad | 2015-01-22 02:08:59 +0000 | [diff] [blame] | 235 | self.objects = object_client.ObjectClient(_auth, |
| 236 | **object_storage_params) |
| 237 | self.containers = container_client.ContainerClient( |
| 238 | _auth, **object_storage_params) |
Ken'ichi Ohmichi | 69dcf44 | 2015-11-30 11:48:01 +0000 | [diff] [blame] | 239 | self.images = images_client.ImagesClientV2( |
Masayuki Igawa | bc7e189 | 2015-03-03 11:46:48 +0900 | [diff] [blame] | 240 | _auth, |
| 241 | CONF.image.catalog_type, |
| 242 | CONF.image.region or CONF.identity.region, |
| 243 | endpoint_type=CONF.image.endpoint_type, |
| 244 | build_interval=CONF.image.build_interval, |
| 245 | build_timeout=CONF.image.build_timeout, |
| 246 | **default_params) |
Ken'ichi Ohmichi | a628707 | 2015-07-02 02:43:15 +0000 | [diff] [blame] | 247 | self.telemetry = telemetry_client.TelemetryClient( |
Ken'ichi Ohmichi | d5dba1c | 2015-01-23 02:23:22 +0000 | [diff] [blame] | 248 | _auth, |
| 249 | CONF.telemetry.catalog_type, |
| 250 | CONF.identity.region, |
| 251 | endpoint_type=CONF.telemetry.endpoint_type, |
| 252 | **default_params_with_timeout_values) |
liu-sheng | 67b730e | 2015-07-16 15:19:33 +0800 | [diff] [blame] | 253 | self.alarming = alarming_client.AlarmingClient( |
| 254 | _auth, |
| 255 | CONF.alarm.catalog_type, |
| 256 | CONF.identity.region, |
| 257 | endpoint_type=CONF.alarm.endpoint_type, |
| 258 | **default_params_with_timeout_values) |
Ken'ichi Ohmichi | a628707 | 2015-07-02 02:43:15 +0000 | [diff] [blame] | 259 | self.volumes = volumes_client.VolumesClient( |
Ken'ichi Ohmichi | f85e9bd | 2015-01-27 12:51:47 +0000 | [diff] [blame] | 260 | _auth, |
| 261 | CONF.volume.catalog_type, |
| 262 | CONF.volume.region or CONF.identity.region, |
| 263 | endpoint_type=CONF.volume.endpoint_type, |
| 264 | build_interval=CONF.volume.build_interval, |
| 265 | build_timeout=CONF.volume.build_timeout, |
| 266 | **default_params) |
Ken'ichi Ohmichi | 43e7fcf | 2016-04-04 11:59:13 -0700 | [diff] [blame] | 267 | self.networks = networks_client.NetworksClient( |
Ken'ichi Ohmichi | a182e86 | 2015-01-21 01:16:37 +0000 | [diff] [blame] | 268 | _auth, |
| 269 | CONF.network.catalog_type, |
| 270 | CONF.network.region or CONF.identity.region, |
| 271 | endpoint_type=CONF.network.endpoint_type, |
| 272 | build_interval=CONF.network.build_interval, |
| 273 | build_timeout=CONF.network.build_timeout, |
| 274 | **default_params) |
Ken'ichi Ohmichi | f3110f0 | 2016-03-21 12:29:03 -0700 | [diff] [blame] | 275 | self.ports = ports_client.PortsClient( |
| 276 | _auth, |
| 277 | CONF.network.catalog_type, |
| 278 | CONF.network.region or CONF.identity.region, |
| 279 | endpoint_type=CONF.network.endpoint_type, |
| 280 | build_interval=CONF.network.build_interval, |
| 281 | build_timeout=CONF.network.build_timeout, |
| 282 | **default_params) |
Ken'ichi Ohmichi | e35f472 | 2015-12-22 04:57:11 +0000 | [diff] [blame] | 283 | self.routers = routers_client.RoutersClient( |
| 284 | _auth, |
| 285 | CONF.network.catalog_type, |
| 286 | CONF.network.region or CONF.identity.region, |
| 287 | endpoint_type=CONF.network.endpoint_type, |
| 288 | build_interval=CONF.network.build_interval, |
| 289 | build_timeout=CONF.network.build_timeout, |
| 290 | **default_params) |
John Warren | 3961acd | 2015-10-02 14:38:53 -0400 | [diff] [blame] | 291 | self.subnets = subnets_client.SubnetsClient( |
| 292 | _auth, |
| 293 | CONF.network.catalog_type, |
| 294 | CONF.network.region or CONF.identity.region, |
| 295 | endpoint_type=CONF.network.endpoint_type, |
| 296 | build_interval=CONF.network.build_interval, |
| 297 | build_timeout=CONF.network.build_timeout, |
| 298 | **default_params) |
Sean Dague | 655e0af | 2014-05-29 09:00:22 -0400 | [diff] [blame] | 299 | |
| 300 | |
| 301 | def load_resources(fname): |
Joe H. Rahme | 0273673 | 2015-01-27 18:33:09 +0100 | [diff] [blame] | 302 | """Load the expected resources from a yaml file.""" |
Sean Dague | 655e0af | 2014-05-29 09:00:22 -0400 | [diff] [blame] | 303 | return yaml.load(open(fname, 'r')) |
| 304 | |
| 305 | |
| 306 | def keystone_admin(): |
| 307 | return OSClient(OPTS.os_username, OPTS.os_password, OPTS.os_tenant_name) |
| 308 | |
| 309 | |
| 310 | def client_for_user(name): |
| 311 | LOG.debug("Entering client_for_user") |
| 312 | if name in USERS: |
| 313 | user = USERS[name] |
| 314 | LOG.debug("Created client for user %s" % user) |
| 315 | return OSClient(user['name'], user['pass'], user['tenant']) |
| 316 | else: |
| 317 | LOG.error("%s not found in USERS: %s" % (name, USERS)) |
| 318 | |
Emilien Macchi | 7a2348b | 2014-06-16 07:32:11 +0200 | [diff] [blame] | 319 | |
Sean Dague | 655e0af | 2014-05-29 09:00:22 -0400 | [diff] [blame] | 320 | ################### |
| 321 | # |
| 322 | # TENANTS |
| 323 | # |
| 324 | ################### |
| 325 | |
| 326 | |
| 327 | def create_tenants(tenants): |
| 328 | """Create tenants from resource definition. |
| 329 | |
| 330 | Don't create the tenants if they already exist. |
| 331 | """ |
| 332 | admin = keystone_admin() |
Daniel Mellado | b04da90 | 2015-11-20 17:43:12 +0100 | [diff] [blame] | 333 | body = admin.tenants.list_tenants()['tenants'] |
Sean Dague | 655e0af | 2014-05-29 09:00:22 -0400 | [diff] [blame] | 334 | existing = [x['name'] for x in body] |
| 335 | for tenant in tenants: |
| 336 | if tenant not in existing: |
Daniel Mellado | b04da90 | 2015-11-20 17:43:12 +0100 | [diff] [blame] | 337 | admin.tenants.create_tenant(tenant)['tenant'] |
Sean Dague | 655e0af | 2014-05-29 09:00:22 -0400 | [diff] [blame] | 338 | else: |
zhangguoqing | 6c09664 | 2016-01-04 06:17:21 +0000 | [diff] [blame] | 339 | LOG.warning("Tenant '%s' already exists in this environment" |
| 340 | % tenant) |
Sean Dague | 655e0af | 2014-05-29 09:00:22 -0400 | [diff] [blame] | 341 | |
Emilien Macchi | bb71e07 | 2014-07-05 19:18:52 +0200 | [diff] [blame] | 342 | |
| 343 | def destroy_tenants(tenants): |
| 344 | admin = keystone_admin() |
| 345 | for tenant in tenants: |
Daniel Mellado | b04da90 | 2015-11-20 17:43:12 +0100 | [diff] [blame] | 346 | tenant_id = identity.get_tenant_by_name(admin.tenant, tenant)['id'] |
| 347 | admin.tenants.delete_tenant(tenant_id) |
Emilien Macchi | bb71e07 | 2014-07-05 19:18:52 +0200 | [diff] [blame] | 348 | |
Sean Dague | 655e0af | 2014-05-29 09:00:22 -0400 | [diff] [blame] | 349 | ############## |
| 350 | # |
| 351 | # USERS |
| 352 | # |
| 353 | ############## |
| 354 | |
| 355 | |
| 356 | def _users_for_tenant(users, tenant): |
| 357 | u_for_t = [] |
| 358 | for user in users: |
| 359 | for n in user: |
| 360 | if user[n]['tenant'] == tenant: |
| 361 | u_for_t.append(user[n]) |
| 362 | return u_for_t |
| 363 | |
| 364 | |
| 365 | def _tenants_from_users(users): |
| 366 | tenants = set() |
| 367 | for user in users: |
| 368 | for n in user: |
| 369 | tenants.add(user[n]['tenant']) |
| 370 | return tenants |
| 371 | |
| 372 | |
Joe H. Rahme | 9c9fc4d | 2015-03-31 00:35:05 +0200 | [diff] [blame] | 373 | def _assign_swift_role(user, swift_role): |
Sean Dague | 655e0af | 2014-05-29 09:00:22 -0400 | [diff] [blame] | 374 | admin = keystone_admin() |
Daniel Mellado | 6b16b92 | 2015-12-07 12:43:08 +0000 | [diff] [blame] | 375 | roles = admin.roles.list_roles() |
Joe H. Rahme | 9c9fc4d | 2015-03-31 00:35:05 +0200 | [diff] [blame] | 376 | role = next(r for r in roles if r['name'] == swift_role) |
Sean Dague | 655e0af | 2014-05-29 09:00:22 -0400 | [diff] [blame] | 377 | LOG.debug(USERS[user]) |
| 378 | try: |
Daniel Mellado | 6b16b92 | 2015-12-07 12:43:08 +0000 | [diff] [blame] | 379 | admin.roles.assign_user_role( |
Sean Dague | 655e0af | 2014-05-29 09:00:22 -0400 | [diff] [blame] | 380 | USERS[user]['tenant_id'], |
| 381 | USERS[user]['id'], |
| 382 | role['id']) |
Masayuki Igawa | d938876 | 2015-01-20 14:56:42 +0900 | [diff] [blame] | 383 | except lib_exc.Conflict: |
Sean Dague | 655e0af | 2014-05-29 09:00:22 -0400 | [diff] [blame] | 384 | # don't care if it's already assigned |
| 385 | pass |
| 386 | |
| 387 | |
| 388 | def create_users(users): |
| 389 | """Create tenants from resource definition. |
| 390 | |
| 391 | Don't create the tenants if they already exist. |
| 392 | """ |
| 393 | global USERS |
| 394 | LOG.info("Creating users") |
| 395 | admin = keystone_admin() |
| 396 | for u in users: |
| 397 | try: |
Daniel Mellado | b04da90 | 2015-11-20 17:43:12 +0100 | [diff] [blame] | 398 | tenant = identity.get_tenant_by_name(admin.tenants, u['tenant']) |
Masayuki Igawa | bfa0760 | 2015-01-20 18:47:17 +0900 | [diff] [blame] | 399 | except lib_exc.NotFound: |
Sean Dague | 655e0af | 2014-05-29 09:00:22 -0400 | [diff] [blame] | 400 | LOG.error("Tenant: %s - not found" % u['tenant']) |
| 401 | continue |
| 402 | try: |
Daniel Mellado | 82c83a5 | 2015-12-09 15:16:49 +0000 | [diff] [blame] | 403 | identity.get_user_by_username(admin.tenants, |
Ken'ichi Ohmichi | d9fed31 | 2015-11-09 13:05:32 +0000 | [diff] [blame] | 404 | tenant['id'], u['name']) |
zhangguoqing | 6c09664 | 2016-01-04 06:17:21 +0000 | [diff] [blame] | 405 | LOG.warning("User '%s' already exists in this environment" |
| 406 | % u['name']) |
Masayuki Igawa | bfa0760 | 2015-01-20 18:47:17 +0900 | [diff] [blame] | 407 | except lib_exc.NotFound: |
Daniel Mellado | 82c83a5 | 2015-12-09 15:16:49 +0000 | [diff] [blame] | 408 | admin.users.create_user( |
Sean Dague | 655e0af | 2014-05-29 09:00:22 -0400 | [diff] [blame] | 409 | u['name'], u['pass'], tenant['id'], |
| 410 | "%s@%s" % (u['name'], tenant['id']), |
| 411 | enabled=True) |
| 412 | |
| 413 | |
Emilien Macchi | bb71e07 | 2014-07-05 19:18:52 +0200 | [diff] [blame] | 414 | def destroy_users(users): |
| 415 | admin = keystone_admin() |
| 416 | for user in users: |
Daniel Mellado | b04da90 | 2015-11-20 17:43:12 +0100 | [diff] [blame] | 417 | tenant_id = identity.get_tenant_by_name(admin.tenants, |
Ken'ichi Ohmichi | 6ea3f98 | 2015-11-09 12:41:13 +0000 | [diff] [blame] | 418 | user['tenant'])['id'] |
Daniel Mellado | 82c83a5 | 2015-12-09 15:16:49 +0000 | [diff] [blame] | 419 | user_id = identity.get_user_by_username(admin.tenants, |
Ken'ichi Ohmichi | d9fed31 | 2015-11-09 13:05:32 +0000 | [diff] [blame] | 420 | tenant_id, user['name'])['id'] |
Daniel Mellado | 82c83a5 | 2015-12-09 15:16:49 +0000 | [diff] [blame] | 421 | admin.users.delete_user(user_id) |
Emilien Macchi | bb71e07 | 2014-07-05 19:18:52 +0200 | [diff] [blame] | 422 | |
| 423 | |
Sean Dague | 655e0af | 2014-05-29 09:00:22 -0400 | [diff] [blame] | 424 | def collect_users(users): |
| 425 | global USERS |
Joe Gordon | 618c9fb | 2014-07-16 15:40:01 +0200 | [diff] [blame] | 426 | LOG.info("Collecting users") |
Sean Dague | 655e0af | 2014-05-29 09:00:22 -0400 | [diff] [blame] | 427 | admin = keystone_admin() |
| 428 | for u in users: |
Daniel Mellado | b04da90 | 2015-11-20 17:43:12 +0100 | [diff] [blame] | 429 | tenant = identity.get_tenant_by_name(admin.tenants, u['tenant']) |
Sean Dague | 655e0af | 2014-05-29 09:00:22 -0400 | [diff] [blame] | 430 | u['tenant_id'] = tenant['id'] |
| 431 | USERS[u['name']] = u |
Daniel Mellado | 82c83a5 | 2015-12-09 15:16:49 +0000 | [diff] [blame] | 432 | body = identity.get_user_by_username(admin.tenants, |
Ken'ichi Ohmichi | d9fed31 | 2015-11-09 13:05:32 +0000 | [diff] [blame] | 433 | tenant['id'], u['name']) |
Sean Dague | 655e0af | 2014-05-29 09:00:22 -0400 | [diff] [blame] | 434 | USERS[u['name']]['id'] = body['id'] |
| 435 | |
| 436 | |
| 437 | class JavelinCheck(unittest.TestCase): |
| 438 | def __init__(self, users, resources): |
| 439 | super(JavelinCheck, self).__init__() |
| 440 | self.users = users |
| 441 | self.res = resources |
| 442 | |
| 443 | def runTest(self, *args): |
| 444 | pass |
| 445 | |
Emilien Macchi | 7a2348b | 2014-06-16 07:32:11 +0200 | [diff] [blame] | 446 | def _ping_ip(self, ip_addr, count, namespace=None): |
| 447 | if namespace is None: |
| 448 | ping_cmd = "ping -c1 " + ip_addr |
| 449 | else: |
| 450 | ping_cmd = "sudo ip netns exec %s ping -c1 %s" % (namespace, |
| 451 | ip_addr) |
| 452 | for current in range(count): |
| 453 | return_code = os.system(ping_cmd) |
| 454 | if return_code is 0: |
| 455 | break |
| 456 | self.assertNotEqual(current, count - 1, |
| 457 | "Server is not pingable at %s" % ip_addr) |
| 458 | |
Sean Dague | 655e0af | 2014-05-29 09:00:22 -0400 | [diff] [blame] | 459 | def check(self): |
| 460 | self.check_users() |
| 461 | self.check_objects() |
| 462 | self.check_servers() |
Emilien Macchi | d18fec1 | 2014-09-15 14:32:54 -0400 | [diff] [blame] | 463 | self.check_volumes() |
Chris Dent | 878f378 | 2014-06-30 17:04:15 +0100 | [diff] [blame] | 464 | self.check_telemetry() |
Emilien Macchi | 7a2348b | 2014-06-16 07:32:11 +0200 | [diff] [blame] | 465 | self.check_secgroups() |
| 466 | |
| 467 | # validate neutron is enabled and ironic disabled: |
| 468 | # Tenant network isolation is not supported when using ironic. |
| 469 | # "admin" has set up a neutron flat network environment within a shared |
| 470 | # fixed network for all tenants to use. |
| 471 | # In this case, network/subnet/router creation can be skipped and the |
| 472 | # server booted the same as nova network. |
| 473 | if (CONF.service_available.neutron and |
| 474 | not CONF.baremetal.driver_enabled): |
| 475 | self.check_networking() |
Sean Dague | 655e0af | 2014-05-29 09:00:22 -0400 | [diff] [blame] | 476 | |
| 477 | def check_users(self): |
| 478 | """Check that the users we expect to exist, do. |
| 479 | |
| 480 | We don't use the resource list for this because we need to validate |
| 481 | that things like tenantId didn't drift across versions. |
| 482 | """ |
Joe Gordon | 618c9fb | 2014-07-16 15:40:01 +0200 | [diff] [blame] | 483 | LOG.info("checking users") |
Matthew Treinish | 7142668 | 2015-04-23 11:19:38 -0400 | [diff] [blame] | 484 | for name, user in six.iteritems(self.users): |
Sean Dague | 655e0af | 2014-05-29 09:00:22 -0400 | [diff] [blame] | 485 | client = keystone_admin() |
Daniel Mellado | 82c83a5 | 2015-12-09 15:16:49 +0000 | [diff] [blame] | 486 | found = client.users.show_user(user['id'])['user'] |
Sean Dague | 655e0af | 2014-05-29 09:00:22 -0400 | [diff] [blame] | 487 | self.assertEqual(found['name'], user['name']) |
| 488 | self.assertEqual(found['tenantId'], user['tenant_id']) |
| 489 | |
| 490 | # also ensure we can auth with that user, and do something |
| 491 | # on the cloud. We don't care about the results except that it |
| 492 | # remains authorized. |
| 493 | client = client_for_user(user['name']) |
David Kranz | ae99b9a | 2015-02-16 13:37:01 -0500 | [diff] [blame] | 494 | client.servers.list_servers() |
Sean Dague | 655e0af | 2014-05-29 09:00:22 -0400 | [diff] [blame] | 495 | |
| 496 | def check_objects(self): |
| 497 | """Check that the objects created are still there.""" |
Joe Gordon | 22cd6de | 2014-07-25 00:16:17 +0000 | [diff] [blame] | 498 | if not self.res.get('objects'): |
Joe Gordon | b9bcdd8 | 2014-07-17 15:44:57 +0000 | [diff] [blame] | 499 | return |
Joe Gordon | 618c9fb | 2014-07-16 15:40:01 +0200 | [diff] [blame] | 500 | LOG.info("checking objects") |
Sean Dague | 655e0af | 2014-05-29 09:00:22 -0400 | [diff] [blame] | 501 | for obj in self.res['objects']: |
| 502 | client = client_for_user(obj['owner']) |
| 503 | r, contents = client.objects.get_object( |
| 504 | obj['container'], obj['name']) |
| 505 | source = _file_contents(obj['file']) |
| 506 | self.assertEqual(contents, source) |
| 507 | |
| 508 | def check_servers(self): |
| 509 | """Check that the servers are still up and running.""" |
Joe Gordon | 22cd6de | 2014-07-25 00:16:17 +0000 | [diff] [blame] | 510 | if not self.res.get('servers'): |
Joe Gordon | b9bcdd8 | 2014-07-17 15:44:57 +0000 | [diff] [blame] | 511 | return |
Joe Gordon | 618c9fb | 2014-07-16 15:40:01 +0200 | [diff] [blame] | 512 | LOG.info("checking servers") |
Sean Dague | 655e0af | 2014-05-29 09:00:22 -0400 | [diff] [blame] | 513 | for server in self.res['servers']: |
| 514 | client = client_for_user(server['owner']) |
| 515 | found = _get_server_by_name(client, server['name']) |
| 516 | self.assertIsNotNone( |
| 517 | found, |
| 518 | "Couldn't find expected server %s" % server['name']) |
| 519 | |
ghanshyam | 0f82525 | 2015-08-25 16:02:50 +0900 | [diff] [blame] | 520 | found = client.servers.show_server(found['id'])['server'] |
Emilien Macchi | 7a2348b | 2014-06-16 07:32:11 +0200 | [diff] [blame] | 521 | # validate neutron is enabled and ironic disabled: |
| 522 | if (CONF.service_available.neutron and |
| 523 | not CONF.baremetal.driver_enabled): |
Emilien Macchi | c3e3e29 | 2015-03-11 17:42:08 -0400 | [diff] [blame] | 524 | _floating_is_alive = False |
Emilien Macchi | 7a2348b | 2014-06-16 07:32:11 +0200 | [diff] [blame] | 525 | for network_name, body in found['addresses'].items(): |
| 526 | for addr in body: |
| 527 | ip = addr['addr'] |
lanoux | 283273b | 2015-12-04 03:01:54 -0800 | [diff] [blame] | 528 | # Use floating IP, fixed IP or other type to |
| 529 | # reach the server. |
Emilien Macchi | c3e3e29 | 2015-03-11 17:42:08 -0400 | [diff] [blame] | 530 | # This is useful in multi-node environment. |
lanoux | 283273b | 2015-12-04 03:01:54 -0800 | [diff] [blame] | 531 | if CONF.validation.connect_method == 'floating': |
Emilien Macchi | c3e3e29 | 2015-03-11 17:42:08 -0400 | [diff] [blame] | 532 | if addr.get('OS-EXT-IPS:type', |
| 533 | 'floating') == 'floating': |
| 534 | self._ping_ip(ip, 60) |
| 535 | _floating_is_alive = True |
lanoux | 283273b | 2015-12-04 03:01:54 -0800 | [diff] [blame] | 536 | elif CONF.validation.connect_method == 'fixed': |
| 537 | if addr.get('OS-EXT-IPS:type', |
| 538 | 'fixed') == 'fixed': |
| 539 | namespace = _get_router_namespace(client, |
| 540 | network_name) |
| 541 | self._ping_ip(ip, 60, namespace) |
Emilien Macchi | 7a2348b | 2014-06-16 07:32:11 +0200 | [diff] [blame] | 542 | else: |
| 543 | self._ping_ip(ip, 60) |
lanoux | 283273b | 2015-12-04 03:01:54 -0800 | [diff] [blame] | 544 | # If CONF.validation.connect_method is floating, validate |
| 545 | # that the floating IP is attached to the server and the |
| 546 | # the server is pingable. |
| 547 | if CONF.validation.connect_method == 'floating': |
Emilien Macchi | c3e3e29 | 2015-03-11 17:42:08 -0400 | [diff] [blame] | 548 | self.assertTrue(_floating_is_alive, |
| 549 | "Server %s has no floating IP." % |
| 550 | server['name']) |
Emilien Macchi | 7a2348b | 2014-06-16 07:32:11 +0200 | [diff] [blame] | 551 | else: |
| 552 | addr = found['addresses']['private'][0]['addr'] |
| 553 | self._ping_ip(addr, 60) |
| 554 | |
| 555 | def check_secgroups(self): |
Joe H. Rahme | 0273673 | 2015-01-27 18:33:09 +0100 | [diff] [blame] | 556 | """Check that the security groups still exist.""" |
Emilien Macchi | 7a2348b | 2014-06-16 07:32:11 +0200 | [diff] [blame] | 557 | LOG.info("Checking security groups") |
| 558 | for secgroup in self.res['secgroups']: |
| 559 | client = client_for_user(secgroup['owner']) |
| 560 | found = _get_resource_by_name(client.secgroups, 'security_groups', |
| 561 | secgroup['name']) |
| 562 | self.assertIsNotNone( |
| 563 | found, |
| 564 | "Couldn't find expected secgroup %s" % secgroup['name']) |
Sean Dague | 655e0af | 2014-05-29 09:00:22 -0400 | [diff] [blame] | 565 | |
Chris Dent | 878f378 | 2014-06-30 17:04:15 +0100 | [diff] [blame] | 566 | def check_telemetry(self): |
| 567 | """Check that ceilometer provides a sane sample. |
| 568 | |
nayna-patel | 1dfbedb | 2015-08-04 11:07:56 +0000 | [diff] [blame] | 569 | Confirm that there is more than one sample and that they have the |
Chris Dent | 878f378 | 2014-06-30 17:04:15 +0100 | [diff] [blame] | 570 | expected metadata. |
| 571 | |
| 572 | If in check mode confirm that the oldest sample available is from |
| 573 | before the upgrade. |
| 574 | """ |
Chris Dent | 0b76f2f | 2014-10-10 14:24:28 +0100 | [diff] [blame] | 575 | if not self.res.get('telemetry'): |
| 576 | return |
Chris Dent | 878f378 | 2014-06-30 17:04:15 +0100 | [diff] [blame] | 577 | LOG.info("checking telemetry") |
| 578 | for server in self.res['servers']: |
| 579 | client = client_for_user(server['owner']) |
David Kranz | 20d06f4 | 2015-02-09 14:54:15 -0500 | [diff] [blame] | 580 | body = client.telemetry.list_samples( |
Chris Dent | 878f378 | 2014-06-30 17:04:15 +0100 | [diff] [blame] | 581 | 'instance', |
| 582 | query=('metadata.display_name', 'eq', server['name']) |
| 583 | ) |
Chris Dent | 878f378 | 2014-06-30 17:04:15 +0100 | [diff] [blame] | 584 | self.assertTrue(len(body) >= 1, 'expecting at least one sample') |
| 585 | self._confirm_telemetry_sample(server, body[-1]) |
| 586 | |
Emilien Macchi | 626b4f8 | 2014-06-15 21:44:29 +0200 | [diff] [blame] | 587 | def check_volumes(self): |
| 588 | """Check that the volumes are still there and attached.""" |
Joe Gordon | 22cd6de | 2014-07-25 00:16:17 +0000 | [diff] [blame] | 589 | if not self.res.get('volumes'): |
Joe Gordon | b9bcdd8 | 2014-07-17 15:44:57 +0000 | [diff] [blame] | 590 | return |
Joe Gordon | 618c9fb | 2014-07-16 15:40:01 +0200 | [diff] [blame] | 591 | LOG.info("checking volumes") |
Emilien Macchi | 626b4f8 | 2014-06-15 21:44:29 +0200 | [diff] [blame] | 592 | for volume in self.res['volumes']: |
| 593 | client = client_for_user(volume['owner']) |
Emilien Macchi | d18fec1 | 2014-09-15 14:32:54 -0400 | [diff] [blame] | 594 | vol_body = _get_volume_by_name(client, volume['name']) |
Emilien Macchi | 626b4f8 | 2014-06-15 21:44:29 +0200 | [diff] [blame] | 595 | self.assertIsNotNone( |
Emilien Macchi | d18fec1 | 2014-09-15 14:32:54 -0400 | [diff] [blame] | 596 | vol_body, |
Emilien Macchi | 626b4f8 | 2014-06-15 21:44:29 +0200 | [diff] [blame] | 597 | "Couldn't find expected volume %s" % volume['name']) |
| 598 | |
| 599 | # Verify that a volume's attachment retrieved |
| 600 | server_id = _get_server_by_name(client, volume['server'])['id'] |
Emilien Macchi | d18fec1 | 2014-09-15 14:32:54 -0400 | [diff] [blame] | 601 | attachment = client.volumes.get_attachment_from_volume(vol_body) |
| 602 | self.assertEqual(vol_body['id'], attachment['volume_id']) |
Emilien Macchi | 626b4f8 | 2014-06-15 21:44:29 +0200 | [diff] [blame] | 603 | self.assertEqual(server_id, attachment['server_id']) |
| 604 | |
Chris Dent | 878f378 | 2014-06-30 17:04:15 +0100 | [diff] [blame] | 605 | def _confirm_telemetry_sample(self, server, sample): |
| 606 | """Check this sample matches the expected resource metadata.""" |
| 607 | # Confirm display_name |
| 608 | self.assertEqual(server['name'], |
| 609 | sample['resource_metadata']['display_name']) |
| 610 | # Confirm instance_type of flavor |
| 611 | flavor = sample['resource_metadata'].get( |
| 612 | 'flavor.name', |
| 613 | sample['resource_metadata'].get('instance_type') |
| 614 | ) |
| 615 | self.assertEqual(server['flavor'], flavor) |
| 616 | # Confirm the oldest sample was created before upgrade. |
| 617 | if OPTS.mode == 'check': |
| 618 | oldest_timestamp = timeutils.normalize_time( |
| 619 | timeutils.parse_isotime(sample['timestamp'])) |
| 620 | self.assertTrue( |
| 621 | oldest_timestamp < JAVELIN_START, |
| 622 | 'timestamp should come before start of second javelin run' |
| 623 | ) |
| 624 | |
Emilien Macchi | 7a2348b | 2014-06-16 07:32:11 +0200 | [diff] [blame] | 625 | def check_networking(self): |
| 626 | """Check that the networks are still there.""" |
| 627 | for res_type in ('networks', 'subnets', 'routers'): |
| 628 | for res in self.res[res_type]: |
| 629 | client = client_for_user(res['owner']) |
| 630 | found = _get_resource_by_name(client.networks, res_type, |
| 631 | res['name']) |
| 632 | self.assertIsNotNone( |
| 633 | found, |
| 634 | "Couldn't find expected resource %s" % res['name']) |
| 635 | |
Sean Dague | 655e0af | 2014-05-29 09:00:22 -0400 | [diff] [blame] | 636 | |
| 637 | ####################### |
| 638 | # |
| 639 | # OBJECTS |
| 640 | # |
| 641 | ####################### |
| 642 | |
| 643 | |
| 644 | def _file_contents(fname): |
| 645 | with open(fname, 'r') as f: |
| 646 | return f.read() |
| 647 | |
| 648 | |
| 649 | def create_objects(objects): |
Joe Gordon | b9bcdd8 | 2014-07-17 15:44:57 +0000 | [diff] [blame] | 650 | if not objects: |
| 651 | return |
Sean Dague | 655e0af | 2014-05-29 09:00:22 -0400 | [diff] [blame] | 652 | LOG.info("Creating objects") |
| 653 | for obj in objects: |
| 654 | LOG.debug("Object %s" % obj) |
Joe H. Rahme | 9c9fc4d | 2015-03-31 00:35:05 +0200 | [diff] [blame] | 655 | swift_role = obj.get('swift_role', 'Member') |
| 656 | _assign_swift_role(obj['owner'], swift_role) |
Sean Dague | 655e0af | 2014-05-29 09:00:22 -0400 | [diff] [blame] | 657 | client = client_for_user(obj['owner']) |
| 658 | client.containers.create_container(obj['container']) |
| 659 | client.objects.create_object( |
| 660 | obj['container'], obj['name'], |
| 661 | _file_contents(obj['file'])) |
| 662 | |
Emilien Macchi | bb71e07 | 2014-07-05 19:18:52 +0200 | [diff] [blame] | 663 | |
| 664 | def destroy_objects(objects): |
| 665 | for obj in objects: |
| 666 | client = client_for_user(obj['owner']) |
| 667 | r, body = client.objects.delete_object(obj['container'], obj['name']) |
Emilien Macchi | d70f510 | 2014-09-10 09:54:49 -0400 | [diff] [blame] | 668 | if not (200 <= int(r['status']) < 299): |
Emilien Macchi | bb71e07 | 2014-07-05 19:18:52 +0200 | [diff] [blame] | 669 | raise ValueError("unable to destroy object: [%s] %s" % (r, body)) |
| 670 | |
| 671 | |
Sean Dague | 655e0af | 2014-05-29 09:00:22 -0400 | [diff] [blame] | 672 | ####################### |
| 673 | # |
| 674 | # IMAGES |
| 675 | # |
| 676 | ####################### |
| 677 | |
| 678 | |
Sean Dague | 319b37a | 2014-07-11 07:28:11 -0400 | [diff] [blame] | 679 | def _resolve_image(image, imgtype): |
| 680 | name = image[imgtype] |
| 681 | fname = os.path.join(OPTS.devstack_base, image['imgdir'], name) |
| 682 | return name, fname |
| 683 | |
| 684 | |
Joe Gordon | 6f0426c | 2014-07-25 01:10:28 +0000 | [diff] [blame] | 685 | def _get_image_by_name(client, name): |
Ken'ichi Ohmichi | e3acc12 | 2015-05-22 00:32:54 +0000 | [diff] [blame] | 686 | body = client.images.list_images() |
Joe Gordon | 6f0426c | 2014-07-25 01:10:28 +0000 | [diff] [blame] | 687 | for image in body: |
| 688 | if name == image['name']: |
| 689 | return image |
| 690 | return None |
| 691 | |
| 692 | |
Sean Dague | 655e0af | 2014-05-29 09:00:22 -0400 | [diff] [blame] | 693 | def create_images(images): |
Joe Gordon | b9bcdd8 | 2014-07-17 15:44:57 +0000 | [diff] [blame] | 694 | if not images: |
| 695 | return |
Joe Gordon | a18d686 | 2014-07-24 22:55:46 +0000 | [diff] [blame] | 696 | LOG.info("Creating images") |
Sean Dague | 655e0af | 2014-05-29 09:00:22 -0400 | [diff] [blame] | 697 | for image in images: |
| 698 | client = client_for_user(image['owner']) |
| 699 | |
Joe H. Rahme | 4352d5d | 2015-03-09 17:41:18 +0100 | [diff] [blame] | 700 | # DEPRECATED: 'format' was used for ami images |
| 701 | # Use 'disk_format' and 'container_format' instead |
| 702 | if 'format' in image: |
| 703 | LOG.warning("Deprecated: 'format' is deprecated for images " |
| 704 | "description. Please use 'disk_format' and 'container_" |
| 705 | "format' instead.") |
| 706 | image['disk_format'] = image['format'] |
| 707 | image['container_format'] = image['format'] |
| 708 | |
Sean Dague | 655e0af | 2014-05-29 09:00:22 -0400 | [diff] [blame] | 709 | # only upload a new image if the name isn't there |
Joe Gordon | 6f0426c | 2014-07-25 01:10:28 +0000 | [diff] [blame] | 710 | if _get_image_by_name(client, image['name']): |
Joe Gordon | a18d686 | 2014-07-24 22:55:46 +0000 | [diff] [blame] | 711 | LOG.info("Image '%s' already exists" % image['name']) |
Sean Dague | 655e0af | 2014-05-29 09:00:22 -0400 | [diff] [blame] | 712 | continue |
| 713 | |
| 714 | # special handling for 3 part image |
| 715 | extras = {} |
Joe H. Rahme | 4352d5d | 2015-03-09 17:41:18 +0100 | [diff] [blame] | 716 | if image['disk_format'] == 'ami': |
Sean Dague | 319b37a | 2014-07-11 07:28:11 -0400 | [diff] [blame] | 717 | name, fname = _resolve_image(image, 'aki') |
David Kranz | 34f1878 | 2015-01-06 13:43:55 -0500 | [diff] [blame] | 718 | aki = client.images.create_image( |
Sean Dague | 319b37a | 2014-07-11 07:28:11 -0400 | [diff] [blame] | 719 | 'javelin_' + name, 'aki', 'aki') |
Ken'ichi Ohmichi | 66494e9 | 2015-06-08 04:28:10 +0000 | [diff] [blame] | 720 | client.images.store_image_file(aki.get('id'), open(fname, 'r')) |
Sean Dague | 655e0af | 2014-05-29 09:00:22 -0400 | [diff] [blame] | 721 | extras['kernel_id'] = aki.get('id') |
| 722 | |
Sean Dague | 319b37a | 2014-07-11 07:28:11 -0400 | [diff] [blame] | 723 | name, fname = _resolve_image(image, 'ari') |
David Kranz | 34f1878 | 2015-01-06 13:43:55 -0500 | [diff] [blame] | 724 | ari = client.images.create_image( |
Sean Dague | 319b37a | 2014-07-11 07:28:11 -0400 | [diff] [blame] | 725 | 'javelin_' + name, 'ari', 'ari') |
Ken'ichi Ohmichi | 66494e9 | 2015-06-08 04:28:10 +0000 | [diff] [blame] | 726 | client.images.store_image_file(ari.get('id'), open(fname, 'r')) |
Sean Dague | 655e0af | 2014-05-29 09:00:22 -0400 | [diff] [blame] | 727 | extras['ramdisk_id'] = ari.get('id') |
| 728 | |
Sean Dague | 319b37a | 2014-07-11 07:28:11 -0400 | [diff] [blame] | 729 | _, fname = _resolve_image(image, 'file') |
David Kranz | 34f1878 | 2015-01-06 13:43:55 -0500 | [diff] [blame] | 730 | body = client.images.create_image( |
Joe H. Rahme | 4352d5d | 2015-03-09 17:41:18 +0100 | [diff] [blame] | 731 | image['name'], image['container_format'], |
| 732 | image['disk_format'], **extras) |
Sean Dague | 655e0af | 2014-05-29 09:00:22 -0400 | [diff] [blame] | 733 | image_id = body.get('id') |
Ken'ichi Ohmichi | 66494e9 | 2015-06-08 04:28:10 +0000 | [diff] [blame] | 734 | client.images.store_image_file(image_id, open(fname, 'r')) |
Sean Dague | 655e0af | 2014-05-29 09:00:22 -0400 | [diff] [blame] | 735 | |
| 736 | |
Joe Gordon | 6f0426c | 2014-07-25 01:10:28 +0000 | [diff] [blame] | 737 | def destroy_images(images): |
| 738 | if not images: |
| 739 | return |
| 740 | LOG.info("Destroying images") |
| 741 | for image in images: |
| 742 | client = client_for_user(image['owner']) |
| 743 | |
| 744 | response = _get_image_by_name(client, image['name']) |
| 745 | if not response: |
nayna-patel | 1dfbedb | 2015-08-04 11:07:56 +0000 | [diff] [blame] | 746 | LOG.info("Image '%s' does not exist" % image['name']) |
Joe Gordon | 6f0426c | 2014-07-25 01:10:28 +0000 | [diff] [blame] | 747 | continue |
| 748 | client.images.delete_image(response['id']) |
| 749 | |
| 750 | |
Sean Dague | 655e0af | 2014-05-29 09:00:22 -0400 | [diff] [blame] | 751 | ####################### |
| 752 | # |
Emilien Macchi | 7a2348b | 2014-06-16 07:32:11 +0200 | [diff] [blame] | 753 | # NETWORKS |
| 754 | # |
| 755 | ####################### |
| 756 | |
| 757 | def _get_router_namespace(client, network): |
| 758 | network_id = _get_resource_by_name(client.networks, |
| 759 | 'networks', network)['id'] |
Ken'ichi Ohmichi | e35f472 | 2015-12-22 04:57:11 +0000 | [diff] [blame] | 760 | n_body = client.routers.list_routers() |
Emilien Macchi | 7a2348b | 2014-06-16 07:32:11 +0200 | [diff] [blame] | 761 | for router in n_body['routers']: |
| 762 | router_id = router['id'] |
Ken'ichi Ohmichi | f3110f0 | 2016-03-21 12:29:03 -0700 | [diff] [blame] | 763 | r_body = client.ports.list_ports(device_id=router_id) |
Emilien Macchi | 7a2348b | 2014-06-16 07:32:11 +0200 | [diff] [blame] | 764 | for port in r_body['ports']: |
| 765 | if port['network_id'] == network_id: |
| 766 | return "qrouter-%s" % router_id |
| 767 | |
| 768 | |
| 769 | def _get_resource_by_name(client, resource, name): |
| 770 | get_resources = getattr(client, 'list_%s' % resource) |
| 771 | if get_resources is None: |
| 772 | raise AttributeError("client doesn't have method list_%s" % resource) |
David Kranz | 34e8812 | 2014-12-11 15:24:05 -0500 | [diff] [blame] | 773 | # Until all tempest client methods are changed to return only one value, |
| 774 | # we cannot assume they all have the same signature so we need to discard |
| 775 | # the unused response first value it two values are being returned. |
| 776 | body = get_resources() |
Brandon Palm | ecd2ec0 | 2016-02-25 09:38:36 -0600 | [diff] [blame] | 777 | if isinstance(body, tuple): |
David Kranz | 34e8812 | 2014-12-11 15:24:05 -0500 | [diff] [blame] | 778 | body = body[1] |
Emilien Macchi | 7a2348b | 2014-06-16 07:32:11 +0200 | [diff] [blame] | 779 | if isinstance(body, dict): |
| 780 | body = body[resource] |
| 781 | for res in body: |
| 782 | if name == res['name']: |
| 783 | return res |
| 784 | raise ValueError('%s not found in %s resources' % (name, resource)) |
| 785 | |
| 786 | |
| 787 | def create_networks(networks): |
| 788 | LOG.info("Creating networks") |
| 789 | for network in networks: |
| 790 | client = client_for_user(network['owner']) |
| 791 | |
| 792 | # only create a network if the name isn't here |
David Kranz | 34e8812 | 2014-12-11 15:24:05 -0500 | [diff] [blame] | 793 | body = client.networks.list_networks() |
Emilien Macchi | 7a2348b | 2014-06-16 07:32:11 +0200 | [diff] [blame] | 794 | if any(item['name'] == network['name'] for item in body['networks']): |
nayna-patel | 1dfbedb | 2015-08-04 11:07:56 +0000 | [diff] [blame] | 795 | LOG.warning("Duplicated network name: %s" % network['name']) |
Emilien Macchi | 7a2348b | 2014-06-16 07:32:11 +0200 | [diff] [blame] | 796 | continue |
| 797 | |
| 798 | client.networks.create_network(name=network['name']) |
| 799 | |
| 800 | |
Jakub Libosvar | 3791ac9 | 2014-11-11 13:23:44 +0100 | [diff] [blame] | 801 | def destroy_networks(networks): |
| 802 | LOG.info("Destroying subnets") |
| 803 | for network in networks: |
| 804 | client = client_for_user(network['owner']) |
| 805 | network_id = _get_resource_by_name(client.networks, 'networks', |
| 806 | network['name'])['id'] |
| 807 | client.networks.delete_network(network_id) |
| 808 | |
| 809 | |
Emilien Macchi | 7a2348b | 2014-06-16 07:32:11 +0200 | [diff] [blame] | 810 | def create_subnets(subnets): |
| 811 | LOG.info("Creating subnets") |
| 812 | for subnet in subnets: |
| 813 | client = client_for_user(subnet['owner']) |
| 814 | |
| 815 | network = _get_resource_by_name(client.networks, 'networks', |
| 816 | subnet['network']) |
| 817 | ip_version = netaddr.IPNetwork(subnet['range']).version |
| 818 | # ensure we don't overlap with another subnet in the network |
| 819 | try: |
| 820 | client.networks.create_subnet(network_id=network['id'], |
| 821 | cidr=subnet['range'], |
| 822 | name=subnet['name'], |
| 823 | ip_version=ip_version) |
Masayuki Igawa | 4b29e47 | 2015-02-16 10:41:54 +0900 | [diff] [blame] | 824 | except lib_exc.BadRequest as e: |
Emilien Macchi | 7a2348b | 2014-06-16 07:32:11 +0200 | [diff] [blame] | 825 | is_overlapping_cidr = 'overlaps with another subnet' in str(e) |
| 826 | if not is_overlapping_cidr: |
| 827 | raise |
| 828 | |
| 829 | |
Jakub Libosvar | 3791ac9 | 2014-11-11 13:23:44 +0100 | [diff] [blame] | 830 | def destroy_subnets(subnets): |
| 831 | LOG.info("Destroying subnets") |
| 832 | for subnet in subnets: |
| 833 | client = client_for_user(subnet['owner']) |
John Warren | 3961acd | 2015-10-02 14:38:53 -0400 | [diff] [blame] | 834 | subnet_id = _get_resource_by_name(client.subnets, |
Jakub Libosvar | 3791ac9 | 2014-11-11 13:23:44 +0100 | [diff] [blame] | 835 | 'subnets', subnet['name'])['id'] |
John Warren | 3961acd | 2015-10-02 14:38:53 -0400 | [diff] [blame] | 836 | client.subnets.delete_subnet(subnet_id) |
Jakub Libosvar | 3791ac9 | 2014-11-11 13:23:44 +0100 | [diff] [blame] | 837 | |
| 838 | |
Emilien Macchi | 7a2348b | 2014-06-16 07:32:11 +0200 | [diff] [blame] | 839 | def create_routers(routers): |
| 840 | LOG.info("Creating routers") |
| 841 | for router in routers: |
| 842 | client = client_for_user(router['owner']) |
| 843 | |
| 844 | # only create a router if the name isn't here |
Ken'ichi Ohmichi | e35f472 | 2015-12-22 04:57:11 +0000 | [diff] [blame] | 845 | body = client.routers.list_routers() |
Emilien Macchi | 7a2348b | 2014-06-16 07:32:11 +0200 | [diff] [blame] | 846 | if any(item['name'] == router['name'] for item in body['routers']): |
nayna-patel | 1dfbedb | 2015-08-04 11:07:56 +0000 | [diff] [blame] | 847 | LOG.warning("Duplicated router name: %s" % router['name']) |
Emilien Macchi | 7a2348b | 2014-06-16 07:32:11 +0200 | [diff] [blame] | 848 | continue |
| 849 | |
Ken'ichi Ohmichi | 6665c97 | 2016-02-24 13:09:09 -0800 | [diff] [blame] | 850 | client.networks.create_router(name=router['name']) |
Emilien Macchi | 7a2348b | 2014-06-16 07:32:11 +0200 | [diff] [blame] | 851 | |
| 852 | |
Jakub Libosvar | 3791ac9 | 2014-11-11 13:23:44 +0100 | [diff] [blame] | 853 | def destroy_routers(routers): |
| 854 | LOG.info("Destroying routers") |
| 855 | for router in routers: |
| 856 | client = client_for_user(router['owner']) |
| 857 | router_id = _get_resource_by_name(client.networks, |
| 858 | 'routers', router['name'])['id'] |
| 859 | for subnet in router['subnet']: |
| 860 | subnet_id = _get_resource_by_name(client.networks, |
| 861 | 'subnets', subnet)['id'] |
Ken'ichi Ohmichi | e35f472 | 2015-12-22 04:57:11 +0000 | [diff] [blame] | 862 | client.routers.remove_router_interface(router_id, |
| 863 | subnet_id=subnet_id) |
| 864 | client.routers.delete_router(router_id) |
Jakub Libosvar | 3791ac9 | 2014-11-11 13:23:44 +0100 | [diff] [blame] | 865 | |
| 866 | |
Emilien Macchi | 7a2348b | 2014-06-16 07:32:11 +0200 | [diff] [blame] | 867 | def add_router_interface(routers): |
| 868 | for router in routers: |
| 869 | client = client_for_user(router['owner']) |
| 870 | router_id = _get_resource_by_name(client.networks, |
| 871 | 'routers', router['name'])['id'] |
| 872 | |
| 873 | for subnet in router['subnet']: |
| 874 | subnet_id = _get_resource_by_name(client.networks, |
| 875 | 'subnets', subnet)['id'] |
| 876 | # connect routers to their subnets |
Ken'ichi Ohmichi | e35f472 | 2015-12-22 04:57:11 +0000 | [diff] [blame] | 877 | client.routers.add_router_interface(router_id, |
| 878 | subnet_id=subnet_id) |
nayna-patel | 1dfbedb | 2015-08-04 11:07:56 +0000 | [diff] [blame] | 879 | # connect routers to external network if set to "gateway" |
Emilien Macchi | 7a2348b | 2014-06-16 07:32:11 +0200 | [diff] [blame] | 880 | if router['gateway']: |
| 881 | if CONF.network.public_network_id: |
| 882 | ext_net = CONF.network.public_network_id |
Ken'ichi Ohmichi | e35f472 | 2015-12-22 04:57:11 +0000 | [diff] [blame] | 883 | client.routers._update_router( |
Emilien Macchi | 7a2348b | 2014-06-16 07:32:11 +0200 | [diff] [blame] | 884 | router_id, set_enable_snat=True, |
| 885 | external_gateway_info={"network_id": ext_net}) |
| 886 | else: |
| 887 | raise ValueError('public_network_id is not configured.') |
| 888 | |
| 889 | |
| 890 | ####################### |
| 891 | # |
Sean Dague | 655e0af | 2014-05-29 09:00:22 -0400 | [diff] [blame] | 892 | # SERVERS |
| 893 | # |
| 894 | ####################### |
| 895 | |
| 896 | def _get_server_by_name(client, name): |
David Kranz | ae99b9a | 2015-02-16 13:37:01 -0500 | [diff] [blame] | 897 | body = client.servers.list_servers() |
Sean Dague | 655e0af | 2014-05-29 09:00:22 -0400 | [diff] [blame] | 898 | for server in body['servers']: |
| 899 | if name == server['name']: |
| 900 | return server |
| 901 | return None |
| 902 | |
| 903 | |
Sean Dague | 655e0af | 2014-05-29 09:00:22 -0400 | [diff] [blame] | 904 | def _get_flavor_by_name(client, name): |
ghanshyam | 19973be | 2015-08-18 15:46:42 +0900 | [diff] [blame] | 905 | body = client.flavors.list_flavors()['flavors'] |
Sean Dague | 655e0af | 2014-05-29 09:00:22 -0400 | [diff] [blame] | 906 | for flavor in body: |
| 907 | if name == flavor['name']: |
| 908 | return flavor |
| 909 | return None |
| 910 | |
| 911 | |
| 912 | def create_servers(servers): |
Joe Gordon | b9bcdd8 | 2014-07-17 15:44:57 +0000 | [diff] [blame] | 913 | if not servers: |
| 914 | return |
Joe Gordon | a18d686 | 2014-07-24 22:55:46 +0000 | [diff] [blame] | 915 | LOG.info("Creating servers") |
Sean Dague | 655e0af | 2014-05-29 09:00:22 -0400 | [diff] [blame] | 916 | for server in servers: |
| 917 | client = client_for_user(server['owner']) |
| 918 | |
| 919 | if _get_server_by_name(client, server['name']): |
Joe Gordon | a18d686 | 2014-07-24 22:55:46 +0000 | [diff] [blame] | 920 | LOG.info("Server '%s' already exists" % server['name']) |
Sean Dague | 655e0af | 2014-05-29 09:00:22 -0400 | [diff] [blame] | 921 | continue |
| 922 | |
| 923 | image_id = _get_image_by_name(client, server['image'])['id'] |
| 924 | flavor_id = _get_flavor_by_name(client, server['flavor'])['id'] |
Emilien Macchi | 7a2348b | 2014-06-16 07:32:11 +0200 | [diff] [blame] | 925 | # validate neutron is enabled and ironic disabled |
| 926 | kwargs = dict() |
| 927 | if (CONF.service_available.neutron and |
| 928 | not CONF.baremetal.driver_enabled and server.get('networks')): |
| 929 | get_net_id = lambda x: (_get_resource_by_name( |
| 930 | client.networks, 'networks', x)['id']) |
| 931 | kwargs['networks'] = [{'uuid': get_net_id(network)} |
| 932 | for network in server['networks']] |
David Kranz | 0fb1429 | 2015-02-11 15:55:20 -0500 | [diff] [blame] | 933 | body = client.servers.create_server( |
Ken'ichi Ohmichi | f2d436e | 2015-09-03 01:13:16 +0000 | [diff] [blame] | 934 | name=server['name'], imageRef=image_id, flavorRef=flavor_id, |
| 935 | **kwargs)['server'] |
Joe Gordon | 10f260b | 2014-07-24 23:27:19 +0000 | [diff] [blame] | 936 | server_id = body['id'] |
| 937 | client.servers.wait_for_server_status(server_id, 'ACTIVE') |
nayna-patel | 1dfbedb | 2015-08-04 11:07:56 +0000 | [diff] [blame] | 938 | # create security group(s) after server spawning |
Emilien Macchi | 7a2348b | 2014-06-16 07:32:11 +0200 | [diff] [blame] | 939 | for secgroup in server['secgroups']: |
Ken'ichi Ohmichi | e6349f3 | 2015-12-09 06:47:54 +0000 | [diff] [blame] | 940 | client.servers.add_security_group(server_id, name=secgroup) |
lanoux | 283273b | 2015-12-04 03:01:54 -0800 | [diff] [blame] | 941 | if CONF.validation.connect_method == 'floating': |
Joe H. Rahme | 9350a10 | 2015-03-29 17:39:20 +0200 | [diff] [blame] | 942 | floating_ip_pool = server.get('floating_ip_pool') |
| 943 | floating_ip = client.floating_ips.create_floating_ip( |
ghanshyam | 9a3a9a2 | 2015-08-18 17:03:55 +0900 | [diff] [blame] | 944 | pool_name=floating_ip_pool)['floating_ip'] |
Emilien Macchi | c3e3e29 | 2015-03-11 17:42:08 -0400 | [diff] [blame] | 945 | client.floating_ips.associate_floating_ip_to_server( |
| 946 | floating_ip['ip'], server_id) |
Sean Dague | 655e0af | 2014-05-29 09:00:22 -0400 | [diff] [blame] | 947 | |
| 948 | |
Joe Gordon | db63b1c | 2014-07-24 23:21:21 +0000 | [diff] [blame] | 949 | def destroy_servers(servers): |
| 950 | if not servers: |
| 951 | return |
| 952 | LOG.info("Destroying servers") |
| 953 | for server in servers: |
| 954 | client = client_for_user(server['owner']) |
| 955 | |
Emilien Macchi | dc5bae2 | 2015-03-16 08:49:02 -0400 | [diff] [blame] | 956 | response = _get_server_by_name(client, server['name']) |
| 957 | if not response: |
Joe Gordon | db63b1c | 2014-07-24 23:21:21 +0000 | [diff] [blame] | 958 | LOG.info("Server '%s' does not exist" % server['name']) |
| 959 | continue |
| 960 | |
Emilien Macchi | dc5bae2 | 2015-03-16 08:49:02 -0400 | [diff] [blame] | 961 | # TODO(EmilienM): disassociate floating IP from server and release it. |
| 962 | client.servers.delete_server(response['id']) |
Ken'ichi Ohmichi | e91a0c6 | 2015-08-13 02:09:16 +0000 | [diff] [blame] | 963 | waiters.wait_for_server_termination(client.servers, response['id'], |
| 964 | ignore_error=True) |
Joe Gordon | db63b1c | 2014-07-24 23:21:21 +0000 | [diff] [blame] | 965 | |
| 966 | |
Emilien Macchi | 7a2348b | 2014-06-16 07:32:11 +0200 | [diff] [blame] | 967 | def create_secgroups(secgroups): |
| 968 | LOG.info("Creating security groups") |
| 969 | for secgroup in secgroups: |
| 970 | client = client_for_user(secgroup['owner']) |
| 971 | |
| 972 | # only create a security group if the name isn't here |
| 973 | # i.e. a security group may be used by another server |
| 974 | # only create a router if the name isn't here |
ghanshyam | b610b77 | 2015-08-24 17:29:38 +0900 | [diff] [blame] | 975 | body = client.secgroups.list_security_groups()['security_groups'] |
Emilien Macchi | 7a2348b | 2014-06-16 07:32:11 +0200 | [diff] [blame] | 976 | if any(item['name'] == secgroup['name'] for item in body): |
| 977 | LOG.warning("Security group '%s' already exists" % |
| 978 | secgroup['name']) |
| 979 | continue |
| 980 | |
David Kranz | 9964b4e | 2015-02-06 15:45:29 -0500 | [diff] [blame] | 981 | body = client.secgroups.create_security_group( |
ghanshyam | b610b77 | 2015-08-24 17:29:38 +0900 | [diff] [blame] | 982 | name=secgroup['name'], |
| 983 | description=secgroup['description'])['security_group'] |
Emilien Macchi | 7a2348b | 2014-06-16 07:32:11 +0200 | [diff] [blame] | 984 | secgroup_id = body['id'] |
| 985 | # for each security group, create the rules |
| 986 | for rule in secgroup['rules']: |
| 987 | ip_proto, from_port, to_port, cidr = rule.split() |
Ken'ichi Ohmichi | 685cd17 | 2015-07-13 01:29:57 +0000 | [diff] [blame] | 988 | client.secrules.create_security_group_rule( |
Ken'ichi Ohmichi | eb7eeec | 2015-07-21 01:00:06 +0000 | [diff] [blame] | 989 | parent_group_id=secgroup_id, ip_protocol=ip_proto, |
| 990 | from_port=from_port, to_port=to_port, cidr=cidr) |
Emilien Macchi | 7a2348b | 2014-06-16 07:32:11 +0200 | [diff] [blame] | 991 | |
| 992 | |
Jakub Libosvar | 3791ac9 | 2014-11-11 13:23:44 +0100 | [diff] [blame] | 993 | def destroy_secgroups(secgroups): |
| 994 | LOG.info("Destroying security groups") |
| 995 | for secgroup in secgroups: |
| 996 | client = client_for_user(secgroup['owner']) |
| 997 | sg_id = _get_resource_by_name(client.secgroups, |
| 998 | 'security_groups', |
| 999 | secgroup['name']) |
| 1000 | # sg rules are deleted automatically |
| 1001 | client.secgroups.delete_security_group(sg_id['id']) |
| 1002 | |
| 1003 | |
Sean Dague | 655e0af | 2014-05-29 09:00:22 -0400 | [diff] [blame] | 1004 | ####################### |
| 1005 | # |
Emilien Macchi | 626b4f8 | 2014-06-15 21:44:29 +0200 | [diff] [blame] | 1006 | # VOLUMES |
| 1007 | # |
| 1008 | ####################### |
| 1009 | |
| 1010 | def _get_volume_by_name(client, name): |
John Warren | 6177c9e | 2015-08-19 20:00:17 +0000 | [diff] [blame] | 1011 | body = client.volumes.list_volumes()['volumes'] |
Emilien Macchi | d18fec1 | 2014-09-15 14:32:54 -0400 | [diff] [blame] | 1012 | for volume in body: |
| 1013 | if name == volume['display_name']: |
Emilien Macchi | 626b4f8 | 2014-06-15 21:44:29 +0200 | [diff] [blame] | 1014 | return volume |
| 1015 | return None |
| 1016 | |
| 1017 | |
| 1018 | def create_volumes(volumes): |
Chris Dent | 51e76de | 2014-10-01 12:07:14 +0100 | [diff] [blame] | 1019 | if not volumes: |
| 1020 | return |
| 1021 | LOG.info("Creating volumes") |
Emilien Macchi | 626b4f8 | 2014-06-15 21:44:29 +0200 | [diff] [blame] | 1022 | for volume in volumes: |
| 1023 | client = client_for_user(volume['owner']) |
| 1024 | |
| 1025 | # only create a volume if the name isn't here |
Emilien Macchi | d18fec1 | 2014-09-15 14:32:54 -0400 | [diff] [blame] | 1026 | if _get_volume_by_name(client, volume['name']): |
| 1027 | LOG.info("volume '%s' already exists" % volume['name']) |
Emilien Macchi | 626b4f8 | 2014-06-15 21:44:29 +0200 | [diff] [blame] | 1028 | continue |
| 1029 | |
Emilien Macchi | d18fec1 | 2014-09-15 14:32:54 -0400 | [diff] [blame] | 1030 | size = volume['gb'] |
| 1031 | v_name = volume['name'] |
Joseph Lanoux | 6809bab | 2014-12-18 14:57:18 +0000 | [diff] [blame] | 1032 | body = client.volumes.create_volume(size=size, |
John Warren | 6177c9e | 2015-08-19 20:00:17 +0000 | [diff] [blame] | 1033 | display_name=v_name)['volume'] |
Yaroslav Lobankov | ed3a35b | 2016-03-24 22:41:30 -0500 | [diff] [blame] | 1034 | waiters.wait_for_volume_status(client.volumes, body['id'], 'available') |
Emilien Macchi | 626b4f8 | 2014-06-15 21:44:29 +0200 | [diff] [blame] | 1035 | |
| 1036 | |
Emilien Macchi | bb71e07 | 2014-07-05 19:18:52 +0200 | [diff] [blame] | 1037 | def destroy_volumes(volumes): |
| 1038 | for volume in volumes: |
| 1039 | client = client_for_user(volume['owner']) |
| 1040 | volume_id = _get_volume_by_name(client, volume['name'])['id'] |
Emilien Macchi | 5ebc27b | 2014-09-15 14:30:35 -0400 | [diff] [blame] | 1041 | client.volumes.detach_volume(volume_id) |
| 1042 | client.volumes.delete_volume(volume_id) |
Emilien Macchi | bb71e07 | 2014-07-05 19:18:52 +0200 | [diff] [blame] | 1043 | |
| 1044 | |
Emilien Macchi | 626b4f8 | 2014-06-15 21:44:29 +0200 | [diff] [blame] | 1045 | def attach_volumes(volumes): |
| 1046 | for volume in volumes: |
| 1047 | client = client_for_user(volume['owner']) |
Emilien Macchi | 626b4f8 | 2014-06-15 21:44:29 +0200 | [diff] [blame] | 1048 | server_id = _get_server_by_name(client, volume['server'])['id'] |
Emilien Macchi | d18fec1 | 2014-09-15 14:32:54 -0400 | [diff] [blame] | 1049 | volume_id = _get_volume_by_name(client, volume['name'])['id'] |
| 1050 | device = volume['device'] |
Ghanshyam | 8fc0ed2 | 2015-12-18 10:25:14 +0900 | [diff] [blame] | 1051 | client.volumes.attach_volume(volume_id, |
| 1052 | instance_uuid=server_id, |
| 1053 | mountpoint=device) |
Emilien Macchi | 626b4f8 | 2014-06-15 21:44:29 +0200 | [diff] [blame] | 1054 | |
| 1055 | |
| 1056 | ####################### |
| 1057 | # |
Sean Dague | 655e0af | 2014-05-29 09:00:22 -0400 | [diff] [blame] | 1058 | # MAIN LOGIC |
| 1059 | # |
| 1060 | ####################### |
| 1061 | |
| 1062 | def create_resources(): |
| 1063 | LOG.info("Creating Resources") |
| 1064 | # first create keystone level resources, and we need to be admin |
nayna-patel | 1dfbedb | 2015-08-04 11:07:56 +0000 | [diff] [blame] | 1065 | # for this. |
Sean Dague | 655e0af | 2014-05-29 09:00:22 -0400 | [diff] [blame] | 1066 | create_tenants(RES['tenants']) |
| 1067 | create_users(RES['users']) |
| 1068 | collect_users(RES['users']) |
| 1069 | |
| 1070 | # next create resources in a well known order |
| 1071 | create_objects(RES['objects']) |
| 1072 | create_images(RES['images']) |
Emilien Macchi | 7a2348b | 2014-06-16 07:32:11 +0200 | [diff] [blame] | 1073 | |
| 1074 | # validate neutron is enabled and ironic is disabled |
| 1075 | if CONF.service_available.neutron and not CONF.baremetal.driver_enabled: |
| 1076 | create_networks(RES['networks']) |
| 1077 | create_subnets(RES['subnets']) |
| 1078 | create_routers(RES['routers']) |
| 1079 | add_router_interface(RES['routers']) |
| 1080 | |
| 1081 | create_secgroups(RES['secgroups']) |
Emilien Macchi | d18fec1 | 2014-09-15 14:32:54 -0400 | [diff] [blame] | 1082 | create_volumes(RES['volumes']) |
Joe H. Rahme | c96129b | 2015-03-30 11:23:31 +0200 | [diff] [blame] | 1083 | |
| 1084 | # Only attempt attaching the volumes if servers are defined in the |
nayna-patel | 1dfbedb | 2015-08-04 11:07:56 +0000 | [diff] [blame] | 1085 | # resource file |
Joe H. Rahme | c96129b | 2015-03-30 11:23:31 +0200 | [diff] [blame] | 1086 | if 'servers' in RES: |
| 1087 | create_servers(RES['servers']) |
| 1088 | attach_volumes(RES['volumes']) |
Sean Dague | 655e0af | 2014-05-29 09:00:22 -0400 | [diff] [blame] | 1089 | |
| 1090 | |
Joe Gordon | db63b1c | 2014-07-24 23:21:21 +0000 | [diff] [blame] | 1091 | def destroy_resources(): |
| 1092 | LOG.info("Destroying Resources") |
| 1093 | # Destroy in inverse order of create |
Joe Gordon | db63b1c | 2014-07-24 23:21:21 +0000 | [diff] [blame] | 1094 | destroy_servers(RES['servers']) |
Joe Gordon | 6f0426c | 2014-07-25 01:10:28 +0000 | [diff] [blame] | 1095 | destroy_images(RES['images']) |
Emilien Macchi | bb71e07 | 2014-07-05 19:18:52 +0200 | [diff] [blame] | 1096 | destroy_objects(RES['objects']) |
Emilien Macchi | bb71e07 | 2014-07-05 19:18:52 +0200 | [diff] [blame] | 1097 | destroy_volumes(RES['volumes']) |
Jakub Libosvar | 3791ac9 | 2014-11-11 13:23:44 +0100 | [diff] [blame] | 1098 | if CONF.service_available.neutron and not CONF.baremetal.driver_enabled: |
| 1099 | destroy_routers(RES['routers']) |
| 1100 | destroy_subnets(RES['subnets']) |
| 1101 | destroy_networks(RES['networks']) |
| 1102 | destroy_secgroups(RES['secgroups']) |
Emilien Macchi | bb71e07 | 2014-07-05 19:18:52 +0200 | [diff] [blame] | 1103 | destroy_users(RES['users']) |
| 1104 | destroy_tenants(RES['tenants']) |
zhangguoqing | 6c09664 | 2016-01-04 06:17:21 +0000 | [diff] [blame] | 1105 | LOG.warning("Destroy mode incomplete") |
Joe Gordon | 6f0426c | 2014-07-25 01:10:28 +0000 | [diff] [blame] | 1106 | |
Joe Gordon | db63b1c | 2014-07-24 23:21:21 +0000 | [diff] [blame] | 1107 | |
Sean Dague | 655e0af | 2014-05-29 09:00:22 -0400 | [diff] [blame] | 1108 | def get_options(): |
| 1109 | global OPTS |
| 1110 | parser = argparse.ArgumentParser( |
| 1111 | description='Create and validate a fixed set of OpenStack resources') |
| 1112 | parser.add_argument('-m', '--mode', |
| 1113 | metavar='<create|check|destroy>', |
| 1114 | required=True, |
| 1115 | help=('One of (create, check, destroy)')) |
| 1116 | parser.add_argument('-r', '--resources', |
| 1117 | required=True, |
| 1118 | metavar='resourcefile.yaml', |
| 1119 | help='Resources definition yaml file') |
Joe Gordon | 28a84ae | 2014-07-17 15:38:28 +0000 | [diff] [blame] | 1120 | |
Sean Dague | 319b37a | 2014-07-11 07:28:11 -0400 | [diff] [blame] | 1121 | parser.add_argument( |
| 1122 | '-d', '--devstack-base', |
| 1123 | required=True, |
| 1124 | metavar='/opt/stack/old', |
| 1125 | help='Devstack base directory for retrieving artifacts') |
Joe Gordon | 28a84ae | 2014-07-17 15:38:28 +0000 | [diff] [blame] | 1126 | parser.add_argument( |
| 1127 | '-c', '--config-file', |
| 1128 | metavar='/etc/tempest.conf', |
| 1129 | help='path to javelin2(tempest) config file') |
| 1130 | |
Sean Dague | 655e0af | 2014-05-29 09:00:22 -0400 | [diff] [blame] | 1131 | # auth bits, letting us also just source the devstack openrc |
| 1132 | parser.add_argument('--os-username', |
| 1133 | metavar='<auth-user-name>', |
| 1134 | default=os.environ.get('OS_USERNAME'), |
| 1135 | help=('Defaults to env[OS_USERNAME].')) |
| 1136 | parser.add_argument('--os-password', |
| 1137 | metavar='<auth-password>', |
| 1138 | default=os.environ.get('OS_PASSWORD'), |
| 1139 | help=('Defaults to env[OS_PASSWORD].')) |
| 1140 | parser.add_argument('--os-tenant-name', |
| 1141 | metavar='<auth-tenant-name>', |
| 1142 | default=os.environ.get('OS_TENANT_NAME'), |
| 1143 | help=('Defaults to env[OS_TENANT_NAME].')) |
| 1144 | |
| 1145 | OPTS = parser.parse_args() |
| 1146 | if OPTS.mode not in ('create', 'check', 'destroy'): |
| 1147 | print("ERROR: Unknown mode -m %s\n" % OPTS.mode) |
| 1148 | parser.print_help() |
| 1149 | sys.exit(1) |
Joe Gordon | 28a84ae | 2014-07-17 15:38:28 +0000 | [diff] [blame] | 1150 | if OPTS.config_file: |
| 1151 | config.CONF.set_config_path(OPTS.config_file) |
Sean Dague | 655e0af | 2014-05-29 09:00:22 -0400 | [diff] [blame] | 1152 | |
| 1153 | |
Joe Gordon | 915eb8e | 2014-07-17 11:25:46 +0200 | [diff] [blame] | 1154 | def setup_logging(): |
Sean Dague | 655e0af | 2014-05-29 09:00:22 -0400 | [diff] [blame] | 1155 | global LOG |
Doug Hellmann | 583ce2c | 2015-03-11 14:55:46 +0000 | [diff] [blame] | 1156 | logging.setup(CONF, __name__) |
Sean Dague | 655e0af | 2014-05-29 09:00:22 -0400 | [diff] [blame] | 1157 | LOG = logging.getLogger(__name__) |
Sean Dague | 655e0af | 2014-05-29 09:00:22 -0400 | [diff] [blame] | 1158 | |
| 1159 | |
| 1160 | def main(): |
Matthew Treinish | 0ee3744 | 2016-01-12 16:05:40 -0500 | [diff] [blame] | 1161 | print("Javelin is deprecated and will be removed from Tempest in the " |
| 1162 | "future.") |
Sean Dague | 655e0af | 2014-05-29 09:00:22 -0400 | [diff] [blame] | 1163 | global RES |
| 1164 | get_options() |
| 1165 | setup_logging() |
Chris Dent | 51e76de | 2014-10-01 12:07:14 +0100 | [diff] [blame] | 1166 | RES.update(load_resources(OPTS.resources)) |
Sean Dague | 655e0af | 2014-05-29 09:00:22 -0400 | [diff] [blame] | 1167 | |
| 1168 | if OPTS.mode == 'create': |
| 1169 | create_resources() |
Joe Gordon | 1a09700 | 2014-07-24 23:44:08 +0000 | [diff] [blame] | 1170 | # Make sure the resources we just created actually work |
| 1171 | checker = JavelinCheck(USERS, RES) |
| 1172 | checker.check() |
Sean Dague | 655e0af | 2014-05-29 09:00:22 -0400 | [diff] [blame] | 1173 | elif OPTS.mode == 'check': |
| 1174 | collect_users(RES['users']) |
| 1175 | checker = JavelinCheck(USERS, RES) |
| 1176 | checker.check() |
| 1177 | elif OPTS.mode == 'destroy': |
Joe Gordon | db63b1c | 2014-07-24 23:21:21 +0000 | [diff] [blame] | 1178 | collect_users(RES['users']) |
| 1179 | destroy_resources() |
Sean Dague | 655e0af | 2014-05-29 09:00:22 -0400 | [diff] [blame] | 1180 | else: |
| 1181 | LOG.error('Unknown mode %s' % OPTS.mode) |
| 1182 | return 1 |
Joe Gordon | 246353a | 2014-07-18 00:10:28 +0200 | [diff] [blame] | 1183 | LOG.info('javelin2 successfully finished') |
Sean Dague | 655e0af | 2014-05-29 09:00:22 -0400 | [diff] [blame] | 1184 | return 0 |
| 1185 | |
| 1186 | if __name__ == "__main__": |
| 1187 | sys.exit(main()) |