fix(1panel): sync MySQL/Redis container hosts on nx update

Upgrade was skipping 1panel-network detection; wizard now overrides stale host.docker.internal from volume/API defaults.

Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
Nexus Deploy
2026-06-06 05:47:43 +08:00
parent 6b1d23c7d7
commit b25d0798f6
6 changed files with 137 additions and 11 deletions
+35 -2
View File
@@ -200,12 +200,39 @@ upsert_env_var() {
fi
}
write_1panel_hosts_volume() {
local mysql_host="${1:-}" redis_host="${2:-}"
[[ -n "$mysql_host" || -n "$redis_host" ]] || return 0
local vol json
vol="$(docker volume ls -q --filter name=nexus-web-data 2>/dev/null | head -1 || true)"
if [[ -z "$vol" ]]; then
warn "未找到 nexus-web-data 卷,跳过写入 1panel-hosts.json"
return 0
fi
json="{"
if [[ -n "$mysql_host" ]]; then
json+="\"db_host\":\"${mysql_host}\""
[[ -n "$redis_host" ]] && json+=","
fi
if [[ -n "$redis_host" ]]; then
json+="\"redis_host\":\"${redis_host}\""
fi
json+="}"
if printf '%s' "$json" | docker run --rm -i -v "${vol}:/data" alpine:3.19 \
sh -c 'cat > /data/1panel-hosts.json && chmod 644 /data/1panel-hosts.json'; then
info "已写入卷 ${vol} → web/data/1panel-hosts.json"
else
warn "写入 1panel-hosts.json 失败(向导仍可读 .env.prod 环境变量)"
fi
}
sync_1panel_service_hosts() {
local root="$1"
local env_file="$root/$ENV_PROD"
local detect="$root/deploy/detect-1panel-services.sh"
[[ -f "$env_file" ]] || return 0
if ! has_1panel_network; then
warn "未找到 1panel-network — 请先在 1Panel 应用商店安装 MySQL/Redis"
return 0
fi
if [[ ! -x "$detect" ]]; then
@@ -215,7 +242,11 @@ sync_1panel_service_hosts() {
warn "未找到 detect-1panel-services.sh,跳过 1Panel 容器名探测"
return 0
fi
local line mysql_host redis_host
local line mysql_host="" redis_host="" detect_out
if ! detect_out="$("$detect" 2>&1)"; then
warn "1Panel 容器探测: ${detect_out:- MySQL/Redis 容器运行}"
return 0
fi
while IFS= read -r line; do
case "$line" in
NEXUS_1PANEL_DB_HOST=*)
@@ -230,7 +261,8 @@ sync_1panel_service_hosts() {
info "1Panel Redis 容器: $redis_hostNEXUS_REDIS_URL 已同步)"
;;
esac
done < <("$detect" 2>/dev/null || true)
done <<<"$detect_out"
write_1panel_hosts_volume "$mysql_host" "$redis_host"
}
compose_cmd() {
@@ -852,6 +884,7 @@ cmd_upgrade() {
apply_pool_to_env_prod "$NEXUS_ROOT" "$(profile_env_file "$NEXUS_ROOT")"
save_host_profile "$NEXUS_ROOT"
purge_legacy_bundled_services "$NEXUS_ROOT"
sync_1panel_service_hosts "$NEXUS_ROOT"
if [[ "$NO_CACHE" == true ]]; then
step "无缓存重建 Nexus 镜像..."
compose_cmd "$NEXUS_ROOT" build --no-cache nexus