diff options
Diffstat (limited to 'snaps/openstack/utils/launch_utils.py')
-rw-r--r-- | snaps/openstack/utils/launch_utils.py | 122 |
1 files changed, 92 insertions, 30 deletions
diff --git a/snaps/openstack/utils/launch_utils.py b/snaps/openstack/utils/launch_utils.py index 49d41e7..ddaad12 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 @@ -49,8 +50,13 @@ 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, keystone_utils +from snaps.openstack.utils.nova_utils import RebootType from snaps.provisioning import ansible_utils +from warnings import warn +warn('This utility will be removed in a subsequent release', + DeprecationWarning) + logger = logging.getLogger('lanuch_utils') DEFAULT_CREDS_KEY = 'admin' @@ -417,10 +423,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) @@ -428,12 +433,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 @@ -459,20 +465,16 @@ 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_utils.apply_playbook( ansible_config['playbook_location'], floating_ips, remote_user, ssh_priv_key_file_path=private_key_filepath, variables=variables, proxy_setting=proxy_settings) - if retval != 0: - # Not a fatal type of event - raise Exception( - 'Error applying playbook found at location - %s', - ansible_config.get('playbook_location')) - elif ansible_config.get('post_processing'): + + if 'post_processing' in ansible_config: post_proc_config = ansible_config['post_processing'] if 'sleep' in post_proc_config: time.sleep(post_proc_config['sleep']) @@ -480,9 +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() - - return retval + vm_dict[vm_name].reboot(RebootType.hard) def __get_connection_info(ansible_config, vm_dict): @@ -528,13 +528,13 @@ def __get_connection_info(ansible_config, vm_dict): return None -def __get_variables(var_config, os_creds, vm_dict, image_dict, flavor_dict, - networks_dict, routers_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 @@ -551,7 +551,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 @@ -566,13 +566,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 @@ -590,12 +590,14 @@ def __get_variable_value(var_config_values, os_creds, vm_dict, image_dict, 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, os_creds_dict) + if var_config_values['type'] == 'os_creds_dict': + return str(__get_os_creds_dict(var_config_values, os_creds_dict)) 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) + os_creds_dict) if var_config_values['type'] == 'port': return __get_vm_port_variable_value(var_config_values, vm_dict) if var_config_values['type'] == 'floating_ip': @@ -604,6 +606,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'] == 'vm-yaml': + return __create_yaml(var_config_values, vm_dict) return None @@ -632,13 +636,19 @@ def __get_vm_attr_variable_value(var_config_values, vm_dict): return vm.get_image_user() -def __get_os_creds_variable_value(var_config_values, os_creds): +def __get_os_creds_variable_value(var_config_values, os_creds_dict): """ Returns the associated OS credentials value :param var_config_values: the configuration dictionary - :param os_creds: the admin OpenStack OSCreds object + :param os_creds_dict: dict of OpenStack credentials where the key is the + name :return: the value """ + if 'creds_name' in var_config_values: + os_creds = os_creds_dict.get[var_config_values['creds_name']] + else: + os_creds = os_creds_dict.get('admin-creds') + if os_creds: if var_config_values['value'] == 'username': logger.info("Returning OS username") @@ -654,6 +664,21 @@ def __get_os_creds_variable_value(var_config_values, os_creds): return os_creds.project_name +def __get_os_creds_dict(var_config_values, os_creds_dict): + """ + Returns the associated OS credentials as a dict + :param var_config_values: the configuration dictionary + :param os_creds_dict: dict of creds where the key is the username + :return: the value dict + """ + if 'creds_name' in var_config_values: + os_creds = os_creds_dict.get[var_config_values['creds_name']] + else: + os_creds = os_creds_dict.get('admin-creds') + if os_creds: + return os_creds.to_dict() + + def __get_network_variable_value(var_config_values, networks_dict): """ Returns the associated network value @@ -701,15 +726,22 @@ def __get_network_variable_value(var_config_values, networks_dict): return broadcast_ip -def __get_router_variable_value(var_config_values, routers_dict, os_creds): +def __get_router_variable_value(var_config_values, routers_dict, + os_creds_dict): """ Returns the associated network value :param var_config_values: the configuration dictionary :param routers_dict: the dictionary containing all networks where the key is the network name - :param os_creds: the admin OpenStack credentials + :param os_creds_dict: dict of OpenStack credentials where the key is the + name :return: the value """ + if 'creds_name' in var_config_values: + os_creds = os_creds_dict.get[var_config_values['creds_name']] + else: + os_creds = os_creds_dict.get('admin-creds') + router_name = var_config_values.get('router_name') router_creator = routers_dict[router_name] @@ -813,6 +845,36 @@ def __get_flavor_variable_value(var_config_values, flavor_dict): return flavor_creator.get_flavor().id +def __create_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 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() + req_vm_names = var_config_values.get('vms') + + for name, vm_creator in vm_dict.items(): + vm_inst = vm_creator.get_vm_inst() + if vm_inst and vm_inst.name in req_vm_names: + out_dict['vms'].append({ + 'name': str(vm_inst.name), + 'id': str(vm_inst.id), + 'os_creds': vm_creator.get_os_creds().to_dict() + }) + + 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 |