Example #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
Example #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)
        new_instances = [
        ]  # To avoid odd issues from iterating over a list you delete from, build a new instead
        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
Example #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
Example #4
0
def main(argv=None):
    parser = argparse.ArgumentParser(
        description='Configure and setup the agent. In a full run it will' +
        ' detect running daemons then configure and start the agent.',
        formatter_class=argparse.ArgumentDefaultsHelpFormatter)
    args = parse_arguments(parser)

    if args.verbose:
        logging.basicConfig(level=logging.DEBUG,
                            format="%(levelname)s: %(message)s")
    else:
        logging.basicConfig(level=logging.INFO,
                            format="%(levelname)s: %(message)s")

    if args.dry_run:
        LOG.info("Running in dry run mode, no changes will be made only"
                 " reported")

    # Detect and if possibly enable the agent service
    agent_service = detect_init(PREFIX_DIR, args.config_dir, args.log_dir,
                                args.template_dir, username=args.user,
                                name=args.agent_service_name)

    # Skip base setup if only installing plugins or running specific detection
    # plugins
    if not args.install_plugins_only and args.detection_plugins is None:
        if not args.skip_enable:
            agent_service.enable()

        # Verify required options
        if (args.username is None or
                args.password is None or
                args.keystone_url is None):
            LOG.error('Username, password and keystone_url are required when'
                      ' running full configuration.')
            parser.print_help()
            sys.exit(1)
        base_configuration(args)

    # Collect the set of detection plugins to run
    detected_plugins = utils.discover_plugins(CUSTOM_PLUGIN_PATH)
    if args.system_only:
        from monasca_setup.detection.plugins.system import System
        plugins = [System]
    elif args.detection_plugins is not None:
        plugins = utils.select_plugins(args.detection_plugins,
                                       detected_plugins)
    elif args.skip_detection_plugins is not None:
        plugins = utils.select_plugins(args.skip_detection_plugins,
                                       detected_plugins, skip=True)
    else:
        plugins = detected_plugins
    plugin_names = [p.__name__ for p in plugins]

    # Remove entries for each plugin from the various plugin config files.
    if args.remove:
        changes = remove_config(args, plugin_names)
    else:
        # Run detection for all the plugins, halting on any failures if plugins
        # were specified in the arguments
        detected_config = plugin_detection(plugins, args.template_dir,
                                           args.detection_args,
                                           args.detection_args_json,
                                           skip_failed=(args.detection_plugins
                                                        is None))
        if detected_config is None:
            # Indicates detection problem, skip remaining steps and give
            # non-zero exit code
            return 1

        changes = modify_config(args, detected_config)

    # Don't restart if only doing detection plugins and no changes found
    if args.detection_plugins is not None and not changes:
        LOG.info(
            'No changes found for plugins {0}, skipping restart of'
            'Monasca Agent'.format(plugin_names))
        return 0
    elif args.dry_run:
        LOG.info('Running in dry mode, skipping changes and restart of'
                 ' Monasca Agent')
        return 0

    # Now that the config is built, start the service
    if args.install_plugins_only:
        LOG.info('Command line option install_plugins_only set, skipping '
                 'service (re)start.')
    else:
        try:
            agent_service.start(restart=True)
        except subprocess.CalledProcessError:
            LOG.error('The service did not startup correctly see %s',
                      args.log_dir)
Example #5
0
def main(argv=None):
    parser = argparse.ArgumentParser(
        description=
        'Configure and setup the agent. In a full run it will detect running' +
        ' daemons then configure and start the agent.',
        formatter_class=argparse.ArgumentDefaultsHelpFormatter)
    args = parse_arguments(parser)

    if args.verbose:
        logging.basicConfig(level=logging.DEBUG,
                            format="%(levelname)s: %(message)s")
    else:
        logging.basicConfig(level=logging.INFO,
                            format="%(levelname)s: %(message)s")

    if args.dry_run:
        log.info(
            "Running in dry run mode, no changes will be made only reported")

    # Detect and if possibly enable the agent service
    agent_service = detect_init(PREFIX_DIR,
                                args.config_dir,
                                args.log_dir,
                                args.template_dir,
                                username=args.user)

    # Skip base setup if only installing plugins or running specific detection
    # plugins
    if not args.install_plugins_only and args.detection_plugins is None:
        if not args.skip_enable:
            agent_service.enable()

        # Verify required options
        if args.username is None or args.password is None or args.keystone_url is None:
            log.error(
                'Username, password and keystone_url are required when running full configuration.'
            )
            parser.print_help()
            sys.exit(1)
        base_configuration(args)

    # Collect the set of detection plugins to run
    detected_plugins = utils.discover_plugins(CUSTOM_PLUGIN_PATH)
    if args.system_only:
        from detection.plugins.system import System
        plugins = [System]
    elif args.detection_plugins is not None:
        plugins = utils.select_plugins(args.detection_plugins,
                                       detected_plugins)
    elif args.skip_detection_plugins is not None:
        plugins = utils.select_plugins(args.skip_detection_plugins,
                                       detected_plugins,
                                       skip=True)
    else:
        plugins = detected_plugins
    plugin_names = [p.__name__ for p in plugins]

    if args.remove:  # Remove entries for each plugin from the various plugin config files
        changes = remove_config(args, plugin_names)
    else:
        # Run detection for all the plugins, halting on any failures if plugins were specified in the arguments
        detected_config = plugin_detection(
            plugins,
            args.template_dir,
            args.detection_args,
            skip_failed=(args.detection_plugins is None))
        if detected_config is None:
            return 1  # Indicates detection problem, skip remaining steps and give non-zero exit code

        changes = modify_config(args, detected_config)

    # Don't restart if only doing detection plugins and no changes found
    if args.detection_plugins is not None and not changes:
        log.info(
            'No changes found for plugins {0}, skipping restart of Monasca Agent'
            .format(plugin_names))
        return 0
    elif args.dry_run:
        log.info(
            'Running in dry mode, skipping changes and restart of Monasca Agent'
        )
        return 0

    # Now that the config is built, start the service
    if args.install_plugins_only:
        log.info('Command line option install_plugins_only set, skipping '
                 'service (re)start.')
    else:
        try:
            agent_service.start(restart=True)
        except subprocess.CalledProcessError:
            log.error('The service did not startup correctly see %s' %
                      args.log_dir)
Example #6
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