summaryrefslogtreecommitdiff
path: root/content/notes/dnscontrol.md
blob: 6fcda27b340d5d915bdc6932764eeb34b4337123 (plain)
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
+++
date = '2026-03-13T17:20:05+08:00'
draft = false
title = 'DNSControl Setup'
tags = ['运维']
+++

*exported directly from my [Logseq](https://logseq.com/) graph cuz I'm lazy.*

- Setting up DNSControl
	- Referrences
		- [用代码和 Git 管理 DNS 记录 —— DNSControl 和 GitHub Actions CI/CD 实践](https://blog.skk.moe/post/dns-as-code-via-dnscontrol/)
		- [Overview | DNSControl](https://docs.dnscontrol.org/getting-started/getting-started)
	- Troubleshooting
		- *Complained on [Fedi](https://akk.eltr.ac/objects/c37875f8-ef12-4632-9f43-b7f734d2f47d)...*
	- Setup
		- Install DNSControl
			- via Homebrew, on macOS
			  ```shell
			  brew install StackExchange/tap/dnscontrol
			  ```
			- download pre-built binary
			  see [releases](https://github.com/StackExchange/dnscontrol/releases/)
		- Set up Git repository for configuration files
			- `mkdir dns && cd dns`
			- `git init`
			- `nvim creds.json`
				- For Cloudflare credentials
				  ```
				  {
				    "cloudflare": {
				      "TYPE": "CLOUDFLAREAPI",
				      "accountid": "$CLOUDFLARE_ACCOUNT_ID",
				      "apitoken": "$CLOUDFLARE_API_TOKEN"
				    }
				  }
				  ```
				- It can read from enviroment variables, as long as it start with a `$`
			- `nvim dnsconfig.js`
				- *Don't type `dnscontrol.js`... I made that mistake*
				- Set up registrar (`none` is OK) and DNS provider (Cloudflare in this case)
				  ```js
				  var REG_NONE = NewRegistrar("none");
				  var DSP_CLOUDFLARE = NewDnsProvider('cloudflare');
				  ```
				- Use `D()` to add domain.
				  ```js
				  D("example.com", REG_NONE, DSP_CLOUDFLARE, ...)
				  ```
		- Get existing DNS records in JavaScript
			- use `dnscontrol get-zone`
			  ```shell
			  dnscontrol get-zone --format=js -out=draft.js "cloudflare" "CLOUDFLAREAPI" "geedea.pro"
			  dnscontrol get-zone --format=js -out=draft.js "cloudflare" "CLOUDFLAREAPI" "eltr.ac"
			  ```
			- copy the content from `draft.js` to `dnsconfig.js`, and modify it correctly
		- Preview and push
			- `dnscontrol preview`
			- `dnscontrol push`
		- Set up automatic workflow (with Forgejo Action)
			```yaml
			  name: Push

			  on:
			    push:
			      branches:
			        - master

			  jobs:
			    push:
			      runs-on: docker
			      steps:
			        - uses: actions/checkout@v3
			        - name: Install and Push
			          env:
			            CLOUDFLARE_ACCOUNT_ID: ${{ secrets.CLOUDFLARE_ACCOUNT_ID }}
			            CLOUDFLARE_API_TOKEN: ${{ secrets.CLOUDFLARE_API_TOKEN }}
			          run: |
			            curl -sL https://github.com/StackExchange/dnscontrol/releases/download/v4.36.1/dnscontrol_4.36.1_linux_amd64.tar.gz | tar xz

			            ./dnscontrol push
            ```