-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdev.sh
More file actions
executable file
·417 lines (348 loc) · 13.8 KB
/
dev.sh
File metadata and controls
executable file
·417 lines (348 loc) · 13.8 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
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
#!/usr/bin/env bash
# dev.sh — AI-agent-optimized dev harness for Pipelit
# All status output goes to stdout as JSON. Human-readable logs go to stderr.
# Usage: ./dev.sh <up|down|restart|status|logs|exec|test> [args...]
set -euo pipefail
# ---------------------------------------------------------------------------
# Configuration
# ---------------------------------------------------------------------------
CONTAINER_NAME="plit-dev"
VOLUME_NAME="plit-dev-data"
PLIT_IMAGE="${PLIT_IMAGE:-plit-e2e:import-test}"
ADMIN_USERNAME="admin"
ADMIN_PASSWORD="testpass123"
# Paths inside the container
PLATFORM_INNER="/root/.local/share/plit/pipelit/platform"
VENV_BIN="/root/.local/share/plit/venv/bin"
PROCFILE_PATH="/root/.config/plit/Procfile"
DB_PATH="/root/.local/share/plit/pipelit.db"
# Host-side paths
PLATFORM_HOST="$(cd "$(dirname "$0")/platform" && pwd)"
# API health check
API_PORT="8000"
API_BASE="http://localhost:${API_PORT}"
HEALTH_URL="${API_BASE}/api/v1/auth/setup-status/"
HEALTH_TIMEOUT=120 # seconds
# Procfile content with --reload enabled for the pipelit process
# All other processes remain unchanged from the stock Procfile.
DEV_PROCFILE=$(cat <<'PROCFILE'
redis: /root/.config/plit/dragonfly --logtostderr --port 6399
gateway: GATEWAY_CONFIG=/root/.config/plit/config.json /usr/local/bin/plit-gw
pipelit: FRONTEND_DIST_PATH=/root/.local/share/plit/pipelit/platform/frontend/dist /root/.local/share/plit/venv/bin/uvicorn main:app --host 0.0.0.0 --port 8000 --reload --reload-dir /root/.local/share/plit/pipelit/platform
scheduler: /root/.local/share/plit/venv/bin/rq worker --worker-class worker_class.PipelitWorker workflows --with-scheduler
worker: /root/.local/share/plit/venv/bin/rq worker-pool workflows -w worker_class.PipelitWorker -n 4
PROCFILE
)
# ---------------------------------------------------------------------------
# Helpers
# ---------------------------------------------------------------------------
log() {
# Human-readable log to stderr so stdout stays clean for JSON
echo "[dev.sh] $*" >&2
}
json_error() {
local msg="$1"
printf '{"status":"error","message":"%s"}\n' "$msg"
}
container_running() {
docker inspect --format '{{.State.Running}}' "$CONTAINER_NAME" 2>/dev/null | grep -q '^true$'
}
container_exists() {
docker inspect "$CONTAINER_NAME" &>/dev/null
}
# ---------------------------------------------------------------------------
# Wait for the API to be healthy
# Returns 0 on success, 1 on timeout
# ---------------------------------------------------------------------------
wait_healthy() {
log "Waiting for API at ${HEALTH_URL} (timeout: ${HEALTH_TIMEOUT}s)..."
local elapsed=0
local interval=3
while [[ $elapsed -lt $HEALTH_TIMEOUT ]]; do
local http_code
http_code=$(curl -s -o /dev/null -w "%{http_code}" --max-time 5 "$HEALTH_URL" 2>/dev/null || true)
if [[ "$http_code" == "200" ]]; then
log "API is healthy after ${elapsed}s."
return 0
fi
log " (${elapsed}s) HTTP ${http_code:-no response} — retrying in ${interval}s..."
sleep "$interval"
elapsed=$(( elapsed + interval ))
done
log "ERROR: API did not become healthy within ${HEALTH_TIMEOUT}s."
return 1
}
# ---------------------------------------------------------------------------
# Query the admin API key from SQLite inside the container
# ---------------------------------------------------------------------------
get_admin_api_key() {
docker exec "$CONTAINER_NAME" \
python3 -c "import sqlite3; conn = sqlite3.connect('${DB_PATH}'); row = conn.execute('SELECT key FROM api_keys LIMIT 1').fetchone(); print(row[0] if row else '')" \
2>/dev/null || true
}
# ---------------------------------------------------------------------------
# Ensure a dedicated agent API key exists (idempotent)
# Creates a key named "claude-agent" if one doesn't already exist.
# Returns the key value.
# ---------------------------------------------------------------------------
ensure_agent_key() {
local admin_key="$1"
# Check if agent key already exists
local existing
existing=$(docker exec "$CONTAINER_NAME" \
python3 -c "
import sqlite3
conn = sqlite3.connect('${DB_PATH}')
row = conn.execute(\"SELECT key FROM api_keys WHERE name='claude-agent'\").fetchone()
print(row[0] if row else '')
" 2>/dev/null || true)
if [[ -n "$existing" ]]; then
echo "$existing"
return
fi
# Create a new agent key via the API
log "Creating agent API key 'claude-agent'..."
local response
response=$(curl -s -X POST "${API_BASE}/api/v1/users/me/keys" \
-H "Authorization: Bearer ${admin_key}" \
-H "Content-Type: application/json" \
-d '{"name": "claude-agent"}' 2>/dev/null)
local key
key=$(echo "$response" | python3 -c "import json,sys; print(json.load(sys.stdin).get('key',''))" 2>/dev/null || true)
if [[ -n "$key" ]]; then
log "Agent API key created."
echo "$key"
else
log "WARNING: Could not create agent API key. Response: ${response}"
echo ""
fi
}
# ---------------------------------------------------------------------------
# Emit the standard JSON status blob
# ---------------------------------------------------------------------------
emit_status_json() {
local admin_key
admin_key=$(get_admin_api_key)
local agent_key=""
if [[ -z "$admin_key" ]]; then
log "WARNING: No admin API key found — agent key creation will be skipped."
else
agent_key=$(ensure_agent_key "$admin_key")
fi
printf '{
"status": "ready",
"container": "%s",
"api_url": "%s",
"frontend_url": "%s",
"gateway_url": "http://localhost:8080",
"admin": {
"username": "%s",
"password": "%s",
"api_key": "%s"
},
"agent": {
"api_key": "%s"
}
}\n' \
"$CONTAINER_NAME" \
"$API_BASE" \
"$API_BASE" \
"$ADMIN_USERNAME" \
"$ADMIN_PASSWORD" \
"$admin_key" \
"$agent_key"
}
# ---------------------------------------------------------------------------
# Install the dev Procfile into the running container
# ---------------------------------------------------------------------------
install_dev_procfile() {
log "Installing dev Procfile (uvicorn --reload) into container..."
printf '%s' "$DEV_PROCFILE" | docker exec -i "$CONTAINER_NAME" \
bash -c "cat > ${PROCFILE_PATH}"
log "Dev Procfile installed."
}
# ---------------------------------------------------------------------------
# Subcommand: up
# ---------------------------------------------------------------------------
cmd_up() {
# Stop existing container if present
if container_exists; then
log "Stopping existing container '${CONTAINER_NAME}'..."
docker rm -f "$CONTAINER_NAME" >/dev/null 2>&1 || true
fi
# Ensure volume exists
if ! docker volume inspect "$VOLUME_NAME" &>/dev/null; then
log "Creating data volume '${VOLUME_NAME}'..."
docker volume create "$VOLUME_NAME" >/dev/null
fi
# Resolve LLM_API_KEY from host environment
local llm_api_key="${LLM_API_KEY:-}"
if [[ -z "$llm_api_key" ]]; then
log "ERROR: LLM_API_KEY env var is required. Export it or pass inline:"
log " LLM_API_KEY=sk-ant-... ./dev.sh up"
json_error "LLM_API_KEY env var is required"
exit 1
fi
log "Starting container '${CONTAINER_NAME}' from image '${PLIT_IMAGE}'..."
docker run -d \
--name "$CONTAINER_NAME" \
--restart unless-stopped \
-p "${API_PORT}:${API_PORT}" \
-p 8080:8080 \
-v "${VOLUME_NAME}:/root/.local/share/plit" \
-v "${PLATFORM_HOST}:${PLATFORM_INNER}" \
-e "ADMIN_USERNAME=${ADMIN_USERNAME}" \
-e "ADMIN_PASSWORD=${ADMIN_PASSWORD}" \
-e LLM_PROVIDER=anthropic \
-e LLM_MODEL=claude-sonnet-4-20250514 \
-e "LLM_API_KEY=${llm_api_key}" \
"$PLIT_IMAGE" \
>/dev/null
log "Container started. Waiting for entrypoint init..."
# Give the entrypoint a moment to start before we overwrite the Procfile.
# We poll until the Procfile exists (entrypoint creates it via plit init).
local wait_init=0
while [[ $wait_init -lt 30 ]]; do
if docker exec "$CONTAINER_NAME" test -f "$PROCFILE_PATH" 2>/dev/null; then
break
fi
sleep 2
wait_init=$(( wait_init + 2 ))
done
install_dev_procfile
# The entrypoint runs `plit start --foreground` which reads the Procfile.
# If the entrypoint already launched honcho before we wrote the file,
# we need to restart the plit process so it picks up our Procfile.
log "Restarting 'plit start' process inside container to pick up dev Procfile..."
docker exec "$CONTAINER_NAME" bash -c \
"pkill -f 'plit start' 2>/dev/null || true; sleep 1; nohup plit start --foreground </dev/null >>/tmp/plit.log 2>&1 &"
# Wait for the API
if ! wait_healthy; then
json_error "API did not become healthy — check 'docker logs ${CONTAINER_NAME}'"
exit 1
fi
emit_status_json
}
# ---------------------------------------------------------------------------
# Subcommand: down
# ---------------------------------------------------------------------------
cmd_down() {
if container_exists; then
log "Removing container '${CONTAINER_NAME}' (volumes preserved)..."
docker rm -f "$CONTAINER_NAME" >/dev/null
log "Container removed."
else
log "Container '${CONTAINER_NAME}' does not exist — nothing to do."
fi
printf '{"status":"stopped","container":"%s"}\n' "$CONTAINER_NAME"
}
# ---------------------------------------------------------------------------
# Subcommand: restart
# ---------------------------------------------------------------------------
cmd_restart() {
if ! container_running; then
json_error "Container '${CONTAINER_NAME}' is not running. Use 'up' first."
exit 1
fi
log "Reinstalling dev Procfile..."
install_dev_procfile
log "Restarting honcho / plit start inside container..."
docker exec "$CONTAINER_NAME" bash -c \
"pkill -f 'plit start' 2>/dev/null || true; pkill -f honcho 2>/dev/null || true; sleep 2; nohup plit start --foreground </dev/null >>/tmp/plit.log 2>&1 &"
if ! wait_healthy; then
json_error "API did not become healthy after restart"
exit 1
fi
emit_status_json
}
# ---------------------------------------------------------------------------
# Subcommand: status
# ---------------------------------------------------------------------------
cmd_status() {
if ! container_running; then
printf '{"status":"stopped","container":"%s"}\n' "$CONTAINER_NAME"
return 0
fi
# Quick liveness check — don't wait, just try once
local http_code
http_code=$(curl -s -o /dev/null -w "%{http_code}" --max-time 5 "$HEALTH_URL" 2>/dev/null || true)
if [[ "$http_code" != "200" ]]; then
printf '{"status":"starting","container":"%s","api_url":"%s"}\n' \
"$CONTAINER_NAME" "$API_BASE"
return 0
fi
emit_status_json
}
# ---------------------------------------------------------------------------
# Subcommand: logs
# ---------------------------------------------------------------------------
cmd_logs() {
local service="${1:-}"
if ! container_exists; then
log "Container '${CONTAINER_NAME}' does not exist."
exit 1
fi
if [[ -n "$service" ]]; then
# Filter by service name, e.g. pipelit.1, worker.1
docker logs -f "$CONTAINER_NAME" 2>&1 | grep --line-buffered "${service}"
else
docker logs -f "$CONTAINER_NAME" 2>&1
fi
}
# ---------------------------------------------------------------------------
# Subcommand: exec
# ---------------------------------------------------------------------------
cmd_exec() {
if [[ $# -eq 0 ]]; then
json_error "exec requires a command argument"
exit 1
fi
if ! container_running; then
json_error "Container '${CONTAINER_NAME}' is not running"
exit 1
fi
# Run in the platform working directory with the venv active.
# Source both .env files so DATABASE_URL, FIELD_ENCRYPTION_KEY, etc. are set.
docker exec \
-w "$PLATFORM_INNER" \
-e "PATH=${VENV_BIN}:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin" \
"$CONTAINER_NAME" \
bash -c "set -a; source /root/.config/plit/.env 2>/dev/null; source /root/.local/share/plit/pipelit/.env 2>/dev/null; set +a; exec \"\$@\"" -- "$@"
}
# ---------------------------------------------------------------------------
# Subcommand: test
# ---------------------------------------------------------------------------
cmd_test() {
if ! container_running; then
json_error "Container '${CONTAINER_NAME}' is not running"
exit 1
fi
log "Running pytest inside container..."
docker exec \
-w "$PLATFORM_INNER" \
-e "PATH=${VENV_BIN}:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin" \
"$CONTAINER_NAME" \
bash -c "set -a; source /root/.config/plit/.env 2>/dev/null; source /root/.local/share/plit/pipelit/.env 2>/dev/null; set +a; exec python -m pytest tests/ -v"
}
# ---------------------------------------------------------------------------
# Dispatch
# ---------------------------------------------------------------------------
if [[ $# -eq 0 ]]; then
printf '{"status":"error","message":"Usage: dev.sh <up|down|restart|status|logs|exec|test> [args...]"}\n'
exit 1
fi
SUBCOMMAND="$1"
shift
case "$SUBCOMMAND" in
up) cmd_up "$@" ;;
down) cmd_down "$@" ;;
restart) cmd_restart "$@" ;;
status) cmd_status "$@" ;;
logs) cmd_logs "$@" ;;
exec) cmd_exec "$@" ;;
test) cmd_test "$@" ;;
*)
json_error "Unknown subcommand: ${SUBCOMMAND}"
exit 1
;;
esac