kappa 3.45 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.
from datetime import datetime
import logging

import click

from kappa.context import Context


@click.group()
@click.argument(
    'config',
    type=click.File('rb'),
    envvar='KAPPA_CONFIG',
)
@click.option(
    '--debug/--no-debug',
    default=False,
    help='Turn on debugging output'
)
@click.pass_context
def cli(ctx, config=None, debug=False):
    config = config
    ctx.obj['debug'] = debug
    ctx.obj['config'] = config

@cli.command()
@click.pass_context
def deploy(ctx):
    context = Context(ctx.obj['config'], ctx.obj['debug'])
    click.echo('deploying...')
    context.deploy()
    click.echo('...done')

@cli.command()
@click.pass_context
def test(ctx):
    context = Context(ctx.obj['config'], ctx.obj['debug'])
    click.echo('testing...')
    context.test()
    click.echo('...done')

@cli.command()
@click.pass_context
def tail(ctx):
    context = Context(ctx.obj['config'], ctx.obj['debug'])
    click.echo('tailing logs...')
    for e in context.tail()[-10:]:
        ts = datetime.utcfromtimestamp(e['timestamp']//1000).isoformat()
        click.echo("{}: {}".format(ts, e['message']))
    click.echo('...done')

@cli.command()
@click.pass_context
def status(ctx):
    context = Context(ctx.obj['config'], ctx.obj['debug'])
    status = context.status()
    click.echo(click.style('Stack', bold=True))
    if status['stack']:
        for stack in status['stack']['Stacks']:
            line = '    {}: {}'.format(stack['StackId'], stack['StackStatus'])
            click.echo(click.style(line, fg='green'))
    else:
        click.echo(click.style('    None', fg='green'))
    click.echo(click.style('Function', bold=True))
    if status['function']:
        line = '    {}'.format(
            status['function']['Configuration']['FunctionName'])
        click.echo(click.style(line, fg='green'))
    else:
        click.echo(click.style('    None', fg='green'))
    click.echo(click.style('Event Sources', bold=True))
    if status['event_sources']:
        for event_source in status['event_sources']:
            if 'EventSource' in event_source:
                line = '    {}: {}'.format(
                    event_source['EventSource'], event_source['IsActive'])
                click.echo(click.style(line, fg='green'))
            else:
                line = '    {}'.format(
                    event_source['CloudFunctionConfiguration']['Id'])
                click.echo(click.style(line, fg='green'))
    else:
        click.echo(click.style('    None', fg='green'))

@cli.command()
@click.pass_context
def delete(ctx):
    context = Context(ctx.obj['config'], ctx.obj['debug'])
    click.echo('deleting...')
    context.delete()
    click.echo('...done')

@cli.command()
@click.pass_context
def add_event_sources(ctx):
    context = Context(ctx.obj['config'], ctx.obj['debug'])
    click.echo('adding event sources...')
    context.add_event_sources()
    click.echo('...done')


if __name__ == '__main__':
    cli(obj={})