From 7dcf91d3c8bd6bbb39469cca1309c9b4eadd061f Mon Sep 17 00:00:00 2001 From: Aurélien Bompard Date: Feb 12 2018 10:03:08 +0000 Subject: Use click for the command-line --- diff --git a/ansible/roles/hubs/templates/honcho-procfile b/ansible/roles/hubs/templates/honcho-procfile index b5fe049..7be0312 100644 --- a/ansible/roles/hubs/templates/honcho-procfile +++ b/ansible/roles/hubs/templates/honcho-procfile @@ -1,6 +1,6 @@ web: /usr/bin/flask-3 run --host 0.0.0.0 --port 5000 -triage: fedora-hubs-triage -worker: fedora-hubs-worker +triage: fedora-hubs triage +worker: fedora-hubs worker sse: /usr/bin/twistd-3 -l - --pidfile= -n hubs-sse fedmsg_hub: /usr/bin/fedmsg-hub-3 fedmsg_relay: /usr/bin/fedmsg-relay-3 diff --git a/deploy/fedora-hubs-triage@.service b/deploy/fedora-hubs-triage@.service index e109355..1f5fb02 100644 --- a/deploy/fedora-hubs-triage@.service +++ b/deploy/fedora-hubs-triage@.service @@ -4,7 +4,7 @@ After=network.target Documentation=https://pagure.io/fedora-hubs/ [Service] -ExecStart=/usr/bin/fedora-hubs-triage +ExecStart=/usr/bin/fedora-hubs triage EnvironmentFile=/etc/sysconfig/fedora-hubs Type=simple User=hubs diff --git a/deploy/fedora-hubs-worker@.service b/deploy/fedora-hubs-worker@.service index 734d506..867180a 100644 --- a/deploy/fedora-hubs-worker@.service +++ b/deploy/fedora-hubs-worker@.service @@ -4,7 +4,7 @@ After=network.target Documentation=https://pagure.io/fedora-hubs/ [Service] -ExecStart=/usr/bin/fedora-hubs-worker +ExecStart=/usr/bin/fedora-hubs worker EnvironmentFile=/etc/sysconfig/fedora-hubs Type=simple User=hubs diff --git a/fedora-hubs.spec b/fedora-hubs.spec index 7a4504c..80922da 100644 --- a/fedora-hubs.spec +++ b/fedora-hubs.spec @@ -25,6 +25,7 @@ BuildRequires: python3-arrow BuildRequires: python3-beautifulsoup4 BuildRequires: python3-bleach BuildRequires: python3-blinker +BuildRequires: python3-click BuildRequires: python3-dateutil BuildRequires: python3-decorator BuildRequires: python3-dogpile-cache @@ -56,6 +57,7 @@ Requires: python3-arrow Requires: python3-beautifulsoup4 Requires: python3-bleach Requires: python3-blinker +Requires: python3-click Requires: python3-dateutil Requires: python3-decorator Requires: python3-dogpile-cache @@ -207,7 +209,7 @@ done %doc README.rst %{python3_sitelib}/* %{_unitdir}/*.service -%{_bindir}/fedora-hubs-* +%{_bindir}/fedora-hubs %dir %{_sysconfdir}/%{name} %config(noreplace) %attr(640,root,%{username}) %{_sysconfdir}/%{name}/hubs.py %config(noreplace) %{_sysconfdir}/%{name}/logging.ini diff --git a/hubs/backend/triage.py b/hubs/backend/triage.py index 47cbc26..d23da00 100755 --- a/hubs/backend/triage.py +++ b/hubs/backend/triage.py @@ -20,13 +20,13 @@ handle. from __future__ import unicode_literals -import argparse import json import logging import logging.config import random import sys +import click import retask.queue import hubs.app @@ -34,6 +34,7 @@ import hubs.database import hubs.feed import hubs.models import hubs.widgets.base +#from hubs.commands import cli log = logging.getLogger('hubs.backend.triage') @@ -175,19 +176,13 @@ def get_widgets(): return widgets -def parse_args(args): - parser = argparse.ArgumentParser( - description='Triage messages from the bus.') - parser.add_argument("-d", "--debug", action="store_true", - help="debugging output level.") - return parser.parse_args() - - -def main(args=None): - args = args if args is not None else sys.argv - args = parse_args(args) +@click.command("triage") +@click.option("-d", "--debug/--no-debug", default=False, + help="Debugging output level.") +def main(debug): + """Triage messages from the bus.""" logging.config.dictConfig(fedmsg_config['logging']) - log_level = logging.DEBUG if args.debug else logging.INFO + log_level = logging.DEBUG if debug else logging.INFO logging.basicConfig(level=log_level) # XXX - for flask.url_for to work diff --git a/hubs/backend/worker.py b/hubs/backend/worker.py index f6bea2e..8b378b7 100755 --- a/hubs/backend/worker.py +++ b/hubs/backend/worker.py @@ -25,12 +25,12 @@ clients with the new content produced here. from __future__ import unicode_literals -import argparse import json import logging import logging.config import sys +import click import fedmsg.meta import retask.queue @@ -74,18 +74,13 @@ def add_sse_task(sse_queue, event, data, target): sse_queue.enqueue(retask.task.Task(json.dumps(sse_task))) -def parse_args(args): - parser = argparse.ArgumentParser(description='Rebuild widget caches.') - parser.add_argument("-d", "--debug", action="store_true", - help="debugging output level.") - return parser.parse_args() - - -def main(args=None): - args = args if args is not None else sys.argv - args = parse_args(args) +@click.command("worker") +@click.option("-d", "--debug/--no-debug", default=False, + help="Debugging output level.") +def main(debug): + """Rebuild widget caches.""" logging.config.dictConfig(fedmsg_config['logging']) - log_level = logging.DEBUG if args.debug else logging.INFO + log_level = logging.DEBUG if debug else logging.INFO logging.basicConfig(level=log_level) # XXX - for flask.url_for to work diff --git a/hubs/commands.py b/hubs/commands.py new file mode 100644 index 0000000..a6cde99 --- /dev/null +++ b/hubs/commands.py @@ -0,0 +1,15 @@ +import click + +from hubs.backend.triage import main as triage +from hubs.backend.worker import main as worker +from hubs.fas.scripts import create_team_from_fas, sync_teams_from_fas + + +@click.group() +def cli(): + pass + +cli.add_command(triage) +cli.add_command(worker) +cli.add_command(create_team_from_fas) +cli.add_command(sync_teams_from_fas) diff --git a/hubs/fas/scripts.py b/hubs/fas/scripts.py index 3ca79d2..7069c13 100644 --- a/hubs/fas/scripts.py +++ b/hubs/fas/scripts.py @@ -1,8 +1,7 @@ from __future__ import unicode_literals, print_function -from argparse import ArgumentParser - +import click from fedora.client import AppError import hubs.app @@ -14,12 +13,10 @@ from .fasclient import FASClient from .api import sync_team_hub, sync_team_hub_roles -def create_group_from_fas(): - parser = ArgumentParser() - parser.add_argument( - "name", help="group to create the hub for") - args = parser.parse_args() - hub_name = args.name +@click.command("create-team-from-fas") +@click.argument("name") +def create_team_from_fas(hub_name): + """Create the team hub NAME from FAS.""" fedmsg_config = get_fedmsg_config() hubs.database.init(fedmsg_config['hubs.sqlalchemy.uri']) hub = Hub.by_name(hub_name, "team") @@ -42,20 +39,18 @@ def create_group_from_fas(): print("It will be synced from FAS in the background.") -def sync_group_from_fas(): - parser = ArgumentParser() - parser.add_argument( - "name", nargs="*", help="team hub to sync") - parser.add_argument( - "--no-roles", action="store_true", help="do not sync roles") - args = parser.parse_args() +@click.command("sync-teams-from-fas") +@click.option("--roles/--no-roles", default=False, help="Sync the roles.") +@click.argument("name", nargs=-1) +def sync_teams_from_fas(roles, names): + """Sync all the team hubs NAMEs from FAS.""" fedmsg_config = get_fedmsg_config() hubs.database.init(fedmsg_config['hubs.sqlalchemy.uri']) - if not args.name: + if not names: hubs_to_sync = Hub.query.filter_by(hub_type="team").all() else: hubs_to_sync = [] - for hub_name in args.name: + for hub_name in names: hub = hubs.models.Hub.by_name(hub_name, "team") if hub is None: print("The team hub {} does not exist.".format(hub_name)) @@ -67,7 +62,7 @@ def sync_group_from_fas(): affected_users = [] try: sync_team_hub(hub.id) - if not args.no_roles: + if roles: affected_users = sync_team_hub_roles(hub.id) except AppError as e: print("Failed syncing hub {}: {}".format(hub.name, e.message)) diff --git a/requirements.txt b/requirements.txt index cc9e29e..91e102c 100644 --- a/requirements.txt +++ b/requirements.txt @@ -3,6 +3,7 @@ arrow beautifulsoup4 bleach blinker +click python-dateutil decorator dogpile.cache diff --git a/setup.py b/setup.py index d2a373c..1e21c79 100644 --- a/setup.py +++ b/setup.py @@ -47,10 +47,7 @@ setup( "cache_invalidator = hubs.backend.consumer:CacheInvalidatorExtraordinaire", # noqa: E501 ], 'console_scripts': [ - "fedora-hubs-triage = hubs.backend.triage:main", - "fedora-hubs-worker = hubs.backend.worker:main", - "fedora-hubs-create-group-from-fas = hubs.fas.scripts:create_group_from_fas", # noqa: E501 - "fedora-hubs-sync-group-from-fas = hubs.fas.scripts:sync_group_from_fas", # noqa: E501 + "fedora-hubs = hubs.commands:cli", ], }, ) diff --git a/systemd/hubs-triage@.service b/systemd/hubs-triage@.service index 400ddd7..db5b9c8 100644 --- a/systemd/hubs-triage@.service +++ b/systemd/hubs-triage@.service @@ -4,7 +4,7 @@ After=network.target Documentation=https://pagure.io/fedora-hubs/ [Service] -ExecStart=/usr/bin/fedora-hubs-triage +ExecStart=/usr/bin/fedora-hubs triage WorkingDirectory=/srv/hubs/fedora-hubs/ Environment=HUBS_CONFIG=/srv/hubs/fedora-hubs/config Type=simple diff --git a/systemd/hubs-worker@.service b/systemd/hubs-worker@.service index a84f3de..d253779 100644 --- a/systemd/hubs-worker@.service +++ b/systemd/hubs-worker@.service @@ -4,7 +4,7 @@ After=network.target Documentation=https://pagure.io/fedora-hubs/ [Service] -ExecStart=/usr/bin/fedora-hubs-worker +ExecStart=/usr/bin/fedora-hubs worker Environment=HUBS_CONFIG=/srv/git/fedora-hubs/config Type=simple User=root