taler-android

Android apps for GNU Taler (wallet, PoS, cashier)
Log | Files | Refs | README | LICENSE

stack.sh (14762B)


      1 #!/bin/bash
      2 # This file is part of GNU Taler
      3 # (C) 2026 Taler Systems S.A.
      4 #
      5 # GNU Taler is free software; you can redistribute it and/or modify it under the
      6 # terms of the GNU General Public License as published by the Free Software
      7 # Foundation; either version 3, or (at your option) any later version.
      8 #
      9 # GNU Taler is distributed in the hope that it will be useful, but WITHOUT ANY
     10 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
     11 # A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
     12 #
     13 # You should have received a copy of the GNU General Public License along with
     14 # GNU Taler; see the file COPYING.  If not, see <http://www.gnu.org/licenses/>
     15 #
     16 # Bring the Taler services for the wallet simulator up and down.
     17 #
     18 # Usage: stack.sh start|stop|status|urls
     19 #
     20 # "start" wipes the run directory and the database, so every run begins from a
     21 # known state.  taler-unified-setup.sh is started with -W (wait for signal) and
     22 # detached, so it keeps running after this script returns; "stop" sends it
     23 # SIGTERM, which makes it shut down every service it started.
     24 
     25 set -eu
     26 
     27 SIM_ROOT=${SIM_ROOT:-/opt/taler-sim}
     28 RUN=${SIM_RUN:-$SIM_ROOT/run/stack}
     29 HERE="$(cd "$(dirname "$0")" && pwd)"
     30 # SIM_CONF picks the variant: sim.conf plain, sim-kyc.conf with KYC rules, or
     31 # sim-twister.conf to put the fault injector in front of the exchange.
     32 CONF_NAME=${SIM_CONF:-sim.conf}
     33 CONF_SRC="$HERE/$CONF_NAME"
     34 # The proxy daemon: the dedicated host builds it into /opt/twister, a CI
     35 # container installs the `taler-twister` package, which puts it on PATH.
     36 TWISTER_BIN=${SIM_TWISTER_BIN:-/opt/twister/bin/taler-twister-service}
     37 if [ ! -x "$TWISTER_BIN" ] && command -v taler-twister-service >/dev/null; then
     38     TWISTER_BIN=$(command -v taler-twister-service)
     39 fi
     40 DB=${SIM_DB:-talersim}
     41 # Services: exchange, merchant, libeufin bank, wirewatch, aggregator, transfer,
     42 # merchant depositcheck, merchant wirewatch, donau.
     43 SERVICES=${SIM_SERVICES:="-e -m -b -w -g -t -M -z -D"}
     44 ACCOUNT=${SIM_ACCOUNT:-exchange-account-sim}
     45 # Without -r, taler-unified-setup.sh never patches the merchant's copy of the
     46 # exchange master key, and the merchant then rejects every order with
     47 # "exchange failed to provide a valid response to /keys".
     48 MERCHANT_EXCHANGE=${SIM_MERCHANT_EXCHANGE:-merchant-exchange-sim}
     49 READY_TIMEOUT=${SIM_READY_TIMEOUT:-300}
     50 PIDFILE="$RUN/stack.pid"
     51 
     52 # Accounts to enable beyond the one taler-unified-setup.sh handles via -u.
     53 # It enables exactly one, so a second wire method stays invisible in /keys
     54 # until it is uploaded explicitly.
     55 EXTRA_ACCOUNTS=${SIM_EXTRA_ACCOUNTS:-exchange-account-iban}
     56 # Payto URIs to sign without a config section behind them.  The wallet decides
     57 # which deposit and transfer forms to offer purely from the wire methods in
     58 # /keys, so signing an address is enough to reach those screens even when no
     59 # service could ever settle on it.
     60 # Bitcoin only, by default.  A Cyclos address belongs here too -- the wallet
     61 # has a Cyclos transfer screen -- but its WireType enum does not know the
     62 # method, and one unknown method makes the whole getDepositWireTypes reply
     63 # throw, which hangs the add-account form for ever (see
     64 # findings/unknown-wire-method-hangs-the-deposit-form.md).  Keeping it in the
     65 # default stack would block every deposit scenario, so it is opt-in:
     66 #
     67 #   SIM_EXTRA_PAYTOS='payto://cyclos/cyclos.example.com/exchange?receiver-name=X' \
     68 #     ./stack.sh start
     69 EXTRA_PAYTOS=${SIM_EXTRA_PAYTOS:-payto://bitcoin/bc1qar0srrr7xfkvy5l643lydnw9re59gtzzwf5mdq?receiver-name=Exchange+Bitcoin}
     70 CURRENCY=${SIM_CURRENCY:-EUR}
     71 
     72 # Ports as configured in sim.conf; the emulator side maps exactly these.
     73 PORT_MERCHANT=8080
     74 PORT_EXCHANGE=8081
     75 PORT_BANK=8082
     76 PORT_AUDITOR=8083
     77 PORT_DONAU=8079
     78 PORT_TWISTER=8088
     79 # A second exchange, started by exchange2.sh right after the stack is up; the
     80 # port is mapped into the emulators either way, so turning it off needs no
     81 # re-mapping.  It shares the stack's bank, whose database this script wipes, so
     82 # it has to be restarted together with the stack rather than left running.
     83 PORT_EXCHANGE2=8091
     84 # Services deployed beside the stack (see services/README.md): the Taler
     85 # mailbox and the Taler directory.  They have their own databases and outlive a
     86 # stack restart, but their ports have to be mapped into the emulators, so they
     87 # belong in the list below.
     88 PORT_MAILBOX=11000
     89 PORT_TALDIR=11001
     90 SIM_EXCHANGE2=${SIM_EXCHANGE2:-1}
     91 EXCHANGE2_JSON=null
     92 # Rendered terms of service, referenced by TERMS_DIR in the configs.
     93 TERMS_OUT=${SIM_TERMS_DIR:-$SIM_ROOT/terms}
     94 
     95 die() { echo "stack.sh: $*" >&2; exit 1; }
     96 
     97 ensure_db() {
     98     local me
     99     me=$(id -un)
    100     if ! sudo -u postgres psql -tAc \
    101          "SELECT 1 FROM pg_roles WHERE rolname='$me'" | grep -q 1; then
    102         echo "creating postgres superuser role $me" >&2
    103         sudo -u postgres createuser -s "$me"
    104     fi
    105     # Drop and recreate so each run starts from an empty database; the service
    106     # dbinit calls in taler-unified-setup.sh only reset the schemas they know.
    107     dropdb --if-exists "$DB"
    108     createdb "$DB"
    109 }
    110 
    111 running_pid() {
    112     [ -s "$PIDFILE" ] || return 1
    113     local pid
    114     pid=$(cat "$PIDFILE")
    115     kill -0 "$pid" 2>/dev/null || return 1
    116     echo "$pid"
    117 }
    118 
    119 start_twister() {
    120     # Only for the twister variant: taler-unified-setup.sh knows nothing about
    121     # it, so the proxy is started here, once the exchange it forwards to is up.
    122     case "$CONF_NAME" in
    123         *twister*) ;;
    124         *) return 0 ;;
    125     esac
    126     [ -x "$TWISTER_BIN" ] || { echo "WARNING: no twister at $TWISTER_BIN" >&2; return 0; }
    127     setsid "$TWISTER_BIN" -c "$CONF_NAME" > twister.log 2>&1 < /dev/null &
    128     echo $! > twister.pid
    129     local waited=0
    130     while [ "$waited" -lt 20 ]; do
    131         # Only that it is listening: the exchange it forwards to is not up yet.
    132         if ss -ltn 2>/dev/null | grep -q ":$PORT_TWISTER "; then
    133             echo "twister listening on $PORT_TWISTER -> 8081" >&2
    134             return 0
    135         fi
    136         sleep 1
    137         waited=$((waited + 1))
    138     done
    139     echo "WARNING: twister is not listening on $PORT_TWISTER; see $RUN/twister.log" >&2
    140 }
    141 
    142 # taler-unified-setup.sh only uploads a wire fee for the one wire method it was
    143 # told about, and wallet-core hides every exchange account whose method has no
    144 # fee -- so an extra account stays invisible to the wallet until we add one.
    145 upload_wire_fee() {
    146     local method=$1 next_year
    147     next_year=$(( 1 + $(date +%Y) ))
    148     if taler-exchange-offline -c "$CONF_NAME" \
    149            wire-fee now "$method" "$CURRENCY:0.01" "$CURRENCY:0.01" \
    150            wire-fee "$next_year" "$method" "$CURRENCY:0.01" "$CURRENCY:0.01" \
    151            upload >> enable-extra-accounts.log 2>&1; then
    152         echo "uploaded $method wire fees" >&2
    153     else
    154         echo "WARNING: no wire fee for $method; see enable-extra-accounts.log" >&2
    155     fi
    156 }
    157 
    158 # The exchange serves rendered terms, one subdirectory per language; what ships
    159 # with it are the .rst sources.  Generating them once is the difference between
    160 # /terms answering with the terms and answering "not configured".
    161 # The wallet's terms screen only shows its language selector when the exchange
    162 # offers more than one language (ReviewExchangeTosScreen.kt:183), and the
    163 # shipped exchange-tos-v0 has an English source only -- so the terms used here
    164 # are exchange-tos-tops-v0, which ships de and en.
    165 TERMS_ID=${SIM_TERMS_ID:-exchange-tos-tops-v0}
    166 TERMS_LANGS=${SIM_TERMS_LANGS:-"en de"}
    167 
    168 ensure_terms() {
    169     [ -s "$TERMS_OUT/en/$TERMS_ID.txt" ] && return 0
    170     command -v taler-terms-generator > /dev/null || {
    171         echo "WARNING: no taler-terms-generator; the exchange will serve no terms" >&2
    172         return 0
    173     }
    174     mkdir -p "$TERMS_OUT"
    175     local src=/usr/share/taler-exchange/terms lang
    176     : > "$TERMS_OUT/generate.log"
    177     for lang in $TERMS_LANGS; do
    178         (cd "$src" &&
    179          taler-terms-generator -i "$TERMS_ID" -l "$lang" -o "$TERMS_OUT" &&
    180          taler-terms-generator -i exchange-pp-v0 -l "$lang" -o "$TERMS_OUT") \
    181             >> "$TERMS_OUT/generate.log" 2>&1 ||
    182             echo "WARNING: could not render the $lang terms; see $TERMS_OUT/generate.log" >&2
    183     done
    184 }
    185 
    186 enable_payto() {
    187     local payto=$1 what=$2 method
    188     if taler-exchange-offline -c "$CONF_NAME" enable-account "$payto" upload \
    189            >> enable-extra-accounts.log 2>&1; then
    190         echo "enabled $what ($payto)" >&2
    191     else
    192         echo "WARNING: could not enable $what; see enable-extra-accounts.log" >&2
    193         return 0
    194     fi
    195     method=$(echo "$payto" | sed -n 's|^payto://\([^/]*\)/.*|\1|p')
    196     [ -n "$method" ] && upload_wire_fee "$method"
    197 }
    198 
    199 enable_extra_accounts() {
    200     local section payto
    201     for section in $EXTRA_ACCOUNTS; do
    202         payto=$(taler-exchange-config -c "$CONF_NAME" -s "$section" -o PAYTO_URI 2>/dev/null)
    203         [ -n "$payto" ] || continue
    204         enable_payto "$payto" "$section"
    205     done
    206     local IFS='
    207 '
    208     for payto in $EXTRA_PAYTOS; do
    209         [ -n "$payto" ] && enable_payto "$payto" "extra payto"
    210     done
    211 }
    212 
    213 start_exchange2() {
    214     [ "$SIM_EXCHANGE2" = "1" ] || return 0
    215     if (cd "$HERE" && ./exchange2.sh start > "$RUN/exchange2.log" 2>&1); then
    216         EXCHANGE2_JSON="\"http://localhost:$PORT_EXCHANGE2/\""
    217         echo "second exchange up on $PORT_EXCHANGE2" >&2
    218     else
    219         echo "WARNING: the second exchange did not start; see exchange2.log" >&2
    220         tail -5 "$RUN/exchange2.log" >&2 || true
    221     fi
    222 }
    223 
    224 write_env() {
    225     local EXCHANGE_URL="http://localhost:$PORT_EXCHANGE/"
    226     local TWISTER_JSON=null
    227     case "$CONF_NAME" in
    228         *twister*)
    229             EXCHANGE_URL="http://localhost:$PORT_TWISTER/"
    230             TWISTER_JSON="{\"url\": \"http://localhost:$PORT_TWISTER/\", \"config\": \"$RUN/$CONF_NAME\"}"
    231             ;;
    232     esac
    233     cat > "$RUN/env.json" <<JSON
    234 {
    235   "host": "localhost",
    236   "ports": [$PORT_DONAU, $PORT_MERCHANT, $PORT_EXCHANGE, $PORT_BANK, $PORT_AUDITOR,
    237             $PORT_TWISTER, $PORT_EXCHANGE2, $PORT_MAILBOX, $PORT_TALDIR],
    238   "exchange": "$EXCHANGE_URL",
    239   "exchange_direct": "http://localhost:$PORT_EXCHANGE/",
    240   "exchange2": $EXCHANGE2_JSON,
    241   "mailbox": "http://localhost:$PORT_MAILBOX/",
    242   "taldir": "http://localhost:$PORT_TALDIR/",
    243   "twister": $TWISTER_JSON,
    244   "merchant": "http://localhost:$PORT_MERCHANT/",
    245   "bank": "http://localhost:$PORT_BANK/",
    246   "donau": "http://localhost:$PORT_DONAU/",
    247   "auditor": "http://localhost:$PORT_AUDITOR/",
    248   "currency": "EUR",
    249   "run_dir": "$RUN",
    250   "bank_admin": {"username": "admin", "password": "secret-password"},
    251   "exchange_bank_account": {"username": "exchange", "password": "password"}
    252 }
    253 JSON
    254 }
    255 
    256 start() {
    257     if pid=$(running_pid); then
    258         die "already running (pid $pid); run 'stack.sh stop' first"
    259     fi
    260     command -v taler-unified-setup.sh >/dev/null || die "taler-unified-setup.sh not in PATH"
    261     ensure_db
    262     rm -rf "$RUN"
    263     mkdir -p "$RUN/home"
    264     # taler-unified-setup.sh rewrites MASTER_PUBLIC_KEY in the config it is
    265     # handed, so it always gets a copy.
    266     # sim.conf is @INLINE@d by the variants, so both have to be in the run
    267     # directory, and taler-unified-setup.sh rewrites the one it is given.
    268     cp "$HERE/sim.conf" "$RUN/sim.conf"
    269     [ "$CONF_NAME" = "sim.conf" ] || cp "$CONF_SRC" "$RUN/$CONF_NAME"
    270     cd "$RUN"
    271     ensure_terms
    272     # Before the services, not after: with the twister variant the exchange's
    273     # own BASE_URL is the proxy, and its key setup talks to that URL. If
    274     # nothing is listening there yet, "Setting up exchange keys" fails and the
    275     # whole stack dies.
    276     start_twister
    277     # shellcheck disable=SC2086
    278     setsid taler-unified-setup.sh -W -c "$CONF_NAME" -u "$ACCOUNT" \
    279            -r "$MERCHANT_EXCHANGE" $SERVICES \
    280            > setup.stdout 2> setup.stderr < /dev/null &
    281     echo $! > "$PIDFILE"
    282     local pid waited
    283     pid=$(cat "$PIDFILE")
    284     waited=0
    285     while [ "$waited" -lt "$READY_TIMEOUT" ]; do
    286         if grep -q '^READY:' setup.stdout 2>/dev/null; then
    287             enable_extra_accounts
    288             start_exchange2
    289             write_env
    290             echo "stack up after ${waited}s: $(cat "$RUN/env.json" | tr -d '\n')" >&2
    291             echo "READY"
    292             return 0
    293         fi
    294         if ! kill -0 "$pid" 2>/dev/null; then
    295             echo "--- setup.stderr (tail) ---" >&2
    296             tail -40 setup.stderr >&2 || true
    297             die "taler-unified-setup.sh died before becoming ready"
    298         fi
    299         sleep 1
    300         waited=$((waited + 1))
    301     done
    302     echo "--- setup.stderr (tail) ---" >&2
    303     tail -40 setup.stderr >&2 || true
    304     die "timed out after ${READY_TIMEOUT}s waiting for READY"
    305 }
    306 
    307 stop() {
    308     # The second exchange first: it has an account at the bank this stack is
    309     # about to take down.
    310     [ "$SIM_EXCHANGE2" = "1" ] && (cd "$HERE" && ./exchange2.sh stop >/dev/null 2>&1 || true)
    311     if pid=$(running_pid); then
    312         kill -TERM "$pid"
    313         local waited=0
    314         while kill -0 "$pid" 2>/dev/null && [ "$waited" -lt 60 ]; do
    315             sleep 1
    316             waited=$((waited + 1))
    317         done
    318         kill -KILL "$pid" 2>/dev/null || true
    319         if [ -s "$RUN/twister.pid" ]; then
    320             kill "$(cat "$RUN/twister.pid")" 2>/dev/null || true
    321             rm -f "$RUN/twister.pid"
    322         fi
    323         echo "stopped" >&2
    324     else
    325         echo "not running" >&2
    326     fi
    327     rm -f "$PIDFILE"
    328     # The supervisor dying is not the same as its children letting go of the
    329     # ports: starting again five seconds later made taler-unified-setup.sh die
    330     # at the merchant, which cost a phase run.  Wait for the ones this stack
    331     # owns to be free.  11000 and 11001 (mailbox, taldir) are deliberately
    332     # left out: they are run separately and outlive the stack.
    333     local waited=0 busy
    334     while [ "$waited" -lt 60 ]; do
    335         busy=""
    336         for p in "$PORT_DONAU" "$PORT_MERCHANT" "$PORT_EXCHANGE" "$PORT_BANK" \
    337                  "$PORT_AUDITOR" "$PORT_EXCHANGE2"; do
    338             if ss -ltn 2>/dev/null | grep -q ":$p\b"; then busy="$busy $p"; fi
    339         done
    340         [ -z "$busy" ] && break
    341         sleep 2
    342         waited=$((waited + 2))
    343     done
    344     [ -n "$busy" ] && echo "still listening after ${waited}s:$busy" >&2
    345     return 0
    346 }
    347 
    348 status() {
    349     if pid=$(running_pid); then
    350         echo "running (pid $pid)"
    351         for p in $PORT_DONAU $PORT_MERCHANT $PORT_EXCHANGE $PORT_BANK; do
    352             printf '  port %s: ' "$p"
    353             if curl -s -m 2 -o /dev/null "http://localhost:$p/config"; then
    354                 echo "ok"
    355             else
    356                 echo "NO /config"
    357             fi
    358         done
    359     else
    360         echo "not running"
    361         return 1
    362     fi
    363 }
    364 
    365 case "${1:-}" in
    366     start) start ;;
    367     stop) stop ;;
    368     status) status ;;
    369     urls) cat "$RUN/env.json" ;;
    370     *) die "usage: stack.sh start|stop|status|urls" ;;
    371 esac