Esempio n. 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 = glob(os.path.join(args.config_dir, 'conf.d', '*.yaml'))
    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)
        new_instances = []  # To avoid odd issues from iterating over a list you delete from build a new instead
        for i in range(len(config['instances'])):
            inst = config['instances'][i]
            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
        if deletes:
            if args.dry_run:
                log.info("Changes would be made to the config file {0}".format(file_path))
            else:
                if len(config['instances']) == 0:
                    log.info("Removing configuration file {0} it is no longer needed.".format(file_path))
                    os.remove(file_path)
                else:
                    log.info("Saving changes to configuration file {0}.".format(file_path))
                    agent_config.save_plugin_config(args.config_dir, plugin_name, args.user, config)
    return changes
Esempio n. 2
0
def modify_config(args, detected_config):
    changes = False
    # Compare existing and detected config for each check plugin and write out the plugin config if changes
    for key, value in detected_config.iteritems():
        if args.overwrite:
            changes = True
            if args.dry_run:
                continue
            else:
                agent_config.save_plugin_config(args.config_dir, key,
                                                args.user, value)
        else:
            old_config = agent_config.read_plugin_config_from_disk(
                args.config_dir, key)
            # merge old and new config, new has precedence
            if old_config is not None:
                agent_config.merge_by_name(value['instances'],
                                           old_config['instances'])
                # Sort before compare, if instances have no name the sort will fail making order changes significant
                try:
                    value['instances'].sort(key=lambda k: k['name'])
                    old_config['instances'].sort(key=lambda k: k['name'])
                except Exception:
                    pass
                if value == old_config:  # Don't write config if no change
                    continue
            changes = True
            if args.dry_run:
                log.info(
                    "Changes would be made to the config file for the {0} check plugin"
                    .format(key))
            else:
                agent_config.save_plugin_config(args.config_dir, key,
                                                args.user, value)
    return changes
Esempio n. 3
0
def modify_config(args, detected_config):
    changes = False
    # Compare existing and detected config for each check plugin and write out the plugin config if changes
    for key, value in detected_config.iteritems():
        if args.overwrite:
            changes = True
            if args.dry_run:
                continue
            else:
                agent_config.save_plugin_config(args.config_dir, key, args.user, value)
        else:
            old_config = agent_config.read_plugin_config_from_disk(args.config_dir, key)
            # merge old and new config, new has precedence
            if old_config is not None:
                agent_config.merge_by_name(value['instances'], old_config['instances'])
                # Sort before compare, if instances have no name the sort will fail making order changes significant
                try:
                    value['instances'].sort(key=lambda k: k['name'])
                    old_config['instances'].sort(key=lambda k: k['name'])
                except Exception:
                    pass
                if value == old_config:  # Don't write config if no change
                    continue
            changes = True
            if args.dry_run:
                log.info("Changes would be made to the config file for the {0} check plugin".format(key))
            else:
                agent_config.save_plugin_config(args.config_dir, key, args.user, value)
    return changes
Esempio n. 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
Esempio n. 5
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.iteritems():
        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