1 ##############################################################################
2 # Copyright (c) 2016 Huawei Technologies Co.,Ltd and others.
4 # All rights reserved. This program and the accompanying materials
5 # are made available under the terms of the Apache License, Version 2.0
6 # which accompanies this distribution, and is available at
7 # http://www.apache.org/licenses/LICENSE-2.0
8 ##############################################################################
10 from __future__ import absolute_import
16 from keystoneauth1 import loading
17 from keystoneauth1 import session
18 from cinderclient import client as cinderclient
19 from novaclient import client as novaclient
20 from glanceclient import client as glanceclient
21 from neutronclient.neutron import client as neutronclient
23 log = logging.getLogger(__name__)
25 DEFAULT_HEAT_API_VERSION = '1'
26 DEFAULT_API_VERSION = '2'
29 # *********************************************
31 # *********************************************
32 def get_credentials():
33 """Returns a creds dictionary filled with parsed from env"""
36 keystone_api_version = os.getenv('OS_IDENTITY_API_VERSION')
38 if keystone_api_version is None or keystone_api_version == '2':
40 tenant_env = 'OS_TENANT_NAME'
41 tenant = 'tenant_name'
44 tenant_env = 'OS_PROJECT_NAME'
45 tenant = 'project_name'
47 # The most common way to pass these info to the script is to do it
48 # through environment variables.
50 "username": os.environ.get("OS_USERNAME"),
51 "password": os.environ.get("OS_PASSWORD"),
52 "auth_url": os.environ.get("OS_AUTH_URL"),
53 tenant: os.environ.get(tenant_env)
57 if os.getenv('OS_USER_DOMAIN_NAME') is not None:
59 "user_domain_name": os.getenv('OS_USER_DOMAIN_NAME')
61 if os.getenv('OS_PROJECT_DOMAIN_NAME') is not None:
63 "project_domain_name": os.getenv('OS_PROJECT_DOMAIN_NAME')
69 def get_session_auth():
70 loader = loading.get_plugin_loader('password')
71 creds = get_credentials()
72 auth = loader.load_from_options(**creds)
77 auth = get_session_auth()
79 cacert = os.environ['OS_CACERT']
81 return session.Session(auth=auth)
83 insecure = os.getenv('OS_INSECURE', '').lower() == 'true'
84 cacert = False if insecure else cacert
85 return session.Session(auth=auth, verify=cacert)
88 def get_endpoint(service_type, endpoint_type='publicURL'):
89 auth = get_session_auth()
90 # for multi-region, we need to specify region
91 # when finding the endpoint
92 return get_session().get_endpoint(auth=auth,
93 service_type=service_type,
94 endpoint_type=endpoint_type,
95 region_name=os.environ.get(
99 # *********************************************
101 # *********************************************
102 def get_heat_api_version(): # pragma: no cover
104 api_version = os.environ['HEAT_API_VERSION']
106 return DEFAULT_HEAT_API_VERSION
108 log.info("HEAT_API_VERSION is set in env as '%s'", api_version)
112 def get_cinder_client_version(): # pragma: no cover
114 api_version = os.environ['OS_VOLUME_API_VERSION']
116 return DEFAULT_API_VERSION
118 log.info("OS_VOLUME_API_VERSION is set in env as '%s'", api_version)
122 def get_cinder_client(): # pragma: no cover
124 return cinderclient.Client(get_cinder_client_version(), session=sess)
127 def get_nova_client_version(): # pragma: no cover
129 api_version = os.environ['OS_COMPUTE_API_VERSION']
131 return DEFAULT_API_VERSION
133 log.info("OS_COMPUTE_API_VERSION is set in env as '%s'", api_version)
137 def get_nova_client(): # pragma: no cover
139 return novaclient.Client(get_nova_client_version(), session=sess)
142 def get_neutron_client_version(): # pragma: no cover
144 api_version = os.environ['OS_NETWORK_API_VERSION']
146 return DEFAULT_API_VERSION
148 log.info("OS_NETWORK_API_VERSION is set in env as '%s'", api_version)
152 def get_neutron_client(): # pragma: no cover
154 return neutronclient.Client(get_neutron_client_version(), session=sess)
157 def get_glance_client_version(): # pragma: no cover
159 api_version = os.environ['OS_IMAGE_API_VERSION']
161 return DEFAULT_API_VERSION
163 log.info("OS_IMAGE_API_VERSION is set in env as '%s'", api_version)
167 def get_glance_client(): # pragma: no cover
169 return glanceclient.Client(get_glance_client_version(), session=sess)
172 # *********************************************
174 # *********************************************
175 def get_instances(nova_client): # pragma: no cover
177 return nova_client.servers.list(search_opts={'all_tenants': 1})
179 log.exception("Error [get_instances(nova_client)]")
182 def get_instance_status(nova_client, instance): # pragma: no cover
184 return nova_client.servers.get(instance.id).status
186 log.exception("Error [get_instance_status(nova_client)]")
189 def get_instance_by_name(nova_client, instance_name): # pragma: no cover
191 return nova_client.servers.find(name=instance_name)
193 log.exception("Error [get_instance_by_name(nova_client, '%s')]",
197 def get_aggregates(nova_client): # pragma: no cover
199 return nova_client.aggregates.list()
201 log.exception("Error [get_aggregates(nova_client)]")
204 def get_availability_zones(nova_client): # pragma: no cover
206 return nova_client.availability_zones.list()
208 log.exception("Error [get_availability_zones(nova_client)]")
211 def get_availability_zone_names(nova_client): # pragma: no cover
213 return [az.zoneName for az in get_availability_zones(nova_client)]
215 log.exception("Error [get_availability_zone_names(nova_client)]")
218 def create_aggregate(nova_client, aggregate_name, av_zone): # pragma: no cover
220 nova_client.aggregates.create(aggregate_name, av_zone)
222 log.exception("Error [create_aggregate(nova_client, %s, %s)]",
223 aggregate_name, av_zone)
229 def get_aggregate_id(nova_client, aggregate_name): # pragma: no cover
231 aggregates = get_aggregates(nova_client)
232 _id = next((ag.id for ag in aggregates if ag.name == aggregate_name))
234 log.exception("Error [get_aggregate_id(nova_client, %s)]",
240 def add_host_to_aggregate(nova_client, aggregate_name,
241 compute_host): # pragma: no cover
243 aggregate_id = get_aggregate_id(nova_client, aggregate_name)
244 nova_client.aggregates.add_host(aggregate_id, compute_host)
246 log.exception("Error [add_host_to_aggregate(nova_client, %s, %s)]",
247 aggregate_name, compute_host)
253 def create_aggregate_with_host(nova_client, aggregate_name, av_zone,
254 compute_host): # pragma: no cover
256 create_aggregate(nova_client, aggregate_name, av_zone)
257 add_host_to_aggregate(nova_client, aggregate_name, compute_host)
259 log.exception("Error [create_aggregate_with_host("
260 "nova_client, %s, %s, %s)]",
261 aggregate_name, av_zone, compute_host)
267 def create_instance(json_body): # pragma: no cover
269 return get_nova_client().servers.create(**json_body)
271 log.exception("Error create instance failed")
275 def create_instance_and_wait_for_active(json_body): # pragma: no cover
277 VM_BOOT_TIMEOUT = 180
278 nova_client = get_nova_client()
279 instance = create_instance(json_body)
280 count = VM_BOOT_TIMEOUT / SLEEP
281 for n in range(count, -1, -1):
282 status = get_instance_status(nova_client, instance)
283 if status.lower() == "active":
285 elif status.lower() == "error":
286 log.error("The instance went to ERROR status.")
289 log.error("Timeout booting the instance.")
293 def delete_instance(nova_client, instance_id): # pragma: no cover
295 nova_client.servers.force_delete(instance_id)
297 log.exception("Error [delete_instance(nova_client, '%s')]",
304 def remove_host_from_aggregate(nova_client, aggregate_name,
305 compute_host): # pragma: no cover
307 aggregate_id = get_aggregate_id(nova_client, aggregate_name)
308 nova_client.aggregates.remove_host(aggregate_id, compute_host)
310 log.exception("Error remove_host_from_aggregate(nova_client, %s, %s)",
311 aggregate_name, compute_host)
317 def remove_hosts_from_aggregate(nova_client,
318 aggregate_name): # pragma: no cover
319 aggregate_id = get_aggregate_id(nova_client, aggregate_name)
320 hosts = nova_client.aggregates.get(aggregate_id).hosts
322 all(remove_host_from_aggregate(nova_client, aggregate_name, host)
326 def delete_aggregate(nova_client, aggregate_name): # pragma: no cover
328 remove_hosts_from_aggregate(nova_client, aggregate_name)
329 nova_client.aggregates.delete(aggregate_name)
331 log.exception("Error [delete_aggregate(nova_client, %s)]",
338 def get_server_by_name(name): # pragma: no cover
340 return get_nova_client().servers.list(search_opts={'name': name})[0]
342 log.exception('Failed to get nova client')
346 def create_flavor(name, ram, vcpus, disk, **kwargs): # pragma: no cover
348 return get_nova_client().flavors.create(name, ram, vcpus, disk, **kwargs)
350 log.exception("Error [create_flavor(nova_client, %s, %s, %s, %s, %s)]",
351 name, ram, disk, vcpus, kwargs['is_public'])
355 def get_image_by_name(name): # pragma: no cover
356 images = get_nova_client().images.list()
358 return next((a for a in images if a.name == name))
359 except StopIteration:
360 log.exception('No image matched')
363 def get_flavor_id(nova_client, flavor_name): # pragma: no cover
364 flavors = nova_client.flavors.list(detailed=True)
367 if f.name == flavor_name:
373 def get_flavor_by_name(name): # pragma: no cover
374 flavors = get_nova_client().flavors.list()
376 return next((a for a in flavors if a.name == name))
377 except StopIteration:
378 log.exception('No flavor matched')
381 def check_status(status, name, iterations, interval): # pragma: no cover
382 for i in range(iterations):
384 server = get_server_by_name(name)
386 log.error('Cannot found %s server', name)
389 if server.status == status:
396 def delete_flavor(flavor_id): # pragma: no cover
398 get_nova_client().flavors.delete(flavor_id)
400 log.exception("Error [delete_flavor(nova_client, %s)]", flavor_id)
406 # *********************************************
408 # *********************************************
409 def get_network_id(neutron_client, network_name): # pragma: no cover
410 networks = neutron_client.list_networks()['networks']
411 return next((n['id'] for n in networks if n['name'] == network_name), None)
414 def get_port_id_by_ip(neutron_client, ip_address): # pragma: no cover
415 ports = neutron_client.list_ports()['ports']
416 return next((i['id'] for i in ports for j in i.get(
417 'fixed_ips') if j['ip_address'] == ip_address), None)
420 # *********************************************
422 # *********************************************
423 def get_image_id(glance_client, image_name): # pragma: no cover
424 images = glance_client.images.list()
425 return next((i.id for i in images if i.name == image_name), None)
428 # *********************************************
430 # *********************************************
431 def get_volume_id(volume_name): # pragma: no cover
432 volumes = get_cinder_client().volumes.list()
433 return next((v.id for v in volumes if v.name == volume_name), None)