├── .gitignore
├── bin
├── linode-change-ip
├── linode-add-record
└── linode-import-zone-file
├── logos
├── logo-box-builtby.png
└── logo-box-madefor.png
├── lib
└── linode.js
├── LICENSE
├── package.json
├── linode-add-record.js
├── linode-change-ip.js
├── README.md
└── linode-import-zone-file.js
/.gitignore:
--------------------------------------------------------------------------------
1 | npm-debug.log
2 | *.DS_Store
3 | .linode-key
4 | node_modules
5 |
--------------------------------------------------------------------------------
/bin/linode-change-ip:
--------------------------------------------------------------------------------
1 | #!/usr/bin/env node
2 |
3 | require('../linode-change-ip.js');
4 |
--------------------------------------------------------------------------------
/bin/linode-add-record:
--------------------------------------------------------------------------------
1 | #!/usr/bin/env node
2 |
3 | require('../linode-add-record.js');
4 |
--------------------------------------------------------------------------------
/bin/linode-import-zone-file:
--------------------------------------------------------------------------------
1 | #!/usr/bin/env node
2 |
3 | require('../linode-import-zone-file.js');
4 |
--------------------------------------------------------------------------------
/logos/logo-box-builtby.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/apostrophecms/linode-dns-tools/master/logos/logo-box-builtby.png
--------------------------------------------------------------------------------
/logos/logo-box-madefor.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/apostrophecms/linode-dns-tools/master/logos/logo-box-madefor.png
--------------------------------------------------------------------------------
/lib/linode.js:
--------------------------------------------------------------------------------
1 | var osenv = require('osenv');
2 | var api = require('linode-api');
3 | var fs = require('fs');
4 |
5 | module.exports = function() {
6 | var apiKey;
7 | try {
8 | apiKey = fs.readFileSync('.linode-key', 'utf8').trim();
9 | } catch (e) {
10 | try {
11 | var home = osenv.home();
12 | apiKey = fs.readFileSync(home + '/.linode-key', 'utf8').trim();
13 | } catch (e) {
14 | throw 'There must be a .linode-key file in the current directory, or in\n' + 'your home directory.';
15 | }
16 | }
17 | return new (api.LinodeClient)(apiKey);
18 | };
19 |
20 |
--------------------------------------------------------------------------------
/LICENSE:
--------------------------------------------------------------------------------
1 | Copyright (c) 2015 P'unk Avenue LLC
2 |
3 | Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
4 |
5 | The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
6 |
7 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
8 |
--------------------------------------------------------------------------------
/package.json:
--------------------------------------------------------------------------------
1 | {
2 | "name": "linode-dns-tools",
3 | "version": "0.1.3",
4 | "description": "Tools to update your Linode DNS entries via the Linode API.",
5 | "main": "app.js",
6 | "scripts": {
7 | "test": "echo \"Error: no test specified\" && exit 1"
8 | },
9 | "repository": {
10 | "type": "git",
11 | "url": "https://github.com/punkave/linode-dns-tools"
12 | },
13 | "keywords": [
14 | "dns",
15 | "linode",
16 | "zone",
17 | "file"
18 | ],
19 | "author": "P'unk Avenue LLC",
20 | "license": "MIT",
21 | "bugs": {
22 | "url": "https://github.com/punkave/linode-dns-tools/issues"
23 | },
24 | "homepage": "https://github.com/punkave/linode-dns-tools",
25 | "dependencies": {
26 | "async": "^0.9.0",
27 | "linode-api": "^0.1.2",
28 | "lodash": "^3.0.1",
29 | "osenv": "^0.1.0",
30 | "yargs": "^1.3.3"
31 | },
32 | "bin": {
33 | "linode-import-zone-file": "./bin/linode-import-zone-file",
34 | "linode-change-ip": "./bin/linode-change-ip",
35 | "linode-add-record": "./bin/linode-add-record"
36 | }
37 | }
38 |
--------------------------------------------------------------------------------
/linode-add-record.js:
--------------------------------------------------------------------------------
1 | var argv = require('yargs').argv;
2 | var _ = require('lodash');
3 | var async = require('async');
4 |
5 | try {
6 | var client = require('./lib/linode.js')();
7 | } catch (e) {
8 | usage(e);
9 | }
10 |
11 | if (!argv['domain']) {
12 | usage('The domain option is required.');
13 | }
14 |
15 | if (!argv['type']) {
16 | usage('The type option is required.');
17 | }
18 |
19 | if (!argv['name']) {
20 | usage('The name option is required.');
21 | }
22 |
23 | if (!argv['target']) {
24 | usage('The target option is required.');
25 | }
26 |
27 | var domain;
28 |
29 | return async.series({
30 | listDomains: function(callback) {
31 | return api('domain.list', {}, function(err, res) {
32 | if (err) {
33 | return callback(err);
34 | }
35 | domain = _.find(res, function(record) {
36 | return record.DOMAIN === argv.domain;
37 | });
38 | if (!domain) {
39 | return callback('No such domain was found.');
40 | }
41 | return callback(null);
42 | });
43 | },
44 | update: function(callback) {
45 | return api('domain.resource.create', {
46 | DomainID: domain.DOMAINID,
47 | Type: argv.type.toUpperCase(),
48 | Name: argv.name.trim(),
49 | Target: argv.target.trim()
50 | }, callback);
51 | }
52 | }, function(err) {
53 | if (err) {
54 | console.error(err);
55 | process.exit(2);
56 | }
57 | process.exit(0);
58 | });
59 |
60 | function api(verb, args, callback) {
61 | if (argv.verbose) {
62 | console.log(verb);
63 | console.log(args);
64 | }
65 | return client.call(verb, args, callback);
66 | }
67 |
68 | function usage(msg) {
69 | if (msg) {
70 | console.error(msg);
71 | console.error('');
72 | }
73 | console.error('Usage: linode-add-record --domain=mycompany.com --type=a --name=foo --target=a.b.c.d [--verbose]\n');
74 | console.error('This command will add a new DNS record. It currently');
75 | console.error('supports only simple records with a name and target.\n');
76 | process.exit(1);
77 | }
78 |
--------------------------------------------------------------------------------
/linode-change-ip.js:
--------------------------------------------------------------------------------
1 | var argv = require('yargs').argv;
2 | var _ = require('lodash');
3 | var async = require('async');
4 |
5 | try {
6 | var client = require('./lib/linode.js')();
7 | } catch (e) {
8 | usage(e);
9 | }
10 |
11 | if (!argv['old']) {
12 | usage('The old-ip option is required.');
13 | }
14 |
15 | if (!argv['new']) {
16 | usage('The new-ip option is required.');
17 | }
18 |
19 | var domains;
20 |
21 | return async.series({
22 | listDomains: function(callback) {
23 | return api('domain.list', {}, function(err, res) {
24 | if (err) {
25 | return callback(err);
26 | }
27 | domains = res;
28 | return callback(null);
29 | });
30 | },
31 | update: function(callback) {
32 | return async.eachSeries(domains, function(domain, callback) {
33 | if (argv.domain) {
34 | if (argv.domain !== domain.DOMAIN) {
35 | return setImmediate(callback);
36 | }
37 | }
38 | return api('domain.resource.list', { DomainID: domain.DOMAINID }, function(err, records) {
39 | if (err) {
40 | return callback(err);
41 | }
42 | var interesting = _.filter(records, function(record) {
43 | return record.TARGET === argv['old'];
44 | });
45 | return async.eachSeries(interesting, function(record, callback) {
46 | record.TARGET = argv['new'];
47 | if (argv.verbose) {
48 | console.log('updating ' + record.TYPE + ' record in domain ' + domain.DOMAIN);
49 | }
50 | return api('domain.resource.update', record, callback);
51 | }, callback);
52 | });
53 | }, callback);
54 | }
55 | }, function(err) {
56 | if (err) {
57 | console.error(err);
58 | process.exit(2);
59 | }
60 | process.exit(0);
61 | });
62 |
63 | function api(verb, args, callback) {
64 | if (argv.verbose) {
65 | console.log(verb);
66 | console.log(args);
67 | }
68 | return client.call(verb, args, callback);
69 | }
70 |
71 | function usage() {
72 | console.error('Usage: linode-change-ip --old=old-ip --new=new-ip [--domain=domain-name]\n');
73 | console.error('This command will change the old IP to the new one in every');
74 | console.error('record where it appears.');
75 | console.error('\n');
76 | console.error('If you do not specify a domain name, the change is made');
77 | console.error('for ALL of your domains.');
78 | process.exit(1);
79 | }
80 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # linode-dns-tools
2 |
3 |
4 |
5 | A collection of tools for the [linode DNS API](https://www.linode.com/api/dns).
6 |
7 | ## Requirements
8 |
9 | You must provide your linode API key, which you can generate via your linode profile. If there is a `.linode-key` file in the current directory, it is used, otherwise the `.linode-key` file in your home directory is used.
10 |
11 | ## Installation
12 |
13 | ```
14 | npm install -g linode-dns-tools
15 | ```
16 |
17 | # The tools
18 |
19 | ## linode-import-zone-file
20 |
21 | Imports bind-style DNS zone files via the Linode API. Very useful if you've exported one from another hosting service that won't allow Linode's automatic zone export feature.
22 |
23 | ### Usage
24 |
25 | ```
26 | linode-import-zone-file zonefile
27 | ```
28 |
29 | It takes a little time depending on how many records you have.
30 |
31 | TODO: currently no support for SRV records. Pull requests welcome.
32 |
33 | Note that if an error is reported, no records beyond that point are imported.
34 |
35 | Runs quietly if nothing is wrong. Use `--verbose` for detailed output.
36 |
37 | ## linode-change-ip
38 |
39 | Globally replace an IP address in all of your domains, or one particular domain. Very useful when you replace a server.
40 |
41 | ### Usage
42 |
43 | ```
44 | linode-change-ip --old=1.1.1.1 --new=2.2.2.2
45 | ```
46 |
47 | Optionally you can do this for just one domain:
48 |
49 | ```
50 | linode-change-ip --old=1.1.1.1 --new=2.2.2.2 --domain=mycompany.com
51 | ```
52 |
53 | Runs quietly if nothing is wrong. Use `--verbose` for detailed output.
54 |
55 | ## linode-add-record
56 |
57 | A simple utility to add a new record.
58 |
59 | ```
60 | linode-add-record --domain=foo.com --type=a --name=bar --target=1.2.3.4
61 | ```
62 |
63 | Currently does not support record types that require more than just a name and target. Pull requests always welcome.
64 |
65 | ## About P'unk Avenue and Apostrophe
66 |
67 | `linode-dns-tools` was created at [P'unk Avenue](http://punkave.com) to support our work on Apostrophe, an open-source content management system built on node.js. If you like `linode-dns-tools` you should definitely [check out apostrophenow.org](http://apostrophenow.org). Also be sure to visit us on [github](http://github.com/punkave).
68 |
69 | ## Support
70 |
71 | Feel free to open issues on [github](http://github.com/punkave/linode-dns-tools).
72 |
73 |
74 |
--------------------------------------------------------------------------------
/linode-import-zone-file.js:
--------------------------------------------------------------------------------
1 | var fs = require('fs');
2 | var argv = require('yargs').argv;
3 | var _ = require('lodash');
4 | var async = require('async');
5 |
6 | if (argv._.length !== 1) {
7 | usage();
8 | }
9 |
10 | try {
11 | var client = require('./lib/linode.js')();
12 | } catch (e) {
13 | usage(e);
14 | }
15 |
16 | var input = fs.readFileSync(argv._[0], 'utf8');
17 |
18 | // 1104251419 ; serial
19 | // 10800 ; refresh
20 | // 3600 ; retry
21 | // 604800 ; expire
22 | // 10800 ; minimum
23 |
24 | var timing = input.match(/\([\s\S]+?\)/);
25 | if (!timing) {
26 | usage("I don't see an SOA record");
27 | }
28 | timing = timing[0];
29 | timing = timing.split(/[\r\n]+/);
30 | timing = _.filter(
31 | _.map(timing, function(item) {
32 | var matches = item.match(/\d+/);
33 | if (matches) {
34 | return matches[0];
35 | }
36 | }), function(item) {
37 | return !!item;
38 | }
39 | );
40 |
41 | if (timing.length !== 5) {
42 | usage('Unable to parse SOA record, should have 5 numbers');
43 | }
44 |
45 | timing = {
46 | serial: timing[0],
47 | refresh: timing[1],
48 | retry: timing[2],
49 | expire: timing[3],
50 | minimum: timing[4]
51 | };
52 |
53 | var lines = input.split(/[\r\n]+/);
54 |
55 | // Match one record, with optional TTL
56 | var regex = new RegExp(/\s*(\S+)\s+((\d+)\s+)?([A-Z]+)\s+(.*?)\s*$/);
57 | var records = _.map(
58 | _.filter(lines, (function(line) {
59 | return line.match(regex);
60 | })),
61 | function(line) {
62 | var fields = line.match(regex);
63 | var result = {
64 | name: fields[1],
65 | ttl: fields[3],
66 | type: fields[4],
67 | value: fields[5]
68 | };
69 |
70 | // Quotation marks are not actually part of the
71 | // value of the TXT record
72 | result.value = result.value.replace(/"/g, '');
73 | if (!result.ttl) {
74 | delete result.ttl;
75 | }
76 | if (result.type === 'MX') {
77 | var matches = result.value.match(/(\d+)\s*(.*)/);
78 | if (!matches) {
79 | usage("MX record doesn't look valid");
80 | }
81 | result.priority = matches[1];
82 | result.value = matches[2];
83 | }
84 | return result;
85 | }
86 | );
87 |
88 | // Don't defeat the purpose by importing NS records,
89 | // let linode provide those
90 | records = _.filter(records, function(record) {
91 | return record.type !== 'NS';
92 | });
93 |
94 | if (!records.length) {
95 | usage("No SOA record found in file.");
96 | }
97 |
98 | var domainName = records[0].name;
99 |
100 | records = records.slice(1);
101 |
102 | var id;
103 |
104 | return async.series({
105 | createDomain: function(callback) {
106 | return api('domain.create', {
107 | Domain: domainName,
108 | Type: 'master',
109 | SOA_Email: 'admin@' + domainName,
110 | Refresh_sec: timing.refresh,
111 | Retry_sec: timing.retry,
112 | Expire_sec: timing.expire,
113 | TTL_sec: timing.minimum
114 | }, function(err, res) {
115 | if (err) {
116 | return callback(err);
117 | }
118 | id = res.DomainID;
119 | return callback(null);
120 | });
121 | },
122 | createRecords: function(callback) {
123 | return async.eachSeries(records, function(item, callback) {
124 | var data = {
125 | DomainID: id,
126 | Type: item.type,
127 | Name: item.name,
128 | Target: item.value.replace(/\.$/, '')
129 | };
130 | if (item.type === 'MX') {
131 | data.Priority = item.priority;
132 | }
133 | if (item.ttl) {
134 | data.TTL_sec = item.ttl;
135 | }
136 | return api('domain.resource.create', data, function(err) {
137 | if (err) {
138 | console.error('Error for this record:');
139 | console.error(data);
140 | console.error('in the ' + domainName + ' domain');
141 | }
142 | return callback(err);
143 | });
144 | }, callback);
145 | }
146 | }, function(err) {
147 | if (err) {
148 | console.error(err);
149 | process.exit(2);
150 | }
151 | process.exit(0);
152 | });
153 |
154 | function api(verb, args, callback) {
155 | if (argv.verbose) {
156 | console.log(verb);
157 | console.log(args);
158 | }
159 | return client.call(verb, args, callback);
160 | }
161 |
162 | function usage(msg) {
163 | if (msg) {
164 | console.error(msg);
165 | }
166 | console.error('Usage: linode-import-zone-file zonefile\n');
167 | console.error('The zone file should be in the BIND format used');
168 | console.error('by most nameservers. Some hosts will let you export');
169 | console.error('such a file.');
170 | process.exit(1);
171 | }
172 |
--------------------------------------------------------------------------------