Skip to content

Commit

Permalink
Refactor as a npm package
Browse files Browse the repository at this point in the history
  • Loading branch information
Leen15 committed Oct 10, 2023
1 parent 920d4e9 commit 8705e1e
Show file tree
Hide file tree
Showing 5 changed files with 287 additions and 155 deletions.
21 changes: 21 additions & 0 deletions .github/workflows/publish.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
name: Publish to NPM
on:
release:
types: [created]
jobs:
build:
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v2
- name: Setup Node
uses: actions/setup-node@v2
with:
node-version: '18.x'
registry-url: 'https://registry.npmjs.org'
- name: Install dependencies and build 🔧
run: npm ci && npm run build
- name: Publish package on NPM 📦
run: npm publish
env:
NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }}
113 changes: 95 additions & 18 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,28 +1,105 @@
### Cloudflare Domains Manager

Inside the `configuration.yaml` file there is the mapping of every DNS record used in this repo.<br/>
This tool is helpful to manage cloudflare domains using a versionate file.<br/>

For use the script just run under the domains folder:
## How to use?

Create a folder for your new project:
```
npm install
npm run update
```
Note:<br/>
This script works only with domains managed by cloudflare.<br/>
You have to create a `.env` file with Cloudflare credentials inside `domains` folder.<br/>
In the configuration, `ttl=1` is the automatic ttl provided by cloudflare.<br/>
In the configuration, `proxied=true` means the record will be proxed by cloudflare system.<br/>
In the configuration, `deleted=true` means the record will be delete from cloudflare. Use it only when you need to delete a dns record. You can remove the dns from the yaml after the deletion.<br/>
Please use load balancers mapping instead of explicit value in DNS records.<br/>
If you have created a new domain in cloudflare you can run this command to get the mandatory zone_id for the configuration file:
mkdir test-domains
cd test-domains
```

Create a new node app:
```
npm init
```

Install this package:
```
npm i cloudflare-domains-manager
```

Create a index.js with this content:
```
const CloudflareDomainsManager = require ('cloudflare-domains-manager')
const cdm = new CloudflareDomainsManager();
cdm.run();
```

Create a .env file with your clouflare data:
```
CF_EMAIL=(your cloudflare email)
CF_KEY= (your cloudflare token)
```
node index.js action=list
you can generate your cloudflare token here: https://dash.cloudflare.com/profile/api-tokens


Edit the package.json adding these lines:
```
"main": "index.js",
"scripts": {
"update": "node index.js action=update",
"list": "node index.js action=list"
}
```

install the dependencies:
```
npm install
```
if you want to update a single domain instead of all, just add it as a parameter:

you can now get a list of your domains with:
```
npm run update uala.it
npm run list
```
Dry run mode is supported, just add it as a parameter:

## The configuration file

if it works, you can now create your `configuration.yaml` file starting with something like:
```
npm run update uala.it --dry-run
load_balancers:
- example-lb: &example-lb example-lb.your-infrastructure.com
- example-lb-ip: &example-lb-ip 172.0.0.10
domains:
- name: example.com
zone_id: Y0UR_Z0N3_1D
dns_records:
- name: example.com
type: CNAME
content: *example-lb
ttl: 1
proxied: true
- name: test.example.com
type: A
content: *example-lb-ip
ttl: 1
proxied: true
- name: www.example.com
type: CNAME
content: *example-lb
ttl: 1
proxied: true
- name: tobedeleted.example.com
type: CNAME
deleted: true
content: *example-lb
ttl: 1
proxied: true
```

In the configuration:
- `ttl=1` is the automatic ttl provided by cloudflare.<br/>
- `proxied=true` means the record will be proxed by cloudflare system.<br/>
- `deleted=true` means the record will be delete from cloudflare. Use it only when you need to delete a dns record. You can remove the dns from the yaml after the deletion.<br/>

Please use load balancers mapping instead of explicit value in DNS records.<br/>


## Commands list

- `npm run list`: Get the list of all your domains in cloudflare
- `npm run update`: Update all your cloudflare domains with the content of `configuration.yaml`. Domains that don't exist in the file will be ignored
- `npm run update example.com`: Update only the domain `example.com`
- `npm run update example.com --dry-run`: Update the domain `example.com` in dry-run mode (nothing will change in cloudflare)
28 changes: 28 additions & 0 deletions configuration.yaml.example
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
load_balancers:
- example-lb: &example-lb example-lb.your-infrastructure.com
- example-lb-ip: &example-lb-ip 172.0.0.10
domains:
- name: example.com
zone_id: Y0UR_Z0N3_1D
dns_records:
- name: example.com
type: CNAME
content: *example-lb
ttl: 1
proxied: true
- name: test.example.com
type: A
content: *example-lb-ip
ttl: 1
proxied: true
- name: www.example.com
type: CNAME
content: *example-lb
ttl: 1
proxied: true
- name: tobedeleted.example.com
type: CNAME
deleted: true
content: *example-lb
ttl: 1
proxied: true
4 changes: 1 addition & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,7 @@
"description": "An easy to use tool to manage your cloudflare domains",
"main": "dist/index.js",
"scripts": {
"build": "npx tsc",
"update": "npx tsc && node dist/index.js action=update",
"list": "npx tsc && node dist/index.js action=list"
"build": "npx tsc"
},
"author": "",
"license": "MIT",
Expand Down
Loading

0 comments on commit 8705e1e

Please sign in to comment.