GrabBag/GrabBagPrj/project_pkg_service.sh

69 lines
1.5 KiB
Bash

#!/bin/bash
# 服务打包
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
source "${SCRIPT_DIR}/project_pkg_common.sh"
write_service_postinst() {
cat > "${PKG_PATH}/DEBIAN/postinst" <<EOF
#!/bin/bash
set -e
SERVICE_SOURCE="${SERVICE_INSTALL_PATH}"
SERVICE_UNIT="${SERVICE_UNIT_NAME}"
SERVICE_TARGET="/etc/systemd/system/\${SERVICE_UNIT}"
ldconfig
if [ ! -f "\${SERVICE_SOURCE}" ]; then
echo "缺少服务文件: \${SERVICE_SOURCE}" >&2
exit 1
fi
if systemctl is-active --quiet "\${SERVICE_UNIT}" 2>/dev/null; then
systemctl stop "\${SERVICE_UNIT}"
fi
if systemctl is-enabled --quiet "\${SERVICE_UNIT}" 2>/dev/null; then
systemctl disable "\${SERVICE_UNIT}"
fi
cp "\${SERVICE_SOURCE}" "\${SERVICE_TARGET}"
chmod 644 "\${SERVICE_TARGET}"
systemctl daemon-reload
systemctl enable "\${SERVICE_UNIT}"
systemctl start "\${SERVICE_UNIT}"
EOF
chmod +x "${PKG_PATH}/DEBIAN/postinst"
}
write_service_postrm() {
cat > "${PKG_PATH}/DEBIAN/postrm" <<EOF
#!/bin/bash
set -e
SERVICE_UNIT="${SERVICE_UNIT_NAME}"
systemctl stop "\${SERVICE_UNIT}" 2>/dev/null || true
systemctl disable "\${SERVICE_UNIT}" 2>/dev/null || true
rm -f "/etc/systemd/system/\${SERVICE_UNIT}"
systemctl daemon-reload
if [ "\${1:-}" = "remove" ]; then
rm -rf "${SERVICE_INSTALL_DIR}"
fi
EOF
chmod +x "${PKG_PATH}/DEBIAN/postrm"
}
# ===== 主流程 =====
load_package_context "$@" "service"
run_common_packaging_steps
log_info "写入服务集成脚本"
write_service_postinst
write_service_postrm
finalize_package