-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathstart-local-secure.sh
More file actions
executable file
·136 lines (113 loc) · 4.68 KB
/
Copy pathstart-local-secure.sh
File metadata and controls
executable file
·136 lines (113 loc) · 4.68 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
#!/usr/bin/env bash
set -euo pipefail
ROOT_DIR="$(cd "$(dirname "$0")" && pwd)"
MONGO_DBPATH="${HOME}/mongodata"
MONGO_LOG="/tmp/openlearnx_mongod.log"
BACKEND_LOG="/tmp/openlearnx_backend.log"
FRONTEND_LOG="/tmp/openlearnx_frontend.log"
FRONTEND_PID_FILE="/tmp/openlearnx_frontend.pid"
VENV_PYTHON="${ROOT_DIR}/venv_openlearnx/bin/python3"
cd "$ROOT_DIR"
echo "[1/8] Checking prerequisites"
command -v mongod >/dev/null 2>&1 || { echo "ERROR: mongod not found"; exit 1; }
command -v pnpm >/dev/null 2>&1 || { echo "ERROR: pnpm not found"; exit 1; }
command -v docker >/dev/null 2>&1 || { echo "ERROR: docker not found"; exit 1; }
[[ -x "$VENV_PYTHON" ]] || { echo "ERROR: Python venv not found at $VENV_PYTHON"; exit 1; }
ensure_docker_access() {
if docker info >/dev/null 2>&1; then
return 0
fi
echo "[Docker] Current user cannot access Docker. Attempting automatic fix..."
if ! sudo -n true >/dev/null 2>&1; then
echo "[Docker] sudo authentication required once to configure Docker access."
sudo -v
fi
sudo systemctl enable --now docker
if ! getent group docker >/dev/null 2>&1; then
sudo groupadd docker
fi
sudo usermod -aG docker "$USER"
sudo chgrp docker /var/run/docker.sock || true
sudo chmod 660 /var/run/docker.sock || true
if docker info >/dev/null 2>&1; then
return 0
fi
echo "[Docker] Group refresh required. Testing with sg docker context..."
if sg docker -c 'docker info >/dev/null 2>&1'; then
return 0
fi
echo "ERROR: Docker access is still unavailable after auto-fix."
echo "Run: newgrp docker (or log out/in) and rerun this script."
exit 1
}
run_backend() {
if docker info >/dev/null 2>&1; then
nohup "$VENV_PYTHON" backend/main.py >"$BACKEND_LOG" 2>&1 &
sleep 1
pgrep -f "python3 .*backend/main.py" | head -n1 || true
return 0
fi
# Start backend in docker group context when group refresh has not propagated.
sg docker -c "nohup '$VENV_PYTHON' '$ROOT_DIR/backend/main.py' >'$BACKEND_LOG' 2>&1 &"
sleep 1
pgrep -f "python3 .*backend/main.py" | head -n1 || true
}
echo "[2/8] Ensuring Docker access"
ensure_docker_access
echo "[3/8] Stopping old local processes"
pkill -f "mongod.*--dbpath ${MONGO_DBPATH}" 2>/dev/null || true
pkill -f "python3 .*backend/main.py" 2>/dev/null || true
pkill -f "pnpm dev" 2>/dev/null || true
pkill -f "next dev" 2>/dev/null || true
echo "[4/8] Starting MongoDB"
mkdir -p "$MONGO_DBPATH"
if pgrep -f "mongod.*--dbpath ${MONGO_DBPATH}" >/dev/null 2>&1; then
echo "MongoDB already running for ${MONGO_DBPATH}; reusing existing process"
else
set +e
mongod --dbpath "$MONGO_DBPATH" --bind_ip 127.0.0.1 --port 27017 --logpath "$MONGO_LOG" --fork >/tmp/openlearnx_mongod_fork.out 2>&1
mongo_start_code=$?
set -e
if [[ $mongo_start_code -ne 0 ]]; then
echo "WARNING: mongod --fork returned ${mongo_start_code}. Checking if service is still running..."
fi
fi
MONGO_PID="$(pgrep -f "mongod.*--dbpath ${MONGO_DBPATH}" | head -n1 || true)"
if [[ -z "$MONGO_PID" ]]; then
echo "ERROR: MongoDB did not start. Check logs: ${MONGO_LOG} and /tmp/openlearnx_mongod_fork.out"
exit 1
fi
echo "Mongo PID: ${MONGO_PID:-N/A}"
echo "[5/8] Starting backend"
BACKEND_PID="$(run_backend)"
if [[ -z "$BACKEND_PID" ]]; then
echo "ERROR: Backend did not start. Check log: $BACKEND_LOG"
exit 1
fi
echo "Backend PID: ${BACKEND_PID:-N/A}"
echo "[6/8] Starting frontend"
(
cd frontend
nohup pnpm dev >"$FRONTEND_LOG" 2>&1 &
echo "$!" >"$FRONTEND_PID_FILE"
)
FRONTEND_PID="$(cat "$FRONTEND_PID_FILE")"
echo "Frontend PID: ${FRONTEND_PID:-N/A}"
echo "[7/8] Waiting for services"
backend_code="$(curl -sS -o /tmp/openlearnx_backend_health_body.txt -w "%{http_code}" --retry 30 --retry-all-errors --retry-connrefused --retry-delay 1 http://127.0.0.1:5000/api/health || true)"
frontend_code="$(curl -sS -o /tmp/openlearnx_frontend_body.txt -w "%{http_code}" --retry 30 --retry-all-errors --retry-connrefused --retry-delay 1 http://127.0.0.1:3000 || true)"
echo "[8/8] Verifying secure compiler execution"
compiler_code="$(curl -sS -o /tmp/openlearnx_compiler_smoke.json -w "%{http_code}" -X POST http://127.0.0.1:5000/api/compiler/execute -H "Content-Type: application/json" -d '{"language":"python","code":"print(\"ok\")"}' || true)"
echo ""
echo "RESULT"
echo " Mongo PID: ${MONGO_PID:-N/A}"
echo " Backend PID: ${BACKEND_PID:-N/A}"
echo " Frontend PID: ${FRONTEND_PID:-N/A}"
echo " Backend health: ${backend_code:-000} (http://127.0.0.1:5000/api/health)"
echo " Frontend health:${frontend_code:-000} (http://127.0.0.1:3000)"
echo " Compiler smoke: ${compiler_code:-000} (/api/compiler/execute)"
echo ""
echo "Logs:"
echo " MongoDB: $MONGO_LOG"
echo " Backend: $BACKEND_LOG"
echo " Frontend: $FRONTEND_LOG"