/** * CLB aideiver: single H5 + single API only (machines for extras stay up, just off CLB). * H5 = test-h5:80 * API = test-api:3001 * * Design: docs/design/20260909-prod-single-h5-api.md * * Usage: * node server/scripts/tencent-clb-single-backend.mjs * node server/scripts/tencent-clb-single-backend.mjs --with-dns # also rewrite DNS to CLB * * Prefer: node server/scripts/tencent-clb-rolling.mjs ensure-single */ import { loadTencentCredentials, tc3Request } from './tencent-lib.mjs'; const REGION = 'ap-nanjing'; const LB_ID = 'lb-3g4yhjra'; const CLB_CNAME = 'lb-3g4yhjra-4f9thqng7wo1xvae.clb.nj-tencentclb.net'; const CLB_VIP = '129.211.237.222'; const KEEP_H5 = { InstanceId: 'ins-9zdly5wm', Port: 80, Weight: 10 }; // test-h5 const KEEP_API = { InstanceId: 'ins-dn3v0znu', Port: 3001, Weight: 10 }; // test-api function sleep(ms) { return new Promise((r) => setTimeout(r, ms)); } async function clb(cred, action, payload) { let last; for (let i = 1; i <= 12; i++) { const json = await tc3Request({ ...cred, service: 'clb', region: REGION, version: '2018-03-17', action, payload, }); if (!json.Response?.Error) return json.Response; const e = json.Response.Error; last = new Error(`${action}: ${e.Code} ${e.Message}`); if (/ResourceInOperating|try later|try again/i.test(`${e.Code} ${e.Message}`)) { console.warn(` busy ${i}/12`); await sleep(5000); continue; } throw last; } throw last; } async function dns(cred, action, payload) { const json = await tc3Request({ ...cred, service: 'dnspod', region: 'ap-guangzhou', version: '2021-03-23', action, payload, }); if (json.Response?.Error) { throw new Error(`${action}: ${json.Response.Error.Code} ${json.Response.Error.Message}`); } return json.Response; } function wantKey(t) { return `${t.InstanceId}:${t.Port}`; } async function syncLocationTargets(cred, listenerId, locationId, label, keep) { const desc = await clb(cred, 'DescribeTargets', { LoadBalancerId: LB_ID, ListenerIds: [listenerId] }); const loc = (desc.Listeners || []) .flatMap((l) => l.Rules || []) .find((r) => r.LocationId === locationId); const current = loc?.Targets || []; const keepKey = wantKey(keep); const extra = current.filter((t) => wantKey(t) !== keepKey); const hasKeep = current.some((t) => wantKey(t) === keepKey); if (extra.length) { console.log(`Deregister extra on ${label}:`, extra.map(wantKey)); await clb(cred, 'DeregisterTargets', { LoadBalancerId: LB_ID, ListenerId: listenerId, LocationId: locationId, Targets: extra.map((t) => ({ Type: 'CVM', InstanceId: t.InstanceId, Port: Number(t.Port), })), }); await sleep(4000); } if (!hasKeep) { console.log(`Register keep on ${label}:`, keepKey); await clb(cred, 'RegisterTargets', { LoadBalancerId: LB_ID, ListenerId: listenerId, LocationId: locationId, Targets: [{ Type: 'CVM', ...keep }], }); await sleep(4000); } else { console.log(`Keep OK on ${label}:`, keepKey); } } async function main() { const withDns = process.argv.includes('--with-dns'); const { secretId, secretKey } = loadTencentCredentials(); const cred = { secretId, secretKey }; const targets = await clb(cred, 'DescribeTargets', { LoadBalancerId: LB_ID }); for (const l of targets.Listeners || []) { if (l.Protocol !== 'HTTPS') continue; for (const r of l.Rules || []) { const label = `${r.Domain}${r.Url}`; if (r.Url === '/' || r.Url === '') { await syncLocationTargets(cred, l.ListenerId, r.LocationId, label, KEEP_H5); } else if (r.Url === '/api' || r.Url.startsWith('/api')) { await syncLocationTargets(cred, l.ListenerId, r.LocationId, label, KEEP_API); } } } if (withDns) { const list = await dns(cred, 'DescribeRecordList', { Domain: 'xdjchuanhui.com', Limit: 100 }); const www = (list.RecordList || []).find((r) => r.Name === 'www' && r.Type !== 'NS'); const apex = (list.RecordList || []).find((r) => r.Name === '@' && r.Type !== 'NS'); if (www) { console.log('DNS www -> CNAME', CLB_CNAME); await dns(cred, 'ModifyRecord', { Domain: 'xdjchuanhui.com', RecordId: www.RecordId, SubDomain: 'www', RecordType: 'CNAME', RecordLine: '默认', Value: CLB_CNAME, TTL: 600, }); } if (apex) { console.log('DNS @ -> A', CLB_VIP); await dns(cred, 'ModifyRecord', { Domain: 'xdjchuanhui.com', RecordId: apex.RecordId, SubDomain: '@', RecordType: 'A', RecordLine: '默认', Value: CLB_VIP, TTL: 600, }); } } else { console.log('Skip DNS (pass --with-dns to rewrite).'); } const after = await clb(cred, 'DescribeTargets', { LoadBalancerId: LB_ID }); const summary = []; for (const l of after.Listeners || []) { if (l.Protocol !== 'HTTPS') continue; for (const r of l.Rules || []) { summary.push({ rule: `${r.Domain}${r.Url}`, targets: (r.Targets || []).map((t) => `${t.InstanceId}:${t.Port}`), }); } } console.log(JSON.stringify({ clbTargets: summary }, null, 2)); } main().catch((e) => { console.error(e); process.exit(1); });