#!/usr/bin/env bash # 切换 A 步(在 47 上跑): # ① 备份 /opt/dshs/lib → /opt/dsh/backups/lib-/ # ② 覆盖 /opt/dshs/lib(T08 集群版代码) # ③ 装 **本地 Worker**(w-47,19100,无隧道 —— Manager 同机直连) # ④ 把既有用户(admin/guest)的归属**预置**为 w-47(否则粘性落点无处可粘、新老用户会被按容量随机调度) # ⑤ 在 PG 里注册 w-47 / w-106 两台 worker set -uo pipefail TS=$(date +%Y%m%d-%H%M%S) W47_TOKEN="dshs-worker-47-c4b7e19f" W106_TOKEN="dshs-worker-7f3a91c05e" PGURL="postgres://dshs:dshs_cluster_2026@127.0.0.1:15432/dshs" TARBALL=/tmp/dshs-lib-new.tgz echo "=== ① 备份 /opt/dshs/lib ===" mkdir -p "/opt/dsh/backups/lib-$TS" cp -a /opt/dshs/lib "/opt/dsh/backups/lib-$TS/lib" && echo " ✓ 备份到 /opt/dsh/backups/lib-$TS/lib($(find /opt/dsh/backups/lib-$TS -type f | wc -l) 文件)" echo "=== ② 覆盖 lib ===" [ -f "$TARBALL" ] || { echo " ✗ 缺少 $TARBALL"; exit 1; } rm -rf /opt/dshs/lib && tar -xzf "$TARBALL" -C /opt/dshs echo " ✓ 已覆盖;cluster 特征检查: $(grep -l "DEPLOY_MODE" /opt/dshs/lib/config.js >/dev/null 2>&1 && echo '有 cluster 代码 ✓' || echo '✗ 未见 cluster 代码')" echo " lease/agent/tunnel: $(ls /opt/dshs/lib/supervisor/lease.js /opt/dshs/lib/worker/agent.js /opt/dshs/lib/worker/tunnel.js 2>/dev/null | wc -l)/3" echo "=== ③ 本地 Worker 单元(w-47,无隧道) ===" cat > /etc/dshs-worker.env < /etc/systemd/system/dshs-worker.service </dev/null 2>&1 systemctl restart dshs-worker; sleep 5 echo " dshs-worker=$(systemctl is-active dshs-worker) healthz=$(curl -s -m 6 http://127.0.0.1:19100/healthz | head -c 120)" echo "=== ④ 既有用户归属预置为 w-47(粘性锚点) ===" PGPASSWORD=dshs_cluster_2026 /usr/bin/psql -h 127.0.0.1 -p 15432 -U dshs -d dshs -tAc \ "insert into dsh_instances (id, user_id, role, status, host_id, epoch, heartbeat_at, lease_until) select 'dsh-'||id, id, 'main', 'stopped', 'w-47', 0, 0, 0 from users on conflict (id) do update set host_id='w-47', lease_until=0" 2>&1 | tail -1 PGPASSWORD=dshs_cluster_2026 /usr/bin/psql -h 127.0.0.1 -p 15432 -U dshs -d dshs -tAc \ "select u.username||' -> '||coalesce(i.host_id,'NULL') from users u left join dsh_instances i on i.user_id=u.id" 2>&1 | sed 's/^/ /' echo "=== ⑤ 注册两台 worker ===" PGPASSWORD=dshs_cluster_2026 /usr/bin/psql -h 127.0.0.1 -p 15432 -U dshs -d dshs -tAc \ "insert into dsh_hosts (id, endpoint, agent_token, capacity_mb, used_mb, status) values ('w-47','http://127.0.0.1:19100','$W47_TOKEN',1024,0,'up'), ('w-106','http://127.0.0.1:19000','$W106_TOKEN',2560,0,'up') on conflict (id) do update set endpoint=excluded.endpoint, agent_token=excluded.agent_token, capacity_mb=excluded.capacity_mb, status='up'" 2>&1 | tail -1 PGPASSWORD=dshs_cluster_2026 /usr/bin/psql -h 127.0.0.1 -p 15432 -U dshs -d dshs -tAc \ "select id||' cap='||capacity_mb||' status='||status||' ep='||endpoint from dsh_hosts order by id" 2>&1 | sed 's/^/ /' echo "=== 回滚剧本(现在就记下) ===" echo " rm -f /etc/systemd/system/dshs.service.d/cluster.conf && systemctl daemon-reload && \\" echo " systemctl restart dshs # 回 SQLite 单机;lib 回滚 = cp -a /opt/dsh/backups/lib-$TS/lib /opt/dshs/lib" echo " 备份时间戳: $TS"