示例#1
0
def remove_config(args, plugin_names):
    """Parse all configuration removing any configuration built by plugins in plugin_names
       Note there is no concept of overwrite for removal.
    :param args: specified arguments
    :param plugin_names: A list of the plugin names to remove from the config
    :return: True if changes, False otherwise
    """
    changes = False
    existing_config_files = _get_config_yaml_files(args.config_dir)
    if existing_config_files == []:
        LOG.warning(
            "Found no existing configuration files, no changes will be made!")
    detected_plugins = utils.discover_plugins(CUSTOM_PLUGIN_PATH)
    plugins = utils.select_plugins(args.detection_plugins, detected_plugins)
    LOG.debug("Plugins selected: %s", plugins)

    if args.detection_args or args.detection_args_json:
        detected_config = plugin_detection(
            plugins,
            args.template_dir,
            args.detection_args,
            args.detection_args_json,
            skip_failed=(args.detection_plugins is None),
            remove=True)
    LOG.debug("Detected configuration: %s", detected_config)

    for file_path in existing_config_files:
        deletes = False
        plugin_name = os.path.splitext(os.path.basename(file_path))[0]
        config = agent_config.read_plugin_config_from_disk(
            args.config_dir, plugin_name)
        # To avoid odd issues from iterating over a list you delete from, build a new instead
        new_instances = []
        if args.detection_args is None:
            # JSON version of detection_args
            for inst in config['instances']:
                if 'built_by' in inst and inst['built_by'] in plugin_names:
                    LOG.debug("Removing %s", inst)
                    changes = True
                    deletes = True
                    continue
                new_instances.append(inst)
            config['instances'] = new_instances
        else:
            for detected_key in detected_config.keys():
                for inst in detected_config[detected_key]['instances']:
                    if inst in config['instances']:
                        LOG.debug("Removing %s", inst)
                        changes = True
                        deletes = True
                        config['instances'].remove(inst)
        # TODO(joadavis) match dry-run functionality like in modify_config
        if deletes:
            agent_config.delete_from_config(args, config, file_path,
                                            plugin_name)
    return changes
示例#2
0
def remove_config(args, plugin_names):
    """Parse all configuration removing any configuration built by plugins in plugin_names
       Note there is no concept of overwrite for removal.
    :param args: specified arguments
    :param plugin_names: A list of the plugin names to remove from the config
    :return: True if changes, false otherwise
    """
    changes = False
    existing_config_files = glob(
        os.path.join(args.config_dir, 'conf.d', '*.yaml'))
    detected_plugins = utils.discover_plugins(CUSTOM_PLUGIN_PATH)
    plugins = utils.select_plugins(args.detection_plugins, detected_plugins)

    if (args.detection_args or args.detection_args_json):
        detected_config = plugin_detection(
            plugins,
            args.template_dir,
            args.detection_args,
            args.detection_args_json,
            skip_failed=(args.detection_plugins is None),
            remove=True)

    for file_path in existing_config_files:
        deletes = False
        plugin_name = os.path.splitext(os.path.basename(file_path))[0]
        config = agent_config.read_plugin_config_from_disk(
            args.config_dir, plugin_name)
        # To avoid odd issues from iterating over a list you delete from, build a new instead
        new_instances = []
        if args.detection_args is None:
            for inst in config['instances']:
                if 'built_by' in inst and inst['built_by'] in plugin_names:
                    changes = True
                    deletes = True
                    continue
                new_instances.append(inst)
            config['instances'] = new_instances
        else:
            for detected_key in detected_config.keys():
                for inst in detected_config[detected_key]['instances']:
                    if inst in config['instances']:
                        changes = True
                        deletes = True
                        config['instances'].remove(inst)
        if deletes:
            agent_config.delete_from_config(args, config, file_path,
                                            plugin_name)
    return changes
