Commit b7b25e16 authored by nanahira's avatar nanahira

check cdn mapping nodes

parent 8aee42aa
Pipeline #330 failed with stages
in 2 minutes and 19 seconds
......@@ -47,13 +47,15 @@ interface DomainRecord {
Remark: string;
}
let config: Config;
interface DomainRecordInfo {
record: DomainRecord;
port: number;
isCDN: boolean;
good: boolean;
}
let config: Config;
const requestOption = {
method: "POST"
}
......@@ -73,12 +75,26 @@ class Checker {
private message(msg: string) {
console.log(`${this.id} => ${msg}`);
}
getRecordPrefix(m: DomainRecord) {
if (!m.Value.endsWith(this.config.domain)) {
return null;
}
return m.Value.slice(0, m.Value.length - 1 - this.config.domain.length);
}
isCDNRecord(m: DomainRecord) {
const valuePrefix = this.getRecordPrefix(m);
return valuePrefix && _.any(this.cdnRecordsRegex, r => !!valuePrefix.match(r));
}
getRecordPattern(recordInfo: DomainRecordInfo) {
const record = recordInfo.record;
return `${record.RR}.${this.config.domain} => ${record.Value}:${recordInfo.port}`;
}
async getRecords(): Promise<DomainRecordInfo[]> {
this.message(`Fetching domain records of ${config.domain}.`)
this.message(`Fetching domain records of ${this.config.domain}.`)
const res: DomainRecordInfo[] = [];
for (let i = 1; ; ++i) {
const ret: DomainRecordReturnResult = await this.client.request("DescribeDomainRecords", {
DomainName: config.domain,
DomainName: this.config.domain,
PageNumber: i,
PageSize: 500,
}, requestOption);
......@@ -87,17 +103,12 @@ class Checker {
break;
}
for (let record of ret.DomainRecords.Record.filter(m => {
return m.RR && m.Type === "CNAME" && _.any(this.cdnRecordsRegex, r => !!m.RR.match(r)) && _.every(this.cdnRecordsRegex, r => {
if (!m.Value.endsWith(config.domain)) {
return true;
}
const valuePrefix = m.Value.slice(0, m.Value.length - 1 - config.domain.length);
return !valuePrefix.match(r);
});
return m.RR && m.Type === "CNAME" && _.any(this.cdnRecordsRegex, r => !!m.RR.match(r))
})) {
const port = _.find(config.cdnRecords, r => record.RR.match(r.match)).port;
this.message(`Found record ${record.RR}.${config.domain} => ${record.Value}:${port}.`);
res.push({record, port});
const port = _.find(this.config.cdnRecords, r => record.RR.match(r.match)).port;
const isCDN = this.isCDNRecord(record);
this.message(`Found record ${record.RR}.${this.config.domain} => ${record.Value}:${port}.`);
res.push({record, port, isCDN, good: false});
}
}
return res;
......@@ -127,11 +138,27 @@ class Checker {
}
async checkRecord(recordInfo: DomainRecordInfo) {
const record = recordInfo.record;
this.message(`Checking record ${record.RR}.${this.config.domain} ${record.Value}:${recordInfo.port} with old status of ${record.Status}.`)
this.message(`Checking record ${this.getRecordPattern(recordInfo)} with old status of ${record.Status}.`)
const good = await this.checkNode(record.Value, recordInfo.port);
await this.handleRecordResult(recordInfo, good);
}
async checkCDNRecord(recordInfo: DomainRecordInfo, nonCDNRecords: DomainRecordInfo[]) {
const record = recordInfo.record;
this.message(`Checking CDN record ${this.getRecordPattern(recordInfo)} with old status of ${record.Status}.`)
const valuePrefix = this.getRecordPrefix(record);
const good = _.any(nonCDNRecords, r => {
return r.record.RR === valuePrefix && r.good;
});
this.message(`CDN Record ${this.getRecordPattern(recordInfo)} is ${good ? "good" : "bad"}.`);
await this.handleRecordResult(recordInfo, good);
}
async handleRecordResult(recordInfo: DomainRecordInfo, good: boolean) {
const record = recordInfo.record;
const status = record.Status;
const targetStatus = (await this.checkNode(record.Value, recordInfo.port)) ? "ENABLE" : "DISABLE";
recordInfo.good = good;
const targetStatus = good ? "ENABLE" : "DISABLE";
if (status != targetStatus) {
this.message(`Changing record status of ${record.RR}.${this.config.domain} ${record.Value}:${recordInfo.port} from ${status} to ${targetStatus}.`);
this.message(`Changing record status of ${this.getRecordPattern(recordInfo)} from ${status} to ${targetStatus}.`);
await this.client.request("SetDomainRecordStatus", {
RecordId: record.RecordId,
Status: targetStatus
......@@ -141,9 +168,14 @@ class Checker {
async start() {
this.message(`Started.`);
const records = await this.getRecords();
await Promise.all(records.map(r => {
const nonCDNRecords = records.filter(m => !m.isCDN);
const CDNRecords = records.filter(m => !!m.isCDN);
await Promise.all(nonCDNRecords.map(r => {
return this.checkRecord(r);
}));
await Promise.all(CDNRecords.map(r => {
return this.checkCDNRecord(r, nonCDNRecords);
}));
this.message(`Finished.`);
}
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment