#!/usr/bin/env bash # Generate deploy/lxc/runtime/version.json (BIT-465). Core logic matches # paperclip tools/deploy/write_version_json.sh — keep behaviour in sync when changing. # # Typical: source /etc/error-reporting/env then run this script (no args). # Or pass explicitly: ER_REPO_ROOT, ER_VERSION_JSON_PATH, ER_VERSION_PROJECT. set -euo pipefail _ENV_FILE="/etc/error-reporting/env" if [[ -f "${_ENV_FILE}" ]]; then set -a # shellcheck disable=SC1090 source "${_ENV_FILE}" set +a fi _GIT_DIR="${ER_REPO_ROOT:-}" if [[ -z "${_GIT_DIR}" ]]; then _SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" _GIT_DIR="$(cd "${_SCRIPT_DIR}/../.." && pwd)" fi _OUTPUT="${ER_VERSION_JSON_PATH:-${_GIT_DIR}/deploy/lxc/runtime/version.json}" _PROJECT="${ER_VERSION_PROJECT:-errors.bitreport.pt}" if [[ ! -d "${_GIT_DIR}/.git" && ! -f "${_GIT_DIR}/.git" ]]; then echo "write-version-json: not a git repository: ${_GIT_DIR}" >&2 exit 1 fi _COMMIT="$(git -C "${_GIT_DIR}" rev-parse HEAD)" _BRANCH="$(git -C "${_GIT_DIR}" rev-parse --abbrev-ref HEAD)" _BUILT_BY="${ER_VERSION_BUILT_BY:-BitReport.pt}" _BUILT_AT="$(date -u +%Y-%m-%dT%H:%M:%SZ)" _OUT_DIR="$(dirname "${_OUTPUT}")" mkdir -p "${_OUT_DIR}" _TMP="${_OUTPUT}.tmp.$$" python3 - "${_TMP}" "${_PROJECT}" "${_COMMIT}" "${_BRANCH}" "${_BUILT_AT}" "${_BUILT_BY}" <<'PY' import json import sys path, project, commit, branch, built_at, built_by = sys.argv[1:7] doc = { "project": project, "commit": commit, "branch": branch, "builtAt": built_at, "builtBy": built_by, } with open(path, "w", encoding="utf-8") as f: json.dump(doc, f, indent=2, sort_keys=False) f.write("\n") PY python3 - "${_TMP}" <<'PY' import json import sys required = {"project", "commit", "branch", "builtAt", "builtBy"} path = sys.argv[1] with open(path, encoding="utf-8") as f: data = json.load(f) if not isinstance(data, dict): sys.exit("version.json: root must be an object") missing = required - data.keys() if missing: sys.exit("version.json: missing keys: " + ", ".join(sorted(missing))) for k in required: v = data[k] if not isinstance(v, str) or not v.strip(): sys.exit(f"version.json: invalid or empty string for {k!r}") PY mv -f "${_TMP}" "${_OUTPUT}" echo "write-version-json: wrote ${_OUTPUT} (commit ${_COMMIT:0:12})"