def main(config=None, check=False, debug=False, environment=None, **kwargs): """Run the main application. :param config: ``str`` Directory from which to pull configs and overrides :param check: ``bool`` Flag to enable check mode :param debug: ``bool`` Flag to enable debug logging :param kwargs: ``dict`` Dictionary of arbitrary arguments; mostly for catching Ansible's required `--list` parameter without name shadowing the `list` built-in. :param environment: ``str`` Directory containing the base env.d """ if debug: _prepare_debug_logger() try: user_defined_config = filesys.load_user_configuration(config) except filesys.MissingDataSource as ex: raise SystemExit(ex) base_env_dir = environment base_env = filesys.load_environment(base_env_dir, {}) environment = filesys.load_environment(config, base_env) # Load existing inventory file if found inventory, inv_path = filesys.load_inventory(config, INVENTORY_SKEL) # Save the users container cidr as a group variable cidr_networks = user_defined_config.get('cidr_networks') if not cidr_networks: raise SystemExit('No container CIDR specified in user config') user_cidr = None if 'container' in cidr_networks: user_cidr = cidr_networks['container'] elif 'management' in cidr_networks: user_cidr = cidr_networks['management'] else: overrides = user_defined_config.get('global_overrides') pns = overrides.get('provider_networks', list()) for pn in pns: p_net = pn.get('network') if not p_net: continue q_name = p_net.get('ip_from_q') if q_name and q_name in cidr_networks: if (p_net.get('address_prefix') in ('container', 'management')): if user_cidr is None: user_cidr = [] user_cidr.append(cidr_networks[q_name]) if user_cidr is None: raise SystemExit('No container or management network ' 'specified in user config.') # make sure user_defined config is self contained _check_config_settings(cidr_networks, user_defined_config, environment.get('container_skel')) # Add the container_cidr into the all global ansible group_vars _parse_global_variables(user_cidr, inventory, user_defined_config) # Load all of the IP addresses that we know are used and set the queue ip.set_used_ips(user_defined_config, inventory) user_defined_setup(user_defined_config, inventory) skel_setup(environment, inventory) _check_group_branches(user_defined_config, environment.get('physical_skel')) logger.debug("Loading physical skel.") skel_load(environment.get('physical_skel'), inventory) logger.debug("Loading component skel") skel_load(environment.get('component_skel'), inventory) container_skel_load(environment.get('container_skel'), inventory, user_defined_config) # Look at inventory and ensure all entries have all required values. _ensure_inventory_uptodate( inventory=inventory, container_skel=environment.get('container_skel'), ) # Load the inventory json inventory_json = json.dumps(inventory, indent=4, separators=(',', ': '), sort_keys=True) if check: if _check_all_conf_groups_present(user_defined_config, environment): return 'Configuration ok!' # Save a list of all hosts and their given IP addresses hostnames_ips = _collect_hostnames(inventory) filesys.write_hostnames(config, hostnames_ips) if logger.isEnabledFor(logging.DEBUG): num_hosts = len(inventory['_meta']['hostvars']) logger.debug("%d hosts found.", num_hosts) # Save new dynamic inventory filesys.save_inventory(inventory_json, inv_path) return inventory_json
def main(config=None, check=False, debug=False, environment=None, **kwargs): """Run the main application. :param config: ``str`` Directory from which to pull configs and overrides :param check: ``bool`` Flag to enable check mode :param debug: ``bool`` Flag to enable debug logging :param kwargs: ``dict`` Dictionary of arbitrary arguments; mostly for catching Ansible's required `--list` parameter without name shadowing the `list` built-in. :param environment: ``str`` Directory containing the base env.d """ if debug: _prepare_debug_logger() try: user_defined_config = filesys.load_user_configuration(config) except filesys.MissingDataSource as ex: raise SystemExit(ex) base_env_dir = environment base_env = filesys.load_environment(base_env_dir, {}) environment = filesys.load_environment(config, base_env) # Load existing inventory file if found inventory, inv_path = filesys.load_inventory(config, INVENTORY_SKEL) # Save the users container cidr as a group variable cidr_networks = user_defined_config.get('cidr_networks') if not cidr_networks: raise SystemExit('No container CIDR specified in user config') if 'container' in cidr_networks: user_cidr = cidr_networks['container'] elif 'management' in cidr_networks: user_cidr = cidr_networks['management'] else: raise SystemExit('No container or management network ' 'specified in user config.') # make sure user_defined config is self contained _check_config_settings( cidr_networks, user_defined_config, environment.get('container_skel') ) # Add the container_cidr into the all global ansible group_vars _parse_global_variables(user_cidr, inventory, user_defined_config) # Load all of the IP addresses that we know are used and set the queue ip.set_used_ips(user_defined_config, inventory) user_defined_setup(user_defined_config, inventory) skel_setup(environment, inventory) _check_group_branches( user_defined_config, environment.get('physical_skel') ) logger.debug("Loading physical skel.") skel_load( environment.get('physical_skel'), inventory ) logger.debug("Loading component skel") skel_load( environment.get('component_skel'), inventory ) container_skel_load( environment.get('container_skel'), inventory, user_defined_config ) # Look at inventory and ensure all entries have all required values. _ensure_inventory_uptodate( inventory=inventory, container_skel=environment.get('container_skel'), ) # Load the inventory json inventory_json = json.dumps( inventory, indent=4, separators=(',', ': '), sort_keys=True ) if check: if _check_all_conf_groups_present(user_defined_config, environment): return 'Configuration ok!' # Save a list of all hosts and their given IP addresses hostnames_ips = _collect_hostnames(inventory) filesys.write_hostnames(config, hostnames_ips) if logger.isEnabledFor(logging.DEBUG): num_hosts = len(inventory['_meta']['hostvars']) logger.debug("%d hosts found.", num_hosts) # Save new dynamic inventory filesys.save_inventory(inventory_json, inv_path) return inventory_json