-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathinit.sh
More file actions
executable file
·253 lines (222 loc) · 6.04 KB
/
init.sh
File metadata and controls
executable file
·253 lines (222 loc) · 6.04 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
#!/usr/bin/env bash
set -euo pipefail
LOG_DIR=".dev-logs"
CONSOLE_IMAGE="${CONSOLE_IMAGE:="quay.io/openshift/origin-console:latest"}"
BACKEND_PORT=8080
PLUGIN_PORT=9001
CONSOLE_PORT=9000
TIMEOUT=60
PID_DIR=".dev-pids"
wait_for_port() {
local port=$1
local label=$2
local elapsed=0
while ! bash -c "echo >/dev/tcp/localhost/$port" 2>/dev/null; do
if [ $elapsed -ge $TIMEOUT ]; then
echo "Error: $label did not start within ${TIMEOUT}s. Check $LOG_DIR/ for details."
exit 1
fi
echo "Waiting for $label (port $port)... ${elapsed}s"
sleep 1
elapsed=$((elapsed + 1))
done
}
kill_tree() {
local pid=$1
local children
children=$(pgrep -P "$pid" 2>/dev/null || true)
for child in $children; do
kill_tree "$child"
done
kill "$pid" 2>/dev/null || true
}
stop_pid() {
local pidfile="$PID_DIR/$1"
local label=$2
if [ ! -f "$pidfile" ]; then
return
fi
local pid
pid=$(cat "$pidfile")
if kill -0 "$pid" 2>/dev/null; then
kill_tree "$pid"
while kill -0 "$pid" 2>/dev/null; do sleep 0.1; done
echo "Stopped $label (PID $pid)."
fi
rm -f "$pidfile"
}
random_free_port() {
local port
while true; do
port=$((RANDOM % 50001 + 10000))
if ! bash -c "echo >/dev/tcp/localhost/$port" 2>/dev/null; then
echo "$port"
return
fi
done
}
write_dev_env() {
cat > .dev-env.json <<EOF
{
"backendPort": $BACKEND_PORT,
"pluginPort": $PLUGIN_PORT,
"consolePort": $CONSOLE_PORT
}
EOF
}
start_backend() {
echo "Building Go backend..."
(cd backend && go build -buildvcs=false -o ../bin/backend .)
(cd backend && go build -buildvcs=false -o ../bin/errserver ./cmd/errserver)
echo "Starting Go backend..."
./bin/backend --http-port "$BACKEND_PORT" >>"$LOG_DIR/backend.log" 2>&1 &
echo $! > "$PID_DIR/backend.pid"
}
start_backend_watcher() {
if ! command -v inotifywait &>/dev/null; then
echo "Warning: inotifywait not found. Install inotify-tools for auto-recompile."
return
fi
echo "Starting backend file watcher..."
(
while true; do
if ! inotifywait -r -e modify,create,delete,move --include '\.(go|mod|sum)$' backend/ >/dev/null 2>&1; then
echo "[watcher] inotifywait failed. Shutting down dev environment."
stop_dev
break
fi
sleep 1 # debounce
echo "[watcher] Detected change, rebuilding backend..."
old_pid=$(cat "$PID_DIR/backend.pid" 2>/dev/null || true)
build_output=$(cd backend && go build -buildvcs=false -o ../bin/backend-tmp . 2>&1) && build_ok=true || build_ok=false
if [ -n "$old_pid" ]; then
kill_tree "$old_pid" 2>/dev/null || true
while kill -0 "$old_pid" 2>/dev/null; do sleep 0.1; done
fi
if $build_ok; then
mv bin/backend-tmp bin/backend
./bin/backend --http-port "$BACKEND_PORT" >>"$LOG_DIR/backend.log" 2>&1 &
echo $! > "$PID_DIR/backend.pid"
echo "[watcher] Backend restarted (PID $!)."
else
echo "[watcher] Build failed. Starting error server."
echo "$build_output"
rm -f bin/backend-tmp
echo "$build_output" > "$LOG_DIR/backend-build-error.txt"
./bin/errserver --port "$BACKEND_PORT" --msg-file "$LOG_DIR/backend-build-error.txt" >>"$LOG_DIR/backend.log" 2>&1 &
errserver_pid=$!
sleep 0.5
if ! kill -0 "$errserver_pid" 2>/dev/null; then
echo "[watcher] Error server failed to start. Shutting down."
stop_dev
break
fi
echo "$errserver_pid" > "$PID_DIR/backend.pid"
fi
done
) >>"$LOG_DIR/backend.log" 2>&1 &
echo $! > "$PID_DIR/backend-watcher.pid"
}
stop_backend() {
stop_pid "backend-watcher.pid" "backend watcher"
stop_pid "backend.pid" "Go backend"
}
stop_plugin() {
stop_pid "webpack.pid" "plugin dev server"
}
stop_console() {
local cidfile="$PID_DIR/console.cid"
if [ ! -f "$cidfile" ]; then
return
fi
local cid
cid=$(cat "$cidfile")
if podman stop "$cid" >/dev/null 2>&1; then
echo "Stopped OpenShift console (container $cid)."
fi
rm -f "$cidfile"
}
stop_dev() {
stop_backend
stop_plugin
stop_console
rm -f .dev-env.json
}
check_prerequisites() {
if ! command -v oc &>/dev/null; then
echo "Error: oc CLI not found. Install from https://console.redhat.com/openshift/downloads"
exit 1
fi
if ! oc whoami &>/dev/null; then
echo "Error: not logged in to OpenShift. Run 'oc login' first."
exit 1
fi
}
install_dependencies() {
if [ ! -d "node_modules" ]; then
echo "Installing dependencies..."
yarn install
fi
}
start_plugin() {
echo "Starting plugin dev server..."
PLUGIN_PORT="$PLUGIN_PORT" yarn start >"$LOG_DIR/webpack.log" 2>&1 &
echo $! > "$PID_DIR/webpack.pid"
}
start_console() {
echo "Starting OpenShift console..."
./start-console.sh \
--backend-port "$BACKEND_PORT" \
--plugin-port "$PLUGIN_PORT" \
--console-port "$CONSOLE_PORT" \
--cidfile "$PID_DIR/console.cid" \
>"$LOG_DIR/console.log" 2>&1 &
}
print_status() {
echo ""
echo "Dev environment started:"
echo " Backend: http://localhost:$BACKEND_PORT"
echo " Console: http://localhost:$CONSOLE_PORT"
echo " Logs: $LOG_DIR/"
echo ""
echo "To stop: ./init.sh --stop"
}
main() {
mkdir -p "$LOG_DIR" "$PID_DIR" bin
check_prerequisites
install_dependencies
stop_dev
write_dev_env
start_backend
wait_for_port "$BACKEND_PORT" "Go backend"
start_backend_watcher
start_plugin
wait_for_port "$PLUGIN_PORT" "Plugin dev server"
start_console
wait_for_port "$CONSOLE_PORT" "OpenShift console"
print_status
}
case "${1:-}" in
--stop)
stop_dev
;;
--randomize-ports)
BACKEND_PORT=$(random_free_port)
PLUGIN_PORT=$(random_free_port)
while [ "$PLUGIN_PORT" -eq "$BACKEND_PORT" ]; do
PLUGIN_PORT=$(random_free_port)
done
CONSOLE_PORT=$(random_free_port)
while [ "$CONSOLE_PORT" -eq "$BACKEND_PORT" ] || [ "$CONSOLE_PORT" -eq "$PLUGIN_PORT" ]; do
CONSOLE_PORT=$(random_free_port)
done
main
;;
"")
main
;;
*)
echo "Usage: $0 [--stop | --randomize-ports]"
exit 1
;;
esac