This repository was archived by the owner on Oct 12, 2023. It is now read-only.
forked from aave/aave-ui
-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathupdate-cloudflare.js
More file actions
79 lines (72 loc) · 2.36 KB
/
update-cloudflare.js
File metadata and controls
79 lines (72 loc) · 2.36 KB
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
const axios = require('axios');
const updateOrCreateRecord = async (name, type, _payload) => {
if (!process.env.CF_API_TOKEN || !process.env.CF_ZONE_ID) {
throw new Error('CF_API_TOKEN or CF_ZONE_ID were not specified');
}
const cfClient = axios.create({
headers: { Authorization: `Bearer ${process.env.CF_API_TOKEN}` },
baseURL: `https://api.cloudflare.com/client/v4/zones/${process.env.CF_ZONE_ID}/dns_records`,
});
const { data: zoneData } = await cfClient.get('/', {
params: { name, type },
});
const payload = {
name,
type,
..._payload,
};
if (zoneData.success) {
if (zoneData.result.length) {
if (zoneData.result.length > 1) {
throw new Error(`Some inconsistency in ${name} ${type} record`);
}
const record = zoneData.result[0];
const { data: recordUpdateData } = await cfClient.put(`/${record.id}`, payload);
if (!recordUpdateData.success) {
throw new Error(
`${name} ${type} record was not updated with errors: ${recordUpdateData.errors}`
);
}
} else {
try {
const { data: recordCreateData } = await cfClient.post(`/`, payload);
if (!recordCreateData.success) {
throw new Error(
`${name} ${type} record was not created with errors: ${recordCreateData.errors}`
);
}
} catch (e) {
console.log('e', e.response.data.errors[0]);
throw e;
}
}
} else {
throw new Error(`request to get ${name} ${type} zone info failed with ${zoneData.errors}`);
}
};
const updateCloudFlareRecord = async (hash, domain) => {
console.log(`domain to update - https://${domain}`);
if (domain != 'app.aave.com') {
console.log('updating CNAME record');
await updateOrCreateRecord(domain, 'CNAME', {
content: `cloudflare-ipfs.com`,
});
}
console.log('updating dns link record');
await updateOrCreateRecord(`_dnslink.${domain}`, 'TXT', {
content: `dnslink=/ipfs/${hash}/`,
});
console.log('done');
};
const publish = async () => {
const domain = process.env.CF_DEPLOYMENT_DOMAIN;
const hash = process.env.HASH;
if (domain && hash) {
console.log(`trying to update DNS for ${domain} with ${hash}`);
await updateCloudFlareRecord(hash, domain);
} else {
console.log('no cloudflare domain specified, skipping DNS update');
}
process.exit(0);
};
publish();