Files
sub2api-add/deploy/uninstall.sh
T
Qiufeng 292dd329af
Business Plugins CI / check (plugin-admin) (push) Successful in 1m36s
Business Plugins CI / check (subscription-admin) (push) Successful in 1m37s
feat: add one-command plugin deployment and removal
2026-08-27 23:48:18 +08:00

60 lines
2.0 KiB
Bash
Executable File

#!/usr/bin/env bash
set -Eeuo pipefail
ETC_DIR=${PLUGIN_ETC_DIR:-/etc/sub2api-add}
VAR_DIR=${PLUGIN_VAR_DIR:-/var/lib/sub2api-add}
PREFIX=${PLUGIN_INSTALL_PREFIX:-/opt/sub2api-add}
SOURCE_DIR=${PLUGIN_SOURCE_DIR:-/opt/sub2api-add}
RUN_USER=${PLUGIN_SYSTEM_USER:-sub2api-plugin}
SELECTION=all
PURGE=false
YES=false
die() { printf '错误:%s\n' "$*" >&2; exit 1; }
[[ $EUID -eq 0 ]] || die "请使用 root 或 sudo 运行"
while (($#)); do
case "$1" in
--plugin) (($# >= 2)) || die "--plugin 需要参数"; SELECTION=$2; shift 2 ;;
--purge) PURGE=true; shift ;;
--yes) YES=true; shift ;;
-h|--help) printf '%s\n' '用法:deploy/uninstall.sh [--plugin all|plugin-admin|subscription-admin] [--purge --yes]'; exit 0 ;;
*) die "未知参数:$1" ;;
esac
done
case "$SELECTION" in
all) PLUGINS=(plugin-admin subscription-admin) ;;
plugin-admin|subscription-admin) PLUGINS=("$SELECTION") ;;
*) die "插件必须是 all、plugin-admin 或 subscription-admin" ;;
esac
if $PURGE && ! $YES; then
[[ -t 0 ]] || die "非交互环境执行 --purge 时必须同时提供 --yes"
read -r -p "将删除插件配置和数据,继续?[y/N] " answer
[[ "$answer" == [yY] ]] || { printf '%s\n' '已取消'; exit 0; }
fi
for plugin in "${PLUGINS[@]}"; do
unit="sub2api-$plugin.service"
systemctl disable --now "$unit" 2>/dev/null || true
rm -f "/etc/systemd/system/$unit"
rm -rf "$PREFIX/$plugin"
if $PURGE; then
rm -f "$ETC_DIR/$plugin.env"
rm -rf "$VAR_DIR/$plugin"
fi
done
systemctl daemon-reload
if $PURGE && [[ -d "$ETC_DIR" ]]; then rmdir "$ETC_DIR" 2>/dev/null || true; fi
if $PURGE && [[ -d "$VAR_DIR" ]]; then rmdir "$VAR_DIR" 2>/dev/null || true; fi
if $PURGE && [[ -d "$SOURCE_DIR" ]]; then rm -rf "$SOURCE_DIR"; fi
if $PURGE; then
userdel "$RUN_USER" 2>/dev/null || true
printf '%s\n' '插件、配置、数据和源码已删除;Core 数据库未被触碰。'
else
printf '%s\n' "插件服务已卸载;配置和数据仍保留在 $ETC_DIR 与 $VAR_DIR。"
fi