-
Notifications
You must be signed in to change notification settings - Fork 381
Expand file tree
/
Copy pathrockydocs.sh
More file actions
executable file
·460 lines (403 loc) · 14.2 KB
/
rockydocs.sh
File metadata and controls
executable file
·460 lines (403 loc) · 14.2 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
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
#!/bin/bash
set -e
# Rocky Linux Documentation - Master Contributor Script
# Production-ready script with NEVRA versioning and modular architecture
#
# Author: Wale Soyinka
# Changelog: tools/CHANGELOG.md
#
VERSION="1.0.0"
RELEASE="13.el10"
AUTHOR="Wale Soyinka"
FULL_VERSION="rockydocs-${VERSION}-${RELEASE}"
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
CONTENT_DIR="$SCRIPT_DIR"
REPO_NAME="$(basename "$CONTENT_DIR")"
INITIAL_PWD="$(pwd)" # Preserve the directory where script was invoked
# Configuration management
CONFIG_DIR="${XDG_CONFIG_HOME:-$HOME/.config}/rockydocs"
CONFIG_FILE="$CONFIG_DIR/config"
# Source function library
source "$(dirname "${BASH_SOURCE[0]}")/tools/rockydocs-functions.sh"
# Load saved workspace configuration
load_workspace_config
# Initialize workspace directories
WORKSPACE_BASE_DIR="${SAVED_WORKSPACE_BASE_DIR:-$(dirname "$CONTENT_DIR")/rockydocs-workspaces}"
APP_DIR="$WORKSPACE_BASE_DIR/docs.rockylinux.org"
APP_COMPAT_LINK="$WORKSPACE_BASE_DIR/app"
# Dependency check (NEW v12)
check_dependencies() {
local missing_deps=()
local required_commands=("git" "python3" "lsof" "pgrep" "jq" "curl")
for cmd in "${required_commands[@]}"; do
if ! command -v "$cmd" >/dev/null 2>&1; then
missing_deps+=("$cmd")
fi
done
if [ ${#missing_deps[@]} -gt 0 ]; then
print_error "Missing required dependencies: ${missing_deps[*]}"
print_info "Please install the missing dependencies and try again."
exit 1
fi
}
# Consolidated git operations (NEW v12)
update_repositories() {
print_info "Updating all repositories with consolidated git operations..."
# Update app repository if it exists
if [ -d "$APP_DIR/.git" ]; then
print_info "Updating app repository..."
cd "$APP_DIR"
run_cmd "git fetch origin main" || print_warning "Failed to fetch app repository updates"
run_cmd "git pull origin main" || print_warning "Failed to pull app repository updates"
fi
# Update cached worktrees if they exist
local worktree_base="$APP_DIR/worktrees"
if [ -d "$worktree_base/main/.git" ]; then
print_info "Updating cached content repositories..."
cd "$worktree_base/main"
run_cmd "git fetch origin rocky-8:rocky-8 rocky-9:rocky-9 main:main" || print_warning "Failed to update cached repositories"
fi
cd "$CONTENT_DIR"
print_success "Repository updates completed"
}
# Optimized web root override (NEW v12)
apply_web_root_override_local() {
print_info "Applying optimized local web root override..."
local original_branch
original_branch=$(git rev-parse --abbrev-ref HEAD)
# Use low-level git commands to avoid expensive checkouts
if ! git show-ref --verify --quiet refs/heads/gh-pages; then
print_error "No gh-pages branch found for web root override"
return 1
fi
# Find the source for web root content (latest symlink or version 10 directory)
local content_source=""
if git ls-tree gh-pages | grep -q "latest"; then
# Check if latest is a symlink and resolve it
local latest_target=$(git show gh-pages:latest 2>/dev/null || echo "")
if [ -n "$latest_target" ] && git ls-tree "gh-pages:$latest_target" >/dev/null 2>&1; then
content_source="$latest_target"
print_info "Found latest symlink pointing to version $latest_target"
elif git ls-tree gh-pages:latest >/dev/null 2>&1; then
content_source="latest"
print_info "Found latest directory"
fi
fi
# Fallback to version 10 if latest not found
if [ -z "$content_source" ] && git ls-tree gh-pages:10 >/dev/null 2>&1; then
content_source="10"
print_info "Using version 10 as content source"
fi
if [ -z "$content_source" ]; then
print_error "No suitable content source found (latest or version 10) on gh-pages branch"
return 1
fi
print_info "Creating optimized commit for web root override using content from: $content_source"
# Create a merged tree that preserves versioned directories AND adds root content
local parent_commit=$(git rev-parse gh-pages)
local parent_tree=$(git rev-parse "gh-pages^{tree}")
local source_tree=$(git rev-parse "gh-pages:$content_source")
# Use a temporary worktree approach to create merged tree
# This preserves existing versioned structure while adding root content
local temp_worktree=".git/temp-merge-$(date +%s)"
run_cmd "git worktree add $temp_worktree gh-pages"
# In the temporary worktree, copy the latest content to root, preserving versioned dirs
(
cd "$temp_worktree"
# Remove existing root content (but keep versioned directories and versions.json)
for item in *; do
if [[ "$item" != "8" && "$item" != "9" && "$item" != "10" && "$item" != "latest" && "$item" != "versions.json" && "$item" != ".nojekyll" ]]; then
rm -rf "$item"
fi
done
# Copy the latest version content to root
if [ -L latest ]; then
local target=$(readlink latest)
cp -r "${target}"/* ./
elif [ -d "10" ]; then
cp -r 10/* ./
fi
# Stage all changes
git add -A
local merged_tree=$(git write-tree)
echo "$merged_tree" > /tmp/merged_tree_sha
)
# Clean up the temporary worktree (force removal since it contains our changes)
run_cmd "git worktree remove --force $temp_worktree"
local merged_tree=$(cat /tmp/merged_tree_sha)
rm -f /tmp/merged_tree_sha
# Create new commit with the merged tree
local new_commit=$(git commit-tree $merged_tree -p $parent_commit -m "feat: Apply optimized local-only web root override from $content_source")
# Update gh-pages branch to point to new commit
run_cmd "git update-ref refs/heads/gh-pages $new_commit"
print_success "Optimized web root override applied successfully from $content_source"
}
# Streamlined static serving (NEW v12)
serve_static() {
print_info "🔧 STREAMLINED STATIC MODE: Fast static file extraction and serving"
# Basic validation
if [ ! -d "$APP_DIR" ]; then
print_error "App directory not found: $APP_DIR"
return 1
fi
cd "$APP_DIR"
# Only extract static site if not already done or if deployment is newer
if ! extract_static_site; then
print_error "Failed to extract static site"
return 1
fi
# Start static server immediately
print_success "🚀 Starting STREAMLINED static server"
print_info " • Instant startup - no rebuild needed"
print_info " • Production-identical behavior"
print_info " • Access: http://localhost:8000"
# Check for port conflicts and resolve them
local port=8000
if ! check_and_resolve_port_conflict $port true; then
print_error "Could not resolve port conflict on port $port"
return 1
fi
# Start static server with PID tracking
cd "$APP_DIR/site-static"
python3 -m http.server 8000 &
local server_pid=$!
add_cleanup_resource "pid:$server_pid"
cd - > /dev/null
wait $server_pid
}
# Docker serving function
serve_docker() {
local static_mode="$1"
print_info "🐳 DOCKER SERVING MODE: Container-based documentation serving"
# Validate Docker setup
if [ ! -d "$APP_DIR" ]; then
print_error "App directory not found: $APP_DIR"
print_info "Run setup first: $0 --setup --docker"
return 1
fi
# Check if Docker image exists
if ! docker image inspect rockydocs-dev >/dev/null 2>&1; then
print_error "Docker image 'rockydocs-dev' not found"
print_info "Run setup first: $0 --setup --docker"
return 1
fi
cd "$APP_DIR"
# Get container name and manage lifecycle
local container_name=$(get_docker_container_name "serve")
stop_docker_container "$container_name"
# Find available port
local port=$(find_available_port 8000 8001 8002)
if [ -z "$port" ]; then
print_error "No available ports (8000, 8001, 8002). Kill existing processes."
return 1
fi
print_success "🚀 Starting Docker container: $container_name"
print_info " • Port: $port"
print_info " • Access: http://localhost:$port"
print_info " • Static mode: $static_mode"
# Start container based on mode
if [ "$static_mode" = "true" ]; then
# Static mode: serve pre-built content
print_info " • Mode: Static (production-like)"
docker run -d --name "$container_name" \
-p "$port:8000" \
-v "$APP_DIR:/app" \
-v "$CONTENT_DIR/docs:/app/content" \
--workdir /app \
rockydocs-dev \
python3 -m http.server 8000 -d /app/site-static
else
# Live mode: MkDocs development server with proper environment
print_info " • Mode: Live (development with auto-reload)"
docker run -d --name "$container_name" \
-p "$port:8000" \
-v "$APP_DIR:/app" \
-v "$CONTENT_DIR/docs:/app/content" \
--workdir /app \
rockydocs-dev \
bash -c "source venv/bin/activate && mkdocs serve -a 0.0.0.0:8000"
fi
# Add container to cleanup
add_cleanup_resource "container:$container_name"
# Health check with longer timeout for mkdocs build
print_info "Waiting for container to be ready (mkdocs build can take 1-2 minutes)..."
if check_docker_health "$container_name" "$port" 120; then
print_success "✅ Docker container ready!"
print_info "Access your documentation at: http://localhost:$port"
# Show container logs briefly
print_info ""
print_info "Container logs (last 10 lines):"
docker logs --tail 10 "$container_name" 2>/dev/null || true
print_info ""
print_warning "Press Ctrl+C to stop the container"
# Follow container logs
docker logs -f "$container_name"
else
print_error "Container failed to start properly"
docker logs "$container_name" 2>/dev/null || true
stop_docker_container "$container_name"
return 1
fi
}
# Parse arguments
COMMAND=""
ENVIRONMENT="venv"
BUILD_TYPE="minimal"
STATIC_MODE=false
LIST_MODE=false
CUSTOM_WORKSPACE=""
while [[ $# -gt 0 ]]; do
case $1 in
--setup)
COMMAND="setup"
shift
;;
--serve)
COMMAND="serve"
shift
;;
--serve-dual)
COMMAND="serve-dual"
shift
;;
--deploy)
COMMAND="deploy"
shift
;;
--clean)
COMMAND="clean"
shift
;;
--reset)
COMMAND="reset"
shift
;;
--status)
COMMAND="status"
shift
;;
--version)
COMMAND="version"
shift
;;
--venv|--docker|--podman)
ENVIRONMENT="${1#--}"
shift
;;
--minimal)
BUILD_TYPE="minimal"
shift
;;
--full)
BUILD_TYPE="full"
shift
;;
--static)
STATIC_MODE=true
shift
;;
--list)
LIST_MODE=true
shift
;;
--workspace)
if [ -n "$2" ]; then
CUSTOM_WORKSPACE="$2"
# Create directory if it doesn't exist, then get realpath
mkdir -p "$2"
WORKSPACE_BASE_DIR="$(realpath "$2")"
APP_DIR="$WORKSPACE_BASE_DIR/docs.rockylinux.org"
APP_COMPAT_LINK="$WORKSPACE_BASE_DIR/app"
print_info "Using custom workspace: $WORKSPACE_BASE_DIR"
# Save new workspace configuration
print_info "Saving new workspace configuration..."
save_workspace_config "$WORKSPACE_BASE_DIR"
shift 2
else
print_error "--workspace requires a path"
exit 1
fi
;;
-h|--help)
if [ -n "$COMMAND" ]; then
case "$COMMAND" in
setup) show_setup_help ;;
serve) show_serve_help ;;
serve-dual) show_serve_dual_help ;;
deploy) show_deploy_help ;;
*) show_help ;;
esac
else
show_help
fi
exit 0
;;
*)
print_error "Unknown option: $1"
show_help
exit 1
;;
esac
done
# Handle subcommand help
if [[ "$2" == "-h" ]]; then
case "$1" in
--setup) show_setup_help ;;
--serve) show_serve_help ;;
--serve-dual) show_serve_dual_help ;;
--deploy) show_deploy_help ;;
*) show_help ;;
esac
exit 0
fi
# Check dependencies before any operation
check_dependencies
# Execute command
case "$COMMAND" in
setup)
update_repositories
setup_environment "$ENVIRONMENT" "$BUILD_TYPE"
;;
serve)
if [ "$ENVIRONMENT" = "docker" ]; then
serve_docker "$STATIC_MODE"
elif [ "$STATIC_MODE" = "true" ]; then
serve_static
else
serve_site "" "$STATIC_MODE"
fi
;;
serve-dual)
serve_dual
;;
deploy)
if [ "$LIST_MODE" = "true" ]; then
list_versions
elif [ "$ENVIRONMENT" = "docker" ]; then
update_repositories
deploy_docker
else
update_repositories
deploy_site
fi
;;
clean)
clean_workspace
;;
reset)
reset_configuration
;;
status)
show_status
;;
version)
echo "$FULL_VERSION"
echo "Author: $AUTHOR"
echo "Rocky Linux Documentation Script"
;;
*)
print_error "No command specified"
show_help
exit 1
;;
esac