summaryrefslogtreecommitdiff
path: root/bin/taler-deployment-config-instances
blob: c7f6cef84bc3112fc52cdae83dd4317ac6846640 (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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
#!/usr/bin/env python3

"""
This script makes sure that the merchant backend instances used by the
test/demo environment are created.

We assume that the merchant backend is running, and that the "~/activate"
file has been sourced to provide the right environment variables.
"""

import requests
from os import environ, system
from sys import exit
from urllib.parse import urljoin
from subprocess import Popen
from time import sleep
import psutil
from getpass import getuser

def expect_env(name):
    val = environ.get(name)
    if not val:
        print(f"{name} not defined.  Please source the ~/activate file.")
        exit(1)
    return val

def wait_merchant():
    for proc in psutil.process_iter():
        if proc.name() == "taler-merchant-httpd" and proc.username == getuser():
            break
        sleep(1)

MERCHANT_BACKEND_BASE_URL = expect_env("TALER_ENV_MERCHANT_BACKEND")
TALER_ENV_NAME = expect_env("TALER_ENV_NAME")
TALER_CONFIG_CURRENCY = expect_env("TALER_CONFIG_CURRENCY")
TALER_ENV_FRONTENDS_APITOKEN = expect_env("TALER_ENV_FRONTENDS_APITOKEN")
authorization_header = {"Authorization": f"Bearer {TALER_ENV_FRONTENDS_APITOKEN}"}

def ensure_instance(instance_id, name, payto_uris, auth):
    # FIXME: Use auth once the default instance also uses token auth
    instance_response = requests.get(
        urljoin(MERCHANT_BACKEND_BASE_URL, f"private/instances/${instance_id}"),
        headers = authorization_header
    )
    if instance_response.status_code == 200:
        return
    req = dict(
        id=instance_id,
        name=name,
        payto_uris=payto_uris,
        address=dict(),
        jurisdiction=dict(),
        default_max_wire_fee=f"{TALER_CONFIG_CURRENCY}:1",
        default_wire_fee_amortization=3,
        default_max_deposit_fee=f"{TALER_CONFIG_CURRENCY}:1",
        default_wire_transfer_delay=dict(d_ms="forever"),
        default_pay_delay=dict(d_ms="forever"),
        # FIXME: Eventually, this should be an actual secret token
        auth=auth,
    )
    # Here authenticats as 'default' (with same credentials of other instances.)
    create_resp = requests.post(
        urljoin(MERCHANT_BACKEND_BASE_URL, "private/instances"),
        json=req,
        headers = authorization_header
    )
    if create_resp.status_code < 200 or create_resp.status_code >= 300:
        print(f"Instance '{instance_id}' could not be (re)created, backend says: {create_resp.text}.  Updating its auth now")
        if instance_id != "Tutorial":
            patch_resp = requests.post(
                urljoin(MERCHANT_BACKEND_BASE_URL,
                f"instances/{instance_id}/private/auth"),
                json=auth,
                headers = authorization_header
            )
            if patch_resp.status_code < 200 or patch_resp.status_code >= 300:
                print(f"Failed to update auth of '{instance_id}'")
                print(patch_resp.text)
                exit(1)
        else:
            exit(1)

def is_merchant_running():
    for proc in psutil.process_iter():
        if proc.name() == "taler-merchant-httpd" and proc.username == getuser():
            return True
    return False


def ensure_default_instance():
    # Assumed is managed by ARM
    if is_merchant_running():
        system("taler-deployment-arm -k taler-merchant")

    checks = 10
    while checks > 0:
        if is_merchant_running():
            sleep(1)
            checks--
            continue
        break

    if checks == 0:
        print("Could not stop the running merchant.")
        exit(1)

    # ARM is _not_ running the merchant at this point.
    env_with_token = environ.copy()
    env_with_token["TALER_MERCHANT_TOKEN"] = TALER_ENV_FRONTENDS_APITOKEN
    
    # Start the merchant natively.
    merchant = Popen(["taler-merchant-httpd"], env=env_with_token)

    # Check it started correctly and it is ready to serve requests.
    checks = 10
    while checks > 0:

        try:
            resp = requests.get(urljoin(MERCHANT_BACKEND_BASE_URL, "/config"), timeout=1.5):
        except Exception:
            sleep(1)
            checks--
            continue

        if resp.status_code != 200:
            sleep(1)
            checks--
            continue

    if checks == 0:
        print("Could not start the merchant (with TALER_MERCHANT_TOKEN in the env).")
        exit(1)

    ensure_instance(
        "default",
        "default",
        payto_uris=[f"payto://x-taler-bank/bank.{TALER_ENV_NAME}.taler.net/Taler"],
        auth=dict(method="token", token=TALER_ENV_FRONTENDS_APITOKEN)
    )

    merchant.terminate()
    merchant.wait()

    print("Starting the merchant again via ARM")
    system("taler-deployment-arm -i taler-merchant")

ensure_default_instance()
wait_merchant()

ensure_instance(
    "blog",
    name="Blog",
    payto_uris=[f"payto://x-taler-bank/bank.{TALER_ENV_NAME}.taler.net/blog"],
    auth=dict(method="token", token=TALER_ENV_FRONTENDS_APITOKEN),
)

ensure_instance(
    "donations",
    name="Donations",
    payto_uris=[f"payto://x-taler-bank/bank.{TALER_ENV_NAME}.taler.net/donations"],
    auth=dict(method="token", token=TALER_ENV_FRONTENDS_APITOKEN),
)

ensure_instance(
    "survey",
    name="Survey",
    payto_uris=[f"payto://x-taler-bank/bank.{TALER_ENV_NAME}.taler.net/survey"],
    auth=dict(method="token", token=TALER_ENV_FRONTENDS_APITOKEN),
)

ensure_instance(
    "pos",
    name="PoS",
    payto_uris=[f"payto://x-taler-bank/bank.{TALER_ENV_NAME}.taler.net/pos"],
    auth=dict(method="token", token=TALER_ENV_FRONTENDS_APITOKEN),
)

ensure_instance(
    "GNUnet",
    name="GNUnet",
    payto_uris=[f"payto://x-taler-bank/bank.{TALER_ENV_NAME}.taler.net/GNUnet"],
    auth=dict(method="token", token=TALER_ENV_FRONTENDS_APITOKEN),
)

# This instance relate to both the donation receiver and the sync service.
ensure_instance(
    "Taler",
    name="Taler",
    payto_uris=[f"payto://x-taler-bank/bank.{TALER_ENV_NAME}.taler.net/Taler"],
    auth=dict(method="token", token=TALER_ENV_FRONTENDS_APITOKEN),
)

ensure_instance(
    "Tor",
    name="Tor",
    payto_uris=[f"payto://x-taler-bank/bank.{TALER_ENV_NAME}.taler.net/Tor"],
    auth=dict(method="token", token=TALER_ENV_FRONTENDS_APITOKEN),
)

# Note: this instance has a fixed secret-token, so as to allow anyone to easily
# run their tutorial.
ensure_instance(
    "Tutorial",
    name="Tutorial",
    payto_uris=[f"payto://x-taler-bank/bank.{TALER_ENV_NAME}.taler.net/Tutorial"],
    auth=dict(method="token", token="secret-token:sandbox")
)