File: prep_cloud.py

package info (click to toggle)
refstack-client 0.0.0~2023.09.19.b60a7e41f7-2
  • links: PTS, VCS
  • area: main
  • in suites: sid, trixie
  • size: 592 kB
  • sloc: python: 1,998; sh: 220; makefile: 25
file content (397 lines) | stat: -rw-r--r-- 15,122 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
# Copyright 2012 OpenStack Foundation
# All Rights Reserved.
#
#    Licensed under the Apache License, Version 2.0 (the "License"); you may
#    not use this file except in compliance with the License. You may obtain
#    a copy of the License at
#
#         http://www.apache.org/licenses/LICENSE-2.0
#
#    Unless required by applicable law or agreed to in writing, software
#    distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
#    WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
#    License for the specific language governing permissions and limitations
#    under the License.
#
# This script aims to configure an initial OpenStack environment with all the
# necessary configurations for tempest's run using nothing but OpenStack's
# native API.
# That includes, creating users, tenants, registering images (cirros),
# configuring neutron and so on.
#
# ASSUMPTION: this script is run by an admin user as it is meant to configure
# the OpenStack environment prior to actual use.

# Config
import ConfigParser
import os
import tarfile

from six.moves.urllib import request as urlreq

# Default client libs
import glanceclient as glance_client
import keystoneclient.v2_0.client as keystone_client

# Import OpenStack exceptions
import glanceclient.exc as glance_exception
import keystoneclient.exceptions as keystone_exception


TEMPEST_TEMP_DIR = os.getenv("TEMPEST_TEMP_DIR", "/tmp").rstrip('/')
TEMPEST_ROOT_DIR = os.getenv("TEMPEST_ROOT_DIR", os.getenv("HOME")).rstrip('/')

# Environment variables override defaults
TEMPEST_CONFIG_DIR = os.getenv("TEMPEST_CONFIG_DIR",
                               "%s%s" % (TEMPEST_ROOT_DIR, "/etc")).rstrip('/')
TEMPEST_CONFIG_FILE = os.getenv("TEMPEST_CONFIG_FILE",
                                "%s%s" % (TEMPEST_CONFIG_DIR, "/tempest.conf"))
TEMPEST_CONFIG_SAMPLE = os.getenv("TEMPEST_CONFIG_SAMPLE",
                                  "%s%s" % (TEMPEST_CONFIG_DIR,
                                            "/tempest.conf.sample"))
# Image references
IMAGE_DOWNLOAD_CHUNK_SIZE = 8 * 1024
IMAGE_UEC_SOURCE_URL = os.getenv("IMAGE_UEC_SOURCE_URL",
                                 "http://download.cirros-cloud.net/0.3.1/"
                                 "cirros-0.3.1-x86_64-uec.tar.gz")
TEMPEST_IMAGE_ID = os.getenv('IMAGE_ID')
TEMPEST_IMAGE_ID_ALT = os.getenv('IMAGE_ID_ALT')
IMAGE_STATUS_ACTIVE = 'active'


class ClientManager(object):
    """
    Manager that provides access to the official python clients for
    calling various OpenStack APIs.
    """
    def __init__(self):
        self.identity_client = None
        self.image_client = None
        self.network_client = None
        self.compute_client = None
        self.volume_client = None

    def get_identity_client(self, **kwargs):
        """
        Returns the openstack identity python client
        :param username: a string representing the username
        :param password: a string representing the user's password
        :param tenant_name: a string representing the tenant name of the user
        :param auth_url: a string representing the auth url of the identity
        :param insecure: True if we wish to disable ssl certificate validation,
        False otherwise
        :returns an instance of openstack identity python client
        """
        if not self.identity_client:
            self.identity_client = keystone_client.Client(**kwargs)

        return self.identity_client

    def get_image_client(self, version="1", *args, **kwargs):
        """
        This method returns OpenStack glance python client
        :param version: a string representing the version of the glance client
        to use.
        :param string endpoint: A user-supplied endpoint URL for the glance
                            service.
        :param string token: Token for authentication.
        :param integer timeout: Allows customization of the timeout for client
                                http requests. (optional)
        :return: a Client object representing the glance client
        """
        if not self.image_client:
            self.image_client = glance_client.Client(version, *args, **kwargs)

        return self.image_client


