kappa 2.15 KB
#!/usr/bin/env python
# Copyright (c) 2014 Mitch Garnaat http://garnaat.org/
#
# Licensed under the Apache License, Version 2.0 (the "License"). You
# may not use this file except in compliance with the License. A copy of
# the License is located at
#
# http://aws.amazon.com/apache2.0/
#
# or in the "license" file accompanying this file. This file is
# distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF
# ANY KIND, either express or implied. See the License for the specific
# language governing permissions and limitations under the License.
import logging

import click
import yaml

from kappa import Kappa

FmtString = '%(asctime)s - %(name)s - %(levelname)s - %(message)s'


def set_debug_logger(logger_names=['kappa'], stream=None):
    """
    Convenience function to quickly configure full debug output
    to go to the console.
    """
    for logger_name in logger_names:
        log = logging.getLogger(logger_name)
        log.setLevel(logging.DEBUG)

    ch = logging.StreamHandler(stream)
    ch.setLevel(logging.DEBUG)

    # create formatter
    formatter = logging.Formatter(FmtString)

    # add formatter to ch
    ch.setFormatter(formatter)

    # add ch to logger
    log.addHandler(ch)


@click.command()
@click.option(
    '--config',
    help="Path to the Kappa config YAML file",
    type=click.File('rb'),
    envvar='KAPPA_CONFIG',
    default=None
)
@click.option(
    '--debug/--no-debug',
    default=False,
    help='Turn on debugging output'
)
@click.option(
    '--dryrun/--no-dryrun',
    default=False,
    help='Do not send the email'
)
@click.argument(
    'command',
    required=True,
    type=click.Choice(['deploy', 'test', 'tail', 'add-event-source', 'delete'])
)
def main(config=None, debug=False, dryrun=False, command=None):
    if debug:
        set_debug_logger()
    config = yaml.load(config)
    kappa = Kappa(config)
    if command == 'deploy':
        kappa.deploy()
    elif command == 'test':
        kappa.test()
    elif command == 'tail':
        kappa.tail()
    elif command == 'delete':
        kappa.delete()
    elif command == 'add-event-source':
        kappa.add_event_source()


if __name__ == '__main__':
    main()