#!/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" <&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" </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