Repository navigation
Expand file tree
/
Copy pathstart.sh
More file actions
executable file
·129 lines (113 loc) · 5.66 KB
/
Copy pathstart.sh
File metadata and controls
executable file
·129 lines (113 loc) · 5.66 KB
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
#!/usr/bin/env bash
# One-command bootstrap + launch for the Agent Spend Policy Engine.
#
# Safe to re-run: creates the venv/accounts only if missing, skips opt-in for
# accounts that are already opted in or still unfunded, and always (re)starts
# the three services fresh.
#
# ./start.sh # fast path once accounts are set up
# ./start.sh --recheck # force the on-chain balance/opt-in check again
#
set -euo pipefail
cd "$(dirname "$0")"
if [ "${1:-}" = "--recheck" ]; then
rm -f data/.setup_verified
fi
if [ ! -d .venv ]; then
echo "==> Creating virtualenv..."
python3 -m venv .venv
fi
source .venv/bin/activate
echo "==> Installing dependencies..."
pip install -q -r requirements.txt
# What this process is actually pointed at, before anything spends. The
# expensive mistake isn't a setting you can't find -- it's one you never
# thought to check because everything looked like it was working. Also fails
# fast and loudly on a bad NETWORK or an unconfirmed mainnet.
echo "==> Configuration"
python3 -m common.config | sed 's/^/ /'
echo ""
# The ports and bind host come from the same resolver the services use.
# This script used to hardcode 4021/4022/4023, so setting POLICY_ENGINE_PORT
# in .env moved every URL the system talks to itself on while leaving the
# ports it actually bound untouched -- a configuration that looks applied
# and isn't, which is the exact failure common/config.py exists to prevent.
eval "$(python3 -m common.config --export)"
if [ ! -f data/accounts.json ]; then
echo "==> Generating Algorand accounts..."
python3 scripts/setup_accounts.py generate
echo ""
echo "!!! New accounts hold nothing until they're funded, and payments fail"
echo "!!! until they are. Funding needs a captcha, so it's the one step that"
echo "!!! can't be automated -- the addresses and the dispenser link are"
echo "!!! printed above. See README.md for the walkthrough."
echo ""
fi
if [ -f data/.setup_verified ]; then
# Accounts were already confirmed funded + opted in on a previous run --
# skip re-checking 4 accounts against the public testnet API on every
# single startup. That's what was making this feel slow: it's a network
# round-trip per account, twice (balance check, then opt-in check), and
# the public AlgoNode endpoint is sometimes multiple seconds per call.
# Run `./start.sh --recheck` (or delete data/.setup_verified) to force it.
echo "==> Accounts already verified funded + opted in (skipping on-chain check; ./start.sh --recheck to force it)"
else
echo "==> Checking account balances..."
python3 scripts/setup_accounts.py balances
echo "==> Opting funded accounts into testnet USDC (skips unfunded/already-done)..."
python3 scripts/setup_accounts.py optin || true
fi
# The dashboard needs two things it can't resolve for itself: where the
# policy engine is, and the admin token every /admin route now requires
# (freeze, approve, onboard, notarize, tamper). Written next to the static
# files, gitignored, regenerated on every launch so a rotated token can't go
# stale. Same trust boundary as data/policy_auth_secret.txt -- a local file
# read by local processes, not a credential handed to a browser over a wire.
echo "==> Writing dashboard/config.js (engine URL + admin token)..."
ADMIN_TOKEN_VALUE="$(python3 -m common.config --admin-token)"
cat > dashboard/config.js <<EOF
// Generated by start.sh -- do not edit, do not commit.
window.POLICY_ENGINE_URL = "${POLICY_ENGINE_URL}";
window.ADMIN_TOKEN = "${ADMIN_TOKEN_VALUE}";
EOF
chmod 600 dashboard/config.js
echo "==> Stopping any previous instances..."
pkill -f "uvicorn resource_server.main:app" 2>/dev/null || true
pkill -f "uvicorn policy_engine.app:app" 2>/dev/null || true
# Both dashboard server names: `http.server <port>` is what older runs of
# this script left behind, and dashboard/serve.py is what it starts now.
# Missing the stale one means the new server dies on "address already in use".
pkill -f "http.server ${DASHBOARD_PORT}" 2>/dev/null || true
pkill -f "dashboard/serve.py" 2>/dev/null || true
sleep 1
echo "==> Starting resource server on :${RESOURCE_SERVER_PORT} ..."
nohup uvicorn resource_server.main:app --host "$BIND_HOST" --port "$RESOURCE_SERVER_PORT" > /tmp/resource_server.log 2>&1 &
RS_PID=$!
echo "==> Starting policy engine on :${POLICY_ENGINE_PORT} ..."
nohup uvicorn policy_engine.app:app --host "$BIND_HOST" --port "$POLICY_ENGINE_PORT" > /tmp/policy_engine.log 2>&1 &
PE_PID=$!
echo "==> Starting dashboard on :${DASHBOARD_PORT} ..."
# dashboard/serve.py, not `python3 -m http.server`: the stdlib server sends
# Last-Modified and no Cache-Control, so browsers heuristically cache
# app.js and can serve a stale copy without revalidating. A half-updated
# dashboard during a demo is invisible and unfixable in the moment.
python3 dashboard/serve.py "$DASHBOARD_PORT" > /tmp/dashboard.log 2>&1 &
DASH_PID=$!
trap "echo; echo Stopping...; kill $RS_PID $PE_PID $DASH_PID 2>/dev/null; true" EXIT INT TERM
sleep 2
echo ""
echo "Dashboard : http://${BIND_HOST}:${DASHBOARD_PORT}/index.html <- start here"
echo "Policy engine : http://${BIND_HOST}:${POLICY_ENGINE_PORT}/health"
echo "Resource server : http://${BIND_HOST}:${RESOURCE_SERVER_PORT}/health"
echo ""
echo "Admin routes need a bearer token (the dashboard already has it):"
echo " curl -H \"Authorization: Bearer \$(python3 -m common.config --admin-token)\" ..."
echo ""
echo "Demo walkthrough : DEMO.md (five minutes, scripted)"
echo "Verify the audit ledger against Algorand, without trusting any of the above:"
echo " python3 scripts/verify_audit.py"
echo ""
echo "Logs: /tmp/resource_server.log /tmp/policy_engine.log /tmp/dashboard.log"
echo "Ctrl+C to stop all three."
echo ""
wait $RS_PID $PE_PID $DASH_PID