|
| 1 | +const { cns } = require('tencent-cloud-sdk') |
| 2 | +class CnsUtils { |
| 3 | + constructor(credentials = {}, region = 'ap-guangzhou') { |
| 4 | + this.region = region |
| 5 | + this.credentials = credentials |
| 6 | + this.cnsClient = new cns(this.credentials) |
| 7 | + } |
| 8 | + |
| 9 | + haveRecord(newRecord, historyRcords) { |
| 10 | + for (let i = 0; i < historyRcords.length; i++) { |
| 11 | + if (newRecord.recordType == 'CNAME' && newRecord.value.slice(-1) != '.') { |
| 12 | + newRecord.value = `${newRecord.value}.` |
| 13 | + } |
| 14 | + if ( |
| 15 | + newRecord.domain == historyRcords[i].domain && |
| 16 | + newRecord.subDomain == historyRcords[i].subDomain && |
| 17 | + newRecord.recordType == historyRcords[i].recordType && |
| 18 | + newRecord.value == historyRcords[i].value && |
| 19 | + newRecord.recordLine == historyRcords[i].recordLine |
| 20 | + ) { |
| 21 | + return historyRcords[i] |
| 22 | + } |
| 23 | + } |
| 24 | + return false |
| 25 | + } |
| 26 | + |
| 27 | + async deploy(inputs = {}) { |
| 28 | + // 准备一些临时参数 |
| 29 | + const recordList = [] |
| 30 | + const recordRelease = [] |
| 31 | + let domainLength = 100 |
| 32 | + let offset = 0 |
| 33 | + const output = { records: [] } |
| 34 | + |
| 35 | + // 获取线上的域名记录列表 |
| 36 | + console.log(`Getting release domain records ... `) |
| 37 | + try { |
| 38 | + while (domainLength == 100) { |
| 39 | + const statusInputs = { |
| 40 | + Action: 'RecordList', |
| 41 | + Region: this.region, |
| 42 | + offset: offset, |
| 43 | + length: domainLength, |
| 44 | + domain: inputs.domain |
| 45 | + } |
| 46 | + let recordReleaseList = await this.cnsClient.request(statusInputs) |
| 47 | + if (recordReleaseList.code != 0) { |
| 48 | + // 如果没找到Domain,则尝试添加Domain |
| 49 | + try { |
| 50 | + console.log(`Get release domain error.`) |
| 51 | + console.log(`Adding domain ...`) |
| 52 | + await this.cnsClient.request({ |
| 53 | + Action: 'DomainCreate', |
| 54 | + Region: this.region, |
| 55 | + domain: inputs.domain |
| 56 | + }) |
| 57 | + console.log(`Added domain`) |
| 58 | + } catch (e) { |
| 59 | + // Domain添加错误,不影响主流程,继续尝试后面的工作 |
| 60 | + console.log(`Add domain error`) |
| 61 | + console.log(`Trying to deploy ...`) |
| 62 | + } |
| 63 | + break |
| 64 | + } |
| 65 | + recordReleaseList = recordReleaseList['data'] |
| 66 | + if (recordReleaseList['records']) { |
| 67 | + for (let i = 0; i < recordReleaseList['records'].length; i++) { |
| 68 | + recordRelease.push({ |
| 69 | + domain: inputs.domain, |
| 70 | + subDomain: recordReleaseList['records'][i].name, |
| 71 | + recordType: recordReleaseList['records'][i].type, |
| 72 | + value: recordReleaseList['records'][i].value, |
| 73 | + recordId: recordReleaseList['records'][i].id, |
| 74 | + mx: recordReleaseList['records'][i].mx, |
| 75 | + ttl: recordReleaseList['records'][i].ttl, |
| 76 | + recordLine: recordReleaseList['records'][i].line |
| 77 | + }) |
| 78 | + } |
| 79 | + domainLength = recordReleaseList['records'].length |
| 80 | + } else { |
| 81 | + domainLength = 0 |
| 82 | + } |
| 83 | + offset = offset + 1 |
| 84 | + } |
| 85 | + console.log(`Getted release domain.`) |
| 86 | + } catch (e) {} |
| 87 | + |
| 88 | + const records = [] |
| 89 | + for (let recordNum = 0; recordNum < inputs.records.length; recordNum++) { |
| 90 | + const tempSubDomain = |
| 91 | + typeof inputs.records[recordNum].subDomain == 'string' |
| 92 | + ? [inputs.records[recordNum].subDomain] |
| 93 | + : inputs.records[recordNum].subDomain |
| 94 | + const tempRecordLine = |
| 95 | + typeof inputs.records[recordNum].recordLine == 'string' |
| 96 | + ? [inputs.records[recordNum].recordLine] |
| 97 | + : inputs.records[recordNum].recordLine |
| 98 | + |
| 99 | + for (let subDomainNum = 0; subDomainNum < tempSubDomain.length; subDomainNum++) { |
| 100 | + for (let recordLineNum = 0; recordLineNum < tempRecordLine.length; recordLineNum++) { |
| 101 | + const tempRecord = JSON.parse(JSON.stringify(inputs.records[recordNum])) |
| 102 | + tempRecord.subDomain = tempSubDomain[subDomainNum] |
| 103 | + tempRecord.recordLine = tempRecordLine[recordLineNum] |
| 104 | + records.push(tempRecord) |
| 105 | + } |
| 106 | + } |
| 107 | + } |
| 108 | + |
| 109 | + // 增加/修改记录 |
| 110 | + console.log(`Doing action about domain records ... `) |
| 111 | + for (let recordNum = 0; recordNum < records.length; recordNum++) { |
| 112 | + const tempInputs = JSON.parse(JSON.stringify(records[recordNum])) |
| 113 | + tempInputs.domain = inputs.domain |
| 114 | + tempInputs.Region = this.region |
| 115 | + if (!tempInputs.status) { |
| 116 | + tempInputs.status = 'enable' // 设置默认值 |
| 117 | + } |
| 118 | + console.log(`Resolving ${tempInputs.subDomain} - ${tempInputs.value}`) |
| 119 | + const releseHistory = this.haveRecord(tempInputs, recordRelease) |
| 120 | + if (tempInputs.recordId || releseHistory) { |
| 121 | + // 修改 |
| 122 | + if (!tempInputs.recordId) { |
| 123 | + tempInputs.recordId = releseHistory.recordId |
| 124 | + } |
| 125 | + tempInputs.recordId = Number(tempInputs.recordId) |
| 126 | + console.log(`Modifying (recordId is ${tempInputs.recordId})... `) |
| 127 | + tempInputs.Action = 'RecordModify' |
| 128 | + try { |
| 129 | + const modifyResult = await this.cnsClient.request(tempInputs) |
| 130 | + if (modifyResult.code != 0) { |
| 131 | + throw new Error(JSON.stringify(modifyResult)) |
| 132 | + } |
| 133 | + } catch (e) { |
| 134 | + throw e |
| 135 | + } |
| 136 | + console.log(`Modified (recordId is ${tempInputs.recordId}) `) |
| 137 | + } else { |
| 138 | + // 新建 |
| 139 | + console.log(`Creating ... `) |
| 140 | + tempInputs.Action = 'RecordCreate' |
| 141 | + try { |
| 142 | + let createOutputs = await this.cnsClient.request(tempInputs) |
| 143 | + if (createOutputs.code != 0) { |
| 144 | + throw new Error(JSON.stringify(createOutputs)) |
| 145 | + } |
| 146 | + createOutputs = createOutputs['data'] |
| 147 | + tempInputs.recordId = createOutputs.record.id |
| 148 | + } catch (e) { |
| 149 | + throw e |
| 150 | + } |
| 151 | + console.log(`Created (recordId is ${tempInputs.recordId}) `) |
| 152 | + } |
| 153 | + recordList.push(tempInputs) |
| 154 | + output.records.push({ |
| 155 | + subDomain: tempInputs.subDomain, |
| 156 | + recordType: tempInputs.recordType, |
| 157 | + recordLine: tempInputs.recordLine, |
| 158 | + recordId: tempInputs.recordId, |
| 159 | + value: tempInputs.value, |
| 160 | + status: tempInputs.status, |
| 161 | + domain: inputs.domain |
| 162 | + }) |
| 163 | + // 改状态 |
| 164 | + console.log(`Modifying status to ${tempInputs.status} `) |
| 165 | + const statusInputs = { |
| 166 | + Action: 'RecordStatus', |
| 167 | + Region: this.region, |
| 168 | + domain: inputs.domain, |
| 169 | + recordId: tempInputs.recordId, |
| 170 | + status: tempInputs.status |
| 171 | + } |
| 172 | + try { |
| 173 | + const statusResult = await this.cnsClient.request(statusInputs) |
| 174 | + if (statusResult.code != 0) { |
| 175 | + throw new Error(JSON.stringify(statusResult)) |
| 176 | + } |
| 177 | + } catch (e) { |
| 178 | + throw e |
| 179 | + } |
| 180 | + console.log(`Modified status to ${tempInputs.status} `) |
| 181 | + } |
| 182 | + return output |
| 183 | + } |
| 184 | + |
| 185 | + async remove(inputs = {}) { |
| 186 | + |
| 187 | + const deleteList = inputs.deleteList || [] |
| 188 | + |
| 189 | + if (deleteList.length > 0) { |
| 190 | + console.log( |
| 191 | + `Deleting records which deployed by this project, but not in this records list. ` |
| 192 | + ) |
| 193 | + for (let recordNum = 0; recordNum < deleteList.length; recordNum++) { |
| 194 | + console.log( |
| 195 | + `Deleting record ${deleteList[recordNum].subDomain} ${deleteList[recordNum].recordId} ` |
| 196 | + ) |
| 197 | + const deleteInputs = { |
| 198 | + Action: 'RecordDelete', |
| 199 | + Region: this.region, |
| 200 | + domain: deleteList[recordNum].domain, |
| 201 | + recordId: deleteList[recordNum].recordId |
| 202 | + } |
| 203 | + try { |
| 204 | + const deleteResult = await this.cnsClient.request(deleteInputs) |
| 205 | + if (deleteResult.code != 0) { |
| 206 | + throw new Error(JSON.stringify(deleteResult)) |
| 207 | + } |
| 208 | + } catch (e) { |
| 209 | + throw e |
| 210 | + } |
| 211 | + console.log( |
| 212 | + `Deleted record ${deleteList[recordNum].subDomain} ${deleteList[recordNum].recordId} ` |
| 213 | + ) |
| 214 | + } |
| 215 | + } |
| 216 | + return true |
| 217 | + } |
| 218 | +} |
| 219 | + |
| 220 | +module.exports = CnsUtils |
0 commit comments