taler-android

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

stack.sh (14481B)


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