Files
Deploy-Laboratory/scripts/verify.sh
jack 231b6713c4 chore: 对齐 00-05 §2 的部署与验证脚本
- 新增 deploy-lab.sh(k3s/longhorn/nginx 铺栈)与 ssh/run-phase2-k3s-on-ylc61-as-jack.sh
- verify.sh:flow/preflight、VERIFY_TEARDOWN 默认、注释与 §2 对应
- 更新 smoke-verify、README、.env.verify.example、根 README 与主要 playbook 头注释
- k3s-delete-lab-stacks 标明重度清场语义

Made-with: Cursor
2026-03-26 07:32:08 +08:00

160 lines
5.6 KiB
Bash
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
#!/usr/bin/env bash
# 验证矩阵自动化入口(对应 docs/00-05 §2「自动化验证流程」步骤 46 的一键串联):
# 4 断言 — 各 verify/XX-YY.yml 内 kubectl / curl / helm 等
# 5 收尾 — 默认 VERIFY_TEARDOWN=1 做本篇资源清理(非整集群卸载)
# 6 串联 — run-all 按 docs/00-02-验证矩阵.md 顺序 fail-fast
#
# 步骤 13接入、环境/轻量清理、部署)由操作者或 scripts/deploy-lab.sh 完成;本脚本不执行 k3s-uninstall。
# 推荐在 Linux 工作机(如 ylc65或控制节点仓库根执行。
set -euo pipefail
ROOT="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)"
MATRIX_MD="${ROOT}/docs/00-02-验证矩阵.md"
# 默认与 §2 一致:验证后清理临时资源
export VERIFY_TEARDOWN="${VERIFY_TEARDOWN:-1}"
load_env() {
if [[ -f "${ROOT}/scripts/.env.verify" ]]; then
set -a
# shellcheck disable=SC1091
source "${ROOT}/scripts/.env.verify"
set +a
echo "[OK] 已加载 scripts/.env.verify"
fi
export VERIFY_TEARDOWN="${VERIFY_TEARDOWN:-1}"
}
parse_doc_ids_from_matrix() {
if [[ ! -f "${MATRIX_MD}" ]]; then
echo "[ERR] matrix 不存在:${MATRIX_MD}" >&2
exit 1
fi
# shellcheck disable=SC2016
awk '
match($0, /`[0-9][0-9]-[0-9][0-9]-[^`]+\.md`/) {
s = substr($0, RSTART+1, RLENGTH-2);
id = substr(s, 1, 5);
if (!seen[id]++) print id;
}
' "${MATRIX_MD}"
}
print_flow() {
cat <<EOF
与 docs/00-05-测试与验证框架.md §2「自动化验证流程一般步骤」对应
1 接入目标环境 inventory + 仓库同步;可选 source scripts/.env.verify本脚本 run/run-all 会自动加载)
2 环境与前置清理 轻量:各 verify playbook 的 teardown重度 k3s 重装勿混进 run-all
3 部署 ./scripts/deploy-lab.sh k3s | longhorn | nginx-matrix* 或手工 ansible-playbook
4 断言 本脚本 run <XX-YY> / run-all → ansible/playbooks/verify/<XX-YY>.yml
5 收尾与记录 VERIFY_TEARDOWN矩阵状态见 docs/00-02-验证矩阵.md建议手工写回
6 一键串联 $0 run-all
相关脚本deploy-lab.sh安装/铺栈、ssh/run-phase2-k3s-on-ylc61-as-jack.sh办公机触发远端 deploy k3s
EOF
}
preflight() {
local inv="${ANSIBLE_INVENTORY:-${ROOT}/ansible/inventory.ini}"
if ! command -v ansible-playbook >/dev/null 2>&1; then
echo "[ERR] 未找到 ansible-playbook" >&2
exit 1
fi
[[ -f "${MATRIX_MD}" ]] || { echo "[ERR] 缺少验证矩阵:${MATRIX_MD}" >&2; exit 1; }
[[ -f "$inv" ]] || { echo "[ERR] inventory 不存在:$inv" >&2; exit 1; }
echo "[RUN] ansible k3s_server -m ping"
ansible k3s_server -i "$inv" -m ping
if [[ "${VERIFY_PREFLIGHT_CLUSTER:-0}" == "1" ]]; then
echo "[RUN] kubectl get nodes控制节点需已安装 K3s"
ansible k3s_server -i "$inv" -b -m ansible.builtin.shell -a \
'KUBECONFIG=/etc/rancher/k3s/k3s.yaml kubectl get nodes' \
|| {
echo "[WARN] 集群侧检查失败:若尚未 deploy k3s可忽略装好后设 VERIFY_PREFLIGHT_CLUSTER=1 再测" >&2
exit 1
}
else
echo "[TIP] 跳过 kubectl 检查。已装 K3s 时可执行VERIFY_PREFLIGHT_CLUSTER=1 $0 preflight"
fi
echo "[OK] preflight 通过"
}
usage() {
cat <<'EOF'
用法scripts/verify.sh <命令> [...]
命令:
flow 打印与 docs/00-05 §2 对齐的「验证流程」说明(不接 Ansible
preflight 检查 ansible-playbook、矩阵与 inventory对 k3s_server 做 ping
若 VERIFY_PREFLIGHT_CLUSTER=1额外 kubectl get nodes未装集群会失败
list 从验证矩阵列出 doc_id顺序同 run-all
run <XX-YY> 运行指定 doc_idansible/playbooks/verify/<XX-YY>.yml
run-all 按验证矩阵顺序运行全部 doc_idfail-fast
环境变量:
VERIFY_TEARDOWN=1 验证后清理本篇资源(默认 1对应 §2 轻量 teardown
VERIFY_PREFLIGHT_CLUSTER 为 1 时 preflight 额外执行 kubectl get nodes
ANSIBLE_INVENTORY 默认 <仓库>/ansible/inventory.ini
nginx_entry_base 例如 http://192.168.2.6102-xx / 03-02 等 HTTP 校验)
nodejs_entry_base 例如 http://192.168.2.6104-01
与「部署」分工:安装 K3s / Longhorn / nginx 铺栈请用 ./scripts/deploy-lab.sh矩阵验收请用本脚本。
示例:
./scripts/verify.sh flow
./scripts/verify.sh preflight
export nginx_entry_base=http://192.168.2.61
./scripts/verify.sh run 02-05
EOF
}
ansible_verify() {
local doc_id="$1"
local inv="${ANSIBLE_INVENTORY:-${ROOT}/ansible/inventory.ini}"
local pb_single="${ROOT}/ansible/playbooks/verify/${doc_id}.yml"
if [[ ! -f "$inv" ]]; then
echo "[ERR] inventory 不存在:$inv" >&2
exit 1
fi
if [[ ! -f "$pb_single" ]]; then
echo "[ERR] verify playbook 不存在fail-fast$pb_single" >&2
exit 1
fi
echo "[RUN] ansible-playbook -i $inv $pb_single"
ansible-playbook -i "$inv" "$pb_single"
}
main() {
load_env
local cmd="${1:-}"
case "$cmd" in
""|-h|--help) usage ;;
flow) print_flow ;;
preflight) preflight ;;
list)
parse_doc_ids_from_matrix
;;
run)
local doc_id="${2:?need doc_id like 02-05}"
ansible_verify "$doc_id"
;;
run-all)
local id
while IFS= read -r id; do
echo ""
echo "########################################## $id"
ansible_verify "$id"
done < <(parse_doc_ids_from_matrix)
;;
*)
echo "[ERR] unknown cmd: $cmd" >&2
usage
exit 1
;;
esac
}
main "$@"