#!/usr/bin/env bash set -euo pipefail case "${1:---help}" in --help|-h) echo 'Usage: start-local.sh --bootstrap' echo 'Initialize the full DataGov environment. Existing service updates use deploy.sh.' exit 0 ;; --bootstrap) [[ $# -eq 1 ]] || { echo 'Unexpected bootstrap arguments' >&2; exit 2; } ;; *) echo 'Use deploy.sh for a service update or --bootstrap for full initialization.' >&2; exit 2 ;; esac readonly SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" readonly PROJECT_ROOT="$(cd "${SCRIPT_DIR}/../.." && pwd)" readonly COMPOSE_FILE="${SCRIPT_DIR}/docker-compose.local.yml" readonly ENV_FILE="${AIXUN_DATAGOV_ENV_FILE:-${SCRIPT_DIR}/.env.datagov}" readonly PROJECT_NAME="aixun-datagov-local" readonly RUNTIME_DIR="${SCRIPT_DIR}/runtime" readonly NACOS_DIR="${PROJECT_ROOT}/script/config/datagov/nacos" if [[ "${AIXUN_DATAGOV_RUNTIME_LOCKED:-0}" != "1" ]]; then exec python3 "${SCRIPT_DIR}/with-runtime-lock.py" "$0" --bootstrap fi "${SCRIPT_DIR}/preflight.sh" set -a # shellcheck disable=SC1090 source "${ENV_FILE}" set +a readonly NACOS_API_URL="http://127.0.0.1:${AIXUN_DATAGOV_NACOS_CLIENT_PORT}/nacos" readonly NACOS_CONSOLE_URL="http://127.0.0.1:${AIXUN_DATAGOV_NACOS_CONSOLE_PORT}" compose() { docker compose --env-file "${ENV_FILE}" -p "${PROJECT_NAME}" -f "${COMPOSE_FILE}" "$@" } wait_for_health() { local container_name="$1" local attempts="$2" local status for ((attempt = 1; attempt <= attempts; attempt++)); do status="$(docker container inspect "${container_name}" --format '{{if .State.Health}}{{.State.Health.Status}}{{else}}{{.State.Status}}{{end}}' 2>/dev/null || true)" if [[ "${status}" == "healthy" ]]; then return fi sleep 2 done echo "${container_name} did not become healthy; inspect its DataGov-only logs." >&2 return 1 } wait_for_http() { local url="$1" local attempts="$2" local code for ((attempt = 1; attempt <= attempts; attempt++)); do code="$(curl -sS --max-time 3 -o /dev/null -w '%{http_code}' "${url}" 2>/dev/null || true)" case "${code}" in 200|401|403) return ;; esac sleep 2 done echo "HTTP endpoint did not become ready: ${url}" >&2 return 1 } initialize_nacos() { local login_response local access_token local response local namespace_response local namespace_exists curl -fsS -X POST "${NACOS_API_URL}/v3/auth/user/admin" \ --data-urlencode "password=${AIXUN_DATAGOV_NACOS_PASSWORD}" >/dev/null 2>&1 || true login_response="$(curl -fsS -X POST "${NACOS_API_URL}/v3/auth/user/login" \ --data-urlencode "username=${AIXUN_DATAGOV_NACOS_USERNAME}" \ --data-urlencode "password=${AIXUN_DATAGOV_NACOS_PASSWORD}")" access_token="$(printf '%s' "${login_response}" | sed -n 's/.*"accessToken"[[:space:]]*:[[:space:]]*"\([^"]*\)".*/\1/p')" [[ -n "${access_token}" ]] || { echo "Nacos authentication failed; no secret was printed." >&2 return 1 } namespace_exists="$(curl -fsS -G "${NACOS_CONSOLE_URL}/v3/console/core/namespace/exist" \ -H "Authorization: Bearer ${access_token}" \ --data-urlencode "customNamespaceId=${AIXUN_DATAGOV_NACOS_NAMESPACE}")" if ! printf '%s' "${namespace_exists}" | rg -q '"code"[[:space:]]*:[[:space:]]*0'; then echo "Nacos namespace existence check failed." >&2 return 1 fi if ! printf '%s' "${namespace_exists}" | rg -q '"data"[[:space:]]*:[[:space:]]*true'; then namespace_response="$(curl -fsS -X POST "${NACOS_CONSOLE_URL}/v3/console/core/namespace" \ -H "Authorization: Bearer ${access_token}" \ --data-urlencode "customNamespaceId=${AIXUN_DATAGOV_NACOS_NAMESPACE}" \ --data-urlencode "namespaceName=${AIXUN_DATAGOV_NACOS_NAMESPACE}" \ --data-urlencode "namespaceDesc=AiXun-DataGov local namespace")" printf '%s' "${namespace_response}" | rg -q '"code"[[:space:]]*:[[:space:]]*0.*"data"[[:space:]]*:[[:space:]]*true' || { echo "Nacos namespace creation failed." >&2 return 1 } fi local config_files=( application-common.yml datasource.yml aixun-datagov-auth.yml aixun-datagov-gateway.yml aixun-datagov-resource.yml aixun-datagov-system.yml aixun-integration.yml aixun-dataops.yml aixun-governance.yml aixun-dataproduct.yml ) for config_file in "${config_files[@]}"; do response="$(curl -fsS -X POST "${NACOS_API_URL}/v3/admin/cs/config" \ -H "Authorization: Bearer ${access_token}" \ --data-urlencode "dataId=${config_file}" \ --data-urlencode "groupName=${AIXUN_DATAGOV_NACOS_GROUP}" \ --data-urlencode "namespaceId=${AIXUN_DATAGOV_NACOS_NAMESPACE}" \ --data-urlencode "type=yaml" \ --data-urlencode "content@${NACOS_DIR}/${config_file}")" if ! printf '%s' "${response}" | rg -q '"code"[[:space:]]*:[[:space:]]*0|"data"[[:space:]]*:[[:space:]]*true'; then echo "Nacos config import failed for ${config_file}; response was suppressed." >&2 return 1 fi done unset access_token login_response namespace_exists namespace_response response } mkdir -p "${RUNTIME_DIR}" "${SCRIPT_DIR}/snapshot-other-projects.sh" >"${RUNTIME_DIR}/before-other-projects.txt" echo "[1/4] Verifying explicitly installed platform schema and seed" if [[ -n "${DATAGOV_PLATFORM_DB_CONFIG:-}" ]]; then python3 "${PROJECT_ROOT}/script/database/verify-platform-db.py" else python3 "${PROJECT_ROOT}/script/database/apply-dameng-schema.py" --config "${DATAGOV_DM_CONFIG}" --verify python3 "${PROJECT_ROOT}/script/database/apply-dameng-seed.py" --config "${DATAGOV_DM_CONFIG}" --verify fi compose up -d redis nacos wait_for_health aixun-datagov-redis 60 wait_for_health aixun-datagov-nacos 90 echo "[2/4] Verifying the isolated schema and importing DataGov Nacos configuration" initialize_nacos if [[ "${AIXUN_DATAGOV_INFRA_ONLY:-0}" == "1" ]]; then echo "[3/4] Application profile intentionally skipped for the M0-009 infrastructure check" else readonly REQUIRED_ARTIFACTS=( "${PROJECT_ROOT}/aixun-gateway/target/aixun-gateway.jar" "${PROJECT_ROOT}/aixun-auth/target/aixun-auth.jar" "${PROJECT_ROOT}/aixun-modules/aixun-system/target/aixun-system.jar" "${PROJECT_ROOT}/aixun-modules/aixun-resource/target/aixun-resource.jar" "${PROJECT_ROOT}/aixun-modules/aixun-integration/target/aixun-integration.jar" "${PROJECT_ROOT}/aixun-modules/aixun-dataops/target/aixun-dataops.jar" "${PROJECT_ROOT}/aixun-modules/aixun-governance/target/aixun-governance.jar" "${PROJECT_ROOT}/aixun-modules/aixun-dataproduct/target/aixun-dataproduct.jar" "${PROJECT_ROOT}/aixun-ui/dist/index.html" ) for artifact in "${REQUIRED_ARTIFACTS[@]}"; do [[ -f "${artifact}" ]] || { echo "Missing build artifact: ${artifact}. Build this application with check.sh first." >&2 exit 1 } done echo "[3/4] Building and starting the approved current DataGov application profile" compose --profile app up -d --build wait_for_http "http://127.0.0.1:${AIXUN_DATAGOV_AUTH_PORT}/actuator/health" 90 wait_for_http "http://127.0.0.1:${AIXUN_DATAGOV_SYSTEM_PORT}/actuator/health" 90 wait_for_http "http://127.0.0.1:${AIXUN_DATAGOV_RESOURCE_PORT}/actuator/health" 90 wait_for_http "http://127.0.0.1:${AIXUN_DATAGOV_INTEGRATION_PORT}/actuator/health" 90 wait_for_http "http://127.0.0.1:${AIXUN_DATAGOV_DATAOPS_PORT}/actuator/health" 90 wait_for_http "http://127.0.0.1:${AIXUN_DATAGOV_GOVERNANCE_PORT}/actuator/health" 90 wait_for_http "http://127.0.0.1:${AIXUN_DATAGOV_DATAPRODUCT_PORT}/actuator/health" 90 wait_for_http "http://127.0.0.1:${AIXUN_DATAGOV_GATEWAY_PORT}/actuator/health" 90 wait_for_http "http://127.0.0.1:${AIXUN_DATAGOV_FRONTEND_PORT}/" 60 fi echo "[4/4] Proving other projects remained unchanged" "${SCRIPT_DIR}/snapshot-other-projects.sh" >"${RUNTIME_DIR}/after-other-projects.txt" if ! cmp -s "${RUNTIME_DIR}/before-other-projects.txt" "${RUNTIME_DIR}/after-other-projects.txt"; then echo "Other-project ownership snapshot changed during the DataGov start; investigate without mutating those projects." >&2 exit 1 fi compose --profile app ps echo "AiXun-DataGov local stack is isolated and ready. Credentials were not printed."