def test_emptying_dictionaries(self):
        self.cinder_config['container_skel']['cinder_volumes_container'] = {}

        self.write_override_env()

        fs.load_environment(TARGET_DIR, self.base_env)

        test_vol = self.base_env['container_skel']['cinder_volumes_container']

        self.assertNotIn('belongs_to', test_vol)
    def test_emptying_dictionaries(self):
        self.cinder_config['container_skel']['cinder_volumes_container'] = {}

        self.write_override_env()

        fs.load_environment(TARGET_DIR, self.base_env)

        test_vol = self.base_env['container_skel']['cinder_volumes_container']

        self.assertNotIn('belongs_to', test_vol)
    def test_cinder_metal_override(self):
        vol = self.cinder_config['container_skel']['cinder_volumes_container']
        vol['properties']['is_metal'] = False

        self.write_override_env()

        fs.load_environment(TARGET_DIR, self.base_env)

        test_vol = self.base_env['container_skel']['cinder_volumes_container']
        self.assertFalse(test_vol['properties']['is_metal'])
    def test_cinder_metal_override(self):
        vol = self.cinder_config['container_skel']['cinder_volumes_container']
        vol['properties']['is_metal'] = False

        self.write_override_env()

        fs.load_environment(TARGET_DIR, self.base_env)

        test_vol = self.base_env['container_skel']['cinder_volumes_container']
        self.assertFalse(test_vol['properties']['is_metal'])
    def test_emptying_lists(self):
        vol = self.cinder_config['container_skel']['cinder_volumes_container']
        vol['belongs_to'] = []

        self.write_override_env()

        fs.load_environment(TARGET_DIR, self.base_env)

        test_vol = self.base_env['container_skel']['cinder_volumes_container']

        self.assertEqual(test_vol['belongs_to'], [])
    def test_emptying_lists(self):
        vol = self.cinder_config['container_skel']['cinder_volumes_container']
        vol['belongs_to'] = []

        self.write_override_env()

        fs.load_environment(TARGET_DIR, self.base_env)

        test_vol = self.base_env['container_skel']['cinder_volumes_container']

        self.assertEqual(test_vol['belongs_to'], [])
    def test_adding_new_keys(self):
        vol = self.cinder_config['container_skel']['cinder_volumes_container']
        vol['a_new_key'] = 'Added'

        self.write_override_env()

        fs.load_environment(TARGET_DIR, self.base_env)

        test_vol = self.base_env['container_skel']['cinder_volumes_container']

        self.assertIn('a_new_key', test_vol)
        self.assertEqual(test_vol['a_new_key'], 'Added')
    def test_adding_new_keys(self):
        vol = self.cinder_config['container_skel']['cinder_volumes_container']
        vol['a_new_key'] = 'Added'

        self.write_override_env()

        fs.load_environment(TARGET_DIR, self.base_env)

        test_vol = self.base_env['container_skel']['cinder_volumes_container']

        self.assertIn('a_new_key', test_vol)
        self.assertEqual(test_vol['a_new_key'], 'Added')
    def skel_setup(self):
        self.environment = fs.load_environment(TARGET_DIR, self.base_env)

        di.skel_setup(self.environment, self.inv)

        di.skel_load(
            self.environment.get('physical_skel'),
            self.inv
        )
    def setUp(self):
        self.base_env = fs.load_environment(BASE_ENV_DIR, {})

        # Use the cinder configuration as our sample for override testing
        with open(path.join(BASE_ENV_DIR, 'env.d', 'cinder.yml'), 'r') as f:
            self.cinder_config = yaml.safe_load(f.read())

        self.override_path = path.join(TARGET_DIR, 'env.d')
        os.mkdir(self.override_path)
    def setUp(self):
        self.base_env = fs.load_environment(BASE_ENV_DIR, {})

        # Use the cinder configuration as our sample for override testing
        with open(path.join(BASE_ENV_DIR, 'env.d', 'cinder.yml'), 'r') as f:
            self.cinder_config = yaml.safe_load(f.read())

        self.override_path = path.join(TARGET_DIR, 'env.d')
        os.mkdir(self.override_path)
    def skel_setup(self):
        self.environment = fs.load_environment(TARGET_DIR, self.base_env)

        di.skel_setup(self.environment, self.inv)

        di.skel_load(
            self.environment.get('physical_skel'),
            self.inv
        )
    def setUp(self):
        self.env = fs.load_environment(BASE_ENV_DIR, {})
        # Copy because we manipulate the structure in each test;
        # not copying would modify the global var in the target code
        self.inv = copy.deepcopy(di.INVENTORY_SKEL)
        # Since we're not running skel_setup, add necessary keys
        self.host_vars = self.inv['_meta']['hostvars']

        # The _ensure_inventory_uptodate function depends on values inserted
        # by the skel_setup function
        di.skel_setup(self.env, self.inv)
    def setUp(self):
        self.env = fs.load_environment(BASE_ENV_DIR, {})
        # Copy because we manipulate the structure in each test;
        # not copying would modify the global var in the target code
        self.inv = copy.deepcopy(di.INVENTORY_SKEL)
        # Since we're not running skel_setup, add necessary keys
        self.host_vars = self.inv['_meta']['hostvars']

        # The _ensure_inventory_uptodate function depends on values inserted
        # by the skel_setup function
        di.skel_setup(self.env, self.inv)
    def test_deleting_elements(self):
        # Leave only the 'properties' dictionary attached to simulate writing
        # a partial override file

        vol = self.cinder_config['container_skel']['cinder_volumes_container']
        keys = vol.keys()
        to_delete = []
        for key in vol.keys():
            if not key == 'properties':
                to_delete.append(key)

        for key in to_delete:
            del vol[key]

        self.write_override_env()

        fs.load_environment(TARGET_DIR, self.base_env)

        test_vol = self.base_env['container_skel']['cinder_volumes_container']

        self.assertIn('belongs_to', test_vol)
    def test_deleting_elements(self):
        # Leave only the 'properties' dictionary attached to simulate writing
        # a partial override file

        vol = self.cinder_config['container_skel']['cinder_volumes_container']
        keys = vol.keys()
        to_delete = []
        for key in vol.keys():
            if not key == 'properties':
                to_delete.append(key)

        for key in to_delete:
            del vol[key]

        self.write_override_env()

        fs.load_environment(TARGET_DIR, self.base_env)

        test_vol = self.base_env['container_skel']['cinder_volumes_container']

        self.assertIn('belongs_to', test_vol)
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
 def setUp(self):
     # Allow custom assertion errors.
     self.longMessage = True
     self.env = fs.load_environment(BASE_ENV_DIR, {})
 def setUp(self):
     self.longMessage = True
     self.loaded_environment = fs.load_environment(BASE_ENV_DIR, {})
 def setUp(self):
     self.env = fs.load_environment(BASE_ENV_DIR, {})
 def setUp(self):
     self.env = fs.load_environment(BASE_ENV_DIR, {})
 def setUp(self):
     self.longMessage = True
     self.loaded_environment = fs.load_environment(BASE_ENV_DIR, {})
 def setUp(self):
     # Allow custom assertion errors.
     self.longMessage = True
     self.env = fs.load_environment(BASE_ENV_DIR, {})
Beispiel #24
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')

    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