sslypushenko | 0de7d05 | 2015-04-16 18:49:55 +0300 | [diff] [blame] | 1 | #!/usr/bin/env python |
| 2 | |
| 3 | # Copyright 2015 Mirantis, Inc. |
| 4 | # |
| 5 | # Licensed under the Apache License, Version 2.0 (the "License"); you may |
| 6 | # not use this file except in compliance with the License. You may obtain |
| 7 | # a copy of the License at |
| 8 | # |
| 9 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | # |
| 11 | # Unless required by applicable law or agreed to in writing, software |
| 12 | # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 13 | # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| 14 | # License for the specific language governing permissions and limitations |
| 15 | # under the License. |
| 16 | |
Jane Zadorozhna | 00fc3dc | 2015-05-27 18:01:56 +0300 | [diff] [blame] | 17 | """ |
| 18 | Utility for creating **accounts.yaml** file for concurrent test runs. |
| 19 | Creates one primary user, one alt user, one swift admin, one stack owner |
| 20 | and one admin (optionally) for each concurrent thread. The utility creates |
| 21 | user for each tenant. The **accounts.yaml** file will be valid and contain |
| 22 | credentials for created users, so each user will be in separate tenant and |
| 23 | have the username, tenant_name, password and roles. |
| 24 | |
| 25 | **Usage:** ``tempest-account-generator [-h] [OPTIONS] accounts_file.yaml``. |
| 26 | |
| 27 | Positional Arguments |
Matthew Treinish | f45ba2e | 2015-08-24 15:05:01 -0400 | [diff] [blame] | 28 | -------------------- |
Jane Zadorozhna | 00fc3dc | 2015-05-27 18:01:56 +0300 | [diff] [blame] | 29 | **accounts_file.yaml** (Required) Provide an output accounts yaml file. Utility |
| 30 | creates a .yaml file in the directory where the command is ran. The appropriate |
| 31 | name for the file is *accounts.yaml* and it should be placed in *tempest/etc* |
| 32 | directory. |
| 33 | |
| 34 | Authentication |
| 35 | -------------- |
| 36 | |
| 37 | Account generator creates users and tenants so it needs the admin credentials |
| 38 | of your cloud to operate properly. The corresponding info can be given either |
| 39 | through CLI options or environment variables. |
| 40 | |
| 41 | You're probably familiar with these, but just to remind:: |
| 42 | |
| 43 | +----------+------------------+----------------------+ |
| 44 | | Param | CLI | Environment Variable | |
| 45 | +----------+------------------+----------------------+ |
| 46 | | Username | --os-username | OS_USERNAME | |
| 47 | | Password | --os-password | OS_PASSWORD | |
| 48 | | Tenant | --os-tenant-name | OS_TENANT_NAME | |
| 49 | +----------+------------------+----------------------+ |
| 50 | |
| 51 | Optional Arguments |
Matthew Treinish | f45ba2e | 2015-08-24 15:05:01 -0400 | [diff] [blame] | 52 | ------------------ |
Jane Zadorozhna | 00fc3dc | 2015-05-27 18:01:56 +0300 | [diff] [blame] | 53 | **-h**, **--help** (Optional) Shows help message with the description of |
| 54 | utility and its arguments, and exits. |
| 55 | |
| 56 | **c /etc/tempest.conf**, **--config-file /etc/tempest.conf** (Optional) Path to |
| 57 | tempest config file. |
| 58 | |
| 59 | **--os-username <auth-user-name>** (Optional) Name used for authentication with |
| 60 | the OpenStack Identity service. Defaults to env[OS_USERNAME]. Note: User should |
| 61 | have permissions to create new user accounts and tenants. |
| 62 | |
| 63 | **--os-password <auth-password>** (Optional) Password used for authentication |
| 64 | with the OpenStack Identity service. Defaults to env[OS_PASSWORD]. |
| 65 | |
| 66 | **--os-tenant-name <auth-tenant-name>** (Optional) Tenant to request |
| 67 | authorization on. Defaults to env[OS_TENANT_NAME]. |
| 68 | |
| 69 | **--tag TAG** (Optional) Resources tag. Each created resource (user, project) |
| 70 | will have the prefix with the given TAG in its name. Using tag is recommended |
| 71 | for the further using, cleaning resources. |
| 72 | |
| 73 | **-r CONCURRENCY**, **--concurrency CONCURRENCY** (Required) Concurrency count |
| 74 | (default: 1). The number of accounts required can be estimated as |
| 75 | CONCURRENCY x 2. Each user provided in *accounts.yaml* file will be in |
| 76 | a different tenant. This is required to provide isolation between test for |
| 77 | running in parallel. |
| 78 | |
| 79 | **--with-admin** (Optional) Creates admin for each concurrent group |
| 80 | (default: False). |
| 81 | |
| 82 | To see help on specific argument, please do: ``tempest-account-generator |
| 83 | [OPTIONS] <accounts_file.yaml> -h``. |
| 84 | """ |
sslypushenko | 0de7d05 | 2015-04-16 18:49:55 +0300 | [diff] [blame] | 85 | import argparse |
David Kranz | 0aa4a7b | 2015-06-08 13:25:41 -0400 | [diff] [blame] | 86 | import netaddr |
sslypushenko | 0de7d05 | 2015-04-16 18:49:55 +0300 | [diff] [blame] | 87 | import os |
David Paterson | 68b8b9d | 2015-12-01 15:44:14 -0800 | [diff] [blame] | 88 | import traceback |
sslypushenko | 0de7d05 | 2015-04-16 18:49:55 +0300 | [diff] [blame] | 89 | |
David Paterson | 68b8b9d | 2015-12-01 15:44:14 -0800 | [diff] [blame] | 90 | from cliff import command |
sslypushenko | 0de7d05 | 2015-04-16 18:49:55 +0300 | [diff] [blame] | 91 | from oslo_log import log as logging |
Ken'ichi Ohmichi | 8844b37 | 2016-01-13 00:09:17 +0000 | [diff] [blame] | 92 | import tempest_lib.auth |
| 93 | from tempest_lib.common.utils import data_utils |
| 94 | import tempest_lib.exceptions |
Ken'ichi Ohmichi | 5a31951 | 2016-01-12 23:58:47 +0000 | [diff] [blame] | 95 | from tempest_lib.services.network import networks_client |
| 96 | from tempest_lib.services.network import subnets_client |
sslypushenko | 0de7d05 | 2015-04-16 18:49:55 +0300 | [diff] [blame] | 97 | import yaml |
| 98 | |
Ken'ichi Ohmichi | 6ea3f98 | 2015-11-09 12:41:13 +0000 | [diff] [blame] | 99 | from tempest.common import identity |
sslypushenko | 0de7d05 | 2015-04-16 18:49:55 +0300 | [diff] [blame] | 100 | from tempest import config |
Matthew Treinish | 36c2e28 | 2015-08-25 00:30:15 -0400 | [diff] [blame] | 101 | from tempest import exceptions as exc |
sslypushenko | 0de7d05 | 2015-04-16 18:49:55 +0300 | [diff] [blame] | 102 | from tempest.services.identity.v2.json import identity_client |
Daniel Mellado | 6b16b92 | 2015-12-07 12:43:08 +0000 | [diff] [blame] | 103 | from tempest.services.identity.v2.json import roles_client |
Daniel Mellado | b04da90 | 2015-11-20 17:43:12 +0100 | [diff] [blame] | 104 | from tempest.services.identity.v2.json import tenants_client |
Daniel Mellado | 82c83a5 | 2015-12-09 15:16:49 +0000 | [diff] [blame] | 105 | from tempest.services.identity.v2.json import users_client |
David Kranz | 0aa4a7b | 2015-06-08 13:25:41 -0400 | [diff] [blame] | 106 | from tempest.services.network.json import network_client |
sslypushenko | 0de7d05 | 2015-04-16 18:49:55 +0300 | [diff] [blame] | 107 | |
| 108 | LOG = None |
| 109 | CONF = config.CONF |
David Paterson | 68b8b9d | 2015-12-01 15:44:14 -0800 | [diff] [blame] | 110 | DESCRIPTION = ('Create accounts.yaml file for concurrent test runs.%s' |
| 111 | 'One primary user, one alt user, ' |
| 112 | 'one swift admin, one stack owner ' |
| 113 | 'and one admin (optionally) will be created ' |
| 114 | 'for each concurrent thread.' % os.linesep) |
sslypushenko | 0de7d05 | 2015-04-16 18:49:55 +0300 | [diff] [blame] | 115 | |
| 116 | |
| 117 | def setup_logging(): |
| 118 | global LOG |
| 119 | logging.setup(CONF, __name__) |
| 120 | LOG = logging.getLogger(__name__) |
| 121 | |
| 122 | |
David Kranz | 0aa4a7b | 2015-06-08 13:25:41 -0400 | [diff] [blame] | 123 | def get_admin_clients(opts): |
sslypushenko | 0de7d05 | 2015-04-16 18:49:55 +0300 | [diff] [blame] | 124 | _creds = tempest_lib.auth.KeystoneV2Credentials( |
| 125 | username=opts.os_username, |
| 126 | password=opts.os_password, |
| 127 | tenant_name=opts.os_tenant_name) |
| 128 | auth_params = { |
| 129 | 'disable_ssl_certificate_validation': |
| 130 | CONF.identity.disable_ssl_certificate_validation, |
| 131 | 'ca_certs': CONF.identity.ca_certificates_file, |
| 132 | 'trace_requests': CONF.debug.trace_requests |
| 133 | } |
| 134 | _auth = tempest_lib.auth.KeystoneV2AuthProvider( |
| 135 | _creds, CONF.identity.uri, **auth_params) |
| 136 | params = { |
| 137 | 'disable_ssl_certificate_validation': |
| 138 | CONF.identity.disable_ssl_certificate_validation, |
| 139 | 'ca_certs': CONF.identity.ca_certificates_file, |
| 140 | 'trace_requests': CONF.debug.trace_requests, |
| 141 | 'build_interval': CONF.compute.build_interval, |
| 142 | 'build_timeout': CONF.compute.build_timeout |
| 143 | } |
Ken'ichi Ohmichi | a628707 | 2015-07-02 02:43:15 +0000 | [diff] [blame] | 144 | identity_admin = identity_client.IdentityClient( |
sslypushenko | 0de7d05 | 2015-04-16 18:49:55 +0300 | [diff] [blame] | 145 | _auth, |
| 146 | CONF.identity.catalog_type, |
| 147 | CONF.identity.region, |
| 148 | endpoint_type='adminURL', |
| 149 | **params |
| 150 | ) |
Daniel Mellado | b04da90 | 2015-11-20 17:43:12 +0100 | [diff] [blame] | 151 | tenants_admin = tenants_client.TenantsClient( |
| 152 | _auth, |
| 153 | CONF.identity.catalog_type, |
| 154 | CONF.identity.region, |
| 155 | endpoint_type='adminURL', |
| 156 | **params |
| 157 | ) |
Daniel Mellado | 6b16b92 | 2015-12-07 12:43:08 +0000 | [diff] [blame] | 158 | roles_admin = roles_client.RolesClient( |
| 159 | _auth, |
| 160 | CONF.identity.catalog_type, |
| 161 | CONF.identity.region, |
| 162 | endpoint_type='adminURL', |
| 163 | **params |
| 164 | ) |
Daniel Mellado | 82c83a5 | 2015-12-09 15:16:49 +0000 | [diff] [blame] | 165 | users_admin = users_client.UsersClient( |
| 166 | _auth, |
| 167 | CONF.identity.catalog_type, |
| 168 | CONF.identity.region, |
| 169 | endpoint_type='adminURL', |
| 170 | **params |
| 171 | ) |
David Kranz | 0aa4a7b | 2015-06-08 13:25:41 -0400 | [diff] [blame] | 172 | network_admin = None |
John Warren | 94d8faf | 2015-09-15 12:22:24 -0400 | [diff] [blame] | 173 | networks_admin = None |
John Warren | 3961acd | 2015-10-02 14:38:53 -0400 | [diff] [blame] | 174 | subnets_admin = None |
John Warren | 94d8faf | 2015-09-15 12:22:24 -0400 | [diff] [blame] | 175 | neutron_iso_networks = False |
David Kranz | 0aa4a7b | 2015-06-08 13:25:41 -0400 | [diff] [blame] | 176 | if (CONF.service_available.neutron and |
| 177 | CONF.auth.create_isolated_networks): |
John Warren | 94d8faf | 2015-09-15 12:22:24 -0400 | [diff] [blame] | 178 | neutron_iso_networks = True |
Ken'ichi Ohmichi | a628707 | 2015-07-02 02:43:15 +0000 | [diff] [blame] | 179 | network_admin = network_client.NetworkClient( |
David Kranz | 0aa4a7b | 2015-06-08 13:25:41 -0400 | [diff] [blame] | 180 | _auth, |
| 181 | CONF.network.catalog_type, |
| 182 | CONF.network.region or CONF.identity.region, |
| 183 | endpoint_type='adminURL', |
| 184 | **params) |
John Warren | 94d8faf | 2015-09-15 12:22:24 -0400 | [diff] [blame] | 185 | networks_admin = networks_client.NetworksClient( |
| 186 | _auth, |
| 187 | CONF.network.catalog_type, |
| 188 | CONF.network.region or CONF.identity.region, |
| 189 | endpoint_type='adminURL', |
| 190 | **params) |
John Warren | 3961acd | 2015-10-02 14:38:53 -0400 | [diff] [blame] | 191 | subnets_admin = subnets_client.SubnetsClient( |
| 192 | _auth, |
| 193 | CONF.network.catalog_type, |
| 194 | CONF.network.region or CONF.identity.region, |
| 195 | endpoint_type='adminURL', |
| 196 | **params) |
Daniel Mellado | 82c83a5 | 2015-12-09 15:16:49 +0000 | [diff] [blame] | 197 | return (identity_admin, tenants_admin, roles_admin, users_admin, |
| 198 | neutron_iso_networks, network_admin, networks_admin, subnets_admin) |
sslypushenko | 0de7d05 | 2015-04-16 18:49:55 +0300 | [diff] [blame] | 199 | |
| 200 | |
| 201 | def create_resources(opts, resources): |
Daniel Mellado | 82c83a5 | 2015-12-09 15:16:49 +0000 | [diff] [blame] | 202 | (identity_admin, tenants_admin, roles_admin, users_admin, |
| 203 | neutron_iso_networks, network_admin, networks_admin, |
| 204 | subnets_admin) = get_admin_clients(opts) |
Daniel Mellado | 6b16b92 | 2015-12-07 12:43:08 +0000 | [diff] [blame] | 205 | roles = roles_admin.list_roles()['roles'] |
sslypushenko | 0de7d05 | 2015-04-16 18:49:55 +0300 | [diff] [blame] | 206 | for u in resources['users']: |
| 207 | u['role_ids'] = [] |
| 208 | for r in u.get('roles', ()): |
| 209 | try: |
| 210 | role = filter(lambda r_: r_['name'] == r, roles)[0] |
sslypushenko | 0de7d05 | 2015-04-16 18:49:55 +0300 | [diff] [blame] | 211 | except IndexError: |
Matthew Treinish | 36c2e28 | 2015-08-25 00:30:15 -0400 | [diff] [blame] | 212 | msg = "Role: %s doesn't exist" % r |
| 213 | raise exc.InvalidConfiguration(msg) |
| 214 | u['role_ids'] += [role['id']] |
Daniel Mellado | b04da90 | 2015-11-20 17:43:12 +0100 | [diff] [blame] | 215 | existing = [x['name'] for x in tenants_admin.list_tenants()['tenants']] |
sslypushenko | 0de7d05 | 2015-04-16 18:49:55 +0300 | [diff] [blame] | 216 | for tenant in resources['tenants']: |
| 217 | if tenant not in existing: |
Daniel Mellado | b04da90 | 2015-11-20 17:43:12 +0100 | [diff] [blame] | 218 | tenants_admin.create_tenant(tenant) |
sslypushenko | 0de7d05 | 2015-04-16 18:49:55 +0300 | [diff] [blame] | 219 | else: |
zhangguoqing | 6c09664 | 2016-01-04 06:17:21 +0000 | [diff] [blame] | 220 | LOG.warning("Tenant '%s' already exists in this environment" |
| 221 | % tenant) |
sslypushenko | 0de7d05 | 2015-04-16 18:49:55 +0300 | [diff] [blame] | 222 | LOG.info('Tenants created') |
| 223 | for u in resources['users']: |
| 224 | try: |
Daniel Mellado | b04da90 | 2015-11-20 17:43:12 +0100 | [diff] [blame] | 225 | tenant = identity.get_tenant_by_name(tenants_admin, u['tenant']) |
sslypushenko | 0de7d05 | 2015-04-16 18:49:55 +0300 | [diff] [blame] | 226 | except tempest_lib.exceptions.NotFound: |
| 227 | LOG.error("Tenant: %s - not found" % u['tenant']) |
| 228 | continue |
| 229 | while True: |
| 230 | try: |
Daniel Mellado | b04da90 | 2015-11-20 17:43:12 +0100 | [diff] [blame] | 231 | identity.get_user_by_username(tenants_admin, |
Ken'ichi Ohmichi | d9fed31 | 2015-11-09 13:05:32 +0000 | [diff] [blame] | 232 | tenant['id'], u['name']) |
sslypushenko | 0de7d05 | 2015-04-16 18:49:55 +0300 | [diff] [blame] | 233 | except tempest_lib.exceptions.NotFound: |
Daniel Mellado | 82c83a5 | 2015-12-09 15:16:49 +0000 | [diff] [blame] | 234 | users_admin.create_user( |
sslypushenko | 0de7d05 | 2015-04-16 18:49:55 +0300 | [diff] [blame] | 235 | u['name'], u['pass'], tenant['id'], |
| 236 | "%s@%s" % (u['name'], tenant['id']), |
| 237 | enabled=True) |
| 238 | break |
| 239 | else: |
zhangguoqing | 6c09664 | 2016-01-04 06:17:21 +0000 | [diff] [blame] | 240 | LOG.warning("User '%s' already exists in this environment. " |
| 241 | "New name generated" % u['name']) |
sslypushenko | 0de7d05 | 2015-04-16 18:49:55 +0300 | [diff] [blame] | 242 | u['name'] = random_user_name(opts.tag, u['prefix']) |
| 243 | |
| 244 | LOG.info('Users created') |
John Warren | 94d8faf | 2015-09-15 12:22:24 -0400 | [diff] [blame] | 245 | if neutron_iso_networks: |
David Kranz | 0aa4a7b | 2015-06-08 13:25:41 -0400 | [diff] [blame] | 246 | for u in resources['users']: |
Daniel Mellado | b04da90 | 2015-11-20 17:43:12 +0100 | [diff] [blame] | 247 | tenant = identity.get_tenant_by_name(tenants_admin, u['tenant']) |
John Warren | 94d8faf | 2015-09-15 12:22:24 -0400 | [diff] [blame] | 248 | network_name, router_name = create_network_resources( |
John Warren | 3961acd | 2015-10-02 14:38:53 -0400 | [diff] [blame] | 249 | network_admin, networks_admin, subnets_admin, tenant['id'], |
| 250 | u['name']) |
David Kranz | 0aa4a7b | 2015-06-08 13:25:41 -0400 | [diff] [blame] | 251 | u['network'] = network_name |
David Paterson | 15be99e | 2015-04-08 21:58:19 -0400 | [diff] [blame] | 252 | u['router'] = router_name |
David Kranz | 0aa4a7b | 2015-06-08 13:25:41 -0400 | [diff] [blame] | 253 | LOG.info('Networks created') |
sslypushenko | 0de7d05 | 2015-04-16 18:49:55 +0300 | [diff] [blame] | 254 | for u in resources['users']: |
| 255 | try: |
Daniel Mellado | b04da90 | 2015-11-20 17:43:12 +0100 | [diff] [blame] | 256 | tenant = identity.get_tenant_by_name(tenants_admin, u['tenant']) |
sslypushenko | 0de7d05 | 2015-04-16 18:49:55 +0300 | [diff] [blame] | 257 | except tempest_lib.exceptions.NotFound: |
| 258 | LOG.error("Tenant: %s - not found" % u['tenant']) |
| 259 | continue |
| 260 | try: |
Daniel Mellado | b04da90 | 2015-11-20 17:43:12 +0100 | [diff] [blame] | 261 | user = identity.get_user_by_username(tenants_admin, |
Ken'ichi Ohmichi | d9fed31 | 2015-11-09 13:05:32 +0000 | [diff] [blame] | 262 | tenant['id'], u['name']) |
sslypushenko | 0de7d05 | 2015-04-16 18:49:55 +0300 | [diff] [blame] | 263 | except tempest_lib.exceptions.NotFound: |
| 264 | LOG.error("User: %s - not found" % u['user']) |
| 265 | continue |
| 266 | for r in u['role_ids']: |
| 267 | try: |
Daniel Mellado | 6b16b92 | 2015-12-07 12:43:08 +0000 | [diff] [blame] | 268 | roles_admin.assign_user_role(tenant['id'], user['id'], r) |
sslypushenko | 0de7d05 | 2015-04-16 18:49:55 +0300 | [diff] [blame] | 269 | except tempest_lib.exceptions.Conflict: |
| 270 | # don't care if it's already assigned |
| 271 | pass |
| 272 | LOG.info('Roles assigned') |
| 273 | LOG.info('Resources deployed successfully!') |
| 274 | |
| 275 | |
John Warren | 94d8faf | 2015-09-15 12:22:24 -0400 | [diff] [blame] | 276 | def create_network_resources(network_admin_client, networks_admin_client, |
John Warren | 3961acd | 2015-10-02 14:38:53 -0400 | [diff] [blame] | 277 | subnets_admin_client, tenant_id, name): |
David Kranz | 0aa4a7b | 2015-06-08 13:25:41 -0400 | [diff] [blame] | 278 | |
| 279 | def _create_network(name): |
John Warren | 94d8faf | 2015-09-15 12:22:24 -0400 | [diff] [blame] | 280 | resp_body = networks_admin_client.create_network( |
David Kranz | 0aa4a7b | 2015-06-08 13:25:41 -0400 | [diff] [blame] | 281 | name=name, tenant_id=tenant_id) |
| 282 | return resp_body['network'] |
| 283 | |
| 284 | def _create_subnet(subnet_name, network_id): |
| 285 | base_cidr = netaddr.IPNetwork(CONF.network.tenant_network_cidr) |
| 286 | mask_bits = CONF.network.tenant_network_mask_bits |
| 287 | for subnet_cidr in base_cidr.subnet(mask_bits): |
| 288 | try: |
John Warren | 3961acd | 2015-10-02 14:38:53 -0400 | [diff] [blame] | 289 | resp_body = subnets_admin_client.\ |
David Kranz | 0aa4a7b | 2015-06-08 13:25:41 -0400 | [diff] [blame] | 290 | create_subnet( |
| 291 | network_id=network_id, cidr=str(subnet_cidr), |
| 292 | name=subnet_name, |
| 293 | tenant_id=tenant_id, |
| 294 | enable_dhcp=True, |
| 295 | ip_version=4) |
| 296 | break |
| 297 | except tempest_lib.exceptions.BadRequest as e: |
| 298 | if 'overlaps with another subnet' not in str(e): |
| 299 | raise |
| 300 | else: |
| 301 | message = 'Available CIDR for subnet creation could not be found' |
| 302 | raise Exception(message) |
| 303 | return resp_body['subnet'] |
| 304 | |
| 305 | def _create_router(router_name): |
| 306 | external_net_id = dict( |
| 307 | network_id=CONF.network.public_network_id) |
| 308 | resp_body = network_admin_client.create_router( |
| 309 | router_name, |
| 310 | external_gateway_info=external_net_id, |
| 311 | tenant_id=tenant_id) |
| 312 | return resp_body['router'] |
| 313 | |
| 314 | def _add_router_interface(router_id, subnet_id): |
| 315 | network_admin_client.add_router_interface_with_subnet_id( |
| 316 | router_id, subnet_id) |
| 317 | |
| 318 | network_name = name + "-network" |
| 319 | network = _create_network(network_name) |
| 320 | subnet_name = name + "-subnet" |
| 321 | subnet = _create_subnet(subnet_name, network['id']) |
| 322 | router_name = name + "-router" |
| 323 | router = _create_router(router_name) |
| 324 | _add_router_interface(router['id'], subnet['id']) |
David Paterson | 15be99e | 2015-04-08 21:58:19 -0400 | [diff] [blame] | 325 | return network_name, router_name |
David Kranz | 0aa4a7b | 2015-06-08 13:25:41 -0400 | [diff] [blame] | 326 | |
| 327 | |
sslypushenko | 0de7d05 | 2015-04-16 18:49:55 +0300 | [diff] [blame] | 328 | def random_user_name(tag, prefix): |
| 329 | if tag: |
| 330 | return data_utils.rand_name('-'.join((tag, prefix))) |
| 331 | else: |
| 332 | return data_utils.rand_name(prefix) |
| 333 | |
| 334 | |
| 335 | def generate_resources(opts): |
| 336 | spec = [{'number': 1, |
| 337 | 'prefix': 'primary', |
| 338 | 'roles': (CONF.auth.tempest_roles + |
| 339 | [CONF.object_storage.operator_role])}, |
| 340 | {'number': 1, |
| 341 | 'prefix': 'alt', |
| 342 | 'roles': (CONF.auth.tempest_roles + |
Matthew Treinish | 36c2e28 | 2015-08-25 00:30:15 -0400 | [diff] [blame] | 343 | [CONF.object_storage.operator_role])}] |
| 344 | if CONF.service_available.swift: |
| 345 | spec.append({'number': 1, |
Matthew Treinish | 7b05b34 | 2015-09-09 17:14:02 -0400 | [diff] [blame] | 346 | 'prefix': 'swift_operator', |
Matthew Treinish | 36c2e28 | 2015-08-25 00:30:15 -0400 | [diff] [blame] | 347 | 'roles': (CONF.auth.tempest_roles + |
Matthew Treinish | 7b05b34 | 2015-09-09 17:14:02 -0400 | [diff] [blame] | 348 | [CONF.object_storage.operator_role])}) |
| 349 | spec.append({'number': 1, |
| 350 | 'prefix': 'swift_reseller_admin', |
| 351 | 'roles': (CONF.auth.tempest_roles + |
| 352 | [CONF.object_storage.reseller_admin_role])}) |
Matthew Treinish | 36c2e28 | 2015-08-25 00:30:15 -0400 | [diff] [blame] | 353 | if CONF.service_available.heat: |
| 354 | spec.append({'number': 1, |
| 355 | 'prefix': 'stack_owner', |
| 356 | 'roles': (CONF.auth.tempest_roles + |
| 357 | [CONF.orchestration.stack_owner_role])}) |
sslypushenko | 0de7d05 | 2015-04-16 18:49:55 +0300 | [diff] [blame] | 358 | if opts.admin: |
| 359 | spec.append({ |
| 360 | 'number': 1, |
| 361 | 'prefix': 'admin', |
| 362 | 'roles': (CONF.auth.tempest_roles + |
| 363 | [CONF.identity.admin_role]) |
| 364 | }) |
| 365 | resources = {'tenants': [], |
| 366 | 'users': []} |
| 367 | for count in range(opts.concurrency): |
| 368 | for user_group in spec: |
| 369 | users = [random_user_name(opts.tag, user_group['prefix']) |
| 370 | for _ in range(user_group['number'])] |
| 371 | for user in users: |
| 372 | tenant = '-'.join((user, 'tenant')) |
| 373 | resources['tenants'].append(tenant) |
| 374 | resources['users'].append({ |
| 375 | 'tenant': tenant, |
| 376 | 'name': user, |
Marc Koderer | 808e8ec | 2015-12-16 15:38:46 +0100 | [diff] [blame] | 377 | 'pass': data_utils.rand_password(), |
sslypushenko | 0de7d05 | 2015-04-16 18:49:55 +0300 | [diff] [blame] | 378 | 'prefix': user_group['prefix'], |
| 379 | 'roles': user_group['roles'] |
| 380 | }) |
| 381 | return resources |
| 382 | |
| 383 | |
| 384 | def dump_accounts(opts, resources): |
| 385 | accounts = [] |
| 386 | for user in resources['users']: |
David Kranz | 0aa4a7b | 2015-06-08 13:25:41 -0400 | [diff] [blame] | 387 | account = { |
sslypushenko | 0de7d05 | 2015-04-16 18:49:55 +0300 | [diff] [blame] | 388 | 'username': user['name'], |
| 389 | 'tenant_name': user['tenant'], |
| 390 | 'password': user['pass'], |
| 391 | 'roles': user['roles'] |
David Kranz | 0aa4a7b | 2015-06-08 13:25:41 -0400 | [diff] [blame] | 392 | } |
David Paterson | bfab6b9 | 2016-01-21 15:39:49 -0800 | [diff] [blame] | 393 | if 'network' in user or 'router' in user: |
David Paterson | 15be99e | 2015-04-08 21:58:19 -0400 | [diff] [blame] | 394 | account['resources'] = {} |
David Kranz | 0aa4a7b | 2015-06-08 13:25:41 -0400 | [diff] [blame] | 395 | if 'network' in user: |
David Paterson | 15be99e | 2015-04-08 21:58:19 -0400 | [diff] [blame] | 396 | account['resources']['network'] = user['network'] |
| 397 | if 'router' in user: |
| 398 | account['resources']['router'] = user['router'] |
David Kranz | 0aa4a7b | 2015-06-08 13:25:41 -0400 | [diff] [blame] | 399 | accounts.append(account) |
sslypushenko | 0de7d05 | 2015-04-16 18:49:55 +0300 | [diff] [blame] | 400 | if os.path.exists(opts.accounts): |
| 401 | os.rename(opts.accounts, '.'.join((opts.accounts, 'bak'))) |
| 402 | with open(opts.accounts, 'w') as f: |
| 403 | yaml.dump(accounts, f, default_flow_style=False) |
| 404 | LOG.info('%s generated successfully!' % opts.accounts) |
| 405 | |
| 406 | |
David Paterson | 68b8b9d | 2015-12-01 15:44:14 -0800 | [diff] [blame] | 407 | def _parser_add_args(parser): |
sslypushenko | 0de7d05 | 2015-04-16 18:49:55 +0300 | [diff] [blame] | 408 | parser.add_argument('-c', '--config-file', |
| 409 | metavar='/etc/tempest.conf', |
| 410 | help='path to tempest config file') |
| 411 | parser.add_argument('--os-username', |
| 412 | metavar='<auth-user-name>', |
| 413 | default=os.environ.get('OS_USERNAME'), |
Jane Zadorozhna | 00fc3dc | 2015-05-27 18:01:56 +0300 | [diff] [blame] | 414 | help='User should have permissions ' |
sslypushenko | 0de7d05 | 2015-04-16 18:49:55 +0300 | [diff] [blame] | 415 | 'to create new user accounts and ' |
| 416 | 'tenants. Defaults to env[OS_USERNAME].') |
| 417 | parser.add_argument('--os-password', |
| 418 | metavar='<auth-password>', |
| 419 | default=os.environ.get('OS_PASSWORD'), |
| 420 | help='Defaults to env[OS_PASSWORD].') |
| 421 | parser.add_argument('--os-tenant-name', |
| 422 | metavar='<auth-tenant-name>', |
| 423 | default=os.environ.get('OS_TENANT_NAME'), |
| 424 | help='Defaults to env[OS_TENANT_NAME].') |
| 425 | parser.add_argument('--tag', |
| 426 | default='', |
| 427 | required=False, |
| 428 | dest='tag', |
| 429 | help='Resources tag') |
| 430 | parser.add_argument('-r', '--concurrency', |
| 431 | default=1, |
| 432 | type=int, |
| 433 | required=True, |
| 434 | dest='concurrency', |
| 435 | help='Concurrency count') |
| 436 | parser.add_argument('--with-admin', |
| 437 | action='store_true', |
| 438 | dest='admin', |
Jane Zadorozhna | 00fc3dc | 2015-05-27 18:01:56 +0300 | [diff] [blame] | 439 | help='Creates admin for each concurrent group') |
sslypushenko | 0de7d05 | 2015-04-16 18:49:55 +0300 | [diff] [blame] | 440 | parser.add_argument('accounts', |
| 441 | metavar='accounts_file.yaml', |
| 442 | help='Output accounts yaml file') |
| 443 | |
David Paterson | 68b8b9d | 2015-12-01 15:44:14 -0800 | [diff] [blame] | 444 | |
| 445 | def get_options(): |
| 446 | usage_string = ('tempest-account-generator [-h] <ARG> ...\n\n' |
| 447 | 'To see help on specific argument, do:\n' |
| 448 | 'tempest-account-generator <ARG> -h') |
| 449 | parser = argparse.ArgumentParser( |
| 450 | description=DESCRIPTION, |
| 451 | formatter_class=argparse.ArgumentDefaultsHelpFormatter, |
| 452 | usage=usage_string |
| 453 | ) |
| 454 | |
| 455 | _parser_add_args(parser) |
sslypushenko | 0de7d05 | 2015-04-16 18:49:55 +0300 | [diff] [blame] | 456 | opts = parser.parse_args() |
sslypushenko | 0de7d05 | 2015-04-16 18:49:55 +0300 | [diff] [blame] | 457 | return opts |
| 458 | |
| 459 | |
David Paterson | 68b8b9d | 2015-12-01 15:44:14 -0800 | [diff] [blame] | 460 | class TempestAccountGenerator(command.Command): |
| 461 | |
| 462 | def get_parser(self, prog_name): |
| 463 | parser = super(TempestAccountGenerator, self).get_parser(prog_name) |
| 464 | _parser_add_args(parser) |
| 465 | return parser |
| 466 | |
| 467 | def take_action(self, parsed_args): |
| 468 | try: |
| 469 | return main(parsed_args) |
| 470 | except Exception: |
| 471 | LOG.exception("Failure generating test accounts.") |
| 472 | traceback.print_exc() |
| 473 | raise |
| 474 | return 0 |
| 475 | |
| 476 | def get_description(self): |
| 477 | return DESCRIPTION |
| 478 | |
| 479 | |
sslypushenko | 0de7d05 | 2015-04-16 18:49:55 +0300 | [diff] [blame] | 480 | def main(opts=None): |
sslypushenko | 0de7d05 | 2015-04-16 18:49:55 +0300 | [diff] [blame] | 481 | setup_logging() |
David Paterson | 68b8b9d | 2015-12-01 15:44:14 -0800 | [diff] [blame] | 482 | if not opts: |
| 483 | LOG.warn("Use of: 'tempest-account-generator' is deprecated, " |
| 484 | "please use: 'tempest account-generator'") |
| 485 | opts = get_options() |
| 486 | if opts.config_file: |
| 487 | config.CONF.set_config_path(opts.config_file) |
sslypushenko | 0de7d05 | 2015-04-16 18:49:55 +0300 | [diff] [blame] | 488 | resources = generate_resources(opts) |
| 489 | create_resources(opts, resources) |
| 490 | dump_accounts(opts, resources) |
| 491 | |
| 492 | if __name__ == "__main__": |
| 493 | main() |