kappa 4.5 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 base64

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 create(ctx):
    context = Context(ctx.obj['config'], ctx.obj['debug'])
    click.echo('creating...')
    context.create()
    click.echo('...done')

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

@cli.command()
@click.pass_context
def invoke(ctx):
    context = Context(ctx.obj['config'], ctx.obj['debug'])
    click.echo('invoking...')
    response = context.invoke()
    log_data = base64.b64decode(response['LogResult'])
    click.echo(log_data)
    click.echo('...done')

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

@cli.command()
@click.pass_context
def invoke_async(ctx):
    context = Context(ctx.obj['config'], ctx.obj['debug'])
    click.echo('invoking async...')
    response = context.invoke_async()
    click.echo(response)
    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('Policy', bold=True))
    if status['policy']:
        line = '    {} ({})'.format(
            status['policy']['PolicyName'],
            status['policy']['Arn'])
        click.echo(click.style(line, fg='green'))
    click.echo(click.style('Role', bold=True))
    if status['role']:
        line = '    {} ({})'.format(
            status['role']['Role']['RoleName'],
            status['role']['Role']['Arn'])
        click.echo(click.style(line, fg='green'))
    click.echo(click.style('Function', bold=True))
    if status['function']:
        line = '    {} ({})'.format(
            status['function']['Configuration']['FunctionName'],
            status['function']['Configuration']['FunctionArn'])
        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 event_source:
                line = '    {}: {}'.format(
                    event_source['EventSourceArn'], event_source['State'])
                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')

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


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