-
Notifications
You must be signed in to change notification settings - Fork 51
/
Copy pathns.sync.js
111 lines (103 loc) · 3.07 KB
/
ns.sync.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
const core = require("@actions/core");
const cloudflare = require("cloudflare")({
token: process.env["CLOUDFLARE_DNS_TOKEN"]
});
const zone_name = ".rundocs.io";
const zone_id = process.env["CLOUDFLARE_ZONE_ID"];
const data = require("./ns.js");
function check(resp) {
if (resp.success) {
return resp;
} else {
throw new Error(resp.messages);
}
}
function fixed(name) {
if (name != zone_name.replace(/^\./, "")) {
name = name + zone_name;
}
return name;
}
function ns(type, proxied, resp, source) {
let job = {
create: [],
update: [],
remove: []
};
for (let name in source) {
let [exist, update] = [false, false];
for (let record of resp.result) {
if (type == record.type) {
if (name == record.name.replace(zone_name, "")) {
if (source[name] != record.content) {
console.log(name, source[name], record.content)
update = record.id;
}
exist = true;
}
}
}
if (exist) {
if (update) {
job.update.push({
name: fixed(name),
id: update,
content: source[name],
type: type,
proxied: proxied
});
}
} else {
job.create.push({
name: fixed(name),
content: source[name],
type: type,
proxied: proxied
});
}
}
// delete if it does not exist in the "source"
for (let record of resp.result) {
if (record.type == type) {
let remove = true;
for (let name in source) {
if (name == record.name.replace(zone_name, "")) {
remove = false;
}
}
if (remove) {
job.remove.push({
name: record.name,
id: record.id,
content: record.content,
type: record.type,
proxied: record.proxied
});
}
}
}
return job;
}
cloudflare.dnsRecords.browse(zone_id)
.then(check)
.then(function(resp) {
return {
cname: ns("CNAME", true, resp, data.cname),
txt: ns("TXT", false, resp, data.txt),
}
})
.then(async function(jobs) {
for (let type in jobs) {
for (let record of jobs[type].create) {
check(await cloudflare.dnsRecords.add(zone_id, record));
}
for (let record of jobs[type].update) {
check(await cloudflare.dnsRecords.edit(zone_id, record.id, record));
}
for (let record of jobs[type].remove) {
check(await cloudflare.dnsRecords.del(zone_id, record.id));
}
}
console.log(JSON.stringify(jobs, null, 2));
})
.catch((e) => core.setFailed(e.message));