This repository was archived by the owner on Oct 8, 2026. It is now read-only.
Repository navigation
Expand file tree
/
Copy pathresource_manager.sh
More file actions
executable file
·407 lines (341 loc) · 13.4 KB
/
Copy pathresource_manager.sh
File metadata and controls
executable file
·407 lines (341 loc) · 13.4 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
#!/bin/bash
# Global Resource Manager for OCG
# Manages port allocation across all OCG projects system-wide
# Global registry file path
GLOBAL_REGISTRY_FILE="$HOME/.ocg/resources.json"
GLOBAL_REGISTRY_LOCK="$HOME/.ocg/resources.lock"
# Ensure ~/.ocg directory exists
ensure_global_registry() {
local ocg_dir="$HOME/.ocg"
if [ ! -d "$ocg_dir" ]; then
mkdir -p "$ocg_dir"
fi
# Create registry if it doesn't exist
if [ ! -f "$GLOBAL_REGISTRY_FILE" ]; then
cat >"$GLOBAL_REGISTRY_FILE" <<'EOF'
{
"phoenix_ports": {},
"playwright_ports": {},
"metadata": {
"last_updated": ""
}
}
EOF
fi
# Validate registry JSON structure
if ! jq . "$GLOBAL_REGISTRY_FILE" >/dev/null 2>&1; then
echo "⚠️ Warning: Global registry corrupted, recreating..." >&2
# Backup corrupted file
if [ -f "$GLOBAL_REGISTRY_FILE" ]; then
cp "$GLOBAL_REGISTRY_FILE" "${GLOBAL_REGISTRY_FILE}.backup.$(date +%s)"
fi
# Recreate clean registry
cat >"$GLOBAL_REGISTRY_FILE" <<'EOF'
{
"phoenix_ports": {},
"playwright_ports": {},
"metadata": {
"last_updated": ""
}
}
EOF
fi
}
# Acquire file lock for atomic operations
acquire_lock() {
local timeout=10
local count=0
while [ $count -lt $timeout ]; do
if mkdir "$GLOBAL_REGISTRY_LOCK" 2>/dev/null; then
return 0
fi
sleep 0.1
((count++))
done
echo "❌ Error: Could not acquire registry lock after ${timeout}s" >&2
return 1
}
# Release file lock
release_lock() {
rmdir "$GLOBAL_REGISTRY_LOCK" 2>/dev/null || true
}
# Update the metadata timestamp
update_timestamp() {
local timestamp=$(date -u +"%Y-%m-%dT%H:%M:%SZ")
jq ".metadata.last_updated = \"$timestamp\"" "$GLOBAL_REGISTRY_FILE" >"${GLOBAL_REGISTRY_FILE}.tmp" &&
mv "${GLOBAL_REGISTRY_FILE}.tmp" "$GLOBAL_REGISTRY_FILE"
}
# Check if a port is already allocated globally
is_port_allocated() {
local port="$1"
local port_type="$2" # phoenix_ports or playwright_ports
ensure_global_registry
if jq -e ".${port_type}.\"${port}\"" "$GLOBAL_REGISTRY_FILE" >/dev/null 2>&1; then
return 0 # Port is allocated
else
return 1 # Port is not allocated
fi
}
# Check if port is in use by system (not just OCG)
is_port_in_use() {
local port="$1"
lsof -i ":$port" >/dev/null 2>&1
}
# Allocate a Phoenix port for a project/workspace
allocate_phoenix_port() {
local project="$1"
local workspace="$2"
if [ -z "$project" ] || [ -z "$workspace" ]; then
echo "❌ Error: Project and workspace name required" >&2
return 1
fi
ensure_global_registry
if ! acquire_lock; then
return 1
fi
# Find next available port starting from 4001
local base_port=4001
local current_port=$base_port
local max_port=4100
while [ $current_port -le $max_port ]; do
if ! is_port_allocated "$current_port" "phoenix_ports" && ! is_port_in_use "$current_port"; then
# Allocate the port
local timestamp=$(date -u +"%Y-%m-%dT%H:%M:%SZ")
jq ".phoenix_ports.\"${current_port}\" = {\"project\": \"$project\", \"workspace\": \"$workspace\", \"allocated_at\": \"$timestamp\"}" \
"$GLOBAL_REGISTRY_FILE" >"${GLOBAL_REGISTRY_FILE}.tmp" &&
mv "${GLOBAL_REGISTRY_FILE}.tmp" "$GLOBAL_REGISTRY_FILE"
update_timestamp
release_lock
echo "$current_port"
return 0
fi
((current_port++))
done
release_lock
echo "❌ Error: Could not find available Phoenix port (checked up to $max_port)" >&2
return 1
}
# Allocate a Playwright port for a project/workspace
allocate_playwright_port() {
local project="$1"
local workspace="$2"
if [ -z "$project" ] || [ -z "$workspace" ]; then
echo "❌ Error: Project and workspace name required" >&2
return 1
fi
ensure_global_registry
if ! acquire_lock; then
return 1
fi
# Find next available port starting from 8901
local base_port=8901
local current_port=$base_port
local max_port=9000
while [ $current_port -le $max_port ]; do
if ! is_port_allocated "$current_port" "playwright_ports" && ! is_port_in_use "$current_port"; then
# Allocate the port
local timestamp=$(date -u +"%Y-%m-%dT%H:%M:%SZ")
jq ".playwright_ports.\"${current_port}\" = {\"project\": \"$project\", \"workspace\": \"$workspace\", \"allocated_at\": \"$timestamp\"}" \
"$GLOBAL_REGISTRY_FILE" >"${GLOBAL_REGISTRY_FILE}.tmp" &&
mv "${GLOBAL_REGISTRY_FILE}.tmp" "$GLOBAL_REGISTRY_FILE"
update_timestamp
release_lock
echo "$current_port"
return 0
fi
((current_port++))
done
release_lock
echo "❌ Error: Could not find available Playwright port (checked up to $max_port)" >&2
return 1
}
# Deallocate all resources for a project/workspace
deallocate_resources() {
local project="$1"
local workspace="$2"
if [ -z "$project" ] || [ -z "$workspace" ]; then
echo "❌ Error: Project and workspace name required" >&2
return 1
fi
ensure_global_registry
if ! acquire_lock; then
return 1
fi
# Find and remove allocations for this project/workspace
local phoenix_removed=0
local playwright_removed=0
# Remove Phoenix port allocations
while IFS= read -r port; do
if [ -n "$port" ]; then
jq "del(.phoenix_ports.\"$port\")" "$GLOBAL_REGISTRY_FILE" >"${GLOBAL_REGISTRY_FILE}.tmp" &&
mv "${GLOBAL_REGISTRY_FILE}.tmp" "$GLOBAL_REGISTRY_FILE"
((phoenix_removed++))
fi
done < <(jq -r ".phoenix_ports | to_entries[] | select(.value.project == \"$project\" and .value.workspace == \"$workspace\") | .key" "$GLOBAL_REGISTRY_FILE" 2>/dev/null || true)
# Remove Playwright port allocations
while IFS= read -r port; do
if [ -n "$port" ]; then
jq "del(.playwright_ports.\"$port\")" "$GLOBAL_REGISTRY_FILE" >"${GLOBAL_REGISTRY_FILE}.tmp" &&
mv "${GLOBAL_REGISTRY_FILE}.tmp" "$GLOBAL_REGISTRY_FILE"
((playwright_removed++))
fi
done < <(jq -r ".playwright_ports | to_entries[] | select(.value.project == \"$project\" and .value.workspace == \"$workspace\") | .key" "$GLOBAL_REGISTRY_FILE" 2>/dev/null || true)
if [ $phoenix_removed -gt 0 ] || [ $playwright_removed -gt 0 ]; then
update_timestamp
echo "✅ Deallocated $phoenix_removed Phoenix port(s) and $playwright_removed Playwright port(s) for $project/$workspace" >&2
fi
release_lock
return 0
}
# List all global resources
list_global_resources() {
ensure_global_registry
echo "🌐 GLOBAL RESOURCE ALLOCATION"
echo ""
# Partition-based view
echo "📋 Resource Partitions:"
local phoenix_count=0
while IFS=$'\t' read -r port project workspace allocated_at; do
if [ -n "$port" ]; then
local partition=$((port - 4000))
# Calculate time since allocation
local time_ago=""
if command -v date >/dev/null 2>&1 && [ -n "$allocated_at" ]; then
if [[ "$OSTYPE" == "darwin"* ]]; then
# macOS date command
local allocated_epoch=$(date -j -f "%Y-%m-%dT%H:%M:%SZ" "$allocated_at" "+%s" 2>/dev/null || echo "")
else
# Linux date command
local allocated_epoch=$(date -d "$allocated_at" "+%s" 2>/dev/null || echo "")
fi
if [ -n "$allocated_epoch" ]; then
local current_epoch=$(date "+%s")
local diff=$((current_epoch - allocated_epoch))
if [ $diff -lt 3600 ]; then
time_ago="$(($diff / 60))m ago"
elif [ $diff -lt 86400 ]; then
time_ago="$(($diff / 3600))h ago"
else
time_ago="$(($diff / 86400))d ago"
fi
fi
fi
echo " $partition → $project/$workspace${time_ago:+ (allocated $time_ago)}"
((phoenix_count++))
fi
done < <(jq -r '.phoenix_ports | to_entries[] | "\(.key)\t\(.value.project)\t\(.value.workspace)\t\(.value.allocated_at)"' "$GLOBAL_REGISTRY_FILE" 2>/dev/null | sort -n)
if [ $phoenix_count -eq 0 ]; then
echo " (none allocated)"
fi
echo ""
# Summary
echo "📊 Resource Summary:"
echo " 📋 Partitions: $phoenix_count/100 allocated (1-100)"
# List unique projects
local projects=$(jq -r '.phoenix_ports, .playwright_ports | to_entries[].value.project' "$GLOBAL_REGISTRY_FILE" 2>/dev/null | sort -u | tr '\n' ', ' | sed 's/,$//')
if [ -n "$projects" ]; then
echo " Projects: $projects"
else
echo " Projects: (none)"
fi
echo ""
echo "💡 Each partition provides: Phoenix (400X), Test (410X), Playwright (890X), Databases (_dev_X, _test_X)"
}
# Cleanup orphaned resources (where workspace directories no longer exist)
cleanup_orphaned_resources() {
ensure_global_registry
if ! acquire_lock; then
return 1
fi
local phoenix_cleaned=0
local playwright_cleaned=0
echo "🧹 Checking for orphaned resources..."
# Check Phoenix port allocations
while IFS=$'\t' read -r port project workspace; do
if [ -n "$port" ] && [ -n "$project" ] && [ -n "$workspace" ]; then
# Try to find a workspace directory for this allocation
# Look in common OCG workspace locations
local found_workspace=false
# Check current project if it matches
if [ -n "$TARGET_REPO_PATH" ]; then
local current_project=$(basename "$TARGET_REPO_PATH")
if [ "$project" = "$current_project" ]; then
if [ -d "$TARGET_REPO_PATH/codegen/workspaces/$workspace" ]; then
found_workspace=true
fi
fi
fi
# If workspace not found, mark for cleanup
if [ "$found_workspace" = false ]; then
echo " Removing orphaned Phoenix port $port for $project/$workspace"
jq "del(.phoenix_ports.\"$port\")" "$GLOBAL_REGISTRY_FILE" >"${GLOBAL_REGISTRY_FILE}.tmp" &&
mv "${GLOBAL_REGISTRY_FILE}.tmp" "$GLOBAL_REGISTRY_FILE"
((phoenix_cleaned++))
fi
fi
done < <(jq -r '.phoenix_ports | to_entries[] | "\(.key)\t\(.value.project)\t\(.value.workspace)"' "$GLOBAL_REGISTRY_FILE" 2>/dev/null)
# Check Playwright port allocations
while IFS=$'\t' read -r port project workspace; do
if [ -n "$port" ] && [ -n "$project" ] && [ -n "$workspace" ]; then
# Try to find a workspace directory for this allocation
local found_workspace=false
# Check current project if it matches
if [ -n "$TARGET_REPO_PATH" ]; then
local current_project=$(basename "$TARGET_REPO_PATH")
if [ "$project" = "$current_project" ]; then
if [ -d "$TARGET_REPO_PATH/codegen/workspaces/$workspace" ]; then
found_workspace=true
fi
fi
fi
# If workspace not found, mark for cleanup
if [ "$found_workspace" = false ]; then
echo " Removing orphaned Playwright port $port for $project/$workspace"
jq "del(.playwright_ports.\"$port\")" "$GLOBAL_REGISTRY_FILE" >"${GLOBAL_REGISTRY_FILE}.tmp" &&
mv "${GLOBAL_REGISTRY_FILE}.tmp" "$GLOBAL_REGISTRY_FILE"
((playwright_cleaned++))
fi
fi
done < <(jq -r '.playwright_ports | to_entries[] | "\(.key)\t\(.value.project)\t\(.value.workspace)"' "$GLOBAL_REGISTRY_FILE" 2>/dev/null)
if [ $phoenix_cleaned -gt 0 ] || [ $playwright_cleaned -gt 0 ]; then
update_timestamp
echo "✅ Cleaned up $phoenix_cleaned Phoenix port(s) and $playwright_cleaned Playwright port(s)"
else
echo "✅ No orphaned resources found"
fi
release_lock
return 0
}
# Fallback to local port scanning if global registry fails
fallback_get_next_phoenix_port() {
local base_port=4001
local current_port=$base_port
echo "⚠️ Using fallback port scanning..." >&2
while true; do
local port_in_use=false
# Check if port is in use by system
if lsof -i ":$current_port" >/dev/null 2>&1; then
port_in_use=true
fi
# Check local workspace directories if TARGET_REPO_PATH is set
if [ -n "$TARGET_REPO_PATH" ] && [ ! "$port_in_use" = true ]; then
for workspace_dir in "$TARGET_REPO_PATH"/codegen/workspaces/*; do
if [ -d "$workspace_dir" ] && [ -f "$workspace_dir/.env" ]; then
if grep -q "^PORT=$current_port" "$workspace_dir/.env" 2>/dev/null; then
port_in_use=true
break
fi
fi
done
fi
if [ ! "$port_in_use" = true ]; then
echo "$current_port"
return 0
fi
((current_port++))
if [ $current_port -gt 4100 ]; then
echo "❌ Error: Could not find available port (checked up to 4100)" >&2
return 1
fi
done
}