Example #1
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 #2
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_matching_args:
        LOG.debug("Calling remove configuration for matching arguments.")
        changes = remove_config_for_matching_args(args, plugin_names)
    elif 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)