def main(): """Run the main application.""" # Parse user args user_args = args() # Get the contents of the system inventory inventory, filepath = filesys.load_inventory(filename=user_args['file']) # Make a table with hosts in the left column and details about each in the # columns to the right if user_args['list_host'] is True: print(print_inventory(inventory, user_args['sort'])) # Groups in first column, containers in each group on the right elif user_args['list_groups'] is True: print(print_groups_per_container(inventory)) # Containers in the first column, groups for each container on the right elif user_args['list_containers'] is True: print(print_containers_per_group(inventory)) elif user_args['export'] is True: print(json.dumps(export_host_info(inventory), indent=2)) elif user_args['clear_ips'] is True: remove_ip_addresses(inventory, filepath) print('Success. . .') elif user_args['remove_group']: remove_inventory_group(user_args['remove_group'], inventory, filepath) print('Success. . .') else: remove_inventory_item(user_args['remove_item'], inventory, filepath) print('Success. . .')
def test_rereading_files(self): # Generate the initial inventory files get_inventory(clean=False) inv, path = fs.load_inventory(TARGET_DIR) self.assertIsInstance(inv, dict) self.assertIn('_meta', inv) # This test is basically just making sure we get more than # INVENTORY_SKEL populated, so we're not going to do deep testing self.assertIn('log_hosts', inv)
def setUp(self): super(TestOverridingEnvIntegration, self).setUp() self.user_defined_config = get_config() # Inventory is necessary since keys are assumed present self.inv, path = fs.load_inventory(TARGET_DIR, di.INVENTORY_SKEL)
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