192 lines
6.3 KiB
Bash
192 lines
6.3 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}"
|
||
LD_CONF_FILE="/etc/ld.so.conf.d/${PACKAGE_SLUG}.conf"
|
||
PKG_NAME="${PACKAGE_NAME}"
|
||
PKG_VERSION="${PKG_RELEASE_VERSION}"
|
||
INSTALL_DIR="${SERVICE_INSTALL_DIR}"
|
||
|
||
log() { echo "[\${PKG_NAME}] \$*"; }
|
||
fail() { echo "[\${PKG_NAME}] [FAIL] \$*" >&2; exit 1; }
|
||
|
||
log "=========================================="
|
||
log "开始安装 \${PKG_NAME} \${PKG_VERSION}"
|
||
log " 安装目录: \${INSTALL_DIR}"
|
||
log " 服务单元: \${SERVICE_UNIT}"
|
||
log "=========================================="
|
||
|
||
# 1. 配置动态库搜索路径
|
||
log "[1/6] 配置动态库搜索路径 \${LD_CONF_FILE}"
|
||
cat > "\${LD_CONF_FILE}" <<'LDEOF'
|
||
/usr/lib
|
||
/opt/sysroot/lib/
|
||
LDEOF
|
||
ldconfig
|
||
log " ldconfig 完成"
|
||
|
||
# 2. DroneScrewServer 专用:启动 mediamtx
|
||
if [ "\${PKG_NAME}" = "DroneScrewServer" ]; then
|
||
mkdir -p "\${INSTALL_DIR}/logs"
|
||
log "[2/6] 检测并启动 mediamtx RTSP 服务器"
|
||
MEDIAMTX_BIN="\${INSTALL_DIR}/bin/mediamtx"
|
||
MEDIAMTX_CFG="\${INSTALL_DIR}/etc/mediamtx_dronescrew.yml"
|
||
MEDIAMTX_SERVICE="\${INSTALL_DIR}/etc/mediamtx-dronescrew.service"
|
||
|
||
if [ -f "\${MEDIAMTX_BIN}" ] && [ -f "\${MEDIAMTX_CFG}" ] && [ -f "\${MEDIAMTX_SERVICE}" ]; then
|
||
chmod +x "\${MEDIAMTX_BIN}"
|
||
|
||
# 复制 service 文件到 systemd 目录
|
||
cp "\${MEDIAMTX_SERVICE}" /etc/systemd/system/mediamtx-dronescrew.service
|
||
chmod 644 /etc/systemd/system/mediamtx-dronescrew.service
|
||
|
||
systemctl daemon-reload
|
||
systemctl enable mediamtx-dronescrew.service >/dev/null 2>&1 || true
|
||
systemctl restart mediamtx-dronescrew.service
|
||
sleep 1
|
||
|
||
if systemctl is-active --quiet mediamtx-dronescrew.service; then
|
||
log " [OK] mediamtx 已启动 (端口 8554)"
|
||
else
|
||
log " [WARN] mediamtx 启动失败,DroneScrewServer RTSP 推流可能不可用"
|
||
log " 手动检查: journalctl -u mediamtx-dronescrew.service -n 20"
|
||
fi
|
||
else
|
||
log " [WARN] mediamtx 文件缺失,跳过启动"
|
||
fi
|
||
else
|
||
log "[2/6] 跳过 mediamtx(非 DroneScrewServer)"
|
||
fi
|
||
|
||
# 3. 校验服务文件
|
||
log "[3/6] 校验服务文件"
|
||
[ -f "\${SERVICE_SOURCE}" ] || fail "缺少服务文件: \${SERVICE_SOURCE}"
|
||
log " OK: \${SERVICE_SOURCE}"
|
||
|
||
# 4. 停止/禁用旧服务
|
||
log "[4/6] 停止旧服务(如在运行)"
|
||
if systemctl is-active --quiet "\${SERVICE_UNIT}" 2>/dev/null; then
|
||
systemctl stop "\${SERVICE_UNIT}"
|
||
log " 已停止 \${SERVICE_UNIT}"
|
||
else
|
||
log " 旧服务未运行,跳过"
|
||
fi
|
||
if systemctl is-enabled --quiet "\${SERVICE_UNIT}" 2>/dev/null; then
|
||
systemctl disable "\${SERVICE_UNIT}" >/dev/null 2>&1 || true
|
||
log " 已禁用旧 unit"
|
||
fi
|
||
|
||
# 5. 注册新 unit
|
||
log "[5/6] 注册并启用新 unit"
|
||
cp "\${SERVICE_SOURCE}" "\${SERVICE_TARGET}"
|
||
chmod 644 "\${SERVICE_TARGET}"
|
||
systemctl daemon-reload
|
||
systemctl enable "\${SERVICE_UNIT}" >/dev/null 2>&1
|
||
log " 已 enable \${SERVICE_UNIT}"
|
||
|
||
# 6. 启动并验证
|
||
log "[6/6] 启动服务"
|
||
set +e
|
||
systemctl start "\${SERVICE_UNIT}"
|
||
START_RC=\$?
|
||
set -e
|
||
sleep 1
|
||
|
||
if systemctl is-active --quiet "\${SERVICE_UNIT}"; then
|
||
log " [OK] \${SERVICE_UNIT} 已在运行"
|
||
systemctl status "\${SERVICE_UNIT}" --no-pager --lines=3 || true
|
||
log "=========================================="
|
||
log "\${PKG_NAME} 安装成功"
|
||
log " 查看实时日志: journalctl -u \${SERVICE_UNIT} -f"
|
||
log " 停止服务: systemctl stop \${SERVICE_UNIT}"
|
||
if [ "\${PKG_NAME}" = "DroneScrewServer" ]; then
|
||
log " mediamtx 日志: journalctl -u mediamtx-dronescrew.service -f"
|
||
log " RTSP 拉流地址: rtsp://<板子IP>:8554/live/dronescrew"
|
||
fi
|
||
log "=========================================="
|
||
else
|
||
log " [FAIL] \${SERVICE_UNIT} 未能启动 (start rc=\${START_RC})"
|
||
log " 最近 20 行日志:"
|
||
journalctl -u "\${SERVICE_UNIT}" --no-pager --lines=20 || true
|
||
fail "服务启动失败,请检查上面的日志"
|
||
fi
|
||
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}"
|
||
LD_CONF_FILE="/etc/ld.so.conf.d/${PACKAGE_SLUG}.conf"
|
||
PKG_NAME="${PACKAGE_NAME}"
|
||
INSTALL_DIR="${SERVICE_INSTALL_DIR}"
|
||
ACTION="\${1:-}"
|
||
|
||
log() { echo "[\${PKG_NAME}] \$*"; }
|
||
|
||
log "=========================================="
|
||
log "卸载 \${PKG_NAME} (动作: \${ACTION})"
|
||
log "=========================================="
|
||
|
||
log "[1/4] 停止并禁用 \${SERVICE_UNIT}"
|
||
systemctl stop "\${SERVICE_UNIT}" 2>/dev/null && log " 已停止" || log " 未在运行"
|
||
systemctl disable "\${SERVICE_UNIT}" >/dev/null 2>&1 && log " 已禁用" || true
|
||
rm -f "/etc/systemd/system/\${SERVICE_UNIT}"
|
||
systemctl daemon-reload
|
||
|
||
# DroneScrewServer 专用:停止 mediamtx
|
||
if [ "\${PKG_NAME}" = "DroneScrewServer" ]; then
|
||
log "[2/4] 停止并删除 mediamtx 服务"
|
||
if systemctl is-active --quiet mediamtx-dronescrew.service 2>/dev/null; then
|
||
systemctl stop mediamtx-dronescrew.service
|
||
log " 已停止 mediamtx"
|
||
fi
|
||
systemctl disable mediamtx-dronescrew.service >/dev/null 2>&1 || true
|
||
rm -f /etc/systemd/system/mediamtx-dronescrew.service
|
||
systemctl daemon-reload
|
||
log " mediamtx 服务已移除"
|
||
else
|
||
log "[2/4] 跳过 mediamtx(非 DroneScrewServer)"
|
||
fi
|
||
|
||
if [ "\${ACTION}" = "remove" ] || [ "\${ACTION}" = "purge" ]; then
|
||
log "[3/4] 清理安装目录 \${INSTALL_DIR}"
|
||
rm -rf "\${INSTALL_DIR}"
|
||
log "[4/4] 清理 ld.so.conf 并刷新缓存"
|
||
rm -f "\${LD_CONF_FILE}"
|
||
ldconfig
|
||
log "=========================================="
|
||
log "\${PKG_NAME} 卸载完成"
|
||
log "=========================================="
|
||
else
|
||
log "[3/4] 跳过目录清理(仅升级阶段,保留安装目录)"
|
||
log "[4/4] 跳过 ld.so.conf 清理"
|
||
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
|