示例#3
0
def remove_config(args, plugin_names):
    """Parse all configuration removing any configuration built by plugins in plugin_names
       Note there is no concept of overwrite for removal.
    :param args: specified arguments
    :param plugin_names: A list of the plugin names to remove from the config
    :return: True if changes, false otherwise
    """
    changes = False
    existing_config_files = glob(os.path.join(args.config_dir, 'conf.d', '*.yaml'))
    detected_plugins = utils.discover_plugins(CUSTOM_PLUGIN_PATH)
    plugins = utils.select_plugins(args.detection_plugins, detected_plugins)

    if (args.detection_args or args.detection_args_json):
        detected_config = plugin_detection(
            plugins, args.template_dir, args.detection_args, args.detection_args_json,
            skip_failed=(args.detection_plugins is None), remove=True)

    for file_path in existing_config_files:
        deletes = False
        plugin_name = os.path.splitext(os.path.basename(file_path))[0]
        config = agent_config.read_plugin_config_from_disk(args.config_dir, plugin_name)
        # To avoid odd issues from iterating over a list you delete from, build a new instead
        new_instances = []
        if args.detection_args is None:
            for inst in config['instances']:
                if 'built_by' in inst and inst['built_by'] in plugin_names:
                    changes = True
                    deletes = True
                    continue
                new_instances.append(inst)
            config['instances'] = new_instances
        else:
            for detected_key in detected_config.keys():
                for inst in detected_config[detected_key]['instances']:
                    if inst in config['instances']:
                        changes = True
                        deletes = True
                        config['instances'].remove(inst)
        if deletes:
            agent_config.delete_from_config(args, config, file_path,
                                            plugin_name)
    return changes
示例#4
0
def modify_config(args, detected_config):
    """Compare existing and detected config for each check plugin and write out
       the plugin config if there are changes
    """
    modified_config = False

    for detection_plugin_name, new_config in detected_config.items():
        if args.overwrite:
            modified_config = True
            if args.dry_run:
                continue
            else:
                agent_config.save_plugin_config(
                    args.config_dir, detection_plugin_name, args.user,
                    new_config)
        else:
            config = agent_config.read_plugin_config_from_disk(
                args.config_dir, detection_plugin_name)
            # merge old and new config, new has precedence
            if config is not None:
                # For HttpCheck, if the new input url has the same host and
                # port but a different protocol comparing with one of the
                # existing instances in http_check.yaml, we want to keep the
                # existing http check instance and replace the url with the
                # new protocol. If name in this instance is the same as the
                # url, we replace name with new url too.
                # For more details please see:
                # monasca-agent/docs/DeveloperDocs/agent_internals.md
                if detection_plugin_name == "http_check":
                    # Save old http_check urls from config for later comparison
                    config_urls = [i['url'] for i in config['instances'] if
                                   'url' in i]

                    # Check endpoint change, use new protocol instead
                    # Note: config is possibly changed after running
                    # check_endpoint_changes function.
                    config = agent_config.check_endpoint_changes(new_config,
                                                                 config)

                agent_config.merge_by_name(new_config['instances'],
                                           config['instances'])
                # Sort before compare, if instances have no name the sort will
                #  fail making order changes significant
                try:
                    new_config['instances'].sort(key=lambda k: k['name'])
                    config['instances'].sort(key=lambda k: k['name'])
                except Exception:
                    pass

                if detection_plugin_name == "http_check":
                    new_config_urls = [i['url'] for i in new_config['instances']
                                       if 'url' in i]
                    # Don't write config if no change
                    if new_config_urls == config_urls and new_config == config:
                        continue
                else:
                    if new_config == config:
                        continue
            modified_config = True
            if args.dry_run:
                LOG.info("Changes would be made to the config file for the {0}"
                         " check plugin".format(detection_plugin_name))
            else:
                agent_config.save_plugin_config(
                    args.config_dir, detection_plugin_name, args.user,
                    new_config)
    return modified_config
