Skip to content

Commit

Permalink
Add cli.
Browse files Browse the repository at this point in the history
  • Loading branch information
heavenshell committed Mar 8, 2015
1 parent 02c0fa6 commit e03a369
Show file tree
Hide file tree
Showing 3 changed files with 93 additions and 1 deletion.
87 changes: 87 additions & 0 deletions mackerel/runner.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
mackerel.runner
~~~~~~~~~~~~~~~
Mackerel client implemented by Python.
Ported from `mackerel-client-ruby`.
<https://github.com/mackerelio/mackerel-client-ruby>
:copyright: (c) 2015 Shinya Ohyanagi, All rights reserved.
:license: BSD, see LICENSE for more details.
"""
import os
import logging
import click
from mackerel.client import Client


class Runner(object):

def __init__(self, verbose=False):
api_key = os.environ.get('MACKEREL_APIKEY')
logger = None
level = logging.INFO
if verbose is True:
level = logging.DEBUG

logging.getLogger('requests').level = logging.ERROR
logging.basicConfig(level=level, format=Client.debug_log_format)
logger = logging.getLogger('mackerel.client')

self.client = Client(mackerel_api_key=api_key, logger=logger)


@click.group()
@click.option('--verbose', default=False, type=click.BOOL)
@click.pass_context
def cli(ctx, verbose):
runner = Runner(verbose)
ctx.obj['instance'] = runner


@cli.command()
@click.option('--host-id', default=None)
@click.option('--status', default=None)
@click.pass_context
def status(ctx, host_id, status):
runner = ctx.obj['instance']
if status is None:
if host_id is None:
data = runner.client.get_hosts()
else:
data = [runner.client.get_host(host_id)]
message = 'name: {0} status: {1} id: {2} roles: {3}'
for d in data:
click.echo(message.format(d.name, d.status, d.id, d.roles))
else:
runner.client.update_host_status(host_id, status)


@cli.command()
@click.option('--name', default=None)
@click.option('--service', default=None)
@click.option('--role', default=None)
@click.pass_context
def info(ctx, name, service, role):
runner = ctx.obj['instance']
data = runner.client.get_hosts(name=name, service=service, role=role)
[click.echo(host.status) for host in data]


@cli.command()
@click.option('--host-id')
@click.pass_context
def retire(ctx, host_id):
runner = ctx.obj['instance']
runner.client.retire_host(host_id)


def main():
cli(obj={})


if __name__ == '__main__':
main()
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
requests
click
simplejson
mock
6 changes: 5 additions & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
import os
from setuptools import setup, find_packages

requires = ['requests', 'simplejson']
requires = ['requests', 'simplejson', 'click']

app_name = 'mackerel.client'

Expand Down Expand Up @@ -43,6 +43,10 @@
'Operating System :: OS Independent',
'Programming Language :: Python'
],
entry_points="""
[console_scripts]
mkr.py = mackerel.runner:main
""",
tests_require=['requests', 'simplejson', 'mock'],
test_suite='tests'
)

0 comments on commit e03a369

Please sign in to comment.