Files
wecom_it_smart_desk/scripts/deploy_inspection_to_jumpserver.sh
T

136 lines
5.4 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.
#!/bin/bash
# =============================================================================
# deploy_inspection_to_jumpserver.sh — 巡检报告 HTML 发布到 jumpserver
# =============================================================================
# 流程:
# 1. 扫描 docs/07-项目管理/巡检报告/ 下所有 HTML 报告
# 2. 调用 jumpserver-V2 skill 的 v2_ops.py upload 上传到 /tmp/
# 3. 用 v2_ops.py exec 按月份目录归档到 /opt/wecom-it-desk/docs-public/inspection/
# 4. (可选)触发 nginx reload(仅当挂载点在容器外时)
#
# 用法:
# bash scripts/deploy_inspection_to_jumpserver.sh # 默认发布全部
# bash scripts/deploy_inspection_to_jumpserver.sh --dry-run # 仅 dry-run
# bash scripts/deploy_inspection_to_jumpserver.sh --month 2026-08 # 仅发布指定月份
#
# 前置:
# - jumpserver-V2 skill 已登录(v2_ops.py status 返回有效)
# - jumpserver 上有静态目录 /opt/wecom-it-desk/docs-public/inspection/
# - jumpserver 上有 nginx 路由 /docs/inspection/ → 该目录
#
# ⚠️ 本脚本默认不修改 nginx 配置(避免误改生产)。
# =============================================================================
set -e
# 颜色
RED='\033[0;31m'; GREEN='\033[0;32m'; YELLOW='\033[1;33m'; BLUE='\033[0;34m'; NC='\033[0m'
info() { echo -e "${BLUE}[INFO]${NC} $1"; }
ok() { echo -e "${GREEN}[OK]${NC} $1"; }
warn() { echo -e "${YELLOW}[WARN]${NC} $1"; }
error() { echo -e "${RED}[ERROR]${NC} $1"; exit 1; }
# 路径常量
SCRIPT_DIR="$(cd "$(dirname "$0")" && pwd)"
PROJECT_ROOT="$(cd "$SCRIPT_DIR/.." && pwd)"
SRC_DIR="$PROJECT_ROOT/docs/07-项目管理/巡检报告"
# jumpserver-V2 skill
V2_OPS="C:\Users\simon\.workbuddy\skills\jumpserver-V2\scripts\v2_ops.py"
PYTHON_BIN="C:\Users\simon\.workbuddy\binaries\python\versions\3.13.12\python.exe"
# 服务器路径
REMOTE_BASE="/opt/wecom-it-desk/docs-public/inspection"
# 参数
DRY_RUN=false
ONLY_MONTH=""
for arg in "$@"; do
case "$arg" in
--dry-run) DRY_RUN=true ;;
--month) ONLY_MONTH="$2"; shift 2 ;;
*) error "未知参数:$arg" ;;
esac
done
# ============================================================
# Step 1: 扫描 HTML 文件
# ============================================================
info "[1/4] 扫描 $SRC_DIR ..."
[ -d "$SRC_DIR" ] || error "源目录不存在:$SRC_DIR"
# 收集所有 .html 文件(按月份分组)
mapfile -t ALL_FILES < <(find "$SRC_DIR" -name "*.html" -type f 2>/dev/null | sort)
[ ${#ALL_FILES[@]} -gt 0 ] || error "未找到任何 HTML 文件"
# 按月份过滤
declare -A MONTH_FILES
for f in "${ALL_FILES[@]}"; do
# 提取 YYYY-MM(相对路径第一段)
rel="${f#$SRC_DIR/}"
month="$(echo "$rel" | cut -d'/' -f1)"
if [ -n "$ONLY_MONTH" ] && [ "$month" != "$ONLY_MONTH" ]; then
continue
fi
MONTH_FILES["$month"]+="$f"$'\n'
done
[ ${#MONTH_FILES[@]} -gt 0 ] || error "指定月份 $ONLY_MONTH 下无 HTML 文件"
info "发现 ${#MONTH_FILES[@]} 个月份,共 $(printf '%s\n' "${ALL_FILES[@]}" | grep -c "\.html") 个文件"
for month in "${!MONTH_FILES[@]}"; do
count=$(echo "${MONTH_FILES[$month]}" | grep -c "\.html" || echo 0)
info " 📅 $month: $count 个报告"
done
# ============================================================
# Step 2: jumpserver 会话状态
# ============================================================
info "[2/4] 检查 jumpserver 会话..."
"$PYTHON_BIN" "$V2_OPS" status 2>&1 | grep -E "✅|❌" || error "jumpserver 会话无效,请先 login"
# ============================================================
# Step 3: 上传 + 归档(按月份)
# ============================================================
info "[3/4] 上传文件到 jumpserver..."
UPLOADED=0
for month in "${!MONTH_FILES[@]}"; do
while IFS= read -r local_file; do
[ -z "$local_file" ] && continue
basename="$(basename "$local_file")"
ts="$(date +%Y%m%d%H%M%S)"
# 中文路径 → jumpserver 必须走 ASCII 临时文件名
# 但 v2_ops.py upload 已自动处理(内部 ASCII staging
info " 📤 $month/$basename"
if [ "$DRY_RUN" = true ]; then
info " (dry-run 跳过)"
else
"$PYTHON_BIN" "$V2_OPS" upload "$local_file" "inspection_${month}_${ts}_${basename}" \
|| error "上传失败:$basename"
# 远端 mkdir + mv 到正确月份目录
remote_cmd="mkdir -p ${REMOTE_BASE}/${month} && mv /tmp/inspection_${month}_${ts}_${basename} ${REMOTE_BASE}/${month}/${basename} && chmod 644 ${REMOTE_BASE}/${month}/${basename}"
"$PYTHON_BIN" "$V2_OPS" exec "$remote_cmd" \
|| error "归档失败:$basename"
UPLOADED=$((UPLOADED + 1))
fi
done <<< "${MONTH_FILES[$month]}"
done
# ============================================================
# Step 4: 完成
# ============================================================
info "[4/4] 部署完成"
echo ""
ok "========== 发布总结 =========="
info "月份数: ${#MONTH_FILES[@]}"
info "上传文件: $UPLOADED"
echo ""
info "📍 访问 URL(前提:nginx 已加 /docs/inspection/ 路由)"
info " https://itsupport.servyou.com.cn/docs/inspection/2026-08/2026-08-05-早班.html"
echo ""
info "🔁 重新发布:直接重跑本脚本即可(脚本会自动覆盖)"
echo ""
info "💡 巡检报告 HTML 由早班巡检自动化任务生成 + 自动调用本脚本发布"
info " (详见 .workbuddy/automations/automation-1782986180887/memory.md 标准步骤清单)"