-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathalicloud.js
126 lines (112 loc) · 3.25 KB
/
alicloud.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
const path = require('path')
const { promisify } = require('util')
const R = require('ramda')
const Core = require('@alicloud/pop-core')
const FCClient = require('@alicloud/fc2')
const { getProfileFromFile } = require('@alicloud/fun/lib/profile')
const sleep = promisify(setTimeout)
let core
let fc2
let config
async function ensureAlicloud() {
if (!config) {
config = await getProfileFromFile()
}
if (!core) {
core = new Core({
accessKeyId: config.accessKeyId,
accessKeySecret: config.accessKeySecret,
endpoint: 'https://alidns.aliyuncs.com',
apiVersion: '2015-01-09',
})
}
if (!fc2) {
fc2 = new FCClient(config.accountId, {
accessKeyID: config.accessKeyId,
accessKeySecret: config.accessKeySecret,
region: config.defaultRegion,
})
}
}
async function getDomainCommonParams(domain) {
await ensureAlicloud()
const split = domain.split('.')
return {
RegionId: config.defaultRegion,
DomainName: [split.pop(), split.pop()].reverse().join('.'),
RR: split.join('.'),
}
}
async function listFCCustomDomains(domain) {
await ensureAlicloud()
const rs = await fc2.listCustomDomains({ prefix: domain.split('.').shift() })
return R.pathOr([], ['data', 'customDomains'], rs).filter(({ domainName }) => domainName === domain)
}
async function listDomainRecord(domain) {
const common = await getDomainCommonParams(domain)
common.KeyWord = common.RR
const params = {
...common,
PageSize: 500,
}
const rs = await core.request('DescribeDomainRecords', params, { method: 'POST' })
return R.pathOr([], ['DomainRecords', 'Record'], rs)
}
async function deleteDomainRecord(recordId) {
await ensureAlicloud()
const rs = await core.request('DeleteDomainRecord', {
RegionId: config.recordId,
RecordId: recordId,
}, { method: 'POST' })
return rs
}
async function addDomainRecord(domain) {
const params = {
...await getDomainCommonParams(domain),
Type: 'CNAME',
Value: `${config.accountId}.${config.defaultRegion}.fc.aliyuncs.com`,
}
const rs = await core.request('AddDomainRecord', params, { method: 'POST' })
return rs
}
async function ensureFCCustomDomains(domain) {
let rs
console.debug('Ensure custom domain', domain)
rs = await listFCCustomDomains(domain)
if (rs.length) {
console.debug('Domain', domain, 'is ready')
return
}
console.debug('Inspect dns for', domain)
let match
const destDomain = `${config.accountId}.${config.defaultRegion}.fc.aliyuncs.com`
rs = await listDomainRecord(domain)
for (let i = 0; i < rs.length; i += 1) {
const { RR: src, Value: dst, RecordId: recordId } = rs[i]
if (dst !== destDomain) {
console.debug('Removing', recordId, src, '->', dst)
// eslint-disable-next-line no-await-in-loop
console.debug(await deleteDomainRecord(recordId))
} else {
match = recordId
}
}
if (!match) {
console.debug('Create dns for', domain)
rs = await addDomainRecord(domain)
match = rs.RecordId
console.debug('Wait 5s to take effect')
await sleep(5000)
}
console.debug('Create custom domain', domain)
rs = await fc2.createCustomDomain(domain)
console.debug(domain, 'is ready')
if (match) {
console.debug('Remove', destDomain, 'for', domain)
await deleteDomainRecord(match)
}
}
module.exports = {
ensureFCCustomDomains,
funExec: path.join(__dirname, '..', 'node_modules', '.bin', 'fun'),
}