3 # jose.lausuch@ericsson.com
4 # valentin.boucher@orange.com
5 # All rights reserved. This program and the accompanying materials
6 # are made available under the terms of the Apache License, Version 2.0
7 # which accompanies this distribution, and is available at
8 # http://www.apache.org/licenses/LICENSE-2.0
17 from cinderclient import client as cinderclient
18 import functest.utils.functest_logger as ft_logger
19 from glanceclient import client as glanceclient
20 from keystoneclient.v2_0 import client as keystoneclient
21 from neutronclient.v2_0 import client as neutronclient
22 from novaclient import client as novaclient
24 logger = ft_logger.Logger("openstack_utils").getLogger()
27 # *********************************************
29 # *********************************************
30 def check_credentials():
32 Check if the OpenStack credentials (openrc) are sourced
34 env_vars = ['OS_AUTH_URL', 'OS_USERNAME', 'OS_PASSWORD', 'OS_TENANT_NAME']
35 return all(map(lambda v: v in os.environ and os.environ[v], env_vars))
38 def get_credentials(service):
39 """Returns a creds dictionary filled with the following keys:
41 * password/api_key (depending on the service)
42 * tenant_name/project_id (depending on the service)
44 :param service: a string indicating the name of the service
45 requesting the credentials.
49 # Check that the env vars exists:
50 envvars = ('OS_USERNAME', 'OS_PASSWORD', 'OS_AUTH_URL', 'OS_TENANT_NAME')
51 for envvar in envvars:
52 if os.getenv(envvar) is None:
53 logger.error("'%s' is not exported as an env variable." % envvar)
56 # Unfortunately, each of the OpenStack client will request slightly
57 # different entries in their credentials dict.
58 if service.lower() in ("nova", "cinder"):
63 tenant = "tenant_name"
65 # The most common way to pass these info to the script is to do it through
66 # environment variables.
68 "username": os.environ.get("OS_USERNAME"),
69 password: os.environ.get("OS_PASSWORD"),
70 "auth_url": os.environ.get("OS_AUTH_URL"),
71 tenant: os.environ.get("OS_TENANT_NAME")
73 cacert = os.environ.get("OS_CACERT")
74 if cacert is not None:
75 # each openstack client uses differnt kwargs for this
76 creds.update({"cacert": cacert,
78 "https_ca_cert": cacert,
79 "https_cacert": cacert,
81 creds.update({"insecure": "True", "https_insecure": "True"})
82 if not os.path.isfile(cacert):
83 logger.info("WARNING: The 'OS_CACERT' environment variable is "
84 "set to %s but the file does not exist." % cacert)
88 def source_credentials(rc_file):
89 pipe = subprocess.Popen(". %s; env" % rc_file, stdout=subprocess.PIPE,
91 output = pipe.communicate()[0]
92 env = dict((line.split("=", 1) for line in output.splitlines()))
93 os.environ.update(env)
97 # *********************************************
99 # *********************************************
100 def get_keystone_client():
101 creds_keystone = get_credentials("keystone")
102 return keystoneclient.Client(**creds_keystone)
105 def get_nova_client():
106 creds_nova = get_credentials("nova")
107 return novaclient.Client('2', **creds_nova)
110 def get_cinder_client():
111 creds_cinder = get_credentials("cinder")
112 return cinderclient.Client('2', creds_cinder['username'],
113 creds_cinder['api_key'],
114 creds_cinder['project_id'],
115 creds_cinder['auth_url'],
116 service_type="volume")
119 def get_neutron_client():
120 creds_neutron = get_credentials("neutron")
121 return neutronclient.Client(**creds_neutron)
124 def get_glance_client():
125 keystone_client = get_keystone_client()
126 glance_endpoint = keystone_client.service_catalog.url_for(
127 service_type='image', endpoint_type='publicURL')
128 return glanceclient.Client(1, glance_endpoint,
129 token=keystone_client.auth_token)
131 # *********************************************
133 # *********************************************
136 def get_instances(nova_client):
138 instances = nova_client.servers.list(search_opts={'all_tenants': 1})
141 logger.error("Error [get_instances(nova_client)]: %s" % e)
145 def get_instance_status(nova_client, instance):
147 instance = nova_client.servers.get(instance.id)
148 return instance.status
150 # logger.error("Error [get_instance_status(nova_client, '%s')]:" %
155 def get_instance_by_name(nova_client, instance_name):
157 instance = nova_client.servers.find(name=instance_name)
160 logger.error("Error [get_instance_by_name(nova_client, '%s')]: %s"
161 % (instance_name, e))
165 def get_flavor_id(nova_client, flavor_name):
166 flavors = nova_client.flavors.list(detailed=True)
169 if f.name == flavor_name:
175 def get_flavor_id_by_ram_range(nova_client, min_ram, max_ram):
176 flavors = nova_client.flavors.list(detailed=True)
179 if min_ram <= f.ram and f.ram <= max_ram:
185 def get_floating_ips(nova_client):
187 floating_ips = nova_client.floating_ips.list()
190 logger.error("Error [get_floating_ips(nova_client)]: %s" % e)
194 def get_hypervisors(nova_client):
197 hypervisors = nova_client.hypervisors.list()
198 for hypervisor in hypervisors:
199 if hypervisor.state == "up":
200 nodes.append(hypervisor.hypervisor_hostname)
203 logger.error("Error [get_hypervisors(nova_client)]: %s" % e)
207 def create_flavor(nova_client, flavor_name, ram, disk, vcpus):
209 flavor = nova_client.flavors.create(flavor_name, ram, vcpus, disk)
211 logger.error("Error [create_flavor(nova_client, '%s', '%s', '%s', "
212 "'%s')]: %s" % (flavor_name, ram, disk, vcpus, e))
217 def create_instance(flavor_name,
220 instance_name="functest-vm",
226 nova_client = get_nova_client()
228 flavor = nova_client.flavors.find(name=flavor_name)
230 flavors = nova_client.flavors.list()
231 logger.error("Error: Flavor '%s' not found. Available flavors are: "
232 "\n%s" % (flavor_name, flavors))
234 if fixed_ip is not None:
235 nics = {"net-id": network_id, "v4-fixed-ip": fixed_ip}
237 nics = {"net-id": network_id}
239 instance = nova_client.servers.create(
244 availability_zone=av_zone,
248 instance = nova_client.servers.create(
253 config_drive=confdrive,
255 availability_zone=av_zone,
261 def create_instance_and_wait_for_active(flavor_name,
271 VM_BOOT_TIMEOUT = 180
272 nova_client = get_nova_client()
273 instance = create_instance(flavor_name,
282 count = VM_BOOT_TIMEOUT / SLEEP
283 for n in range(count, -1, -1):
284 status = get_instance_status(nova_client, instance)
285 if status.lower() == "active":
287 elif status.lower() == "error":
288 logger.error("The instance %s went to ERROR status."
292 logger.error("Timeout booting the instance %s." % instance_name)
296 def create_floating_ip(neutron_client):
297 extnet_id = get_external_net_id(neutron_client)
298 props = {'floating_network_id': extnet_id}
300 ip_json = neutron_client.create_floatingip({'floatingip': props})
301 fip_addr = ip_json['floatingip']['floating_ip_address']
302 fip_id = ip_json['floatingip']['id']
304 logger.error("Error [create_floating_ip(neutron_client)]: %s" % e)
306 return {'fip_addr': fip_addr, 'fip_id': fip_id}
309 def add_floating_ip(nova_client, server_id, floatingip_id):
311 nova_client.servers.add_floating_ip(server_id, floatingip_id)
314 logger.error("Error [add_floating_ip(nova_client, '%s', '%s')]: %s"
315 % (server_id, floatingip_id, e))
319 def delete_instance(nova_client, instance_id):
321 nova_client.servers.force_delete(instance_id)
324 logger.error("Error [delete_instance(nova_client, '%s')]: %s"
329 def delete_floating_ip(nova_client, floatingip_id):
331 nova_client.floating_ips.delete(floatingip_id)
334 logger.error("Error [delete_floating_ip(nova_client, '%s')]:"
335 % (floatingip_id, e))
339 # *********************************************
341 # *********************************************
342 def get_network_list(neutron_client):
343 network_list = neutron_client.list_networks()['networks']
344 if len(network_list) == 0:
350 def get_router_list(neutron_client):
351 router_list = neutron_client.list_routers()['routers']
352 if len(router_list) == 0:
358 def get_port_list(neutron_client):
359 port_list = neutron_client.list_ports()['ports']
360 if len(port_list) == 0:
366 def get_network_id(neutron_client, network_name):
367 networks = neutron_client.list_networks()['networks']
370 if n['name'] == network_name:
376 def get_subnet_id(neutron_client, subnet_name):
377 subnets = neutron_client.list_subnets()['subnets']
380 if s['name'] == subnet_name:
386 def get_router_id(neutron_client, router_name):
387 routers = neutron_client.list_routers()['routers']
390 if r['name'] == router_name:
396 def get_private_net(neutron_client):
397 # Checks if there is an existing shared private network
398 networks = neutron_client.list_networks()['networks']
399 if len(networks) == 0:
402 if (net['router:external'] is False) and (net['shared'] is True):
407 def get_external_net(neutron_client):
408 for network in neutron_client.list_networks()['networks']:
409 if network['router:external']:
410 return network['name']
414 def get_external_net_id(neutron_client):
415 for network in neutron_client.list_networks()['networks']:
416 if network['router:external']:
421 def check_neutron_net(neutron_client, net_name):
422 for network in neutron_client.list_networks()['networks']:
423 if network['name'] == net_name:
424 for subnet in network['subnets']:
429 def create_neutron_net(neutron_client, name):
430 json_body = {'network': {'name': name,
431 'admin_state_up': True}}
433 network = neutron_client.create_network(body=json_body)
434 network_dict = network['network']
435 return network_dict['id']
437 logger.error("Error [create_neutron_net(neutron_client, '%s')]: %s"
442 def create_neutron_subnet(neutron_client, name, cidr, net_id):
443 json_body = {'subnets': [{'name': name, 'cidr': cidr,
444 'ip_version': 4, 'network_id': net_id}]}
446 subnet = neutron_client.create_subnet(body=json_body)
447 return subnet['subnets'][0]['id']
449 logger.error("Error [create_neutron_subnet(neutron_client, '%s', "
450 "'%s', '%s')]: %s" % (name, cidr, net_id, e))
454 def create_neutron_router(neutron_client, name):
455 json_body = {'router': {'name': name, 'admin_state_up': True}}
457 router = neutron_client.create_router(json_body)
458 return router['router']['id']
460 logger.error("Error [create_neutron_router(neutron_client, '%s')]: %s"
465 def create_neutron_port(neutron_client, name, network_id, ip):
466 json_body = {'port': {
467 'admin_state_up': True,
469 'network_id': network_id,
470 'fixed_ips': [{"ip_address": ip}]
473 port = neutron_client.create_port(body=json_body)
474 return port['port']['id']
476 logger.error("Error [create_neutron_port(neutron_client, '%s', '%s', "
477 "'%s')]: %s" % (name, network_id, ip, e))
481 def update_neutron_net(neutron_client, network_id, shared=False):
482 json_body = {'network': {'shared': shared}}
484 neutron_client.update_network(network_id, body=json_body)
487 logger.error("Error [update_neutron_net(neutron_client, '%s', '%s')]: "
488 "%s" % (network_id, str(shared), e))
492 def update_neutron_port(neutron_client, port_id, device_owner):
493 json_body = {'port': {
494 'device_owner': device_owner,
497 port = neutron_client.update_port(port=port_id,
499 return port['port']['id']
501 logger.error("Error [update_neutron_port(neutron_client, '%s', '%s')]:"
502 " %s" % (port_id, device_owner, e))
506 def add_interface_router(neutron_client, router_id, subnet_id):
507 json_body = {"subnet_id": subnet_id}
509 neutron_client.add_interface_router(router=router_id, body=json_body)
512 logger.error("Error [add_interface_router(neutron_client, '%s', "
513 "'%s')]: %s" % (router_id, subnet_id, e))
517 def add_gateway_router(neutron_client, router_id):
518 ext_net_id = get_external_net_id(neutron_client)
519 router_dict = {'network_id': ext_net_id}
521 neutron_client.add_gateway_router(router_id, router_dict)
524 logger.error("Error [add_gateway_router(neutron_client, '%s')]: %s"
529 def delete_neutron_net(neutron_client, network_id):
531 neutron_client.delete_network(network_id)
534 logger.error("Error [delete_neutron_net(neutron_client, '%s')]: %s"
539 def delete_neutron_subnet(neutron_client, subnet_id):
541 neutron_client.delete_subnet(subnet_id)
544 logger.error("Error [delete_neutron_subnet(neutron_client, '%s')]: %s"
549 def delete_neutron_router(neutron_client, router_id):
551 neutron_client.delete_router(router=router_id)
554 logger.error("Error [delete_neutron_router(neutron_client, '%s')]: %s"
559 def delete_neutron_port(neutron_client, port_id):
561 neutron_client.delete_port(port_id)
564 logger.error("Error [delete_neutron_port(neutron_client, '%s')]: %s"
569 def remove_interface_router(neutron_client, router_id, subnet_id):
570 json_body = {"subnet_id": subnet_id}
572 neutron_client.remove_interface_router(router=router_id,
576 logger.error("Error [remove_interface_router(neutron_client, '%s', "
577 "'%s')]: %s" % (router_id, subnet_id, e))
581 def remove_gateway_router(neutron_client, router_id):
583 neutron_client.remove_gateway_router(router_id)
586 logger.error("Error [remove_gateway_router(neutron_client, '%s')]: %s"
591 def create_network_full(logger,
598 # Check if the network already exists
599 network_id = get_network_id(neutron_client, net_name)
600 subnet_id = get_subnet_id(neutron_client, subnet_name)
601 router_id = get_router_id(neutron_client, router_name)
603 if network_id != '' and subnet_id != '' and router_id != '':
604 logger.info("A network with name '%s' already exists..." % net_name)
606 neutron_client.format = 'json'
607 logger.info('Creating neutron network %s...' % net_name)
608 network_id = create_neutron_net(neutron_client, net_name)
613 logger.debug("Network '%s' created successfully" % network_id)
614 logger.debug('Creating Subnet....')
615 subnet_id = create_neutron_subnet(neutron_client, subnet_name,
620 logger.debug("Subnet '%s' created successfully" % subnet_id)
621 logger.debug('Creating Router...')
622 router_id = create_neutron_router(neutron_client, router_name)
627 logger.debug("Router '%s' created successfully" % router_id)
628 logger.debug('Adding router to subnet...')
630 if not add_interface_router(neutron_client, router_id, subnet_id):
633 logger.debug("Interface added successfully.")
635 logger.debug('Adding gateway to router...')
636 if not add_gateway_router(neutron_client, router_id):
639 logger.debug("Gateway added successfully.")
641 network_dic = {'net_id': network_id,
642 'subnet_id': subnet_id,
643 'router_id': router_id}
647 def create_bgpvpn(neutron_client, **kwargs):
648 # route_distinguishers
650 json_body = {"bgpvpn": kwargs}
651 return neutron_client.create_bgpvpn(json_body)
654 def create_network_association(neutron_client, bgpvpn_id, neutron_network_id):
655 json_body = {"network_association": {"network_id": neutron_network_id}}
656 return neutron_client.create_network_association(bgpvpn_id, json_body)
659 def update_bgpvpn(neutron_client, bgpvpn_id, **kwargs):
660 json_body = {"bgpvpn": kwargs}
661 return neutron_client.update_bgpvpn(bgpvpn_id, json_body)
664 def delete_bgpvpn(neutron_client, bgpvpn_id):
665 return neutron_client.delete_bgpvpn(bgpvpn_id)
667 # *********************************************
669 # *********************************************
672 def get_security_groups(neutron_client):
674 security_groups = neutron_client.list_security_groups()[
676 return security_groups
678 logger.error("Error [get_security_groups(neutron_client)]: %s" % e)
682 def get_security_group_id(neutron_client, sg_name):
683 security_groups = get_security_groups(neutron_client)
685 for sg in security_groups:
686 if sg['name'] == sg_name:
692 def create_security_group(neutron_client, sg_name, sg_description):
693 json_body = {'security_group': {'name': sg_name,
694 'description': sg_description}}
696 secgroup = neutron_client.create_security_group(json_body)
697 return secgroup['security_group']
699 logger.error("Error [create_security_group(neutron_client, '%s', "
700 "'%s')]: %s" % (sg_name, sg_description, e))
704 def create_secgroup_rule(neutron_client, sg_id, direction, protocol,
705 port_range_min=None, port_range_max=None):
706 if port_range_min is None and port_range_max is None:
707 json_body = {'security_group_rule': {'direction': direction,
708 'security_group_id': sg_id,
709 'protocol': protocol}}
710 elif port_range_min is not None and port_range_max is not None:
711 json_body = {'security_group_rule': {'direction': direction,
712 'security_group_id': sg_id,
713 'port_range_min': port_range_min,
714 'port_range_max': port_range_max,
715 'protocol': protocol}}
717 logger.error("Error [create_secgroup_rule(neutron_client, '%s', '%s', "
718 "'%s', '%s', '%s', '%s')]:" % (neutron_client,
723 " Invalid values for port_range_min, port_range_max")
726 neutron_client.create_security_group_rule(json_body)
729 logger.error("Error [create_secgroup_rule(neutron_client, '%s', '%s', "
730 "'%s', '%s', '%s', '%s')]: %s" % (neutron_client,
739 def create_security_group_full(logger, neutron_client,
740 sg_name, sg_description):
741 sg_id = get_security_group_id(neutron_client, sg_name)
743 logger.info("Using existing security group '%s'..." % sg_name)
745 logger.info("Creating security group '%s'..." % sg_name)
746 SECGROUP = create_security_group(neutron_client,
750 logger.error("Failed to create the security group...")
753 sg_id = SECGROUP['id']
755 logger.debug("Security group '%s' with ID=%s created successfully."
756 % (SECGROUP['name'], sg_id))
758 logger.debug("Adding ICMP rules in security group '%s'..."
760 if not create_secgroup_rule(neutron_client, sg_id,
762 logger.error("Failed to create the security group rule...")
765 logger.debug("Adding SSH rules in security group '%s'..."
767 if not create_secgroup_rule(
768 neutron_client, sg_id, 'ingress', 'tcp', '22', '22'):
769 logger.error("Failed to create the security group rule...")
772 if not create_secgroup_rule(
773 neutron_client, sg_id, 'egress', 'tcp', '22', '22'):
774 logger.error("Failed to create the security group rule...")
779 def add_secgroup_to_instance(nova_client, instance_id, secgroup_id):
781 nova_client.servers.add_security_group(instance_id, secgroup_id)
784 logger.error("Error [add_secgroup_to_instance(nova_client, '%s', "
785 "'%s')]: %s" % (instance_id, secgroup_id, e))
789 def update_sg_quota(neutron_client, tenant_id, sg_quota, sg_rule_quota):
790 json_body = {"quota": {
791 "security_group": sg_quota,
792 "security_group_rule": sg_rule_quota
796 neutron_client.update_quota(tenant_id=tenant_id,
800 logger.error("Error [update_sg_quota(neutron_client, '%s', '%s', "
801 "'%s')]: %s" % (tenant_id, sg_quota, sg_rule_quota, e))
805 def delete_security_group(neutron_client, secgroup_id):
807 neutron_client.delete_security_group(secgroup_id)
810 logger.error("Error [delete_security_group(neutron_client, '%s')]: %s"
815 # *********************************************
817 # *********************************************
818 def get_images(nova_client):
820 images = nova_client.images.list()
823 logger.error("Error [get_images]: %s" % e)
827 def get_image_id(glance_client, image_name):
828 images = glance_client.images.list()
831 if i.name == image_name:
837 def create_glance_image(glance_client, image_name, file_path, disk="qcow2",
838 container="bare", public=True, logger=None):
839 if not os.path.isfile(file_path):
840 logger.error("Error: file %s does not exist." % file_path)
843 image_id = get_image_id(glance_client, image_name)
846 logger.info("Image %s already exists." % image_name)
849 logger.info("Creating image '%s' from '%s'..." % (image_name,
851 with open(file_path) as fimage:
852 image = glance_client.images.create(name=image_name,
855 container_format=container,
860 logger.error("Error [create_glance_image(glance_client, '%s', '%s', "
861 "'%s')]: %s" % (image_name, file_path, str(public), e))
865 def delete_glance_image(nova_client, image_id):
867 nova_client.images.delete(image_id)
870 logger.error("Error [delete_glance_image(nova_client, '%s')]: %s"
875 # *********************************************
877 # *********************************************
878 def get_volumes(cinder_client):
880 volumes = cinder_client.volumes.list(search_opts={'all_tenants': 1})
883 logger.error("Error [get_volumes(cinder_client)]: %s" % e)
887 def list_volume_types(cinder_client, public=True, private=True):
889 volume_types = cinder_client.volume_types.list()
891 volume_types = [vt for vt in volume_types if not vt.is_public]
893 volume_types = [vt for vt in volume_types if vt.is_public]
896 logger.error("Error [list_volume_types(cinder_client)]: %s" % e)
900 def create_volume_type(cinder_client, name):
902 volume_type = cinder_client.volume_types.create(name)
905 logger.error("Error [create_volume_type(cinder_client, '%s')]: %s"
910 def update_cinder_quota(cinder_client, tenant_id, vols_quota,
911 snapshots_quota, gigabytes_quota):
912 quotas_values = {"volumes": vols_quota,
913 "snapshots": snapshots_quota,
914 "gigabytes": gigabytes_quota}
917 cinder_client.quotas.update(tenant_id, **quotas_values)
920 logger.error("Error [update_cinder_quota(cinder_client, '%s', '%s', "
921 "'%s' '%s')]: %s" % (tenant_id, vols_quota,
922 snapshots_quota, gigabytes_quota, e))
926 def delete_volume(cinder_client, volume_id, forced=False):
930 cinder_client.volumes.detach(volume_id)
932 logger.error(sys.exc_info()[0])
933 cinder_client.volumes.force_delete(volume_id)
935 cinder_client.volumes.delete(volume_id)
938 logger.error("Error [delete_volume(cinder_client, '%s', '%s')]: %s"
939 % (volume_id, str(forced), e))
943 def delete_volume_type(cinder_client, volume_type):
945 cinder_client.volume_types.delete(volume_type)
948 logger.error("Error [delete_volume_type(cinder_client, '%s')]: %s"
953 # *********************************************
955 # *********************************************
956 def get_tenants(keystone_client):
958 tenants = keystone_client.tenants.list()
961 logger.error("Error [get_tenants(keystone_client)]: %s" % e)
965 def get_users(keystone_client):
967 users = keystone_client.users.list()
970 logger.error("Error [get_users(keystone_client)]: %s" % e)
974 def get_tenant_id(keystone_client, tenant_name):
975 tenants = keystone_client.tenants.list()
978 if t.name == tenant_name:
984 def get_user_id(keystone_client, user_name):
985 users = keystone_client.users.list()
988 if u.name == user_name:
994 def get_role_id(keystone_client, role_name):
995 roles = keystone_client.roles.list()
998 if r.name == role_name:
1004 def create_tenant(keystone_client, tenant_name, tenant_description):
1006 tenant = keystone_client.tenants.create(tenant_name,
1010 except Exception, e:
1011 logger.error("Error [create_tenant(cinder_client, '%s', '%s')]: %s"
1012 % (tenant_name, tenant_description, e))
1016 def create_user(keystone_client, user_name, user_password,
1017 user_email, tenant_id):
1019 user = keystone_client.users.create(user_name, user_password,
1020 user_email, tenant_id,
1023 except Exception, e:
1024 logger.error("Error [create_user(keystone_client, '%s', '%s', '%s'"
1025 "'%s')]: %s" % (user_name, user_password,
1026 user_email, tenant_id, e))
1030 def add_role_user(keystone_client, user_id, role_id, tenant_id):
1032 keystone_client.roles.add_user_role(user_id, role_id, tenant_id)
1034 except Exception, e:
1035 logger.error("Error [add_role_user(keystone_client, '%s', '%s'"
1036 "'%s')]: %s " % (user_id, role_id, tenant_id, e))
1040 def delete_tenant(keystone_client, tenant_id):
1042 keystone_client.tenants.delete(tenant_id)
1044 except Exception, e:
1045 logger.error("Error [delete_tenant(keystone_client, '%s')]: %s"
1050 def delete_user(keystone_client, user_id):
1052 keystone_client.users.delete(user_id)
1054 except Exception, e:
1055 logger.error("Error [delete_user(keystone_client, '%s')]: %s"