X-Git-Url: https://gerrit.opnfv.org/gerrit/gitweb?a=blobdiff_plain;f=snaps%2Fopenstack%2Futils%2Flaunch_utils.py;h=6e8b94ea68ec6fc810701eebc2826ff86aec0717;hb=refs%2Fchanges%2F51%2F57851%2F1;hp=1899ef63e26645a1926bf64734cfd2870c50e148;hpb=e477198192a4f442fcaa6a587d03bad90cafd539;p=snaps.git diff --git a/snaps/openstack/utils/launch_utils.py b/snaps/openstack/utils/launch_utils.py index 1899ef6..6e8b94e 100644 --- a/snaps/openstack/utils/launch_utils.py +++ b/snaps/openstack/utils/launch_utils.py @@ -24,6 +24,7 @@ import os import time from keystoneauth1.exceptions import Unauthorized +from snaps import file_utils from snaps.config.flavor import FlavorConfig from snaps.config.image import ImageConfig from snaps.config.keypair import KeypairConfig @@ -48,7 +49,8 @@ from snaps.openstack.create_user import OpenStackUser from snaps.openstack.create_volume import OpenStackVolume from snaps.openstack.create_volume_type import OpenStackVolumeType from snaps.openstack.os_credentials import OSCreds, ProxySettings -from snaps.openstack.utils import deploy_utils, neutron_utils +from snaps.openstack.utils import deploy_utils, neutron_utils, keystone_utils +from snaps.openstack.utils.nova_utils import RebootType from snaps.provisioning import ansible_utils logger = logging.getLogger('lanuch_utils') @@ -307,6 +309,8 @@ def __create_instances(os_creds_dict, creator_class, config_class, config, creator.create() out[inst_config['name']] = creator + else: + raise Exception('Unable to instantiate creator') logger.info('Initialized configured %ss', config_key) @@ -415,10 +419,9 @@ def __apply_ansible_playbooks(ansible_configs, os_creds_dict, vm_dict, 'SSH requests') return False - os_creds = os_creds_dict.get('admin-creds') __apply_ansible_playbook( - ansible_config, os_creds, vm_dict, image_dict, flavor_dict, - networks_dict, routers_dict) + ansible_config, os_creds_dict, vm_dict, image_dict, + flavor_dict, networks_dict, routers_dict) # Return to original directory os.chdir(orig_cwd) @@ -426,12 +429,13 @@ def __apply_ansible_playbooks(ansible_configs, os_creds_dict, vm_dict, return True -def __apply_ansible_playbook(ansible_config, os_creds, vm_dict, image_dict, - flavor_dict, networks_dict, routers_dict): +def __apply_ansible_playbook(ansible_config, os_creds_dict, vm_dict, + image_dict, flavor_dict, networks_dict, + routers_dict): """ Applies an Ansible configuration setting :param ansible_config: the configuration settings - :param os_creds: the OpenStack admin credentials object + :param os_creds_dict: dict where the key is the name and value is OSCreds :param vm_dict: the dictionary of newly instantiated VMs where the name is the key :param image_dict: the dictionary of newly instantiated images where the @@ -457,8 +461,8 @@ def __apply_ansible_playbook(ansible_config, os_creds, vm_dict, image_dict, 'completed') variables = __get_variables( - ansible_config.get('variables'), os_creds, vm_dict, image_dict, - flavor_dict, networks_dict, routers_dict) + ansible_config.get('variables'), os_creds_dict, vm_dict, + image_dict, flavor_dict, networks_dict, routers_dict) retval = ansible_utils.apply_playbook( ansible_config['playbook_location'], floating_ips, remote_user, @@ -478,7 +482,7 @@ def __apply_ansible_playbook(ansible_config, os_creds, vm_dict, image_dict, for vm_name in post_proc_config['reboot']: if vm_name in vm_dict: logger.info('Rebooting VM - %s', vm_name) - vm_dict[vm_name].reboot() + vm_dict[vm_name].reboot(RebootType.hard) return retval @@ -526,13 +530,13 @@ def __get_connection_info(ansible_config, vm_dict): return None -def __get_variables(var_config, os_creds, vm_dict, image_dict, flavor_dict, +def __get_variables(var_config, os_creds_dict, vm_dict, image_dict, flavor_dict, networks_dict, routers_dict): """ Returns a dictionary of substitution variables to be used for Ansible templates :param var_config: the variable configuration settings - :param os_creds: the OpenStack admin credentials object + :param os_creds_dict: dict where the key is the name and value is OSCreds :param vm_dict: the dictionary of newly instantiated VMs where the name is the key :param image_dict: the dictionary of newly instantiated images where the @@ -549,7 +553,7 @@ def __get_variables(var_config, os_creds, vm_dict, image_dict, flavor_dict, variables = dict() for key, value in var_config.items(): value = __get_variable_value( - value, os_creds, vm_dict, image_dict, flavor_dict, + value, os_creds_dict, vm_dict, image_dict, flavor_dict, networks_dict, routers_dict) if key and value: variables[key] = value @@ -564,13 +568,13 @@ def __get_variables(var_config, os_creds, vm_dict, image_dict, flavor_dict, return None -def __get_variable_value(var_config_values, os_creds, vm_dict, image_dict, +def __get_variable_value(var_config_values, os_creds_dict, vm_dict, image_dict, flavor_dict, networks_dict, routers_dict): """ Returns the associated variable value for use by Ansible for substitution purposes :param var_config_values: the configuration dictionary - :param os_creds: the OpenStack admin credentials object + :param os_creds_dict: dict where the key is the name and value is OSCreds :param vm_dict: the dictionary of newly instantiated VMs where the name is the key :param image_dict: the dictionary of newly instantiated images where the @@ -583,17 +587,18 @@ def __get_variable_value(var_config_values, os_creds, vm_dict, image_dict, the name is the key :return: """ + admin_creds = os_creds_dict.get('admin-creds') if var_config_values['type'] == 'string': return __get_string_variable_value(var_config_values) if var_config_values['type'] == 'vm-attr': return __get_vm_attr_variable_value(var_config_values, vm_dict) if var_config_values['type'] == 'os_creds': - return __get_os_creds_variable_value(var_config_values, os_creds) + return __get_os_creds_variable_value(var_config_values, admin_creds) if var_config_values['type'] == 'network': return __get_network_variable_value(var_config_values, networks_dict) if var_config_values['type'] == 'router': return __get_router_variable_value(var_config_values, routers_dict, - os_creds) + admin_creds) if var_config_values['type'] == 'port': return __get_vm_port_variable_value(var_config_values, vm_dict) if var_config_values['type'] == 'floating_ip': @@ -602,6 +607,8 @@ def __get_variable_value(var_config_values, os_creds, vm_dict, image_dict, return __get_image_variable_value(var_config_values, image_dict) if var_config_values['type'] == 'flavor': return __get_flavor_variable_value(var_config_values, flavor_dict) + if var_config_values['type'] == 'snaps-env-yaml': + return __create_snaps_env_yaml(var_config_values, vm_dict) return None @@ -713,18 +720,22 @@ def __get_router_variable_value(var_config_values, routers_dict, os_creds): if router_creator: if 'external_fixed_ip' == var_config_values.get('attr'): - neutron = neutron_utils.neutron_client(os_creds) - ext_nets = neutron_utils.get_external_networks(neutron) + session = keystone_utils.keystone_session(os_creds) + neutron = neutron_utils.neutron_client(os_creds, session) + try: + ext_nets = neutron_utils.get_external_networks(neutron) - subnet_name = var_config_values.get('subnet_name') + subnet_name = var_config_values.get('subnet_name') - for ext_net in ext_nets: - for subnet in ext_net.subnets: - if subnet_name == subnet.name: - router = router_creator.get_router() - for fixed_ips in router.external_fixed_ips: - if subnet.id == fixed_ips['subnet_id']: - return fixed_ips['ip_address'] + for ext_net in ext_nets: + for subnet in ext_net.subnets: + if subnet_name == subnet.name: + router = router_creator.get_router() + for fixed_ips in router.external_fixed_ips: + if subnet.id == fixed_ips['subnet_id']: + return fixed_ips['ip_address'] + finally: + keystone_utils.close_session(session) def __get_vm_port_variable_value(var_config_values, vm_dict): @@ -807,6 +818,43 @@ def __get_flavor_variable_value(var_config_values, flavor_dict): return flavor_creator.get_flavor().id +def __create_snaps_env_yaml(var_config_values, vm_dict): + """ + Creates a yaml file containing an OpenStack pod's credentials with a list + of server IDs that can be used for obtaining SNAPS-OO instances for + manipulation such as rebooting + :param var_config_values: the configuration dictionary + :param os_creds: the admin credentials for accessing OpenStack + :param vm_dict: the dictionary containing all vm creators where the + key is the name + :return: the name of the generated file + """ + out_dict = dict() + out_dict['vms'] = list() + + for name, vm_creator in vm_dict.items(): + vm_inst = vm_creator.get_vm_inst() + inst_creds = vm_creator._os_creds + if vm_inst: + out_dict['vms'].append({ + 'name': str(vm_inst.name), + 'id': str(vm_inst.id), + 'os_creds': { + 'username': inst_creds.username, + 'password': inst_creds.password, + 'auth_url': inst_creds.auth_url, + 'project_name': inst_creds.project_name, + 'identity_api_version': inst_creds.identity_api_version, + } + }) + + out_file = file_utils.persist_dict_to_yaml( + out_dict, var_config_values.get('file_name')) + + if out_file: + return out_file.name + + def __cleanup(creators, clean_image=False): """ Cleans up environment