-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdns-records.ts
executable file
·154 lines (135 loc) · 4.88 KB
/
dns-records.ts
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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
#!/usr/bin/env -S deno run -A
// SPDX-License-Identifier: MPL-2.0
import {program} from "commander";
import which from "https://esm.sh/[email protected]"
import * as path from "jsr:@std/path";
const gitRoot = path.fromFileUrl(new URL("..", import.meta.url).href)
const whereCli = path.resolve(which.sync("pipenv"))
const configFile = `${gitRoot}octodns-config.yml`
const dnsRecordsYamlDir = `${gitRoot}dns`
let stderrLog: string
let stdoutLog: string
program.description("a Deno script to manage DNS records easily over octodns")
program.command("import")
.aliases(["dump"])
.description("dump DNS records from DNS hosting providers")
.argument("<domain>", "Domain name to extract DNS records from the provider API")
.argument("[provider]", "DNS nameserver provider name", "cf")
.action(async(domain: string, provider: string) => {
const addTrailingDot = (domain: string) => {
if (domain == "*") {
return "'*'"
} else {
return `${domain}.`
}
}
const args = [
"run",
"dns-export"
]
args.push(addTrailingDot(domain),`${provider || "cf"}`)
console.log(`trying to exec ${whereCli} ${args.join(" ")}`)
const ops = new Deno.Command(whereCli, {
args,
env: {
"PIPENV_DONT_LOAD_ENV": "1"
},
stderr: "piped",
stdout: "piped"
}).spawn()
const stdoutStreamReader = ops.stdout.pipeThrough(new TextDecoderStream()).getReader()
const stderrStreamReader = ops.stderr.pipeThrough(new TextDecoderStream()).getReader()
while (true) {
const {value: stdoutData, done: stdoutDone} = await stdoutStreamReader.read()
if (stdoutDone) break
console.log(`stdout: ${stdoutData}`)
stdoutLog += stdoutData
}
while (true) {
const {value: stderrData, done: stderrDone} = await stderrStreamReader.read()
if (stderrDone) break
console.log(`stderr: ${stderrData}`)
stderrLog += stderrData
}
if ((await ops.status).success != true) {
console.error(`Run error with code ${(await ops.status).code}`)
Deno.exit((await ops.status).code)
}
})
program.command("plan")
.aliases(["dry-run","check", "validate", ])
.description("validate DNS record changes")
.action(async(opts) => {
if (Deno.env.get("CLOUDFLARE_TOKEN") == undefined) {
throw new Error("Cloudflare API token missing, maybe forgot to set DOTENV_PRIVATE_KEY?")
}
const args = ["run", "dns-dryrun"]
console.log(`trying to exec ${whereCli} ${args.join(" ")}`)
const ops = new Deno.Command(whereCli, {
args,
env: {
"PIPENV_DONT_LOAD_ENV": "1"
},
stderr: "piped",
stdout: "piped"
}).spawn()
const stdoutStreamReader = ops.stdout.pipeThrough(new TextDecoderStream()).getReader()
const stderrStreamReader = ops.stderr.pipeThrough(new TextDecoderStream()).getReader()
while (true) {
const {value: stdoutData, done: stdoutDone} = await stdoutStreamReader.read()
if (stdoutDone) break
console.log(`stdout: ${stdoutData}`)
stdoutLog += stdoutData
}
while (true) {
const {value: stderrData, done: stderrDone} = await stderrStreamReader.read()
if (stderrDone) break
console.log(`stderr: ${stderrData}`)
stderrLog += stderrData
}
if ((await ops.status).success != true) {
console.error(`Run error with code ${(await ops.status).code}`)
Deno.exit((await ops.status).code)
}
})
program.command("apply")
.aliases(["deploy"])
.description("deploy DNS record changes")
.option("-f, --force-apply, --force", "force apply to skip change threadshold checks in octodns")
.action(async(opts) => {
if (Deno.env.get("CLOUDFLARE_TOKEN") == undefined) {
throw new Error("Cloudflare API token missing, maybe forgot to set DOTENV_PRIVATE_KEY?")
}
const args = ["run", "dns-apply"]
if (opts.forceApply == true) {
args.push("--force")
}
console.log(`trying to exec ${whereCli} ${args.join(" ")}`)
const ops = new Deno.Command(whereCli, {
args,
env: {
"PIPENV_DONT_LOAD_ENV": "1"
},
stderr: "piped",
stdout: "piped"
}).spawn()
const stdoutStreamReader = ops.stdout.pipeThrough(new TextDecoderStream()).getReader()
const stderrStreamReader = ops.stderr.pipeThrough(new TextDecoderStream()).getReader()
while (true) {
const {value: stdoutData, done: stdoutDone} = await stdoutStreamReader.read()
if (stdoutDone) break
console.log(`stdout: ${stdoutData}`)
stdoutLog += stdoutData
}
while (true) {
const {value: stderrData, done: stderrDone} = await stderrStreamReader.read()
if (stderrDone) break
console.log(`stderr: ${stderrData}`)
stderrLog += stderrData
}
if ((await ops.status).success != true) {
console.error(`Run error with code ${(await ops.status).code}`)
Deno.exit((await ops.status).code)
}
})
program.parse()