|
1 | 1 | #!/usr/bin/env bash |
2 | 2 | set -euo pipefail |
3 | 3 |
|
4 | | -# ====== WIKI SYNC SECTION ====== |
5 | | -FORCE_UPDATE=0 |
6 | | -REFRESH_SECONDS=${DOCS_REFRESH_SECONDS:-3600} # default 1 hour |
7 | | -# Allow skipping wiki sync entirely (useful in restricted-network envs) |
8 | | -SKIP_WIKI=${DATAVIZHUB_SKIP_WIKI_SYNC:-0} |
9 | | -# Hard timeout for git clone so startup never blocks indefinitely |
10 | | -GIT_CLONE_TIMEOUT_SECONDS=${GIT_TIMEOUT_SECONDS:-20} |
11 | | - |
12 | | -while [[ ${1:-} ]]; do |
13 | | - case "$1" in |
14 | | - --force) FORCE_UPDATE=1; shift ;; |
15 | | - --refresh-seconds) REFRESH_SECONDS="$2"; shift 2 ;; |
16 | | - *) echo "[entrypoint] Unknown option: "$1"" >&2; exit 2 ;; |
17 | | - esac |
18 | | -done |
19 | | - |
20 | 4 | # Copy default .env if missing |
21 | 5 | if [[ ! -f .env && -f .devcontainer/.env ]]; then |
22 | 6 | echo "[entrypoint] Seeding .env from .devcontainer/.env" |
23 | 7 | cp .devcontainer/.env .env |
24 | 8 | fi |
25 | 9 |
|
26 | | -WIKI_URL="https://github.com/NOAA-GSL/datavizhub.wiki.git" |
27 | | -DOCS_DIR="wiki" |
28 | | -META_FILE="$DOCS_DIR/.mirror_meta" |
29 | | - |
30 | | -# Fix stale, non-writable wiki dirs |
31 | | -if [[ -d "$DOCS_DIR" && ! -w "$DOCS_DIR" ]]; then |
32 | | - echo "[entrypoint] Existing $DOCS_DIR not writable — removing" |
33 | | - rm -rf "$DOCS_DIR" || true |
34 | | -fi |
35 | | - |
36 | | -now_epoch() { date +%s; } |
37 | | -should_update=0 |
38 | | - |
39 | | -if [[ ! -d "$DOCS_DIR" ]]; then |
40 | | - should_update=1 |
41 | | -elif [[ $FORCE_UPDATE -eq 1 ]]; then |
42 | | - should_update=1 |
43 | | -else |
44 | | - last_sync=0 |
45 | | - if [[ -f "$META_FILE" ]]; then |
46 | | - # Avoid sourcing untrusted content; parse the expected key instead |
47 | | - last_sync=$(grep -E '^last_sync_epoch=' "$META_FILE" | tail -n1 | cut -d'=' -f2 | tr -d '"' || true) |
48 | | - last_sync=${last_sync:-0} |
49 | | - fi |
50 | | - now=$(now_epoch) |
51 | | - age=$(( now - last_sync )) |
52 | | - if (( age >= REFRESH_SECONDS )); then |
53 | | - should_update=1 |
54 | | - fi |
55 | | -fi |
56 | | - |
57 | | -if [[ "$SKIP_WIKI" == "1" ]]; then |
58 | | - echo "[entrypoint] Skipping wiki sync (DATAVIZHUB_SKIP_WIKI_SYNC=1)" |
59 | | -else |
60 | | - if [[ $should_update -eq 1 ]]; then |
61 | | - echo "[entrypoint] Cloning wiki (timeout ${GIT_CLONE_TIMEOUT_SECONDS}s)..." |
62 | | - tmp_dir="${DOCS_DIR}.new" |
63 | | - rm -rf "$tmp_dir" |
64 | | - CLONE_CMD=(git clone --depth=1 "$WIKI_URL" "$tmp_dir") |
65 | | - if command -v timeout >/dev/null 2>&1; then |
66 | | - if timeout "${GIT_CLONE_TIMEOUT_SECONDS}s" "${CLONE_CMD[@]}"; then |
67 | | - clone_ok=1 |
68 | | - else |
69 | | - clone_ok=0 |
70 | | - fi |
71 | | - else |
72 | | - # Fallback: run clone in background and wait up to N seconds |
73 | | - set +e |
74 | | - "${CLONE_CMD[@]}" & |
75 | | - pid=$! |
76 | | - waited=0 |
77 | | - while kill -0 "$pid" >/dev/null 2>&1; do |
78 | | - if [[ $waited -ge $GIT_CLONE_TIMEOUT_SECONDS ]]; then |
79 | | - echo "[entrypoint] WARN: git clone exceeded ${GIT_CLONE_TIMEOUT_SECONDS}s; continuing without wiki" |
80 | | - kill "$pid" >/dev/null 2>&1 || true |
81 | | - clone_ok=0 |
82 | | - break |
83 | | - fi |
84 | | - sleep 1 |
85 | | - waited=$(( waited + 1 )) |
86 | | - done |
87 | | - if [[ ${clone_ok:-1} -ne 0 && ! -d "$tmp_dir" ]]; then |
88 | | - # If the process finished but dir not present, treat as failure |
89 | | - clone_ok=0 |
90 | | - fi |
91 | | - set -e |
92 | | - fi |
93 | | - if [[ ${clone_ok:-0} -eq 1 ]]; then |
94 | | - rm -rf "$tmp_dir/.git" |
95 | | - echo "source_url=\"$WIKI_URL\"" > "$tmp_dir/.mirror_meta" |
96 | | - echo "last_sync_epoch=$(now_epoch)" >> "$tmp_dir/.mirror_meta" |
97 | | - rm -rf "$DOCS_DIR" || true |
98 | | - mv "$tmp_dir" "$DOCS_DIR" || cp -R "$tmp_dir"/. "$DOCS_DIR" |
99 | | - rm -rf "$tmp_dir" |
100 | | - echo "[entrypoint] Wiki synced" |
101 | | - else |
102 | | - echo "[entrypoint] WARN: Wiki clone skipped/failed; proceeding without update" |
103 | | - rm -rf "$tmp_dir" || true |
104 | | - fi |
105 | | - else |
106 | | - echo "[entrypoint] Wiki is fresh; skipping sync" |
107 | | - fi |
108 | | -fi |
109 | | - |
110 | 10 | # ====== SERVICE START SECTION ====== |
111 | 11 | AUTOSTART_API=${DATAVIZHUB_AUTOSTART_API:-1} |
112 | 12 | AUTOSTART_RQ=${DATAVIZHUB_AUTOSTART_RQ:-${DATAVIZHUB_USE_REDIS:-0}} |
|
0 commit comments