def get_tempest_config(path_to_config):
    """
    Gets the tempest configuration file as a ConfigParser object
    :param path_to_config: path to the config file
    :return: a ConfigParser object representing the tempest configuration file
    """
    # get the sample config file from the sample
    config = ConfigParser.ConfigParser()
    config.readfp(open(path_to_config))

    return config


def update_config_admin_credentials(config, config_section):
    """
    Updates the tempest config with the admin credentials
    :param config: a ConfigParser object representing the tempest config file
    :param config_section: the section name where the admin credentials are
    """
    # Check if credentials are present, default uses the config credentials
    OS_USERNAME = os.getenv('OS_USERNAME',
                            config.get(config_section, "admin_username"))
    OS_PASSWORD = os.getenv('OS_PASSWORD',
                            config.get(config_section, "admin_password"))
    OS_TENANT_NAME = os.getenv('OS_TENANT_NAME',
                               config.get(config_section, "admin_tenant_name"))
    OS_AUTH_URL = os.getenv('OS_AUTH_URL', config.get(config_section, "uri"))

    if not (OS_AUTH_URL and
            OS_USERNAME and
            OS_PASSWORD and
            OS_TENANT_NAME):
        raise Exception("Admin environment variables not found.")

    # TODO(tkammer): Add support for uri_v3
    config_identity_params = {'uri': OS_AUTH_URL,
                              'admin_username': OS_USERNAME,
                              'admin_password': OS_PASSWORD,
                              'admin_tenant_name': OS_TENANT_NAME}

    update_config_section_with_params(config,
                                      config_section,
                                      config_identity_params)


def update_config_section_with_params(config, config_section, params):
    """
    Updates a given config object with given params
    :param config: a ConfigParser object representing the tempest config file
    :param config_section: the section we would like to update
    :param params: the parameters we wish to update for that section
    """
    for option, value in params.items():
        config.set(config_section, option, value)


def get_identity_client_kwargs(config, config_section):
    """
    Get the required arguments for the identity python client
    :param config: a ConfigParser object representing the tempest config file
    :param config_section: the section name in the configuration where the
    arguments can be found
    :return: a dictionary representing the needed arguments for the identity
    client
    """
    username = config.get(config_section, 'admin_username')
    password = config.get(config_section, 'admin_password')
    tenant_name = config.get(config_section, 'admin_tenant_name')
    auth_url = config.get(config_section, 'uri')
    dscv = config.get(config_section, 'disable_ssl_certificate_validation')
    kwargs = {'username': username,
              'password': password,
              'tenant_name': tenant_name,
              'auth_url': auth_url,
              'insecure': dscv}

    return kwargs


def create_user_with_tenant(identity_client, username, password, tenant_name):
    """
    Creates a user using a given identity client
    :param identity_client: openstack identity python client
    :param username: a string representing the username
    :param password: a string representing the user's password
    :param tenant_name: a string representing the tenant name of the user
    """
    # Try to create the necessary tenant
    tenant_id = None
    try:
        tenant_description = "Tenant for Tempest %s user" % username
        tenant = identity_client.tenants.create(tenant_name,
                                                tenant_description)
        tenant_id = tenant.id
    except keystone_exception.Conflict:

        # if already exist, use existing tenant
        tenant_list = identity_client.tenants.list()
        for tenant in tenant_list:
            if tenant.name == tenant_name:
                tenant_id = tenant.id

    # Try to create the user
    try:
        email = "%s@test.com" % username
        identity_client.users.create(name=username,
                                     password=password,
                                     email=email,
                                     tenant_id=tenant_id)
    except keystone_exception.Conflict:

        # if already exist, use existing user
        pass


def create_users_and_tenants(identity_client,
                             config,
                             config_section):
    """
    Creates the two non admin users and tenants for tempest
    :param identity_client: openstack identity python client
    :param config: a ConfigParser object representing the tempest config file
    :param config_section: the section name of identity in the config
    """
    # Get the necessary params from the config file
    tenant_name = config.get(config_section, 'tenant_name')
    username = config.get(config_section, 'username')
    password = config.get(config_section, 'password')

    alt_tenant_name = config.get(config_section, 'alt_tenant_name')
    alt_username = config.get(config_section, 'alt_username')
    alt_password = config.get(config_section, 'alt_password')

    # Create the necessary users for the test runs
    create_user_with_tenant(identity_client, username, password, tenant_name)
    create_user_with_tenant(identity_client, alt_username, alt_password,
                            alt_tenant_name)


