koopa-admin-log/scripts/taler-bank/install-demo-withdraw-api.sh
2026-09-18 13:05:31 +02:00

130 lines
5.3 KiB
Bash
Executable file

#!/bin/bash
# Install + start demo-withdraw API + auto-confirm loop inside bank container.
# Host:
# ./scripts/taler-bank/install-demo-withdraw-api.sh
set -euo pipefail
ROOT=$(cd "$(dirname "$0")" && pwd)
CTR="${BANK_CONTAINER:-goa-regio-ng}"
podman cp "$ROOT/demo-withdraw-api.py" "$CTR:/usr/local/bin/demo-withdraw-api.py"
podman cp "$ROOT/auto-confirm-withdrawals.sh" "$CTR:/usr/local/bin/auto-confirm-withdrawals.sh"
podman cp "$ROOT/refresh-demo-withdraw.sh" "$CTR:/usr/local/bin/refresh-demo-withdraw.sh"
if [ -f "$ROOT/systemd/goa-auto-confirm-withdrawals.service" ]; then
podman cp "$ROOT/systemd/goa-auto-confirm-withdrawals.service" \
"$CTR:/etc/systemd/system/goa-auto-confirm-withdrawals.service"
fi
podman exec -u root "$CTR" chmod 755 \
/usr/local/bin/demo-withdraw-api.py \
/usr/local/bin/auto-confirm-withdrawals.sh \
/usr/local/bin/refresh-demo-withdraw.sh
# nginx: ensure demo-withdraw / auto-account / Integration → :19096
# FP pattern: Caddy @wdGet GET → :9013 → this location → demo-api
# (single in-CTR python; no nested podman)
podman exec -u root "$CTR" bash -lc '
set -e
python3 - <<PY
from pathlib import Path
p = Path("/etc/nginx/sites-available/bank-landing")
t = p.read_text()
changed = False
anchor = " location /intro/ {"
blocks = []
if "demo-withdraw.json" not in t:
blocks.append(""" location = /intro/demo-withdraw.json {
proxy_pass http://127.0.0.1:19096/demo-withdraw.json;
proxy_http_version 1.1;
proxy_set_header Host \$host;
add_header Cache-Control "no-store" always;
add_header Access-Control-Allow-Origin * always;
}
""")
print("nginx demo-withdraw location added")
else:
print("nginx already has demo-withdraw")
if "auto-account.json" not in t:
blocks.append(""" location = /intro/auto-account.json {
proxy_pass http://127.0.0.1:19096/auto-account.json;
proxy_http_version 1.1;
proxy_set_header Host \$host;
add_header Cache-Control "no-store" always;
add_header Access-Control-Allow-Origin * always;
}
""")
print("nginx auto-account location added")
else:
print("nginx already has auto-account")
if "taler-integration/withdrawal-operation" not in t:
blocks.append(""" # FP pattern: GET Integration via demo-api (suggested/required_exchange)
# Caddy @wdGet → :9013 → here → :19096; POST/other stays on :9012
location /taler-integration/withdrawal-operation/ {
proxy_pass http://127.0.0.1:19096;
proxy_http_version 1.1;
proxy_set_header Host \$host;
proxy_read_timeout 70s;
add_header Cache-Control "no-store" always;
add_header Access-Control-Allow-Origin * always;
}
""")
print("nginx Integration withdrawal-operation location added")
else:
print("nginx already has Integration withdrawal-operation")
if blocks:
if anchor not in t:
raise SystemExit("nginx anchor location /intro/ not found")
t = t.replace(anchor, "".join(blocks) + anchor, 1)
p.write_text(t)
changed = True
if not changed:
print("nginx locations already complete")
PY
nginx -t && nginx -s reload || true
'
# start/restart API + single auto-confirm loop (flock inside script)
podman exec -u root "$CTR" bash -lc '
# stop demo-withdraw by pid (avoid pkill -f self-match); wait for :19096
ps -eo pid=,args= | awk "/demo-withdraw-api\\.py/ && !/awk/ {print \$1}" | while read p; do kill \$p 2>/dev/null || true; done
sleep 1
ps -eo pid=,args= | awk "/demo-withdraw-api\\.py/ && !/awk/ {print \$1}" | while read p; do kill -9 \$p 2>/dev/null || true; done
sleep 0.5
nohup python3 /usr/local/bin/demo-withdraw-api.py \
>>/var/log/demo-withdraw-api.log 2>&1 </dev/null &
echo "api pid $!"
# fail loud if bind lost to a leftover listener
sleep 0.5
ps -eo pid=,args= | awk "/demo-withdraw-api\\.py/ && !/awk/" || {
echo "ERROR: demo-withdraw-api failed to stay up (port busy?)" >&2
tail -20 /var/log/demo-withdraw-api.log >&2 || true
exit 1
}
# Prefer systemd unit (Restart=always). Fall back to nohup if systemctl missing.
if [ -f /var/log/auto-confirm-withdrawals.log ]; then
sz=$(wc -c </var/log/auto-confirm-withdrawals.log)
if [ "${sz:-0}" -gt 5000000 ]; then
tail -100 /var/log/auto-confirm-withdrawals.log > /tmp/ac-keep.log
: > /var/log/auto-confirm-withdrawals.log
cat /tmp/ac-keep.log >> /var/log/auto-confirm-withdrawals.log
fi
fi
# stop ad-hoc loops (by pid; avoid pkill -f self-match)
ps -eo pid=,args= | awk "/auto-confirm-withdrawals\\.sh --loop/ && !/awk/ {print \$1}" | while read p; do kill \$p 2>/dev/null || true; done
sleep 0.5
if command -v systemctl >/dev/null 2>&1 && [ -f /etc/systemd/system/goa-auto-confirm-withdrawals.service ]; then
systemctl daemon-reload
systemctl enable --now goa-auto-confirm-withdrawals.service
systemctl restart goa-auto-confirm-withdrawals.service
systemctl --no-pager --full status goa-auto-confirm-withdrawals.service | head -20
else
nohup env QUIET=1 WATCH_MAX=80 \
/usr/local/bin/auto-confirm-withdrawals.sh --loop 2 \
>>/var/log/auto-confirm-withdrawals.log 2>&1 </dev/null &
echo "auto-confirm pid $! (no systemd unit)"
sleep 1
ps -eo pid=,args= | awk "/auto-confirm-withdrawals\\.sh --loop/ && !/awk/"
fi
curl -sS -m 8 http://127.0.0.1:19096/demo-withdraw.json | head -c 300; echo
'
echo "OK: demo-withdraw API + auto-confirm in $CTR"
echo "Public: https://bank.hacktivism.ch/intro/demo-withdraw.json"