Repository navigation
Expand file tree
/
Copy pathrecreate_dynamic_resource_docs.sh
More file actions
executable file
·141 lines (131 loc) · 5.15 KB
/
Copy pathrecreate_dynamic_resource_docs.sh
File metadata and controls
executable file
·141 lines (131 loc) · 5.15 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
#!/usr/bin/env bash
#
# recreate_dynamic_resource_docs.sh
#
# Recreate the OGCR Dynamic Resource Docs (the DOCS in dynamic_resource_docs.py,
# e.g. registry_activities) from their .scala files. For each doc:
# 1. Compile it (dry run, nothing stored). If it doesn't compile (e.g. the
# instance has allow_user_generated_scala_code=false), skip it, so the doc
# already on OBP is left in place.
# 2. Check OBP has every entity a Query doc reads (check-entities); skip it,
# leaving it in place, if one is missing (e.g. activity_on_chain, which
# OGCR-chain-cache creates).
# 3. Delete the doc on OBP, if there is one.
# 4. Create it again.
# 5. Call it anonymously to check it is served and public. OBP caches its list
# of resource docs (40s by default), so a 404 is retried for up to 90s.
# A doc that fails a step does not stop the others; every failure is listed at
# the end and the script then exits 1.
#
# Usage:
# ./recreate_dynamic_resource_docs.sh [DOC ...] [--yes] [--dry-run]
#
# Without DOC names, every doc in dynamic_resource_docs.py is recreated.
#
# --dry-run only compiles each doc (OBP's compile endpoint stores nothing) and
# lists the docs on OBP, so you can see which would be deleted; it changes
# nothing and asks no question.
#
# It first shows the host it will work on and asks for confirmation; --yes skips
# the question (for automation). Without a terminal to ask on and without --yes,
# it stops.
#
# Notes:
# - Token/host come from obp_client.py (or your .env), same as the Python scripts.
# - The .scala reads its entities from SPACE_BANK_ID, which must match
# OBP_ENTITY_SPACE_ID; dynamic_resource_docs.py refuses to push it otherwise.
# - See dynamic_resource_docs.py for the OBP props and Roles this needs.
set -euo pipefail
# Run from the directory this script lives in, so relative paths resolve.
cd "$(dirname "$0")"
# Prefer the project virtualenv if it exists (has requests installed).
if [ -z "${PYTHON:-}" ] && [ -x ".venv/bin/python" ]; then
PYTHON=".venv/bin/python"
fi
PYTHON="${PYTHON:-python3}"
# Run on the OBP target: LOCAL (default), PROD or BOTH, from --target= or OBP_TARGET.
source ./target.sh
ASSUME_YES=false
DRY_RUN=false
DOC_NAMES=()
for arg in "$@"; do
case "$arg" in
--yes) ASSUME_YES=true ;;
--dry-run) DRY_RUN=true ;;
-*) echo "Unknown option: $arg" >&2; exit 2 ;;
*) DOC_NAMES+=("$arg") ;;
esac
done
if [ ${#DOC_NAMES[@]} -eq 0 ]; then
read -r -a DOC_NAMES <<< "$("$PYTHON" -c 'from dynamic_resource_docs import DOCS; print(*DOCS)')"
fi
TARGET_HOST="$("$PYTHON" -c 'from obp_client import obp_host; print(obp_host)')"
if [ "$DRY_RUN" = true ]; then
echo "[DRY RUN] Nothing will be changed on ${TARGET_HOST}"
echo "[DRY RUN] Would delete and recreate: ${DOC_NAMES[*]}"
echo "[DRY RUN] Docs on OBP now (any of the above listed here would be deleted first):"
"$PYTHON" dynamic_resource_docs.py list
COMPILE_FAILED=false
for doc in "${DOC_NAMES[@]}"; do
echo "[DRY RUN] Compiling ${doc}"
"$PYTHON" dynamic_resource_docs.py compile "$doc" || COMPILE_FAILED=true
"$PYTHON" dynamic_resource_docs.py check-entities "$doc" || COMPILE_FAILED=true
done
if [ "$COMPILE_FAILED" = true ]; then
echo "✗ Some docs don't compile or read a missing entity; a real run would skip them and leave them as they are." >&2
exit 1
fi
echo "[DRY RUN] Done. Nothing was changed."
exit 0
fi
echo "=================================================="
echo " About to DELETE and RECREATE the Dynamic Resource Docs"
echo " Host: ${TARGET_HOST}"
echo " Docs: ${DOC_NAMES[*]}"
echo "=================================================="
if [ "$ASSUME_YES" != true ]; then
if [ ! -t 0 ]; then
echo "No terminal to confirm on; re-run with --yes to proceed." >&2
exit 1
fi
read -r -p "Type 'yes' to continue: " ANSWER
if [ "$ANSWER" != "yes" ]; then
echo "Aborted. Nothing was changed."
exit 1
fi
fi
FAILED=()
RECREATED=()
for doc in "${DOC_NAMES[@]}"; do
echo
echo "=================================================="
echo " ${doc}"
echo "=================================================="
# A doc that fails is reported at the end; the other docs are still recreated.
if ! "$PYTHON" dynamic_resource_docs.py compile "$doc"; then
FAILED+=("${doc}: compile failed, left as it is on OBP")
continue
fi
if ! "$PYTHON" dynamic_resource_docs.py check-entities "$doc"; then
FAILED+=("${doc}: check-entities failed (an entity it reads is missing), left as it is on OBP")
continue
fi
if ! "$PYTHON" dynamic_resource_docs.py delete "$doc"; then
FAILED+=("${doc}: delete failed")
continue
fi
if ! "$PYTHON" dynamic_resource_docs.py create "$doc"; then
FAILED+=("${doc}: create failed, the doc is no longer on OBP")
continue
fi
RECREATED+=("$doc")
# e.g. the create is waiting for approval, or OBP's cache has not caught up
"$PYTHON" dynamic_resource_docs.py verify "$doc" --wait 90 || FAILED+=("${doc}: verify failed")
done
echo
echo "Done. Dynamic Resource Docs recreated: ${RECREATED[*]:-(none)}"
if [ ${#FAILED[@]} -gt 0 ]; then
echo "✗ ${#FAILED[@]} doc(s) need attention:" >&2
printf ' ✗ %s\n' "${FAILED[@]}" >&2
exit 1
fi