Пример #1
0
def merge_conf_file(src_file, dst_file, merge_mode="stanza_overwrite"):
    if not os.path.isfile(src_file):
        return
    if not os.path.isfile(dst_file):
        return
    if bn(src_file) in merge_black_list:
        return

    parser = TABConfigParser()
    parser.read(src_file)
    src_dict = parser.item_dict()
    parser.read(dst_file)
    dst_dict = parser.item_dict()

    if merge_mode == "stanza_overwrite":
        for stanza, key_values in src_dict.items():
            if stanza in dst_dict:
                parser.remove_section(stanza)

            parser.add_section(stanza)

            for k, v in key_values.items():
                parser.set(stanza, k, v)
    elif merge_mode == "item_overwrite":
        for stanza, key_values in src_dict.items():
            if stanza not in dst_dict:
                parser.add_section(stanza)

            for k, v in key_values.items():
                if v:
                    parser.set(stanza, k, v)
                else:
                    parser.remove_option(stanza, k)
    else:
        # overwrit the whole file
        parser.read(src_file)

    with open(dst_file, "w") as df:
        parser.write(df)
Пример #2
0
def remove_alert_from_conf_file(alert, conf_file, logger):
    if not alert or not conf_file:
        logger.info('alert="%s", conf_file="%s"', alert, conf_file)
        return

    if not isinstance(alert, dict):
        msg = 'alert="{}", event="alert is not a dict, don\'t remove anything form file {}"'.format(alert, conf_file)
        raise aae.AlertCleaningFormatFailure(msg)

    parser = TABConfigParser()
    parser.read(conf_file)
    conf_dict = parser.item_dict()

    for stanza, key_values in conf_dict.items():
        if stanza == alert[ac.SHORT_NAME] or \
            stanza == alert[ac.SHORT_NAME] + "_modaction_result" or \
                stanza == "eventtype=" + alert[ac.SHORT_NAME] + "_modaction_result":
            logger.info('alert="%s", conf_file="%s", stanza="%s"',
                        alert[ac.SHORT_NAME],
                        conf_file, stanza)
            parser.remove_section(stanza)

    with open(conf_file, "w") as cf:
        parser.write(cf)
Пример #3
0
def _merge_conf_file(app_root_dir, conf_file_name):
    '''
    :param app_root_dir: the root directory full path
    :param conf_file_name: the conf file short path, like 'app.conf'. It must endswith ".conf"
    '''
    if not conf_file_name.endswith('.conf'):
        _logger.error('%s is not a conf file. Can not merge it.',
                         conf_file_name)
        return
    dft_conf = os.path.join(app_root_dir, "default", conf_file_name)
    usr_conf = os.path.join(app_root_dir, "local", conf_file_name)
    if os.path.isfile(dft_conf):
        if os.path.isfile(usr_conf):
            parser = TABConfigParser()
            parser.read(usr_conf)
            # for the inputs.conf, filter all the input instance stanzas in local conf
            if usr_conf.split(os.path.sep)[-1] == 'inputs.conf':
                to_be_delete_sections = [s for s in parser.sections() if len(s.split("://")) == 2]
                if to_be_delete_sections:
                    _logger.info("Remove stanzas %s in conf %s", to_be_delete_sections, usr_conf)
                for s in to_be_delete_sections:
                    parser.remove_section(s)

            local_dict = parser.item_dict()
            parser.read(dft_conf)
            default_dict = parser.item_dict()
            # overwrite the key values by local dict
            for stanza, key_values in list(local_dict.items()):
                if stanza not in default_dict:
                    parser.add_section(stanza)
                for k, v in list(key_values.items()):
                    parser.set(stanza, k, v)

            with open(dft_conf, "w") as conf_file:
                parser.write(conf_file)

            _logger.info("%s is merged to %s", usr_conf, dft_conf)
        else:
            _logger.debug("No need to merge. User Conf %s not found!",
                             usr_conf)
    else:
        if os.path.isfile(usr_conf):
            p = TABConfigParser()
            p.read(usr_conf)
            if p.sections() or p.fields_outside_stanza:
                shutil.copyfile(usr_conf, dft_conf)
                _logger.info("copy %s to %s, because %s not found.", usr_conf, dft_conf, dft_conf)
            else:
                os.remove(usr_conf)
                _logger.info('remove {} because it is a empty conf file.'.format(usr_conf))
                return
        else:
            _logger.error(
                "Both default conf %s and user conf %s are not found!",
                dft_conf, usr_conf)
            return
    # if it is inputs.conf, set default disabled = 0
    if (conf_file_name.split(os.path.sep)[-1]
        ) == 'inputs.conf' and os.path.isfile(dft_conf):
        parser = TABConfigParser()
        parser.read(dft_conf)
        item_dict = parser.item_dict()
        for section, key_values in list(item_dict.items()):
            splits = section.split("://")
            # it's default section
            if len(splits) == 1:
                parser.set(section, "disabled", 0)

        with open(dft_conf, "w") as fp:
            parser.write(fp)