summaryrefslogtreecommitdiff
path: root/bin/taler-bank-manage
blob: 3b3156240bcf8e4c11585b3bbe44dc78e3d162a9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
#!/usr/bin/env python3

##
# This file is part of TALER
# (C) 2017 Taler Systems SA
#
# TALER is free software; you can redistribute it and/or
# modify it under the terms of the GNU Affero General Public
# License as published by the Free Software Foundation; either
# version 3, or (at your option) any later version.
#
# TALER is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty
# of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
# See the GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public
# License along with TALER; see the file COPYING.  If not,
# see <http://www.gnu.org/licenses/>
#
#  @author Florian Dold
#  @file CLI tool to manage all the bank's tasks.

import argparse
import django
import sys
import os
import os.path
import site
import logging
import inspect
import click

import talerbank
from taler.util.talerconfig import TalerConfig
from django.core.management import call_command

SITE_PACKAGES = os.path.abspath(os.path.dirname(inspect.getfile(talerbank)) + "/..")

LOGGER = logging.getLogger(__name__)

# No perfect match to our logging format, but good enough ...
UWSGI_LOGFMT = "%(ltime) %(proto) %(method) %(uri) %(proto) => %(status)"
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "talerbank.settings")

@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"] + sys.argv[2:])


def handle_serve_http(port):
    import django
    django.setup()
    print("migrating")
    call_command('migrate')
    print("providing accounts")
    call_command('provide_accounts')
    print("checking")
    call_command('check')
    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,)
    params = ["uwsgi", "uwsgi",
              "--static-map", "/static=%s/talerbank/app/static" % SITE_PACKAGES,
              "--die-on-term",
              "--no-orphans",
              "--master",
              "--http", httpspec,
              "--log-format", UWSGI_LOGFMT,
              "--module", "talerbank.wsgi"]
    os.execlp(*params)


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"))
    serve_uwsgi = TC["bank"]["uwsgi_serve"].value_string(required=True).lower()
    params = ["uwsgi", "uwsgi",
              "--static-map", "/static=%s/talerbank/app/static" % SITE_PACKAGES,
              "--die-on-term",
              "--no-orphans",
              "--master",
              "--log-format", UWSGI_LOGFMT,
              "--module", "talerbank.wsgi"]
    if serve_uwsgi == "tcp":
        port = TC["bank"]["uwsgi_port"].value_int(required=True)
        spec = ":%d" % (port,)
        params.extend(["--socket", spec])
    else:
        spec = TC["bank"]["uwsgi_unixpath"].value_filename(required=True)
        mode = TC["bank"]["uwsgi_unixpath_mode"].value_filename(required=True)
        params.extend(["--socket", spec])
        params.extend(["--chmod-socket="+mode])
        os.makedirs(os.path.dirname(spec), exist_ok=True)
    logging.info("launching uwsgi with argv %s", params[1:])
    os.execlp(*params)

@cli.command(help="Print config values.")
def config():
    TC = TalerConfig.from_file(os.environ.get("TALER_CONFIG_FILE"))
    TC.dump()

cli()