-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdev.sh
More file actions
executable file
Β·307 lines (279 loc) Β· 12.3 KB
/
dev.sh
File metadata and controls
executable file
Β·307 lines (279 loc) Β· 12.3 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
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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
#!/bin/bash
set -e
show_usage() {
echo "Usage: ./dev.sh [command]"
echo ""
echo "Local Development Helper β Run key-server + CLI locally"
echo ""
echo "Local Commands:"
echo " up Start key-server-dev (port 8787) in background"
echo " down Stop all dev services"
echo " logs Follow key-server-dev logs"
echo " status Show service status and health"
echo " test-api Run quick smoke test against local key-server"
echo " cli [args] Build & run xpose CLI pointed at local key-server"
echo " shell Open a bash shell in the dev container"
echo ""
echo "Staging Commands (Cloudflare):"
echo " staging-setup Create D1 database + deploy staging worker (one-time)"
echo " staging-deploy Build & deploy key-server to staging"
echo " staging-test Smoke test against staging worker"
echo " staging-cli Run CLI pointed at staging worker"
echo ""
echo " help Show this help message"
}
COMMAND=${1:-help}
# --- Auto-detect container engine ---
if command -v podman &>/dev/null; then
ENGINE="podman"
COMPOSE_CMD="podman-compose"
elif command -v docker &>/dev/null; then
ENGINE="docker"
COMPOSE_CMD="docker compose"
else
echo "β Neither podman nor docker found. Please install one."
exit 1
fi
case "$COMMAND" in
up)
echo "π Starting key-server-dev..."
$COMPOSE_CMD up key-server-dev -d --build
echo ""
echo "β³ Waiting for key-server to be healthy (first build may take 2-5 min)..."
echo " Run './dev.sh logs' in another terminal to watch progress."
echo ""
# Wait for healthy
MAX_WAIT=300
ELAPSED=0
while [ $ELAPSED -lt $MAX_WAIT ]; do
STATUS=$($COMPOSE_CMD ps key-server-dev --format json 2>/dev/null | grep -o '"Health":"[^"]*"' | cut -d'"' -f4 || echo "unknown")
if [ "$STATUS" = "healthy" ]; then
echo "β
key-server-dev is healthy!"
echo ""
echo " API: http://localhost:8787"
echo " CLI: ./dev.sh cli 3000"
echo " Logs: ./dev.sh logs"
exit 0
fi
sleep 5
ELAPSED=$((ELAPSED + 5))
echo " ... waiting ($ELAPSED/${MAX_WAIT}s) status=$STATUS"
done
echo "β οΈ Timed out waiting for healthy status. Check logs:"
echo " ./dev.sh logs"
exit 1
;;
down)
echo "π Stopping dev services..."
$COMPOSE_CMD down
echo "β
Done."
;;
logs)
$COMPOSE_CMD logs -f key-server-dev
;;
status)
$COMPOSE_CMD ps key-server-dev
echo ""
echo "Quick API check:"
curl -sf http://localhost:8787/api/config 2>/dev/null && echo "" || echo "β Key server not responding"
;;
test-api)
echo "π§ͺ Running smoke tests against http://localhost:8787..."
echo ""
# Test /api/config
echo " [1/4] GET /api/config"
CONFIG=$(curl -sf http://localhost:8787/api/config)
echo "$CONFIG" | grep -q "min_cli_version" && echo " β
Config OK" || { echo " β Config failed"; exit 1; }
# Test /api/stats
echo " [2/4] GET /api/stats"
STATS=$(curl -sf http://localhost:8787/api/stats)
echo "$STATS" | grep -q "total" && echo " β
Stats OK" || { echo " β Stats failed"; exit 1; }
# Test admin add tunnel
echo " [3/4] POST /admin/tunnels (add test tunnel)"
ADD=$(curl -sf -X POST -H "Authorization: Bearer my-secret-token" \
-H "Content-Type: application/json" \
-d '{"id": "dev-t1", "name": "dev-test", "token": "dev-tok1"}' \
http://localhost:8787/admin/tunnels)
echo "$ADD" | grep -q '"success":true' && echo " β
Add tunnel OK" || { echo " β Add tunnel failed: $ADD"; exit 1; }
# Test stats updated
echo " [4/4] GET /api/stats (verify tunnel added)"
STATS2=$(curl -sf http://localhost:8787/api/stats)
echo "$STATS2" | grep -q '"available":1' && echo " β
Stats updated OK" || echo " β οΈ Stats: $STATS2"
echo ""
echo "π All smoke tests passed!"
;;
cli)
shift
echo "π¨ Building and running xpose CLI β http://localhost:8787"
$COMPOSE_CMD run --rm \
-e XPOSE_SERVER_URL=http://key-server-dev:8787 \
dev bash -c "cd packages/cli && cargo run -- $*"
;;
shell)
$COMPOSE_CMD run --rm dev bash
;;
staging-setup)
echo "π§ Setting up staging environment..."
echo ""
# Check for .env.staging
if [ ! -f .env.staging ]; then
echo "π Creating .env.staging from template..."
cp .env.staging.example .env.staging
echo "β οΈ Please edit .env.staging and set CLOUDFLARE_API_TOKEN first."
echo " Then re-run: ./dev.sh staging-setup"
exit 1
fi
source .env.staging
if [ -z "$CLOUDFLARE_API_TOKEN" ] || [ "$CLOUDFLARE_API_TOKEN" = "your-api-token-here" ]; then
echo "β CLOUDFLARE_API_TOKEN not set in .env.staging"
exit 1
fi
# Create D1 database if not already done
if [ -z "$STAGING_D1_ID" ] || [ "$STAGING_D1_ID" = "your-staging-d1-id-here" ]; then
echo "ποΈ Creating D1 database: tunnel-db-staging..."
D1_OUTPUT=$($COMPOSE_CMD run --rm \
-e CLOUDFLARE_API_TOKEN="$CLOUDFLARE_API_TOKEN" \
dev bash -c "cd packages/key-server && wrangler d1 create tunnel-db-staging" 2>&1)
echo "$D1_OUTPUT"
NEW_ID=$(echo "$D1_OUTPUT" | grep -o 'database_id.*=.*"[^"]*"' | grep -o '"[^"]*"' | tr -d '"' || true)
if [ -z "$NEW_ID" ]; then
NEW_ID=$(echo "$D1_OUTPUT" | grep -oP '[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}' | head -1 || true)
fi
if [ -n "$NEW_ID" ]; then
echo ""
echo "β
D1 database created! ID: $NEW_ID"
sed -i "s/STAGING_D1_ID=.*/STAGING_D1_ID=$NEW_ID/" .env.staging
sed -i "s/__STAGING_D1_ID__/$NEW_ID/" packages/key-server/wrangler.jsonc
echo "β
Updated .env.staging and wrangler.jsonc"
else
echo "β οΈ Could not auto-extract D1 ID. Please update .env.staging manually."
exit 1
fi
else
echo "β
D1 database already configured: $STAGING_D1_ID"
sed -i "s/__STAGING_D1_ID__/$STAGING_D1_ID/" packages/key-server/wrangler.jsonc 2>/dev/null || true
fi
source .env.staging
# Run migrations + deploy inside container
echo ""
echo "π¦ Running D1 migrations + deploying to staging..."
$COMPOSE_CMD run --rm \
-e CLOUDFLARE_API_TOKEN="$CLOUDFLARE_API_TOKEN" \
-e CLOUDFLARE_ACCOUNT_ID="${CLOUDFLARE_ACCOUNT_ID:-}" \
dev bash -c "
cd packages/key-server &&
wrangler d1 migrations apply tunnel-db-staging --remote --env staging &&
echo '' &&
echo 'π Deploying key-server-staging...' &&
wrangler deploy --env staging
"
# Set tunnel provisioning secrets (if configured)
if [ -n "${CLOUDFLARE_API_TUNNEL_TOKEN:-}" ] && [ "$CLOUDFLARE_API_TUNNEL_TOKEN" != "your-tunnel-api-token-here" ]; then
echo ""
echo "π Setting tunnel provisioning secrets..."
$COMPOSE_CMD run --rm \
-e CLOUDFLARE_API_TOKEN="$CLOUDFLARE_API_TOKEN" \
-e CLOUDFLARE_ACCOUNT_ID="${CLOUDFLARE_ACCOUNT_ID:-}" \
dev bash -c "
cd packages/key-server &&
echo '${CLOUDFLARE_API_TUNNEL_TOKEN}' | wrangler secret put CLOUDFLARE_API_TUNNEL_TOKEN --env staging &&
echo '${CLOUDFLARE_ACCOUNT_ID}' | wrangler secret put CLOUDFLARE_ACCOUNT_ID --env staging
"
# Set tunnel domain only if configured (optional β omit for quick tunnels)
if [ -n "${CLOUDFLARE_TUNNEL_DOMAIN:-}" ] && [ "$CLOUDFLARE_TUNNEL_DOMAIN" != "your-tunnel-domain-here" ]; then
$COMPOSE_CMD run --rm \
-e CLOUDFLARE_API_TOKEN="$CLOUDFLARE_API_TOKEN" \
-e CLOUDFLARE_ACCOUNT_ID="${CLOUDFLARE_ACCOUNT_ID:-}" \
dev bash -c "
cd packages/key-server &&
echo '${CLOUDFLARE_TUNNEL_DOMAIN}' | wrangler secret put CLOUDFLARE_TUNNEL_DOMAIN --env staging
"
echo "β
Tunnel secrets configured (with custom domain: $CLOUDFLARE_TUNNEL_DOMAIN)"
else
echo "β
Tunnel secrets configured (quick tunnels β no custom domain)"
fi
else
echo ""
echo "β οΈ Tunnel secrets not set (CLOUDFLARE_API_TUNNEL_TOKEN not configured in .env.staging)"
echo " CLI tunnel allocation will not work until secrets are configured."
fi
echo ""
echo "π Staging setup complete!"
echo ""
echo " Worker URL: Check output above or Cloudflare dashboard"
echo " Update STAGING_WORKER_URL in .env.staging"
echo ""
echo " Next: ./dev.sh staging-test"
;;
staging-deploy)
echo "π Deploying key-server to staging..."
if [ ! -f .env.staging ]; then
echo "β .env.staging not found. Run './dev.sh staging-setup' first."
exit 1
fi
source .env.staging
$COMPOSE_CMD run --rm \
-e CLOUDFLARE_API_TOKEN="$CLOUDFLARE_API_TOKEN" \
-e CLOUDFLARE_ACCOUNT_ID="${CLOUDFLARE_ACCOUNT_ID:-}" \
dev bash -c "
cd packages/key-server &&
wrangler d1 migrations apply tunnel-db-staging --remote --env staging &&
wrangler deploy --env staging
"
echo ""
echo "β
Staging deploy complete!"
;;
staging-test)
if [ ! -f .env.staging ]; then
echo "β .env.staging not found. Run './dev.sh staging-setup' first."
exit 1
fi
source .env.staging
if [ -z "$STAGING_WORKER_URL" ] || echo "$STAGING_WORKER_URL" | grep -q 'your-subdomain'; then
echo "β STAGING_WORKER_URL not configured in .env.staging"
echo " Set it to your staging worker URL, e.g.: https://key-server-staging.xxx.workers.dev"
exit 1
fi
URL="$STAGING_WORKER_URL"
echo "π§ͺ Running smoke tests against staging: $URL"
echo ""
echo " [1/3] GET /api/config"
CONFIG=$(curl -sf "$URL/api/config")
echo "$CONFIG" | grep -q "min_cli_version" && echo " β
Config OK" || { echo " β Config failed"; exit 1; }
echo " [2/3] GET /api/stats"
STATS=$(curl -sf "$URL/api/stats")
echo "$STATS" | grep -q "total" && echo " β
Stats OK" || { echo " β Stats failed"; exit 1; }
echo " [3/3] POST /admin/tunnels"
ADD=$(curl -sf -X POST -H "Authorization: Bearer staging-secret-token" \
-H "Content-Type: application/json" \
-d '{"id": "staging-t1", "name": "staging-test", "token": "staging-tok1"}' \
"$URL/admin/tunnels")
echo "$ADD" | grep -q '"success":true' && echo " β
Add tunnel OK" || { echo " β Add tunnel failed: $ADD"; exit 1; }
echo ""
echo "π All staging smoke tests passed!"
;;
staging-cli)
if [ ! -f .env.staging ]; then
echo "β .env.staging not found. Run './dev.sh staging-setup' first."
exit 1
fi
source .env.staging
if [ -z "$STAGING_WORKER_URL" ] || echo "$STAGING_WORKER_URL" | grep -q 'your-subdomain'; then
echo "β STAGING_WORKER_URL not configured in .env.staging"
exit 1
fi
shift
echo "π¨ Building and running xpose CLI β $STAGING_WORKER_URL"
$COMPOSE_CMD run --rm \
-e XPOSE_SERVER_URL="$STAGING_WORKER_URL" \
dev bash -c "cd packages/cli && cargo run -- $*"
;;
help|--help|-h)
show_usage
;;
*)
echo "Unknown command: $COMMAND"
show_usage
exit 1
;;
esac