diff --git a/setup.py b/setup.py --- a/setup.py +++ b/setup.py @@ -50,6 +50,10 @@ vcversioner={}, include_package_data=True, zip_safe=False, + entry_points=''' + [console_scripts] + swh-vault=swh.vault.cli:main + ''', classifiers=[ "Programming Language :: Python :: 3", "Intended Audience :: Developers", diff --git a/swh/vault/api/server.py b/swh/vault/api/server.py --- a/swh/vault/api/server.py +++ b/swh/vault/api/server.py @@ -5,7 +5,6 @@ import aiohttp.web import asyncio -import click import collections from swh.core import config @@ -204,17 +203,5 @@ return make_app(api_cfg, **kwargs) -@click.command() -@click.argument('config-path', required=1) -@click.option('--host', default='0.0.0.0', help="Host to run the server") -@click.option('--port', default=5005, type=click.INT, - help="Binding port of the server") -@click.option('--debug/--nodebug', default=True, - help="Indicates if the server should run in debug mode") -def launch(config_path, host, port, debug): - app = make_app(config.read(config_path, DEFAULT_CONFIG), debug=bool(debug)) - aiohttp.web.run_app(app, host=host, port=int(port)) - - if __name__ == '__main__': - launch() + print('Deprecated. Use swh-vault ') diff --git a/swh/vault/cli.py b/swh/vault/cli.py new file mode 100644 --- /dev/null +++ b/swh/vault/cli.py @@ -0,0 +1,91 @@ +import logging +import click +import aiohttp + +from swh.core import config +from swh.vault import get_vault +from swh.vault.api.server import make_app, DEFAULT_CONFIG + + +CONTEXT_SETTINGS = dict(help_option_names=['-h', '--help']) + + +@click.group(context_settings=CONTEXT_SETTINGS) +@click.option('--config-file', '-C', default=None, + type=click.Path(exists=True, dir_okay=False,), + help="Configuration file.") +@click.option('--database', '-d', default=None, + help="Scheduling database DSN (imply cls is 'local')") +@click.option('--url', '-u', default=None, + help="Scheduler's url access (imply cls is 'remote')") +@click.option('--log-level', '-l', default='INFO', + type=click.Choice(logging._nameToLevel.keys()), + help="Log level (default to INFO)") +@click.option('--no-stdout', is_flag=True, default=False, + help="Do NOT output logs on the console") +@click.pass_context +def cli(ctx, config_file, database, url, log_level, no_stdout): + """Software Heritage Scheduler CLI interface + + Default to use the the local scheduler instance (plugged to the + main scheduler db). + + """ + from swh.scheduler.celery_backend.config import setup_log_handler + log_level = setup_log_handler( + loglevel=log_level, colorize=False, + format='[%(levelname)s] %(name)s -- %(message)s', + log_console=not no_stdout) + + ctx.ensure_object(dict) + + logger = logging.getLogger(__name__) + vault = None + conf = config.read(config_file, DEFAULT_CONFIG) + if 'vault' not in conf: + raise ValueError("missing 'vault' configuration") + + if database: + conf['vault']['cls'] = 'local' + conf['vault']['args']['db'] = database + elif url: + conf['vault']['cls'] = 'remote' + conf['vault']['args'] = {'url': url} + sched_conf = conf['vault'] + try: + logger.debug('Instanciating vault with %s' % ( + sched_conf)) + vault = get_vault(**conf) + except ValueError: + # it's the subcommand to decide whether not having a proper + # vault instance is a problem. + pass + + ctx.obj['vault'] = vault + ctx.obj['config'] = conf + ctx.obj['loglevel'] = log_level + + +@cli.command('api-server') +@click.option('--host', default='0.0.0.0', help="Host to run the server") +@click.option('--port', default=5005, type=click.INT, + help="Binding port of the server") +@click.option('--debug/--nodebug', default=True, + help="Indicates if the server should run in debug mode") +@click.pass_context +def serve(ctx, host, port, debug): + if ctx.obj['config']['vault']['cls'] == 'remote': + click.echo("The API server can only be started with a 'local' " + "configuration", err=True) + ctx.exit(1) + app = make_app(ctx.obj['config'], backend=ctx.obj['vault'], + debug=bool(debug)) + aiohttp.web.run_app(app, host=host, port=int(port)) + + +def main(): + return cli(auto_envvar_prefix='SWH_VAULT') + + +if __name__ == '__main__': + main()