Files
servboard/data/servctl.sh
T
zhangjie 5b2219f901
/ 自动构建并推送Docker镜像 (push) Failing after 3s
Init
2026-03-25 17:34:00 +08:00

168 lines
4.6 KiB
Bash
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
#!/bin/bash
# servctl.sh - ServBoard services 管理工具
# 数据库路径
DB_PATH="$(dirname "$0")/dashboard.db"
# 检查依赖
if ! command -v sqlite3 &> /dev/null; then
echo "错误: 未安装 sqlite3" >&2
exit 1
fi
if [ ! -f "$DB_PATH" ]; then
echo "错误: 数据库文件不存在: $DB_PATH" >&2
exit 1
fi
show_help() {
cat <<EOF
用法:
$0 -l 显示当前服务列表
$0 -h 显示帮助
$0 -r <name> 删除服务
$0 -t <name> 切换服务状态
$0 -a <name> <description> <port(1-65535)> <protocol(http/https)> [<icon>] [<path>] 添加服务
示例(无空格时无需引号):
$0 -a jellyfin 媒体服务器 8096 http jellyfin.png
$0 -a watchtower 自动更新容器 8080 https
$0 -a nextcloud 文件同步 8080 http nextcloud.png nextcloud
$0 -r watchtower
$0 -t jellyfin
$0 -l
EOF
}
list_services() {
sqlite3 -header -column "$DB_PATH" \
"SELECT id, name, description, port, protocol, enabled, IFNULL(icon, '') AS icon, IFNULL(path, '') AS path FROM services ORDER BY id;"
}
add_service_from_args() {
if [ $# -lt 4 ]; then
echo "错误: -a 需要至少 4 个参数 (name description port protocol)" >&2
show_help
exit 1
fi
NAME="$1"
DESC="$2"
PORT="$3"
PROTO_RAW="$4"
ICON="${5:-}"
PATH="${6:-}"
# 清理端口空白
PORT=$(echo "$PORT" | tr -d '[:space:]')
# 校验端口
if ! [[ "$PORT" =~ ^[0-9]+$ ]] || [ "$PORT" -lt 1 ] || [ "$PORT" -gt 65535 ]; then
echo "错误: 端口必须是 1-65535 的整数(当前输入: '$PORT')" >&2
exit 1
fi
# 校验协议
PROTO=$(echo "$PROTO_RAW" | tr '[:upper:]' '[:lower:]')
if [ "$PROTO" != "http" ] && [ "$PROTO" != "https" ]; then
echo "错误: protocol 必须是 http 或 https(当前输入: '$PROTO_RAW')" >&2
exit 1
fi
ENABLED=1
if [ -n "$ICON" ] && [ -n "$PATH" ]; then
SQL="INSERT INTO services (name, description, port, protocol, enabled, icon, path) VALUES ('$NAME', '$DESC', $PORT, '$PROTO', $ENABLED, '$ICON', '$PATH');"
elif [ -n "$ICON" ]; then
SQL="INSERT INTO services (name, description, port, protocol, enabled, icon) VALUES ('$NAME', '$DESC', $PORT, '$PROTO', $ENABLED, '$ICON');"
elif [ -n "$PATH" ]; then
SQL="INSERT INTO services (name, description, port, protocol, enabled, path) VALUES ('$NAME', '$DESC', $PORT, '$PROTO', $ENABLED, '$PATH');"
else
SQL="INSERT INTO services (name, description, port, protocol, enabled) VALUES ('$NAME', '$DESC', $PORT, '$PROTO', $ENABLED);"
fi
if sqlite3 "$DB_PATH" "$SQL"; then
echo "✅ 添加成功: $NAME ($PORT/$PROTO)"
if [ -n "$PATH" ]; then
echo " 路径: $PATH"
fi
else
echo "❌ 添加失败(可能名称重复或超长)" >&2
exit 1
fi
}
delete_service() {
if [ -z "$1" ]; then
echo "错误: -r 需要指定服务名" >&2
exit 1
fi
NAME="$1"
COUNT=$(sqlite3 "$DB_PATH" "SELECT COUNT(*) FROM services WHERE name = '$NAME';")
if [ "$COUNT" -eq 0 ]; then
echo "⚠️ 未找到服务: $NAME" >&2
exit 1
fi
sqlite3 "$DB_PATH" "DELETE FROM services WHERE name = '$NAME';"
echo "🗑️ 已删除: $NAME"
}
toggle_service() {
if [ -z "$1" ]; then
echo "错误: -t 需要指定服务名" >&2
exit 1
fi
NAME="$1"
CURRENT=$(sqlite3 "$DB_PATH" "SELECT enabled FROM services WHERE name = '$NAME';")
if [ -z "$CURRENT" ]; then
echo "⚠️ 未找到服务: $NAME" >&2
exit 1
fi
NEW_STATE=$((1 - CURRENT))
sqlite3 "$DB_PATH" "UPDATE services SET enabled = $NEW_STATE WHERE name = '$NAME';"
if [ "$NEW_STATE" -eq 1 ]; then
echo "🔌 已启用: $NAME"
else
echo "📴 已禁用: $NAME"
fi
}
# === 主逻辑:手动解析参数(无需 getopts)===
case "${1:-}" in
-l)
list_services
;;
-h)
show_help
;;
-r)
if [ -z "$2" ]; then
echo "错误: -r 需要服务名" >&2
exit 1
fi
delete_service "$2"
;;
-t)
if [ -z "$2" ]; then
echo "错误: -t 需要服务名" >&2
exit 1
fi
toggle_service "$2"
;;
-a)
if [ $# -lt 5 ]; then
echo "错误: -a 需要至少 4 个参数" >&2
show_help
exit 1
fi
shift
add_service_from_args "$@"
;;
"")
echo "提示: 运行 '$0 -h' 查看帮助"
;;
*)
echo "未知选项: $1" >&2
show_help
exit 1
;;
esac