From b713b739f9895ca62211b03958c227b0be243d68 Mon Sep 17 00:00:00 2001 From: xiaolipro <2357729423@qq.com> Date: Tue, 8 Sep 2026 14:14:56 +0800 Subject: [PATCH] =?UTF-8?q?fix=EF=BC=9A=E7=94=A8=20python3=20=E6=A0=A1?= =?UTF-8?q?=E9=AA=8C=20JSON=EF=BC=88=E4=B8=80=E8=88=AC=E7=B3=BB=E7=BB=9F?= =?UTF-8?q?=E8=87=AA=E5=B8=A6=EF=BC=89=EF=BC=8C=E4=B8=8D=E5=86=8D=E5=BC=BA?= =?UTF-8?q?=E5=88=B6=20jq?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- scripts/export-from-consul.sh | 25 ++++++++++++++++++++++--- scripts/sync-to-consul.sh | 17 ++++++++++++++--- 2 files changed, 36 insertions(+), 6 deletions(-) diff --git a/scripts/export-from-consul.sh b/scripts/export-from-consul.sh index 0ed348f..d74d871 100644 --- a/scripts/export-from-consul.sh +++ b/scripts/export-from-consul.sh @@ -14,8 +14,27 @@ mkdir -p "$IDWMS_DIR" need() { command -v "$1" >/dev/null || { echo "FAIL: 需要 $1"; exit 1; }; } need kubectl need curl -need jq +command -v python3 >/dev/null || command -v jq >/dev/null || { + echo "FAIL: 需要 python3 或 jq"; exit 1; +} +json_pretty() { + local raw="$1" out="$2" + if command -v python3 >/dev/null; then + python3 -c 'import json,sys; json.dump(json.load(sys.stdin), open(sys.argv[1],"w",encoding="utf-8"), ensure_ascii=False, indent=2); open(sys.argv[1],"a",encoding="utf-8").write("\n")' "$out" <<<"$raw" + else + jq . <<<"$raw" >"$out" + fi +} + +json_ok_str() { + local raw="$1" + if command -v python3 >/dev/null; then + python3 -c 'import json,sys; json.loads(sys.stdin.read())' <<<"$raw" + else + jq empty <<<"$raw" + fi +} if [[ -z "${CONSUL_HTTP_ADDR:-}" ]]; then CIP="$(kubectl -n "$NS" get svc consul -o jsonpath='{.spec.clusterIP}')" CONSUL_HTTP_ADDR="http://${CIP}:8500" @@ -48,11 +67,11 @@ for name in "${TARGETS[@]}"; do echo "WARN: ${key} 为空或不存在,跳过" continue fi - if ! jq empty <<<"$raw" 2>/dev/null; then + if ! json_ok_str "$raw" 2>/dev/null; then echo "FAIL: ${key} 不是 JSON" exit 1 fi - jq . <<<"$raw" >"$out" + json_pretty "$raw" "$out" echo "OK → ${out}" done diff --git a/scripts/sync-to-consul.sh b/scripts/sync-to-consul.sh index 9d306ee..c21d129 100644 --- a/scripts/sync-to-consul.sh +++ b/scripts/sync-to-consul.sh @@ -22,8 +22,19 @@ IDWMS_DIR="${ROOT}/idwms" need() { command -v "$1" >/dev/null || { echo "FAIL: 需要 $1"; exit 1; }; } need kubectl need curl -need jq -command -v envsubst >/dev/null || { echo "FAIL: 需要 envsubst(gettext)"; exit 1; } +command -v envsubst >/dev/null || { echo "FAIL: 需要 envsubst(apt install gettext-base)"; exit 1; } + +# JSON 校验:优先 python3(runner 主机通常有),其次 jq +json_ok() { + local f="$1" + if command -v python3 >/dev/null; then + python3 -c 'import json,sys; json.load(open(sys.argv[1],encoding="utf-8"))' "$f" + elif command -v jq >/dev/null; then + jq empty "$f" + else + echo "FAIL: 需要 python3 或 jq 校验 JSON"; return 1 + fi +} secret_b64() { local name="$1" key="$2" @@ -116,7 +127,7 @@ for name in "${TARGETS[@]}"; do envsubst '${MYSQL_ROOT_PASSWORD} ${REDIS_PASSWORD} ${RABBITMQ_DEFAULT_PASS} ${ELASTIC_PASSWORD} ${JWT_SECRET_KEY}' \ <"$src" >"${TMP}/${name}.json" - if ! jq empty "${TMP}/${name}.json" 2>/dev/null; then + if ! json_ok "${TMP}/${name}.json" 2>/dev/null; then echo "FAIL: ${name}.json 渲染后不是合法 JSON" exit 1 fi