diff --git a/jsite.sh b/jsite.sh index ef5a6d9..5227e30 100644 --- a/jsite.sh +++ b/jsite.sh @@ -436,6 +436,9 @@ get_available_port() { # 检查项目是否已有分配的端口 if [ -f "$port_file" ]; then + # 确保jq工具已安装 + ensure_jq_installed + # 使用jq查询已分配的端口 if command -v jq &> /dev/null; then local existing_port=$(jq -r ".$site_name // empty" "$port_file" 2>/dev/null || echo "") @@ -462,12 +465,15 @@ get_available_port() { user_specified_port=true fi - if [ "$user_specified_port" = true ]; then - # 用户传入了自定义端口,优先使用用户指定的端口 - # 检查用户指定的端口是否已被使用 - local port_available=true - if [ -f "$port_file" ]; then - if command -v jq &> /dev/null; then + if [ "$user_specified_port" = true ]; then + # 用户传入了自定义端口,优先使用用户指定的端口 + # 检查用户指定的端口是否已被使用 + local port_available=true + if [ -f "$port_file" ]; then + # 确保jq工具已安装 + ensure_jq_installed + + if command -v jq &> /dev/null; then local used_ports=$(jq -r '.[]' "$port_file" 2>/dev/null || echo "") if echo "$used_ports" | grep -q "^$base_port$"; then port_available=false @@ -486,6 +492,9 @@ get_available_port() { else # 用户指定的端口已被使用,使用最大端口号 + 1 if [ -f "$port_file" ]; then + # 确保jq工具已安装 + ensure_jq_installed + if command -v jq &> /dev/null; then local max_port=$(jq -r 'max(.[])' "$port_file" 2>/dev/null || echo "$base_port") if [ -n "$max_port" ] && [ "$max_port" != "null" ]; then @@ -502,6 +511,9 @@ get_available_port() { else # 用户没有传入自定义端口,使用自动分配逻辑 if [ -f "$port_file" ]; then + # 确保jq工具已安装 + ensure_jq_installed + if command -v jq &> /dev/null; then # 使用jq找到最大端口号 local max_port=$(jq -r 'max(.[])' "$port_file" 2>/dev/null || echo "$base_port") @@ -535,6 +547,9 @@ save_port_assignment() { chown jingrow:jingrow /home/jingrow/jsite fi + # 确保jq工具已安装 + ensure_jq_installed + # 检查jq是否可用 if command -v jq &> /dev/null; then # 使用jq保存到JSON文件 @@ -586,6 +601,9 @@ get_or_assign_port() { local already_saved=false if [ -f "$port_file" ]; then + # 确保jq工具已安装 + ensure_jq_installed + if command -v jq &> /dev/null; then local existing_port=$(jq -r ".$site_name // empty" "$port_file" 2>/dev/null || echo "") if [ -n "$existing_port" ] && [ "$existing_port" != "null" ] && [ "$existing_port" != "empty" ]; then @@ -622,6 +640,9 @@ show_port_assignments() { fi log_info "当前端口分配情况:" + # 确保jq工具已安装 + ensure_jq_installed + if command -v jq &> /dev/null; then # 使用jq格式化输出 jq -r 'to_entries[] | " - \(.key): \(.value)"' "$port_file" 2>/dev/null || log_warning "无法解析端口分配文件" @@ -1121,6 +1142,45 @@ install_docker() { log_success "将jingrow用户添加到docker组" } +# 6.1 安装jq工具(如果未安装) +install_jq() { + log_info "检查jq工具安装状态..." + + if command -v jq &> /dev/null; then + log_warning "jq工具已安装" + else + log_info "开始安装jq工具..." + + # 设置非交互式环境 + export DEBIAN_FRONTEND=noninteractive + export DEBCONF_NONINTERACTIVE_SEEN=true + + # 更新包索引 + apt-get update + + # 安装jq工具 + apt-get install -y --force-yes jq + + if command -v jq &> /dev/null; then + log_success "jq工具安装完成" + else + log_error "jq工具安装失败" + return 1 + fi + fi +} + +# 6.2 确保jq工具已安装 +ensure_jq_installed() { + if ! command -v jq &> /dev/null; then + log_warning "jq工具未安装,正在安装..." + if ! install_jq; then + log_error "jq工具安装失败,无法处理JSON文件" + return 1 + fi + fi +} + # 7. 启动traefik start_traefik() { log_info "启动traefik服务..." @@ -1472,6 +1532,9 @@ delete_site() { # 删除端口分配记录 log_info "删除端口分配记录..." local port_file="$JSITE_BASE_DIR/site_port.json" + # 确保jq工具已安装 + ensure_jq_installed + if [ -f "$port_file" ] && command -v jq &> /dev/null; then if jq "del(.$SITE_NAME)" "$port_file" > "${port_file}.tmp" 2>/dev/null; then mv "${port_file}.tmp" "$port_file" @@ -1726,6 +1789,9 @@ main() { log_warning "跳过Docker安装" fi + # 安装jq工具(用于处理JSON文件) + install_jq + install_nodejs if ! clone_jsite_project; then