Skip to content

Instantly share code, notes, and snippets.

@noahgcook
Created September 24, 2026 04:22
Show Gist options
  • Select an option

  • Save noahgcook/6a55a32a090a133c10b4aad1ff98a0d1 to your computer and use it in GitHub Desktop.

Select an option

Save noahgcook/6a55a32a090a133c10b4aad1ff98a0d1 to your computer and use it in GitHub Desktop.
#!/usr/bin/env bash
# Environment: ORION_URL, ORION_USER, ORION_PASSWORD.
# Optional: PROJECT_IDS (space-separated project IDs).
# Needs Bash, curl, jq, git, and a tenant Admin with an Orion password.
set -euo pipefail
umask 077
: "${ORION_URL:?Set ORION_URL}" "${ORION_USER:?Set ORION_USER}" "${ORION_PASSWORD:?Set ORION_PASSWORD}"
ORION_URL=${ORION_URL%/}
repo=$(git rev-parse --show-toplevel)
cd "$repo"
if [ -n "$(git status --porcelain --untracked-files=all)" ]; then
echo "Use a clean checkout; commit the script and workflow first." >&2
exit 1
fi
out=orion-snapshots
if [ -L "$out" ] || { [ -e "$out" ] && ! git ls-files --error-unmatch "$out/.orion-export" >/dev/null 2>&1; }; then
echo "Refusing to replace an unmanaged export directory." >&2
exit 1
fi
work=$(mktemp -d)
trap 'rm -rf "$work"' EXIT
stage="$work/snapshot"
mkdir -p "$stage"/{projects,governance,knowledge-base,catalog-metrics,access/group-members}
printf 'Generated by scripts/orion-export.sh\n' > "$stage/.orion-export"
# Reject redirects as well as HTTP errors. Save responses outside Git until checked.
fetch() {
local path=$1 file=$2 status
shift 2
status=$(curl -fsS --connect-timeout 20 --max-time 120 \
-o "$file" -w '%{http_code}' "$@" "$ORION_URL$path")
if [ "$status" != 200 ]; then
echo "Unexpected HTTP $status for $path" >&2
return 1
fi
}
fetch /api/auth/login "$work/login.json" -X POST \
--data-urlencode "username=$ORION_USER" --data-urlencode "password=$ORION_PASSWORD"
TOKEN=$(jq -er '.access_token | select(type == "string" and length > 0)' "$work/login.json")
api() { fetch "$1" "$2" -H "Authorization: Bearer $TOKEN"; }
# IDs are filenames: reject unsafe values and duplicate IDs instead of overwriting.
ids() {
jq -er '
if type != "array" then error("Expected an array")
elif any(.[]; (.id | type) != "string") then error("Expected string IDs")
elif any(.[]; (.id | test("^[A-Za-z0-9_-]+$") | not)) then error("Unsafe ID")
elif (map(.id) | length) != (map(.id) | unique | length) then error("Duplicate IDs")
else map(.id) | sort | join("\n") end' "$1"
}
# 1. Project definitions and governance rules.
if [ -z "${PROJECT_IDS:-}" ]; then
api /api/projects/ "$work/projects.json"
PROJECT_IDS=$(ids "$work/projects.json")
fi
for id in $PROJECT_IDS; do
[[ "$id" =~ ^[A-Za-z0-9_-]+$ ]] || { echo "Invalid project ID" >&2; exit 1; }
api "/api/projects/$id/dsl-export/" "$stage/projects/$id.dsl.toml"
test -s "$stage/projects/$id.dsl.toml"
api "/api/projects/$id/governance/rules/" "$work/rules.json"
jq -e 'type == "array"' "$work/rules.json" >/dev/null
jq -S 'sort_by(.name, .id)' "$work/rules.json" > "$stage/governance/$id.json"
done
# 2. Knowledge Base document content; page settings are not included here.
api '/api/wiki/?page_type=document' "$work/pages.json"
page_ids=$(ids "$work/pages.json")
for id in $page_ids; do
api "/api/wiki/$id" "$work/page.json"
jq -e '(.title | type) == "string" and (.path | type) == "string" and
(.content == null or (.content | type) == "string")' "$work/page.json" >/dev/null
jq -r '"---\ntitle: \(.title | @json)\npath: \(.path | @json)\n---\n\n\(.content // "")"' \
"$work/page.json" > "$stage/knowledge-base/$id.md"
done
# 3. Catalog definitions. Fetch every page, including catalogs larger than 1,000.
offset=0
while :; do
api "/api/catalog-metrics/?limit=100&offset=$offset" "$work/catalog.json"
jq -e '(.items | type) == "array" and (.total | type) == "number" and
.total >= 0 and .total == (.total | floor)' "$work/catalog.json" >/dev/null
jq '.items' "$work/catalog.json" > "$work/items.json"
metric_ids=$(ids "$work/items.json")
count=$(jq '.items | length' "$work/catalog.json")
total=$(jq '.total' "$work/catalog.json")
for id in $metric_ids; do
if [ -e "$stage/catalog-metrics/$id.toml" ]; then
echo "Catalog changed during pagination; rerun the export." >&2
exit 1
fi
api "/api/catalog-metrics/$id" "$work/metric.json"
jq -er '.toml | select(type == "string" and length > 0)' \
"$work/metric.json" > "$stage/catalog-metrics/$id.toml"
done
offset=$((offset + count))
[ "$offset" -lt "$total" ] || break
[ "$count" -gt 0 ] || { echo "Incomplete catalog response" >&2; exit 1; }
done
# 4. Company name, context, and profile.
api /api/company/ "$work/company.json"
jq -e '(.name | type) == "string"' "$work/company.json" >/dev/null
jq -r '"# \(.name)\n\n## Context\n\n\(.context // "")\n\n## Profile\n\n\(.profile // "")"' \
"$work/company.json" > "$stage/company.md"
# 5. Data source inventory: an explicit field selection omits connection details.
api /api/datasource/ "$work/datasources.json"
jq -e 'type == "array"' "$work/datasources.json" >/dev/null
jq -S '[.[] | {id, name, type, description, active}] | sort_by(.name, .id)' \
"$work/datasources.json" > "$stage/datasources.json"
# 6. User roles, groups, and memberships (not every access-control setting).
api /api/user/all/ "$work/users.json"
jq -e 'type == "array"' "$work/users.json" >/dev/null
jq -S '[.[] | {id, email, first_name, last_name, role, is_active}] | sort_by(.email, .id)' \
"$work/users.json" > "$stage/access/users.json"
api /api/groups/ "$work/groups.json"
group_ids=$(ids "$work/groups.json")
jq -S '[.[] | {id, name, description, project_ids: (.project_ids | sort)}] | sort_by(.name, .id)' \
"$work/groups.json" > "$stage/access/groups.json"
for id in $group_ids; do
api "/api/groups/$id/members" "$work/members.json"
jq -e 'type == "array"' "$work/members.json" >/dev/null
jq -S '[.[] | {user_id, group_role}] | sort_by(.user_id)' \
"$work/members.json" > "$stage/access/group-members/$id.json"
done
# Replace only this job's generated directory, after the export has succeeded.
rm -rf -- "$out"
mv "$stage" "$out"
git add -A -- "$out"
if git diff --cached --quiet; then
echo "No exported differences"
exit 0
fi
git commit -m "Orion snapshot $(date -u +%Y-%m-%d)"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment