summaryrefslogtreecommitdiff
path: root/taler-merchant-survey.in
blob: a5d5403295306256620975ec7c5a9f4101811954 (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
#!/usr/bin/env python3

##
# This file is part of TALER
# (C) 2017 INRIA
#
# 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 Standalone script to launch the Survey site.

import argparse
import sys
import os
import site
import logging
from talersurvey.talerconfig import TalerConfig

os.environ.setdefault("TALER_PREFIX", "@prefix@")
site.addsitedir("%s/lib/python%d.%d/site-packages" % (
    "@prefix@",
    sys.version_info.major,
    sys.version_info.minor))


## @cond
LOGGER = logging.getLogger(__name__)
# No perfect match to our logging format, but good enough ...
UWSGI_LOGFMT = "%(ltime) %(proto) %(method) %(uri) %(proto) => %(status)"
## @endcond


##
# This function interprets the 'serve-http' subcommand.
# The effect it to launch the Survey site as a HTTP service.
#
# @param args command line options.
def handle_serve_http(args):
    port = args.port
    if port is None:
        port = TC["survey"]["http_port"].value_int(required=True)
    spec = ":%d" % (port,)
    os.execlp("uwsgi", "uwsgi",
              "--master",
              "--die-on-term",
              "--log-format", UWSGI_LOGFMT,
              "--http", spec,
              "--wsgi-file", "@prefix@/share/taler/frontend-survey.wsgi")


##
# This function interprets the 'serve-uwsgi' subcommand.
# The effect is to launch the Survey UWSGI service.  This
# type of service is usually used when the HTTP Survey interface
# is accessed via a reverse proxy (like Nginx, for example).
#
# @param command line options.
def handle_serve_uwsgi(args):
    del args # pacify PEP checkers
    serve_uwsgi = TC["survey"]["uwsgi_serve"].value_string(required=True).lower()
    params = ["uwsgi", "uwsgi",
              "--master",
              "--die-on-term",
              "--log-format", UWSGI_LOGFMT,
              "--wsgi-file", "@prefix@/share/taler/frontend-survey.wsgi"]
    if serve_uwsgi == "tcp":
        port = TC["survey"]["uwsgi_port"].value_int(required=True)
        spec = ":%d" % (port,)
        params.extend(["--socket", spec])
    elif serve_uwsgi == "unix":
        spec = TC["survey"]["uwsgi_unixpath"].value_filename(required=True)
        mode = TC["survey"]["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)


## @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)
SUB = PARSER.add_subparsers()

P = SUB.add_parser('serve-http', help="Serve over HTTP")
P.add_argument("--port", "-p", dest="port", type=int, default=None, metavar="PORT")
P.set_defaults(func=handle_serve_http)

P = SUB.add_parser('serve-uwsgi', help="Serve over UWSGI")
P.set_defaults(func=handle_serve_uwsgi)

ARGS = PARSER.parse_args()
## @endcond

if getattr(ARGS, 'func', None) is None:
    PARSER.print_help()
    sys.exit(1)

if ARGS.config is not None:
    os.environ["TALER_CONFIG_FILE"] = ARGS.config
TC = TalerConfig.from_file(os.environ.get("TALER_CONFIG_FILE"))

ARGS.func(ARGS)