Ejemplo n.º 1
0
def add_boto_cli_arguments(parser, include_log_level=True, include_credentials=True, include_region=True):

    group = get_group(parser, 'boto')

    if include_log_level:
        group.add_argument(
            '--boto-log-level',
            default=DEFAULT['log_level'](),
            choices=LEVELS.keys(),
            help="Verbosity of boto logging. (default: %(default)s)",
        )

    if include_credentials:
        group.add_argument(
            '--boto-access-key',
            default=DEFAULT['access_key'](),
            help="AWS Access Key to use. Defaults to ENV[{0}]".format(ACCESS_KEY),
        )

        group.add_argument(
            '--boto-secret-key',
            default=DEFAULT['secret_key'](),
            help="AWS Secret Key to use. Defaults to ENV[{0}]".format(SECRET_KEY),
        )

    if include_region:
        group.add_argument(
            '--boto-region',
            default=DEFAULT['region'](),
            choices=[r.name for r in boto.ec2.regions()],
            help=(
                "EC2 Region to connect to. Defaults to ENV[{0}]. If not ENV set, defaults to us-east-1.".format(REGION)
            ),
        )
Ejemplo n.º 2
0
def add_boto_cli_arguments(parser):

    group = get_group(parser, 'boto')

    group.add_argument(
        '--boto-log-level',
        default=DEFAULT['log_level'](),
        choices=LEVELS.keys(),
        help='Verbosity of boto logging. (default: %(default)s)'
    )

    group.add_argument(
        '--boto-access-key',
        default=DEFAULT['access_key'](),
        help='AWS Access Key to use. Defaults to ENV[{0}]'.format(ACCESS_KEY)
    )

    group.add_argument(
        '--boto-secret-key',
        default=DEFAULT['secret_key'](),
        help='AWS Secret Key to use. Defaults to ENV[{0}]'.format(SECRET_KEY),
    )

    group.add_argument(
        '--boto-region',
        default=DEFAULT['region'](),
        choices=[r.name for r in boto.ec2.regions()],
        help='EC2 Region to connect to. (default: %(default)s)',
    )
Ejemplo n.º 3
0
def add_logging_args(parser, stdout_default=True):
    """
    Add logging-related command-line arguments to the given parser.

    Logging arguments are added to the 'logging' argument group which is
    created if it doesn't already exist.

    :argument parser: parser instance to which the arguments will be added
    """
    group = get_group(parser, "logging")

    group.add_argument(
        "--log-level",
        default=DEFAULT_LOG_LEVEL,
        choices=LEVELS.keys(),
        help="Verbosity of logging. (default: %(default)s)",
    )
    group.add_argument(
        "--log-file", default=None, help="Full-qualified path to the log file " "(default: %(default)s)."
    )

    group.add_argument(
        "--no-syslog-facility",
        dest="syslog_facility",
        action="store_const",
        default=DEFAULT_LOG_FACILITY,
        const=None,
        help="disable syslog facility",
    )
    group.add_argument(
        "--syslog-facility", default=DEFAULT_LOG_FACILITY, help="syslog facility to use " "(default: %(default)s)."
    )
    ###
    ### If logging to stdout is enabled (the default, defined by the log_to_stdout arg
    ### in __init__(), we provide a --no-log-to-stdout cli arg to disable it.
    ###
    ### If our calling script or subclass chooses to disable stdout logging by default,
    ### we instead provide a --log-to-stdout arg to enable it, for debugging etc.
    ###
    ### This is particularly useful for Icinga monitoring scripts, where we don't want
    ### logging info to reach stdout during normal operation, because Icinga ingests
    ### everything that's written there.
    ###
    if stdout_default:
        group.add_argument(
            "--no-log-to-stdout",
            dest="log_to_stdout",
            default=True,
            action="store_false",
            help="Suppress logging to stdout/stderr " "(default: %(default)s).",
        )
    else:
        group.add_argument(
            "--log-to-stdout",
            default=False,
            action="store_true",
            help="Log to stdout/stderr -- useful for debugging! " "(default: %(default)s).",
        )

    return parser
Ejemplo n.º 4
0
def add_boto_cli_arguments(parser, include_log_level=True, include_credentials=True, include_region=True):

    group = get_group(parser, 'boto')

    if include_log_level:
        group.add_argument(
            '--boto-log-level',
            default=DEFAULT['log_level'](),
            choices=LEVELS.keys(),
            help="Verbosity of boto logging. (default: %(default)s)",
        )

    if include_credentials:
        group.add_argument(
            '--boto-access-key',
            default=DEFAULT['access_key'](),
            help="AWS Access Key to use. Defaults to ENV[{0}]".format(ACCESS_KEY),
        )

        group.add_argument(
            '--boto-secret-key',
            default=DEFAULT['secret_key'](),
            help="AWS Secret Key to use. Defaults to ENV[{0}]".format(SECRET_KEY),
        )

    if include_region:
        group.add_argument(
            '--boto-region',
            default=DEFAULT['region'](),
            choices=[r.name for r in boto.ec2.regions()],
            help=(
                "EC2 Region to connect to. Defaults to ENV[{0}]. If not ENV set, defaults to us-east-1.".format(REGION)
            ),
        )
