Exemplo n.º 1
0
 def _build_run_vars(self):
     if self.options.get('options'):
         run_vars = util.merge_dicts(
             util.safe_load(self.pipe_config.dump_configs()), self.options)
     else:
         run_vars = util.safe_load(self.pipe_config.dump_configs())
     return run_vars
Exemplo n.º 2
0
    def _build_group_configs(self):
        """
        Performs the merging of variables defined in file_vars with
        those found in group_vars. The inspiration for this
        functionality was taken from Ansible's group_vars and file_vars.
        If a file definition exists in file_vars.d/ that also exists
        in a group_vars RunConfig, we overwrite the group_vars RunConfig
        variable with the one found in file_vars.
        :return: list
        """
        group_configs = []
        for group in self._read_group_vars():
            for step, config in group['config'].items():
                if step == f'pi_{self.name}':
                    run_config = RunConfig(group['file'], config,
                                           self.base_config)
                    for file_definition in run_config.files:
                        for file, file_name in self._read_file_vars():
                            file_config = util.safe_load(file)
                            try:
                                if file_definition['file'] == file_config[
                                        'file']:
                                    file_definition.update(file_config)
                            except KeyError as e:
                                message = f'Invalid file_vars config in {file_name}. ' \
                                          f'\n\nInvalid Key: {e}'
                                util.sysexit_with_message(message)
                    group_configs.append(run_config)
                elif self.name == 'validate':
                    run_config = RunConfig(group['file'], config,
                                           self.base_config)
                    for file_definition in run_config.files:
                        for file, file_name in self._read_file_vars():
                            file_config = util.safe_load(file)
                            try:
                                if file_definition['file'] == file_config[
                                        'file']:
                                    file_definition.update(file_config)
                            except KeyError as e:
                                message = f'Invalid file_vars config in {file_name}.' \
                                          f'\n\nInvalid Key: {e}'
                                util.sysexit_with_message(message)
                    group_configs.append(run_config)
        if not len(group_configs):
            message = f'No group configs found for pi_{self.name} in' \
                      f'{self.base_config.vars_dir}/group_vars.d/'
            util.sysexit_with_message(message)

        return group_configs
Exemplo n.º 3
0
    def _read_group_vars(self):
        """
        Read all files in {base_dir}/piedpiper.d/{vars_dir}/group_vars.d/
        and returns a list of variable configurations. We first parse all.yml
        if it exists so that it is the first item in the list. This allows for
        the other group_vars files to override the values in all.yml
        :return: list
        """
        group_vars_dir = f'{self.base_config.vars_dir}/group_vars.d'

        group_configs = []
        if os.path.isdir(group_vars_dir):
            for root, dirs, files in os.walk(
                    f'{self.base_config.vars_dir}/group_vars.d/'):
                if not len(files):
                    message = f'No group_vars found in {self.base_config.vars_dir}'
                    util.sysexit_with_message(message)
                for file in files:
                    with open(os.path.join(root, file)) as f:
                        group_config = f.read()
                        group_configs.append({
                            'file':
                            file,
                            'config':
                            util.safe_load(group_config)
                        })
            return group_configs
        else:
            message = f'Failed to read group_vars in {self.base_config.vars_dir}.'
            util.sysexit_with_message(message)
Exemplo n.º 4
0
 def _read_config(self, config):
     """
     Read pi_global_vars configuration file
     and return a YAML object.
     :param config: Path to configuration file
     :return: YAML object
     """
     try:
         with open(config) as c:
             return util.safe_load(c)
     except IOError as e:
         message = f"Failed to parse config. \n\n{e}"
         util.sysexit_with_message(message)
Exemplo n.º 5
0
    def _build_pipe_config(self):
        """
        Read pipe_vars.d for configuration file for the pipe.
        Each child class will have its own pi_{self.name}.yml file located in
        {vars_dir}/pipe_vars.d/ which will be read during
        creation of the child class object.

        :return: Configuration dictionary for the pipe
        """
        try:
            with open(
                f'{self.base_config.vars_dir}/pipe_vars.d/pi_{self.name}.yml'
            ) as config:
                return util.safe_load(config)
        except IOError as e:
            message = f"Failed to parse pi_{self.name}.yml. \n\n{e}"
            util.sysexit_with_message(message)