Files
SupplierSystem/server/scripts/tencent-clb-single-backend.mjs
T
Ubuntu deb3e72750
CI / server typecheck + tests (push) Canceled after 0s
CI / web typecheck + tests + miniprogram syntax (push) Canceled after 0s
CI / eslint (观察项,C7 骨架) (push) Canceled after 0s
CI / npm audit (critical 硬拦) (push) Canceled after 0s
chore: 以 AI 生鲜配送系统 当前版本作为 SupplierSystem 起点
来源仓库:jxc/ai-fresh-delivery
来源提交:f0b1a180e2deb7e4673b9d1c8542619f7bc0f73d
快照树:  09f679334ff38f0ff25e907961c94a8223bcaedd(与来源 HEAD 工作树逐字节一致,3801 个文件)

按约定本仓库不带任何历史提交:来源仓库的历史中含真实凭据
(生产 SSH 私钥 deploy/id_rsa_tencent、server/.env、.env.tencent/.env.production/.env.gitea、
deploy/小程序AppSecret.txt,其中 MP_APPSECRET / S3 AccessKey 经哈希比对与线上同值),
整包推送会把这些凭据永久带入本仓库,故只取「当前版本」的单提交快照。
供应商系统后续开发自本提交开始。
2026-10-08 17:35:43 +08:00

177 lines
5.3 KiB
JavaScript

/**
* 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);
});