Skip to content

Commit

Permalink
Merge pull request #10 from garnaat/add-status-command
Browse files Browse the repository at this point in the history
Some refactoring.  Added a status command.  Rewrote the CLI to take more...
  • Loading branch information
garnaat committed Mar 5, 2015
2 parents 89b41b7 + 171582b commit ed19928
Show file tree
Hide file tree
Showing 8 changed files with 144 additions and 43 deletions.
115 changes: 82 additions & 33 deletions bin/kappa
Original file line number Diff line number Diff line change
Expand Up @@ -18,47 +18,96 @@ import click
from kappa.context import Context


@click.command()
@click.option(
'--config',
help="Path to the Kappa config YAML file",
@click.group()
@click.argument(
'config',
type=click.File('rb'),
envvar='KAPPA_CONFIG',
default=None
)
@click.option(
'--debug/--no-debug',
default=False,
help='Turn on debugging output'
)
@click.argument(
'command',
required=True,
type=click.Choice(['deploy', 'test', 'tail', 'add-event-sources', 'delete'])
)
def main(config=None, debug=False, command=None):
ctx = Context(config, debug)
if command == 'deploy':
click.echo('Deploying ...')
ctx.deploy()
click.echo('...done')
elif command == 'test':
click.echo('Sending test data ...')
ctx.test()
click.echo('...done')
elif command == 'tail':
events = ctx.tail()
for event in events:
print(event['message'])
elif command == 'delete':
click.echo('Deleting ...')
ctx.delete()
click.echo('...done')
elif command == 'add-event-sources':
click.echo('Adding event sources ...')
ctx.add_event_sources()
click.echo('...done')
@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...')
context.tail()
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__':
main()
cli(obj={})
16 changes: 12 additions & 4 deletions kappa/context.py
Original file line number Diff line number Diff line change
Expand Up @@ -108,10 +108,7 @@ def add_event_sources(self):
event_source.add(self.function)

def deploy(self):
if self._stack.exists():
self._stack.update()
else:
self._stack.create()
self._stack.update()
self.function.upload()

def test(self):
Expand All @@ -123,3 +120,14 @@ def tail(self):
def delete(self):
self._stack.delete()
self.function.delete()
for event_source in self.event_sources:
event_source.remove(self.function)

def status(self):
status = {}
status['stack'] = self._stack.status()
status['function'] = self.function.status()
status['event_sources'] = []
for event_source in self.event_sources:
status['event_sources'].append(event_source.status(self.function))
return status
22 changes: 22 additions & 0 deletions kappa/event_source.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@

import logging

from botocore.exceptions import ClientError

import kappa.aws

LOG = logging.getLogger(__name__)
Expand Down Expand Up @@ -70,6 +72,17 @@ def remove(self, function):
LOG.debug(response)
return response

def status(self, function):
LOG.debug('getting status for event source %s', self.arn)
try:
response = self._lambda.get_event_source(
UUID=self._get_uuid(function))
LOG.debug(response)
except ClientError:
LOG.debug('event source %s does not exist', self.arn)
response = None
return response


class S3EventSource(EventSource):

Expand Down Expand Up @@ -112,3 +125,12 @@ def remove(self, function):
Bucket=self._get_bucket_name(),
NotificationConfiguration=response)
LOG.debug(response)

def status(self, function):
LOG.debug('status for s3 notification for %s', function.name)
response = self._s3.get_bucket_notification(
Bucket=self._get_bucket_name())
LOG.debug(response)
if 'CloudFunctionConfiguration' not in response:
response = None
return response
13 changes: 13 additions & 0 deletions kappa/function.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
import os
import zipfile

from botocore.exceptions import ClientError

import kappa.aws
import kappa.log

Expand Down Expand Up @@ -148,6 +150,17 @@ def delete(self):
LOG.debug(response)
return response

def status(self):
LOG.debug('getting status for function %s', self.name)
try:
response = self._lambda_svc.get_function(
FunctionName=self.name)
LOG.debug(response)
except ClientError:
LOG.debug('function %s not found', self.name)
response = None
return response

def invoke_asynch(self, data_file):
LOG.debug('_invoke_async %s', data_file)
with open(data_file) as fp:
Expand Down
13 changes: 11 additions & 2 deletions kappa/stack.py
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ def wait(self):
msg = 'Could not create stack %s: %s' % (self.name, status)
raise ValueError(msg)

def create(self):
def _create(self):
LOG.debug('create_stack: stack_name=%s', self.name)
template_body = open(self.template_path).read()
try:
Expand All @@ -110,7 +110,7 @@ def create(self):
LOG.exception('Unable to create stack')
self.wait()

def update(self):
def _update(self):
LOG.debug('create_stack: stack_name=%s', self.name)
template_body = open(self.template_path).read()
try:
Expand All @@ -125,6 +125,15 @@ def update(self):
LOG.exception('Unable to update stack')
self.wait()

def update(self):
if self.exists():
self._update()
else:
self._create()

def status(self):
return self.exists()

def delete(self):
LOG.debug('delete_stack: stack_name=%s', self.name)
try:
Expand Down
2 changes: 1 addition & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
botocore==0.82.0
botocore==0.94.0
click==3.3
PyYAML>=3.11
mock>=1.0.1
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import os

requires = [
'botocore==0.82.0',
'botocore==0.94.0',
'click==3.3',
'PyYAML>=3.11'
]
Expand Down
4 changes: 2 additions & 2 deletions tests/unit/test_stack.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,10 +56,10 @@ def test_exists(self):
stack = Stack(mock_context, Config)
self.assertTrue(stack.exists())

def test_create(self):
def test_update(self):
mock_context = mock.Mock()
stack = Stack(mock_context, Config)
stack.create()
stack.update()

def test_delete(self):
mock_context = mock.Mock()
Expand Down

0 comments on commit ed19928

Please sign in to comment.