示例#5
0
def remove_config_for_matching_args(args, plugin_names):
    """Parse all configuration removing any configuration built by plugins in plugin_names
       Will use the generated config fields to match against the stored configs
       Intended for use when removing all config for a deleted compute host.  May delete
       more than intended in other uses, so be cautious.
       Note there is no concept of overwrite for removal.
    :param args: specified arguments. detection_args or detection_args_json are Required.
    :param plugin_names: A list of the plugin names to remove from the config
    :return: True if changes, False otherwise
    """
    changes = False
    existing_config_files = _get_config_yaml_files(args.config_dir)
    if existing_config_files == []:
        LOG.warning(
            "Found no existing configuration files, no changes will be made!")
    detected_plugins = utils.discover_plugins(CUSTOM_PLUGIN_PATH)
    plugins = utils.select_plugins(args.detection_plugins, detected_plugins)
    LOG.debug("Plugins selected: %s", plugins)

    if args.detection_args or args.detection_args_json:
        detected_config = plugin_detection(
            plugins,
            args.template_dir,
            args.detection_args,
            args.detection_args_json,
            skip_failed=(args.detection_plugins is None),
            remove=True)
    else:
        # this method requires detection_args
        LOG.warning("Removing a configuration for matching arguments requires"
                    " arguments. No changes to configuration will be made!")
        return changes
    LOG.debug("Detected configuration: %s", detected_config)

    for file_path in existing_config_files:
        deletes = False
        plugin_name = os.path.splitext(os.path.basename(file_path))[0]
        config = agent_config.read_plugin_config_from_disk(
            args.config_dir, plugin_name)
        # To avoid odd issues from iterating over a list you delete from, build a new instead
        new_instances = []
        if args.detection_args is None:
            # using detection_args_json
            LOG.error(
                "Only key-value argument format is currently supported for removing "
                "matching configuration. JSON format is not yet supported. "
                "No changes to configuration will be made!")
            return False
        else:
            # here is where it will differ from remove_config()
            # detected_config = generated based on args, config = read from disk
            # for each field in the detected_config instance, check it matches the one on disk
            # note that the one on disk is allowed to have more fields
            for exist_inst in config['instances']:
                for detected_key in detected_config.keys():
                    for detected_inst in detected_config[detected_key][
                            'instances']:
                        if len(detected_inst.keys()) < 1:
                            new_instances.append(exist_inst)
                            continue
                        needs_delete = True
                        for detect_inst_key in detected_inst.keys():
                            if detect_inst_key in exist_inst.keys():
                                if detected_inst[detect_inst_key] != exist_inst[
                                        detect_inst_key]:
                                    needs_delete = False
                            else:
                                # not a match
                                needs_delete = False
                                continue
                        if needs_delete:
                            LOG.debug("Removing configuration %s", exist_inst)
                            changes = True
                            deletes = True
                            continue
                        new_instances.append(exist_inst)
            config['instances'] = new_instances
        # TODO(joadavis) match dry-run functionality like in modify_config
        if deletes:
            agent_config.delete_from_config(args, config, file_path,
                                            plugin_name)
    return changes
示例#6
0
def modify_config(args, detected_config):
    """Compare existing and detected config for each check plugin and write out
       the plugin config if there are changes
    """
    modified_config = False

    for detection_plugin_name, new_config in detected_config.items():
        if args.overwrite:
            modified_config = True
            if args.dry_run:
                continue
            else:
                agent_config.save_plugin_config(args.config_dir,
                                                detection_plugin_name,
                                                args.user, new_config)
        else:
            config = agent_config.read_plugin_config_from_disk(
                args.config_dir, detection_plugin_name)
            # merge old and new config, new has precedence
            if config is not None:
                # For HttpCheck, if the new input url has the same host and
                # port but a different protocol comparing with one of the
                # existing instances in http_check.yaml, we want to keep the
                # existing http check instance and replace the url with the
                # new protocol. If name in this instance is the same as the
                # url, we replace name with new url too.
                # For more details please see:
                # monasca-agent/docs/DeveloperDocs/agent_internals.md
                if detection_plugin_name == "http_check":
                    # Save old http_check urls from config for later comparison
                    config_urls = [
                        i['url'] for i in config['instances'] if 'url' in i
                    ]

                    # Check endpoint change, use new protocol instead
                    # Note: config is possibly changed after running
                    # check_endpoint_changes function.
                    config = agent_config.check_endpoint_changes(
                        new_config, config)

                agent_config.merge_by_name(new_config['instances'],
                                           config['instances'])
                # Sort before compare, if instances have no name the sort will
                #  fail making order changes significant
                try:
                    new_config['instances'].sort(key=lambda k: k['name'])
                    config['instances'].sort(key=lambda k: k['name'])
                except Exception:
                    pass

                if detection_plugin_name == "http_check":
                    new_config_urls = [
                        i['url'] for i in new_config['instances'] if 'url' in i
                    ]
                    # Don't write config if no change
                    if new_config_urls == config_urls and new_config == config:
                        continue
                else:
                    if new_config == config:
                        continue
            modified_config = True
            if args.dry_run:
                LOG.info("Changes would be made to the config file for the {0}"
                         " check plugin".format(detection_plugin_name))
            else:
                agent_config.save_plugin_config(args.config_dir,
                                                detection_plugin_name,
                                                args.user, new_config)
    return modified_config