import fs from 'fs'; import path from 'path'; import { fileURLToPath } from 'url'; import { loadTencentCredentials, runShellCommand, probeHttp, testLogin, } from './tencent-lib.mjs'; const __dirname = path.dirname(fileURLToPath(import.meta.url)); const root = path.join(__dirname, '..', '..'); const DIAG_SCRIPT = `#!/bin/bash set +e echo "===== HOST =====" hostname uname -a echo "===== UPTIME =====" uptime echo "===== DOCKER =====" docker ps -a 2>&1 echo "===== COMPOSE FILES =====" find /root /home /opt /var/www -maxdepth 4 -name 'docker-compose*.yml' 2>/dev/null | head -10 echo "===== LISTEN PORTS =====" ss -tlnp 2>/dev/null || netstat -tlnp 2>/dev/null echo "===== NGINX =====" nginx -v 2>&1 ls -la /etc/nginx/sites-enabled/ /etc/nginx/conf.d/ 2>&1 echo "===== NODE PROCESSES =====" ps aux | grep -E 'node|nginx|docker' | grep -v grep echo "===== LOCAL HEALTH =====" curl -sS -m 5 http://127.0.0.1:3001/api/health 2>&1 curl -sS -m 5 http://127.0.0.1:8080/api/health 2>&1 curl -sS -m 5 http://127.0.0.1/api/health 2>&1 echo "===== DEPLOY DIRS =====" ls -la /root 2>&1 | head -20 `; const NGINX_CONF = fs.readFileSync(path.join(root, 'deploy', 'nginx.api-proxy.conf'), 'utf8'); const SETUP_NGINX = fs.readFileSync(path.join(root, 'scripts', 'setup-prod-api-nginx.sh'), 'utf8'); function buildNginxInstallScript() { const confB64 = Buffer.from(NGINX_CONF, 'utf8').toString('base64'); const setupB64 = Buffer.from(SETUP_NGINX, 'utf8').toString('base64'); return `#!/bin/bash set -e echo "$confB64" | base64 -d > /tmp/nginx.api-proxy.conf echo "$setupB64" | base64 -d > /tmp/setup-prod-api-nginx.sh chmod +x /tmp/setup-prod-api-nginx.sh bash /tmp/setup-prod-api-nginx.sh /tmp/nginx.api-proxy.conf curl -sS -m 5 http://127.0.0.1/api/health || true `; } const MIGRATE_SCRIPT = `#!/bin/bash set -e echo "===== FIND APP CONTAINER =====" CID=$(docker ps --format '{{.Names}}' | grep -E 'canteen|app|api' | head -1) if [ -z "$CID" ]; then echo "No docker app container; try bare node" APP_DIR=$(find /root /home /opt -maxdepth 4 -type d -name server 2>/dev/null | head -1) if [ -n "$APP_DIR" ] && [ -f "$APP_DIR/package.json" ]; then cd "$(dirname "$APP_DIR")" npm run migrate 2>&1 || (cd server && npm run migrate 2>&1) else echo "Cannot locate app for migrate" exit 1 fi else echo "Container: $CID" docker exec "$CID" node dist/migrate.js 2>&1 || docker exec "$CID" sh -c 'cd /app/server && node dist/migrate.js' 2>&1 fi echo "===== LOGIN TEST LOCAL =====" curl -sS -m 8 -X POST http://127.0.0.1:3001/api/auth/token \\ -H 'Content-Type: application/json' \\ -d '{"email":"superadmin@meoo.local","password":"SuperAdmin@123"}' 2>&1 | head -c 400 echo curl -sS -m 8 -X POST http://127.0.0.1/api/auth/token \\ -H 'Content-Type: application/json' \\ -d '{"email":"superadmin@meoo.local","password":"SuperAdmin@123"}' 2>&1 | head -c 400 echo `; async function main() { const { secretId, secretKey, env } = loadTencentCredentials(); const cred = { secretId, secretKey }; const region = env.TENCENT_REGION || 'ap-nanjing'; const h5Id = env.TENCENT_PROD_H5_INSTANCE || 'ins-9zdly5wm'; const apiId = env.TENCENT_PROD_API_INSTANCE || 'ins-dn3v0znu'; const h5Ip = env.TENCENT_PROD_H5_IP || '119.45.34.106'; const apiIp = env.TENCENT_PROD_API_IP || '146.56.223.139'; console.log('=== 1) 生产 HTTP 探测 ===\n'); for (const [label, base] of [ ['test-h5', `http://${h5Ip}`], ['test-api:3001', `http://${apiIp}:3001`], ['test-api:80', `http://${apiIp}`], ]) { console.log(`--- ${label} ---`); const health = await probeHttp(base); health.forEach((h) => console.log(JSON.stringify(h))); const login = await testLogin(`${base}/api`); console.log('login:', JSON.stringify(login)); console.log(''); } console.log('=== 2) TAT 远程诊断 test-h5 ===\n'); try { const h5 = await runShellCommand(cred, region, [h5Id], DIAG_SCRIPT, 120); console.log(`state=${h5.state}\n${h5.output}`); if (h5.errOut) console.log('err:', h5.errOut); } catch (err) { console.error('test-h5 TAT failed:', err.message); } console.log('\n=== 3) TAT 远程诊断 test-api ===\n'); try { const api = await runShellCommand(cred, region, [apiId], DIAG_SCRIPT, 120); console.log(`state=${api.state}\n${api.output}`); if (api.errOut) console.log('err:', api.errOut); } catch (err) { console.error('test-api TAT failed:', err.message); } console.log('\n=== 4) test-api 配置 Nginx 反代 /api → 3001 ===\n'); try { const nginx = await runShellCommand(cred, region, [apiId], buildNginxInstallScript(), 180); console.log(`state=${nginx.state}\n${nginx.output}`); if (nginx.errOut) console.log('err:', nginx.errOut); } catch (err) { console.error('nginx setup failed:', err.message); } console.log('\n=== 5) 生产 bcrypt 迁移 + 容器内登录验证 ===\n'); for (const [label, id] of [ ['test-h5', h5Id], ['test-api', apiId], ]) { console.log(`--- ${label} ---`); try { const mig = await runShellCommand(cred, region, [id], MIGRATE_SCRIPT, 180); console.log(`state=${mig.state}\n${mig.output}`); } catch (err) { console.error(`${label} migrate failed:`, err.message); } console.log(''); } console.log('=== 6) 反代后再次探测 test-api:80 ===\n'); const after = await probeHttp(`http://${apiIp}`, ['/api/health', '/api/auth/token']); after.forEach((h) => console.log(JSON.stringify(h))); const loginAfter = await testLogin(`http://${apiIp}/api`); console.log('login:', JSON.stringify(loginAfter)); } main().catch((err) => { console.error(err); process.exit(1); });