示例#1
0
def main():
    """Run the main application."""
    # Parse user args
    user_args = args()

    # Get the contents of the system inventory
    inventory, filename = filesys.load_from_json(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)
        inventory_json = json.dumps(inventory, indent=2)
        filesys.save_inventory(inventory_json, filename)
        print('Success. . .')
    else:
        du.recursive_dict_removal(inventory, user_args['remove_item'])
        inventory_json = json.dumps(inventory, indent=2)
        filesys.save_inventory(inventory_json, filename)
        print('Success. . .')
示例#2
0
def main():
    """Run the main application."""
    # Parse user args
    user_args = args()

    # Get the contents of the system inventory
    inventory, filename = filesys.load_from_json(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)
        inventory_json = json.dumps(inventory, indent=2)
        filesys.save_inventory(inventory_json, filename)
        print('Success. . .')
    else:
        du.recursive_dict_removal(inventory, user_args['remove_item'])
        inventory_json = json.dumps(inventory, indent=2)
        filesys.save_inventory(inventory_json, filename)
        print('Success. . .')
示例#3
0
def remove_ip_addresses(inventory, filepath=None):
    """Removes container IP address information from the inventory dictionary

    Writes the changes into the inventory file in filepath if specified

    All container_networks information for containers will be deleted.
    """
    hostvars = inventory['_meta']['hostvars']

    for host, variables in hostvars.items():
        if variables.get('is_metal', False):
            continue

        ip_vars = [
            'container_networks', 'container_address', 'ansible_host',
            'ansible_ssh_host'
        ]

        # Don't raise a KeyError if the entries have already been removed.
        for ip_var in ip_vars:
            variables.pop(ip_var, None)

    if filepath is not None:
        inventory_json = json.dumps(inventory,
                                    indent=2,
                                    separators=(',', ': '))
        filesys.save_inventory(inventory_json, filepath)
示例#4
0
def remove_inventory_item(remove_item, inventory, filepath=None):
    """Removes inventory item from the inventory dictionary

    Writes the changes into the inventory file in filepath if available

    All container_networks information for containers will be deleted.
    """
    du.recursive_dict_removal(inventory, remove_item)

    if filepath is not None:
        inventory_json = json.dumps(inventory, indent=2,
                                    separators=(',', ': '))
        filesys.save_inventory(inventory_json, filepath)
示例#5
0
def remove_ip_addresses(inventory, filepath=None):
    """Removes container IP address information from the inventory dictionary

    Writes the changes into the inventory file in filepath if specified

    All container_networks information for containers will be deleted.
    """
    hostvars = inventory['_meta']['hostvars']

    for host, variables in hostvars.items():
        if variables.get('is_metal', False):
            continue

        ip_vars = ['container_networks', 'container_address',
                   'ansible_host', 'ansible_ssh_host']

        # Don't raise a KeyError if the entries have already been removed.
        for ip_var in ip_vars:
            variables.pop(ip_var, None)

    if filepath is not None:
        inventory_json = json.dumps(inventory, indent=2,
                                    separators=(',', ': '))
        filesys.save_inventory(inventory_json, filepath)
示例#6
0
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,
        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
示例#7
0
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
示例#8
0
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:
        log_fmt = "%(lineno)d - %(funcName)s: %(message)s"
        logging.basicConfig(format=log_fmt, filename='inventory.log')
        logger.setLevel(logging.DEBUG)
        logger.info("Beginning new inventory run")

    # Get the path to the user configuration files
    config_path = find_config_path(user_config_path=config)

    user_defined_config = load_user_configuration(config_path)
    base_env_dir = environment
    base_env = load_environment(base_env_dir, {})
    environment = load_environment(config_path, base_env)

    # Load existing inventory file if found
    dynamic_inventory = load_inventory(config_path, 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, dynamic_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, dynamic_inventory)
    user_defined_setup(user_defined_config, dynamic_inventory)
    skel_setup(environment, dynamic_inventory)
    logger.debug("Loading physical skel.")
    skel_load(environment.get('physical_skel'), dynamic_inventory)
    logger.debug("Loading component skel")
    skel_load(environment.get('component_skel'), dynamic_inventory)
    container_skel_load(environment.get('container_skel'), dynamic_inventory,
                        user_defined_config)

    # Look at inventory and ensure all entries have all required values.
    _ensure_inventory_uptodate(
        inventory=dynamic_inventory,
        container_skel=environment.get('container_skel'),
    )

    # Load the inventory json
    dynamic_inventory_json = json.dumps(dynamic_inventory,
                                        indent=4,
                                        sort_keys=True)

    if check:
        if _check_all_conf_groups_present(user_defined_config, environment):
            return 'Configuration ok!'

    # Generate a list of all hosts and their used IP addresses
    hostnames_ips = {}
    for _host, _vars in dynamic_inventory['_meta']['hostvars'].iteritems():
        host_hash = hostnames_ips[_host] = {}
        for _key, _value in _vars.iteritems():
            if _key.endswith('address') or _key == 'ansible_host':
                host_hash[_key] = _value

    # Save a list of all hosts and their given IP addresses
    hostnames_ip_file = os.path.join(config_path,
                                     'openstack_hostnames_ips.yml')
    with open(hostnames_ip_file, 'wb') as f:
        f.write(json.dumps(hostnames_ips, indent=4, sort_keys=True))

    if logger.isEnabledFor(logging.DEBUG):
        num_hosts = len(dynamic_inventory['_meta']['hostvars'])
        logger.debug("%d hosts found." % num_hosts)

    # Save new dynamic inventory
    save_inventory(dynamic_inventory_json, config_path)

    return dynamic_inventory_json