def get_image_client_kwargs(identity_client, config, config_section):
    """
    Get the required arguments for the image python client
    :param identity_client: openstack identity python client
    :param config: a ConfigParser object representing the tempest config file
    :param config_section: the section name of identity in the config
    :return: a dictionary representing the needed arguments for the image
    client
    """

    token = identity_client.auth_token
    endpoint = identity_client.\
        service_catalog.url_for(service_type='image', endpoint_type='publicURL'
                                )
    dscv = config.get(config_section, 'disable_ssl_certificate_validation')
    kwargs = {'endpoint': endpoint,
              'token': token,
              'insecure': dscv}

    return kwargs


def images_exist(image_client):
    """
    Checks whether the images ID's located in the environment variable are
    indeed registered
    :param image_client: the openstack python client representing the image
    client
    """
    exist = True
    if not TEMPEST_IMAGE_ID or not TEMPEST_IMAGE_ID_ALT:
        exist = False
    else:
        try:
            image_client.images.get(TEMPEST_IMAGE_ID)
            image_client.images.get(TEMPEST_IMAGE_ID_ALT)
        except glance_exception.HTTPNotFound:
            exist = False

    return exist


def download_and_register_uec_images(image_client, download_url,
                                     download_folder):
    """
    Downloads and registered the UEC AKI/AMI/ARI images
    :param image_client:
    :param download_url: the url of the uec tar file
    :param download_folder: the destination folder we wish to save the file to
    """
    basename = os.path.basename(download_url)
    path = os.path.join(download_folder, basename)

    request = urlreq.urlopen(download_url)

    # First, download the file
    with open(path, "wb") as fp:
        while True:
            chunk = request.read(IMAGE_DOWNLOAD_CHUNK_SIZE)
            if not chunk:
                break

            fp.write(chunk)

    # Then extract and register images
    tar = tarfile.open(path, "r")
    for name in tar.getnames():
        file_obj = tar.extractfile(name)
        format = "aki"

        if file_obj.name.endswith(".img"):
            format = "ami"

        if file_obj.name.endswith("initrd"):
            format = "ari"

        # Register images in image client
        image_client.images.create(name=file_obj.name, disk_format=format,
                                   container_format=format, data=file_obj,
                                   is_public="true")

    tar.close()


def create_images(image_client, config, config_section,
                  download_url=IMAGE_UEC_SOURCE_URL,
                  download_folder=TEMPEST_TEMP_DIR):
    """
    Creates images for tempest's use and registers the environment variables
    IMAGE_ID and IMAGE_ID_ALT with registered images
    :param image_client: OpenStack python image client
    :param config: a ConfigParser object representing the tempest config file
    :param config_section: the section name where the IMAGE ids are set
    :param download_url: the URL from which we should download the UEC tar
    :param download_folder: the place where we want to save the download file
    """
    if not images_exist(image_client):
        # Falls down to the default uec images
        download_and_register_uec_images(image_client, download_url,
                                         download_folder)
        image_ids = []
        for image in image_client.images.list():
            image_ids.append(image.id)

        os.environ["IMAGE_ID"] = image_ids[0]
        os.environ["IMAGE_ID_ALT"] = image_ids[1]

    params = {'image_ref': os.getenv("IMAGE_ID"),
              'image_ref_alt': os.getenv("IMAGE_ID_ALT")}

    update_config_section_with_params(config, config_section, params)


def main():
    """
    Main module to control the script
    """
    # Check if config file exists or fall to the default sample otherwise
    path_to_config = TEMPEST_CONFIG_SAMPLE

    if os.path.isfile(TEMPEST_CONFIG_FILE):
        path_to_config = TEMPEST_CONFIG_FILE

    config = get_tempest_config(path_to_config)
    update_config_admin_credentials(config, 'identity')

    client_manager = ClientManager()

    # Set the identity related info for tempest
    identity_client_kwargs = get_identity_client_kwargs(config,
                                                        'identity')
    identity_client = client_manager.get_identity_client(
        **identity_client_kwargs)

    # Create the necessary users and tenants for tempest run
    create_users_and_tenants(identity_client, config, 'identity')

    # Set the image related info for tempest
    image_client_kwargs = get_image_client_kwargs(identity_client,
                                                  config,
                                                  'identity')
    image_client = client_manager.get_image_client(**image_client_kwargs)

    # Create the necessary users and tenants for tempest run
    create_images(image_client, config, 'compute')

    # TODO(tkammer): add network implementation


if __name__ == "__main__":
    main()