summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMS <ms@taler.net>2020-11-05 14:13:29 +0100
committerMS <ms@taler.net>2020-11-05 14:13:29 +0100
commit845e0c347a3ac55fa266547cd95e536661f33bcc (patch)
treeff3511da76459537067ee597ea86c81b2206ef7f
parent986457c76f4ecea48af8286a2b8c65b1309e2e67 (diff)
downloadbank-845e0c347a3ac55fa266547cd95e536661f33bcc.tar.gz
bank-845e0c347a3ac55fa266547cd95e536661f33bcc.tar.bz2
bank-845e0c347a3ac55fa266547cd95e536661f33bcc.zip
migrate to Click
-rwxr-xr-xbin/taler-bank-manage141
1 files changed, 45 insertions, 96 deletions
diff --git a/bin/taler-bank-manage b/bin/taler-bank-manage
index 7c06b0d..3b31562 100755
--- a/bin/taler-bank-manage
+++ b/bin/taler-bank-manage
@@ -29,6 +29,7 @@ import os.path
import site
import logging
import inspect
+import click
import talerbank
from taler.util.talerconfig import TalerConfig
@@ -36,33 +37,54 @@ from django.core.management import call_command
SITE_PACKAGES = os.path.abspath(os.path.dirname(inspect.getfile(talerbank)) + "/..")
-## Not commenting global variables, at least for now.
-# @cond
LOGGER = logging.getLogger(__name__)
# No perfect match to our logging format, but good enough ...
UWSGI_LOGFMT = "%(ltime) %(proto) %(method) %(uri) %(proto) => %(status)"
-## @endcond
+os.environ.setdefault("DJANGO_SETTINGS_MODULE", "talerbank.settings")
-##
-# This function interprets the 'django' subcommand.
-# This command usually manages database tasks, and in
-# general what is not related to running the bank.
-#
-# @param args command line options.
+@click.group(help="Manager script of Taler bank.")
+@click.pass_context
+@click.option("--http-port", help="Set HTTP as the serving protocol, and set the port.")
+@click.option("--config", help="Path to the config file.")
+@click.option("--with-db", help="Database connection string.")
+def cli(ctx, http_port, config, with_db):
+ if with_db:
+ os.environ.setdefault("TALER_BANK_ALTDB", with_db)
+ if config:
+ os.environ["TALER_CONFIG_FILE"] = config
+
+ ctx.obj = dict(http_port=http_port, config=config)
+
+@cli.command(help="Serve the bank")
+@click.pass_obj
+def serve(obj):
+ # if --http-port option is found, then serve via HTTP.
+ # Otherwise serve on whatever protocol is specified in the config.
+ serve = "http"
+ if not obj["http_port"]:
+ TC = TalerConfig.from_file(os.environ.get("TALER_CONFIG_FILE"))
+ serve = TC["bank"]["serve"].value_string(required=True).lower()
+
+ if serve == "http":
+ return handle_serve_http(obj["http_port"])
+ handle_serve_uwsgi()
+
+
+@cli.command(
+ help="Invoke 'django' sub-commands",
+ context_settings=dict(ignore_unknown_options=True)
+)
+@click.argument("args", nargs=-1, type=click.UNPROCESSED)
def handle_django(args):
django.setup()
# always run 'migrate' first, in case a virgin db is being used.
call_command('migrate')
from django.core.management import execute_from_command_line
- execute_from_command_line([sys.argv[0] + " django"] + args.command)
+ execute_from_command_line([sys.argv[0] + " django"] + sys.argv[2:])
-##
-# This function interprets the 'serve-http' subcommand.
-# The effect it to launch the bank HTTP service.
-#
-# @param args command line options.
-def handle_serve_http(args):
+
+def handle_serve_http(port):
import django
django.setup()
print("migrating")
@@ -71,9 +93,8 @@ def handle_serve_http(args):
call_command('provide_accounts')
print("checking")
call_command('check')
- port = args.port
- TC = TalerConfig.from_file(os.environ.get("TALER_CONFIG_FILE", args.config))
if port is None:
+ TC = TalerConfig.from_file(os.environ.get("TALER_CONFIG_FILE"))
port = TC["bank"]["http_port"].value_int(required=True)
httpspec = ":%d" % (port,)
@@ -87,26 +108,13 @@ def handle_serve_http(args):
"--module", "talerbank.wsgi"]
os.execlp(*params)
-def handle_serve(args):
- TC = TalerConfig.from_file(os.environ.get("TALER_CONFIG_FILE", args.config))
- serve = TC["bank"]["serve"].value_string(required=True).lower()
- if serve == "http":
- return handle_serve_http(args)
- handle_serve_uwsgi(args)
-##
-# This function interprets the 'serve-uwsgi' subcommand.
-# The effect is to launch the bank UWSGI service. This
-# type of service is usually used when the HTTP bank interface
-# is accessed via a reverse proxy (like Nginx, for example).
-#
-# @param command line options.
-def handle_serve_uwsgi(args):
+def handle_serve_uwsgi():
django.setup()
call_command('migrate')
call_command('provide_accounts')
call_command('check')
- TC = TalerConfig.from_file(os.environ.get("TALER_CONFIG_FILE", args.config))
+ TC = TalerConfig.from_file(os.environ.get("TALER_CONFIG_FILE"))
serve_uwsgi = TC["bank"]["uwsgi_serve"].value_string(required=True).lower()
params = ["uwsgi", "uwsgi",
"--static-map", "/static=%s/talerbank/app/static" % SITE_PACKAGES,
@@ -128,68 +136,9 @@ def handle_serve_uwsgi(args):
logging.info("launching uwsgi with argv %s", params[1:])
os.execlp(*params)
-##
-# Currently deprecated.
-def handle_sampledata():
- django.setup()
- call_command('sample_donations')
-
-
-##
-# Interprets the '--config' option.
-#
-# @param args command line options.
-def handle_config(args):
- TC = TalerConfig.from_file(os.environ.get("TALER_CONFIG_FILE", args.config))
- TC.from_file(args.config)
+@cli.command(help="Print config values.")
+def config():
+ TC = TalerConfig.from_file(os.environ.get("TALER_CONFIG_FILE"))
TC.dump()
-
-## Not commenting global variables, at least for now.
-# @cond
-PARSER = argparse.ArgumentParser()
-PARSER.set_defaults(func=None)
-PARSER.add_argument('--config', '-c', help="configuration file to use",
- metavar="CONFIG", type=str, dest="config", default=None)
-PARSER.add_argument('--http-port', '-p', help="serve via HTTP on PORT (has precedence over the configuration)",
- metavar="PORT", type=int, dest="port", default=None)
-
-PARSER.add_argument('--with-db', type=str, metavar="dbtype:///dbname", dest="altdb",
- help="use 'dbname' (currently only 'dbtype'=='postgres' is supported)")
-SUB = PARSER.add_subparsers()
-
-P = SUB.add_parser('django', help="Run django-admin command")
-P.add_argument("command", nargs=argparse.REMAINDER)
-P.set_defaults(func=handle_django)
-
-P = SUB.add_parser('serve', help="Serve bank according to the configuration")
-P.set_defaults(func=handle_serve)
-
-P = SUB.add_parser('config', help="show config")
-P.set_defaults(func=handle_config)
-
-ARGS = PARSER.parse_args()
-if not ARGS.func:
- print("Please give one of the following sub-commands: serve, django, config")
- sys.exit(1)
-
-os.environ.setdefault("DJANGO_SETTINGS_MODULE", "talerbank.settings")
-
-if ARGS.altdb:
- LOGGER.info("Setting alternate db: %s" % ARGS.altdb)
- os.environ.setdefault("TALER_BANK_ALTDB", ARGS.altdb)
-
-if ARGS.config is not None:
- os.environ["TALER_CONFIG_FILE"] = ARGS.config
-
-if ARGS.port:
- LOGGER.info("Serve via HTTP, on port: %s" % ARGS.port)
- handle_serve_http(ARGS)
- sys.exit(0)
-
-try:
- ARGS.func(ARGS)
-except django.db.utils.OperationalError as error:
- LOGGER.error(error)
- sys.exit(4)
-## @endcond
+cli()