Ejemplo n.º 5
0
def add_boto_cli_arguments(parser):

    group = get_group(parser, 'boto')

    group.add_argument(
        '--boto-log-level',
        default=DEFAULT['log_level'](),
        choices=LEVELS.keys(),
        help='Verbosity of boto logging. (default: %(default)s)'
    )

    group.add_argument(
        '--boto-access-key',
        default=DEFAULT['access_key'](),
        help='AWS Access Key to use. Defaults to ENV[%s]' % ACCESS_KEY,
    )

    group.add_argument(
        '--boto-secret-key',
        default=DEFAULT['secret_key'](),
        help='AWS Secret Key to use. Defaults to ENV[%s]' % SECRET_KEY,
    )

    group.add_argument(
        '--boto-region',
        default=DEFAULT['region'](),
        choices=[r.name for r in boto.ec2.regions()],
        help='EC2 Region to connect to. (default: %(default)s)',
    )
Ejemplo n.º 6
0
    def test_add_logging_args_no_stdout(self):
        """
        krux.parser.add_logging_args() correctly sets up an _ArgumentGroup for log related arguments
        """
        actual = add_logging_args(parser=self._parser)

        # Check whether the return value is correct
        self.assertEqual(self._parser, actual)

        # Check whether an _ArgumentGroup was successfully created
        self._parser.add_argument_group.assert_called_once_with(title='logging', env_var_prefix=None)

        # Check whether the arguments were correctly created
        add_argument_calls = [
            call(
                '--log-level',
                default=DEFAULT_LOG_LEVEL,
                choices=LEVELS.keys(),
                env_var='LOG_LEVEL',
                help='Verbosity of logging.',
            ),
            call('--log-file', default=None, env_var='LOG_FILE', help='Full-qualified path to the log file'),
            call(
                '--no-syslog-facility',
                dest='syslog_facility',
                action='store_const',
                default=DEFAULT_LOG_FACILITY,
                const=None,
                env_var=False,
                add_default_help=False,
                help='disable syslog facility',
            ),
            call(
                '--syslog-facility',
                default=DEFAULT_LOG_FACILITY,
                env_var='SYSLOG_FACILITY',
                help='syslog facility to use',
            ),
            call(
                '--no-log-to-stdout',
                dest='log_to_stdout',
                default=True,
                action='store_false',
                env_var=False,
                help='Suppress logging to stdout/stderr',
            ),
        ]
        self.assertEqual(add_argument_calls, self._group.add_argument.call_args_list)
Ejemplo n.º 7
0
    def test_add_logging_args_yes_stdout(self):
        """
        krux.parser.add_logging_args() correctly disables stdout logs by default when stdout_default is set to False
        """
        add_logging_args(parser=self._parser, stdout_default=False)

        # Check whether the arguments were correctly created
        add_argument_calls = [
            call(
                '--log-level',
                default=DEFAULT_LOG_LEVEL,
                choices=LEVELS.keys(),
                env_var='LOG_LEVEL',
                help='Verbosity of logging.',
            ),
            call('--log-file', default=None, env_var='LOG_FILE', help='Full-qualified path to the log file'),
            call(
                '--no-syslog-facility',
                dest='syslog_facility',
                action='store_const',
                default=DEFAULT_LOG_FACILITY,
                const=None,
                env_var=False,
                add_default_help=False,
                help='disable syslog facility',
            ),
            call(
                '--syslog-facility',
                default=DEFAULT_LOG_FACILITY,
                env_var='SYSLOG_FACILITY',
                help='syslog facility to use',
            ),
            call(
                '--log-to-stdout',
                default=False,
                action='store_true',
                env_var=False,
                help='Log to stdout/stderr -- useful for debugging!',
            ),
        ]
        self.assertEqual(add_argument_calls, self._group.add_argument.call_args_list)
Ejemplo n.º 8
0
def add_logging_args(parser, stdout_default=True):
    """
    Add logging-related command-line arguments to the given parser.

    Logging arguments are added to the 'logging' argument group which is
    created if it doesn't already exist.

    :argument parser: parser instance to which the arguments will be added
    """
    group = get_group(parser=parser, group_name='logging')

    group.add_argument(
        '--log-level',
        default=DEFAULT_LOG_LEVEL,
        choices=LEVELS.keys(),
        env_var='LOG_LEVEL',
        help='Verbosity of logging.'
    )
    group.add_argument(
        '--log-file',
        default=None,
        env_var='LOG_FILE',
        help='Full-qualified path to the log file',
    )

    group.add_argument(
        '--no-syslog-facility',
        dest='syslog_facility',
        action='store_const',
        default=DEFAULT_LOG_FACILITY,
        const=None,
        env_var=False,
        add_default_help=False,
        help='disable syslog facility',
    )
    group.add_argument(
        '--syslog-facility',
        default=DEFAULT_LOG_FACILITY,
        env_var='SYSLOG_FACILITY',
        help='syslog facility to use',
    )
    #
    # If logging to stdout is enabled (the default, defined by the log_to_stdout arg
    # in __init__(), we provide a --no-log-to-stdout cli arg to disable it.
    #
    # If our calling script or subclass chooses to disable stdout logging by default,
    # we instead provide a --log-to-stdout arg to enable it, for debugging etc.
    #
    # This is particularly useful for Icinga monitoring scripts, where we don't want
    # logging info to reach stdout during normal operation, because Icinga ingests
    # everything that's written there.
    #
    # TODO: With the environment variable support, this use case should be handled
    #       via the environment variable. Consider removing this in v3.0
    if stdout_default:
        group.add_argument(
            '--no-log-to-stdout',
            dest='log_to_stdout',
            default=True,
            action='store_false',
            env_var=False,
            help='Suppress logging to stdout/stderr',
        )
    else:
        group.add_argument(
            '--log-to-stdout',
            default=False,
            action='store_true',
            env_var=False,
            help='Log to stdout/stderr -- useful for debugging!',
        )

    return parser