62e0fe94ef
完成资源卡按类型和按依赖分类展现的功能 Reviewed-on: http://192.168.35.82/git/GenarrativeAI/Genarrative/pulls/129 Co-authored-by: menghao <mh18530625731@163.com> Co-committed-by: menghao <mh18530625731@163.com>
91 lines
2.5 KiB
Bash
91 lines
2.5 KiB
Bash
#!/usr/bin/env bash
|
|
|
|
set -euo pipefail
|
|
|
|
MAINTENANCE_FILE="${GENARRATIVE_MAINTENANCE_FILE:-/var/lib/genarrative/maintenance/enabled}"
|
|
MAINTENANCE_PAGE_FILE="${GENARRATIVE_MAINTENANCE_PAGE_FILE:-/var/lib/genarrative/maintenance/page.html}"
|
|
PAGE_SOURCE=""
|
|
REASON_PARTS=()
|
|
page_temp=""
|
|
marker_temp=""
|
|
|
|
cleanup_temps() {
|
|
if [[ -n "${page_temp}" ]]; then
|
|
rm -f -- "${page_temp}"
|
|
fi
|
|
if [[ -n "${marker_temp}" ]]; then
|
|
rm -f -- "${marker_temp}"
|
|
fi
|
|
}
|
|
|
|
trap cleanup_temps EXIT
|
|
|
|
replace_file_atomically() {
|
|
local source_file="$1"
|
|
local target_file="$2"
|
|
|
|
if [[ -L "${target_file}" ]]; then
|
|
echo "[maintenance] 原子替换目标不能是符号链接: ${target_file}" >&2
|
|
exit 1
|
|
fi
|
|
if [[ -d "${target_file}" ]]; then
|
|
echo "[maintenance] 原子替换目标不能是目录: ${target_file}" >&2
|
|
exit 1
|
|
fi
|
|
# 源文件与目标文件位于同一目录,POSIX rename 语义即可保证原子替换。
|
|
# 不使用 GNU mv 专属的 -T,确保 macOS/BSD 本地门禁也能执行。
|
|
mv -f "${source_file}" "${target_file}"
|
|
}
|
|
|
|
while [[ $# -gt 0 ]]; do
|
|
case "$1" in
|
|
--page-file)
|
|
PAGE_SOURCE="${2:?缺少 --page-file 的值}"
|
|
shift 2
|
|
;;
|
|
-h|--help)
|
|
echo "用法: $0 [--page-file <临时公告 HTML>] [维护原因]"
|
|
exit 0
|
|
;;
|
|
--*)
|
|
echo "[maintenance] 未知参数: $1" >&2
|
|
exit 1
|
|
;;
|
|
*)
|
|
REASON_PARTS+=("$1")
|
|
shift
|
|
;;
|
|
esac
|
|
done
|
|
|
|
REASON="${REASON_PARTS[*]:-manual}"
|
|
|
|
if [[ -n "${PAGE_SOURCE}" && ( ! -f "${PAGE_SOURCE}" || ! -s "${PAGE_SOURCE}" ) ]]; then
|
|
echo "[maintenance] 临时公告页不存在或为空: ${PAGE_SOURCE}" >&2
|
|
exit 1
|
|
fi
|
|
|
|
mkdir -p "$(dirname "${MAINTENANCE_FILE}")" "$(dirname "${MAINTENANCE_PAGE_FILE}")"
|
|
|
|
if [[ -n "${PAGE_SOURCE}" ]]; then
|
|
page_temp="$(mktemp "${MAINTENANCE_PAGE_FILE}.tmp.XXXXXX")"
|
|
install -m 0644 -- "${PAGE_SOURCE}" "${page_temp}"
|
|
replace_file_atomically "${page_temp}" "${MAINTENANCE_PAGE_FILE}"
|
|
page_temp=""
|
|
echo "[maintenance] 已安装本次运行态公告页: ${MAINTENANCE_PAGE_FILE}"
|
|
elif [[ ! -f "${MAINTENANCE_FILE}" && ( -e "${MAINTENANCE_PAGE_FILE}" || -L "${MAINTENANCE_PAGE_FILE}" ) ]]; then
|
|
rm -f -- "${MAINTENANCE_PAGE_FILE}"
|
|
fi
|
|
|
|
marker_temp="$(mktemp "${MAINTENANCE_FILE}.tmp.XXXXXX")"
|
|
{
|
|
printf "enabled_at=%s\n" "$(date -u +%Y-%m-%dT%H:%M:%SZ)"
|
|
printf "reason=%s\n" "${REASON}"
|
|
} >"${marker_temp}"
|
|
|
|
chmod 0644 "${marker_temp}"
|
|
replace_file_atomically "${marker_temp}" "${MAINTENANCE_FILE}"
|
|
marker_temp=""
|
|
trap - EXIT
|
|
echo "[maintenance] 已进入维护模式: ${MAINTENANCE_FILE}"
|