auto-install Redis with multi-package-manager support

This commit is contained in:
jingrow 2025-11-14 22:23:46 +08:00
parent 0f3617e09e
commit 9e2461af1a

59
dev.sh
View File

@ -53,6 +53,56 @@ install_uv() {
return 1
}
# 安装 Redis
install_redis() {
info "尝试自动安装 Redis..."
local pkg_manager=""
local pkg_name=""
local install_cmd=""
# 检测包管理器
if command -v apt-get &> /dev/null; then
pkg_manager="apt-get"
pkg_name="redis-server"
install_cmd="apt-get update && apt-get install -y $pkg_name"
elif command -v yum &> /dev/null; then
pkg_manager="yum"
pkg_name="redis"
install_cmd="yum install -y $pkg_name"
elif command -v dnf &> /dev/null; then
pkg_manager="dnf"
pkg_name="redis"
install_cmd="dnf install -y $pkg_name"
else
error "未检测到支持的包管理器apt-get/yum/dnf请手动安装 Redis"
return 1
fi
# 执行安装(自动处理 root 权限)
if [ "$EUID" -eq 0 ]; then
eval "$install_cmd" || {
error "$pkg_manager 安装 Redis 失败"
return 1
}
else
warn "需要 root 权限安装 Redis尝试使用 sudo..."
if ! sudo bash -c "$install_cmd"; then
error "sudo 安装 Redis 失败,请手动安装: sudo $install_cmd"
return 1
fi
fi
# 验证安装
if command -v redis-server &> /dev/null; then
success "Redis 安装完成"
return 0
else
error "Redis 安装后未找到 redis-server 命令"
return 1
fi
}
# 清理进程
cleanup() {
info "清理现有进程..."
@ -98,6 +148,15 @@ check_redis() {
return 0
fi
# 检查 redis-server 是否安装
if ! command -v redis-server &> /dev/null; then
warn "redis-server 未安装,将尝试自动安装"
install_redis || {
error "自动安装 Redis 失败,请根据系统手动安装: sudo apt-get install -y redis-server (Ubuntu/Debian) 或 sudo yum install -y redis (CentOS/RHEL)"
return 1
}
fi
warn "Redis 未运行,尝试启动..."
if command -v redis-server &> /dev/null; then
redis-server ./redis.conf &> /dev/null &