-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathdev.sh
More file actions
executable file
·209 lines (193 loc) · 9.17 KB
/
dev.sh
File metadata and controls
executable file
·209 lines (193 loc) · 9.17 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
#!/bin/bash
# =============================================================================
# dev.sh — Pythinker development workflow
#
# Uses Docker Compose Watch for file sync + HMR. Compose Watch syncs files
# from the host into running containers via the Docker API (tar+cp), bypassing
# OrbStack's TCC/FDA bind-mount restriction on ~/Desktop/Projects. Files land
# on the container's native ext4 filesystem, giving instant inotify events to
# Vite and optional uvicorn --reload — no polling required.
#
# HMR flow:
# Edit file → Compose Watch (tar+cp to container) → inotify fires →
# Frontend: Vite HMR → instant browser update (no page reload)
# Backend: uvicorn --reload (default on) → Python auto-restart (~1s)
# Sandbox: uvicorn --reload → Python auto-restart (~1s)
# Gateway: sync+restart on code change (channel pipeline, Telegram etc.)
#
# Commands:
# ./dev.sh Build + start full stack + live watch (DEFAULT)
# ./dev.sh watch Same as above (explicit)
# ./dev.sh attach Attach Compose Watch to ALREADY-RUNNING containers (no rebuild, no up)
# Use after: docker compose up -d or ./dev.sh up -d
# Restores file sync + HMR/uvicorn --reload without restarting the stack.
# ./dev.sh up -d Start without watch (no HMR — manual use only)
# ./dev.sh logs -f backend Follow backend logs
# ./dev.sh down -v Stop + remove volumes
# ./dev.sh [--monitoring] <cmd> Include Prometheus/Grafana stack
#
# Legacy (rsync fallback — only needed if compose watch doesn't work):
# ./dev.sh sync One-shot rsync backend + frontend to /private/tmp
# ./dev.sh sync backend Sync backend only
# ./dev.sh sync frontend Sync frontend only
# =============================================================================
set -euo pipefail
PROJECT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
# ── Legacy rsync helpers (fallback if compose watch isn't available) ──────────
# These sync source code to /private/tmp staging dirs for bind-mount fallback.
# With compose watch, these are only needed for one-off debugging.
BACKEND_SRC="$PROJECT_DIR/backend"
FRONTEND_SRC="$PROJECT_DIR/frontend"
BACKEND_TMP="/private/tmp/pythinker-backend"
FRONTEND_TMP="/private/tmp/pythinker-frontend"
sync_backend() {
mkdir -p "$BACKEND_TMP"
rsync -a --delete \
--exclude='__pycache__/' \
--exclude='*.pyc' \
--exclude='.venv/' \
--exclude='.git/' \
--exclude='*.egg-info/' \
--exclude='.pytest_cache/' \
"$BACKEND_SRC/" "$BACKEND_TMP/"
echo "✓ Backend synced → $BACKEND_TMP"
}
sync_frontend() {
mkdir -p "$FRONTEND_TMP"
rsync -a --delete "$FRONTEND_SRC/src/" "$FRONTEND_TMP/src/"
rsync -a --delete "$FRONTEND_SRC/public/" "$FRONTEND_TMP/public/"
for f in index.html package.json package-lock.json bun.lock \
vite.config.ts tsconfig.json tsconfig.app.json \
tsconfig.node.json env.d.ts; do
[[ -f "$FRONTEND_SRC/$f" ]] && cp -f "$FRONTEND_SRC/$f" "$FRONTEND_TMP/$f"
done
echo "✓ Frontend synced → $FRONTEND_TMP"
}
cmd_sync() {
local target="${1:-all}"
case "$target" in
backend) sync_backend ;;
frontend) sync_frontend ;;
all) sync_backend; sync_frontend ;;
*)
echo "Usage: ./dev.sh sync [backend|frontend|all]" >&2
exit 1
;;
esac
}
# ── Docker Compose helpers ────────────────────────────────────────────────────
# Always run from project root so compose watch paths resolve correctly.
# This allows calling ./dev.sh from any working directory (e.g., from an IDE terminal).
cd "$PROJECT_DIR"
# Match host Docker socket GID so backend/gateway can use /var/run/docker.sock (diagnostics,
# sandbox lifecycle). Override with DOCKER_GID in .env when needed.
if [[ -z "${DOCKER_GID:-}" ]] && [[ -S /var/run/docker.sock ]]; then
if _g="$(stat -f '%g' /var/run/docker.sock 2>/dev/null)"; then
export DOCKER_GID="$_g"
elif _g="$(stat -c '%g' /var/run/docker.sock 2>/dev/null)"; then
export DOCKER_GID="$_g"
fi
unset _g
fi
if command -v docker &>/dev/null && docker compose version &>/dev/null 2>&1; then
COMPOSE="docker compose"
elif command -v docker-compose &>/dev/null; then
COMPOSE="docker-compose"
else
echo "Error: Neither docker compose nor docker-compose found" >&2
exit 1
fi
# Optional flags:
# --monitoring -> include Prometheus + Grafana + Loki stack
# Default: docker-compose.yml (no -f flag needed, Docker Compose picks it up automatically)
COMPOSE_FILES=""
ENABLE_MONITORING=0
POSITIONAL_ARGS=()
for arg in "$@"; do
case "$arg" in
--monitoring)
ENABLE_MONITORING=1
;;
*)
POSITIONAL_ARGS+=("$arg")
;;
esac
done
set -- "${POSITIONAL_ARGS[@]}"
if [[ "$ENABLE_MONITORING" -eq 1 ]]; then
COMPOSE_FILES="-f docker-compose.yml -f docker-compose-monitoring.yml"
fi
# ── Dispatch ─────────────────────────────────────────────────────────────────
CMD="${1:-watch}"
case "$CMD" in
watch)
# Default dev workflow: build + start full stack + live file sync via Docker Compose Watch
echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━"
echo " Pythinker Dev — Docker Compose Watch"
echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━"
echo " Frontend : ./frontend/src → /app/src [Vite HMR]"
echo " Backend : ./backend/app → /app/app [uvicorn --reload]"
echo " Sandbox : ./sandbox/app → /app/app [uvicorn --reload]"
echo " Gateway : ./backend/app → /app/app [sync+restart]"
echo ""
echo " Tip: containers already running? Use ./dev.sh attach instead."
echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━"
echo ""
$COMPOSE $COMPOSE_FILES up --build --watch
;;
attach)
# Attach Compose Watch to ALREADY-RUNNING containers (no rebuild, no `compose up`).
# Requires Docker Compose v2 with `watch`; fails fast if nothing is running.
shift
_ps_ok=0
_running_q=""
if _running_q=$($COMPOSE $COMPOSE_FILES ps --status running -q 2>/dev/null); then
_ps_ok=1
else
echo "Note: could not list running services (upgrade Docker Compose v2.22+ for a preflight check)." >&2
fi
if [[ "$_ps_ok" -eq 1 ]]; then
if [[ -z "${_running_q//[$'\t\r\n ']/}" ]]; then
if $COMPOSE $COMPOSE_FILES ps -q 2>/dev/null | grep -q .; then
echo "Compose has containers but none are in \"running\" state." >&2
else
echo "No running containers for this Compose project." >&2
fi
echo "" >&2
echo "Start the stack first, then attach watch:" >&2
echo " docker compose up -d && ./dev.sh attach" >&2
echo " # or foreground with watch from scratch:" >&2
echo " ./dev.sh watch" >&2
exit 1
fi
fi
unset _ps_ok _running_q
echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━"
echo " Pythinker Dev — Attaching Compose Watch"
echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━"
echo " Watching for file changes (containers already running)..."
echo " Frontend : ./frontend/src → /app/src [Vite HMR]"
echo " Backend : ./backend/app → /app/app [uvicorn --reload]"
echo " Sandbox : ./sandbox/app → /app/app [uvicorn --reload]"
echo " Gateway : ./backend/app → /app/app [sync+restart]"
echo ""
echo " Ctrl+C stops watch only — containers keep running."
echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━"
echo ""
# --no-up: do not run compose up; --prune: drop stale sync state
exec $COMPOSE $COMPOSE_FILES watch --no-up --prune "$@"
;;
sync)
# Legacy: rsync to /private/tmp staging dirs
shift
cmd_sync "${1:-all}"
;;
up|start|restart)
# Pass through to docker compose (no auto-sync needed — image has source code)
$COMPOSE $COMPOSE_FILES "$@"
;;
*)
# All other compose commands pass through unchanged
$COMPOSE $COMPOSE_FILES "$@"
;;
esac