dnsync 1.0.0
Sign up to get free protection for your applications and to get access to all the features.
- data/Gemfile +6 -0
- data/LICENSE +21 -0
- data/README.md +180 -0
- data/Rakefile +150 -0
- data/bin/dnsync +6 -0
- data/dnsync.gemspec +88 -0
- data/lib/dnsync/answer.rb +28 -0
- data/lib/dnsync/cli.rb +207 -0
- data/lib/dnsync/dnsimple.rb +65 -0
- data/lib/dnsync/http_status.rb +56 -0
- data/lib/dnsync/nsone.rb +106 -0
- data/lib/dnsync/record.rb +46 -0
- data/lib/dnsync/record_identifier.rb +34 -0
- data/lib/dnsync/recurring_zone_updater.rb +152 -0
- data/lib/dnsync/zone.rb +36 -0
- data/lib/dnsync/zone_difference.rb +52 -0
- data/lib/dnsync/zone_updater.rb +22 -0
- data/lib/dnsync.rb +4 -0
- data/test/record_identifier_test.rb +35 -0
- data/test/record_test.rb +28 -0
- data/test/test_helper.rb +5 -0
- metadata +184 -0
data/Gemfile
ADDED
data/LICENSE
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License
|
2
|
+
|
3
|
+
Copyright (c) 2014 Eric Lindvall
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
13
|
+
all copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
21
|
+
THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,180 @@
|
|
1
|
+
# Dnsync
|
2
|
+
|
3
|
+
Dnsync provides a simple way to replicate records from DNSimple to NSONE
|
4
|
+
(because DNSimple does not yet support AXFR for zone transfer). For a domain
|
5
|
+
owner, using two different DNS networks is much better DDoS protection than
|
6
|
+
relying on any single one.
|
7
|
+
|
8
|
+
|
9
|
+
## Installation
|
10
|
+
|
11
|
+
$ gem install dnsync
|
12
|
+
|
13
|
+
or to install a prerelease, add to your `Gemfile`:
|
14
|
+
|
15
|
+
gem 'dnsync', :git => 'git://github.com/papertrail/dnsync.git'
|
16
|
+
|
17
|
+
and use:
|
18
|
+
|
19
|
+
$ bundle exec dnsync
|
20
|
+
|
21
|
+
## Using
|
22
|
+
|
23
|
+
Dnsync can be used to either do one-time synchronization or run in the
|
24
|
+
foreground forever, synchronizing every 10 seconds.
|
25
|
+
|
26
|
+
|
27
|
+
### Doing a one-time synchronization
|
28
|
+
|
29
|
+
To do a one-time synchronization:
|
30
|
+
|
31
|
+
$ dnsync <options> sync
|
32
|
+
|
33
|
+
|
34
|
+
### Monitoring a DNSimple zone for changes
|
35
|
+
|
36
|
+
To monitor a DNSimple domain for changes and automatically propagate the
|
37
|
+
changes to NSONE:
|
38
|
+
|
39
|
+
$ dnsync <options> monitor
|
40
|
+
|
41
|
+
|
42
|
+
## Configuration
|
43
|
+
|
44
|
+
Configuration and authentication can be provided either by command line
|
45
|
+
arguments, environment variables, or environment variable files.
|
46
|
+
|
47
|
+
Environment variable files are files that contain a list of enviroment variable
|
48
|
+
name value pairs, like:
|
49
|
+
|
50
|
+
```
|
51
|
+
DNSYNC_DNSIMPLE_EMAIL=user@email.com
|
52
|
+
DNSYNC_DNSIMPLE_TOKEN=xxxxxxxxxx
|
53
|
+
```
|
54
|
+
|
55
|
+
The files are looked for in:
|
56
|
+
|
57
|
+
* `$HOME/dnsync.env`
|
58
|
+
* `<dnsync-code-root>/.env`
|
59
|
+
* `$PWD/.env`
|
60
|
+
|
61
|
+
|
62
|
+
### General options
|
63
|
+
|
64
|
+
To specify the domain to synchronize, the command line argument is:
|
65
|
+
|
66
|
+
```
|
67
|
+
--domain=DOMAIN Domain to synchronize
|
68
|
+
```
|
69
|
+
|
70
|
+
Alternatively, the environment variable `DNSYNC_DOMAIN` can be used.
|
71
|
+
|
72
|
+
### Monitor options
|
73
|
+
|
74
|
+
The `monitor` command has a few options to help configure how it behaves:
|
75
|
+
|
76
|
+
```
|
77
|
+
--monitor-frequency=FREQUENCY
|
78
|
+
Frequency to check DNSimple for updates
|
79
|
+
--status-port=PORT Port to run status HTTP server on
|
80
|
+
|
81
|
+
```
|
82
|
+
|
83
|
+
These arguments can also be specified as environment variables:
|
84
|
+
`DNSYNC_MONITOR_FREQUENCY` and `DNSYNC_STATUS_PORT`.
|
85
|
+
|
86
|
+
|
87
|
+
### DNSimple
|
88
|
+
|
89
|
+
To authenticate against DNSimple, the command line arguments are:
|
90
|
+
|
91
|
+
```
|
92
|
+
--dnsimple-email=EMAIL DNSimple email address
|
93
|
+
--dnsimple-token=TOKEN DNSimple token
|
94
|
+
```
|
95
|
+
|
96
|
+
Alternately, the environment variables `DNSYNC_DNSIMPLE_EMAIL` and
|
97
|
+
`DNSYNC_DNSIMPLE_TOKEN` can be used.
|
98
|
+
|
99
|
+
### NSONE
|
100
|
+
|
101
|
+
To authenticate against NSONE, the command line arguments are:
|
102
|
+
|
103
|
+
```
|
104
|
+
--nsone-token=TOKEN NSONE token
|
105
|
+
```
|
106
|
+
|
107
|
+
Alternatively, the environment variable `DNSYNC_NSONE_TOKEN` is used.
|
108
|
+
|
109
|
+
|
110
|
+
## Running on Heroku
|
111
|
+
|
112
|
+
Heroku provides a simple place to run synchronization. The process for
|
113
|
+
getting a synchronizer up and running is:
|
114
|
+
|
115
|
+
### 1. Create a new directory for the heroku app:
|
116
|
+
|
117
|
+
$ mkdir dns-synchronizer
|
118
|
+
$ cd dns-synchronizer
|
119
|
+
$ git init
|
120
|
+
$ touch .gitignore
|
121
|
+
$ git add .gitignore
|
122
|
+
$ git commit -m 'Initial commit'
|
123
|
+
|
124
|
+
### 2. Create a basic ruby project:
|
125
|
+
|
126
|
+
$ bundle init
|
127
|
+
$ echo "gem 'dnsync', :git => 'git://github.com/papertrail/dnsync.git'" >> Gemfile
|
128
|
+
$ bundle
|
129
|
+
|
130
|
+
### 3. Create a `Procfile` to run the worker:
|
131
|
+
|
132
|
+
$ echo 'web: bundle exec dnsync --status-port=$PORT monitor' >> Procfile
|
133
|
+
|
134
|
+
### 4. Commit the changes:
|
135
|
+
|
136
|
+
$ git add Gemfile Gemfile.lock Procfile
|
137
|
+
$ git commit -m 'Setting up dnsync'
|
138
|
+
|
139
|
+
### 5. Create a heroku app:
|
140
|
+
|
141
|
+
$ heroku apps:create
|
142
|
+
|
143
|
+
### 6. Deploy to heroku:
|
144
|
+
|
145
|
+
$ git push heroku master
|
146
|
+
|
147
|
+
### 7. Set configuration variables:
|
148
|
+
|
149
|
+
$ heroku config:set DNSYNC_DNSIMPLE_EMAIL=user@domain.com DNSYNC_DNSIMPLE_TOKEN=xxxx \
|
150
|
+
DNSYNC_NSONE_TOKEN=xxxx DNSYNC_DOMAIN=domain.com
|
151
|
+
|
152
|
+
### 8. Setting up monitoring
|
153
|
+
|
154
|
+
Now that the service is running and synchronizing, it would be wise to
|
155
|
+
monitor of the service.
|
156
|
+
|
157
|
+
As always, it's good to keep an eye on your logs, so use `heroku logs -t` or
|
158
|
+
[send the logs](http://help.papertrailapp.com/kb/hosting-services/heroku/) to
|
159
|
+
[Papertrail](https://papertrailapp.com/).
|
160
|
+
|
161
|
+
To ensure that the synchronization is working properly, poll the status URL
|
162
|
+
with your favorite website monitor.
|
163
|
+
|
164
|
+
Poll `https://<your-app>.herokuapp.com/status` for a 200 response code.
|
165
|
+
|
166
|
+
If it responds with a non-200 error code, it will return the reason for the
|
167
|
+
monitoring failure.
|
168
|
+
|
169
|
+
|
170
|
+
## Debugging
|
171
|
+
|
172
|
+
### Getting a zone dump
|
173
|
+
|
174
|
+
To get a zone dump from DNSimple:
|
175
|
+
|
176
|
+
$ dnsync --domain=domain.com dump dnsimple
|
177
|
+
|
178
|
+
To get a zone dump from NSONE:
|
179
|
+
|
180
|
+
$ dnsync --domain=domain.com dump nsone
|
data/Rakefile
ADDED
@@ -0,0 +1,150 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'rake'
|
3
|
+
require 'date'
|
4
|
+
|
5
|
+
#############################################################################
|
6
|
+
#
|
7
|
+
# Helper functions
|
8
|
+
#
|
9
|
+
#############################################################################
|
10
|
+
|
11
|
+
def name
|
12
|
+
@name ||= Dir['*.gemspec'].first.split('.').first
|
13
|
+
end
|
14
|
+
|
15
|
+
def version
|
16
|
+
line = File.read("lib/#{name}.rb")[/^\s*VERSION\s*=\s*.*/]
|
17
|
+
line.match(/.*VERSION\s*=\s*['"](.*)['"]/)[1]
|
18
|
+
end
|
19
|
+
|
20
|
+
def date
|
21
|
+
Date.today.to_s
|
22
|
+
end
|
23
|
+
|
24
|
+
def rubyforge_project
|
25
|
+
name
|
26
|
+
end
|
27
|
+
|
28
|
+
def gemspec_file
|
29
|
+
"#{name}.gemspec"
|
30
|
+
end
|
31
|
+
|
32
|
+
def gem_file
|
33
|
+
"#{name}-#{version}.gem"
|
34
|
+
end
|
35
|
+
|
36
|
+
def replace_header(head, header_name)
|
37
|
+
head.sub!(/(\.#{header_name}\s*= ').*'/) { "#{$1}#{send(header_name)}'"}
|
38
|
+
end
|
39
|
+
|
40
|
+
#############################################################################
|
41
|
+
#
|
42
|
+
# Standard tasks
|
43
|
+
#
|
44
|
+
#############################################################################
|
45
|
+
|
46
|
+
task :default => :test
|
47
|
+
|
48
|
+
require 'rake/testtask'
|
49
|
+
Rake::TestTask.new(:test) do |test|
|
50
|
+
test.libs << 'lib' << 'test'
|
51
|
+
test.pattern = 'test/**/*_test.rb'
|
52
|
+
test.verbose = true
|
53
|
+
end
|
54
|
+
|
55
|
+
# desc "Generate RCov test coverage and open in your browser"
|
56
|
+
# task :coverage do
|
57
|
+
# require 'rcov'
|
58
|
+
# sh "rm -fr coverage"
|
59
|
+
# sh "rcov test/test_*.rb"
|
60
|
+
# sh "open coverage/index.html"
|
61
|
+
# end
|
62
|
+
|
63
|
+
# require 'rake/rdoctask'
|
64
|
+
# Rake::RDocTask.new do |rdoc|
|
65
|
+
# rdoc.rdoc_dir = 'rdoc'
|
66
|
+
# rdoc.title = "#{name} #{version}"
|
67
|
+
# rdoc.rdoc_files.include('README*')
|
68
|
+
# rdoc.rdoc_files.include('lib/**/*.rb')
|
69
|
+
# end
|
70
|
+
|
71
|
+
desc "Open an irb session preloaded with this library"
|
72
|
+
task :console do
|
73
|
+
sh "irb -rubygems -r ./lib/#{name}.rb"
|
74
|
+
end
|
75
|
+
|
76
|
+
#############################################################################
|
77
|
+
#
|
78
|
+
# Custom tasks (add your own tasks here)
|
79
|
+
#
|
80
|
+
#############################################################################
|
81
|
+
|
82
|
+
|
83
|
+
|
84
|
+
#############################################################################
|
85
|
+
#
|
86
|
+
# Packaging tasks
|
87
|
+
#
|
88
|
+
#############################################################################
|
89
|
+
|
90
|
+
desc "Create tag v#{version} and build and push #{gem_file} to Rubygems"
|
91
|
+
task :release => :build do
|
92
|
+
unless `git branch` =~ /^\* master$/
|
93
|
+
puts "You must be on the master branch to release!"
|
94
|
+
exit!
|
95
|
+
end
|
96
|
+
sh "git commit --allow-empty -a -m 'Release #{version}'"
|
97
|
+
sh "git tag v#{version}"
|
98
|
+
sh "git push origin master"
|
99
|
+
sh "git push origin v#{version}"
|
100
|
+
sh "gem push pkg/#{name}-#{version}.gem"
|
101
|
+
end
|
102
|
+
|
103
|
+
desc "Build #{gem_file} into the pkg directory"
|
104
|
+
task :build => :gemspec do
|
105
|
+
sh "mkdir -p pkg"
|
106
|
+
sh "gem build #{gemspec_file}"
|
107
|
+
sh "mv #{gem_file} pkg"
|
108
|
+
end
|
109
|
+
|
110
|
+
desc "Generate #{gemspec_file}"
|
111
|
+
task :gemspec => :validate do
|
112
|
+
# read spec file and split out manifest section
|
113
|
+
spec = File.read(gemspec_file)
|
114
|
+
head, manifest, tail = spec.split(" # = MANIFEST =\n")
|
115
|
+
|
116
|
+
# replace name version and date
|
117
|
+
replace_header(head, :name)
|
118
|
+
replace_header(head, :version)
|
119
|
+
replace_header(head, :date)
|
120
|
+
#comment this out if your rubyforge_project has a different name
|
121
|
+
replace_header(head, :rubyforge_project)
|
122
|
+
|
123
|
+
# determine file list from git ls-files
|
124
|
+
files = `git ls-files`.
|
125
|
+
split("\n").
|
126
|
+
sort.
|
127
|
+
reject { |file| file =~ /^\./ }.
|
128
|
+
reject { |file| file =~ /^(rdoc|pkg)/ }.
|
129
|
+
map { |file| " #{file}" }.
|
130
|
+
join("\n")
|
131
|
+
|
132
|
+
# piece file back together and write
|
133
|
+
manifest = " s.files = %w[\n#{files}\n ]\n"
|
134
|
+
spec = [head, manifest, tail].join(" # = MANIFEST =\n")
|
135
|
+
File.open(gemspec_file, 'w') { |io| io.write(spec) }
|
136
|
+
puts "Updated #{gemspec_file}"
|
137
|
+
end
|
138
|
+
|
139
|
+
desc "Validate #{gemspec_file}"
|
140
|
+
task :validate do
|
141
|
+
libfiles = Dir['lib/*'] - ["lib/#{name}.rb", "lib/#{name}"]
|
142
|
+
unless libfiles.empty?
|
143
|
+
puts "Directory `lib` should only contain a `#{name}.rb` file and `#{name}` dir."
|
144
|
+
exit!
|
145
|
+
end
|
146
|
+
unless Dir['VERSION*'].empty?
|
147
|
+
puts "A `VERSION` file at root level violates Gem best practices."
|
148
|
+
exit!
|
149
|
+
end
|
150
|
+
end
|
data/bin/dnsync
ADDED
data/dnsync.gemspec
ADDED
@@ -0,0 +1,88 @@
|
|
1
|
+
## This is the rakegem gemspec template. Make sure you read and understand
|
2
|
+
## all of the comments. Some sections require modification, and others can
|
3
|
+
## be deleted if you don't need them. Once you understand the contents of
|
4
|
+
## this file, feel free to delete any comments that begin with two hash marks.
|
5
|
+
## You can find comprehensive Gem::Specification documentation, at
|
6
|
+
## http://docs.rubygems.org/read/chapter/20
|
7
|
+
Gem::Specification.new do |s|
|
8
|
+
s.specification_version = 2 if s.respond_to? :specification_version=
|
9
|
+
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
10
|
+
s.rubygems_version = '1.3.5'
|
11
|
+
|
12
|
+
## Leave these as is they will be modified for you by the rake gemspec task.
|
13
|
+
## If your rubyforge_project name is different, then edit it and comment out
|
14
|
+
## the sub! line in the Rakefile
|
15
|
+
s.name = 'dnsync'
|
16
|
+
s.version = '1.0.0'
|
17
|
+
s.date = '2014-12-30'
|
18
|
+
|
19
|
+
## Make sure your summary is short. The description may be as long
|
20
|
+
## as you like.
|
21
|
+
s.summary = "DNS Synchronizer"
|
22
|
+
s.description = "DNS Synchronizer between DNSimple and NSONE"
|
23
|
+
|
24
|
+
## List the primary authors. If there are a bunch of authors, it's probably
|
25
|
+
## better to set the email to an email list or something. If you don't have
|
26
|
+
## a custom homepage, consider using your GitHub URL or the like.
|
27
|
+
s.authors = ["Eric Lindvall"]
|
28
|
+
s.email = 'eric@papertrailapp.com'
|
29
|
+
s.homepage = 'https://github.com/papertrail/dnsync'
|
30
|
+
|
31
|
+
## This gets added to the $LOAD_PATH so that 'lib/NAME.rb' can be required as
|
32
|
+
## require 'NAME.rb' or'/lib/NAME/file.rb' can be as require 'NAME/file.rb'
|
33
|
+
s.require_paths = %w[lib]
|
34
|
+
|
35
|
+
## If your gem includes any executables, list them here.
|
36
|
+
s.executables = ["dnsync"]
|
37
|
+
|
38
|
+
## Specify any RDoc options here. You'll want to add your README and
|
39
|
+
## LICENSE files to the extra_rdoc_files list.
|
40
|
+
s.rdoc_options = ["--charset=UTF-8"]
|
41
|
+
s.extra_rdoc_files = %w[README.md LICENSE]
|
42
|
+
|
43
|
+
## List your runtime dependencies here. Runtime dependencies are those
|
44
|
+
## that are needed for an end user to actually USE your code.
|
45
|
+
s.add_dependency('faraday')
|
46
|
+
s.add_dependency('faraday_middleware')
|
47
|
+
s.add_dependency('configlet')
|
48
|
+
s.add_dependency('activesupport')
|
49
|
+
s.add_dependency('scrolls')
|
50
|
+
s.add_dependency('atomic')
|
51
|
+
|
52
|
+
## List your development dependencies here. Development dependencies are
|
53
|
+
## those that are only needed during development
|
54
|
+
s.add_development_dependency('minitest')
|
55
|
+
|
56
|
+
## Leave this section as-is. It will be automatically generated from the
|
57
|
+
## contents of your Git repository via the gemspec task. DO NOT REMOVE
|
58
|
+
## THE MANIFEST COMMENTS, they are used as delimiters by the task.
|
59
|
+
# = MANIFEST =
|
60
|
+
s.files = %w[
|
61
|
+
Gemfile
|
62
|
+
LICENSE
|
63
|
+
README.md
|
64
|
+
Rakefile
|
65
|
+
bin/dnsync
|
66
|
+
dnsync.gemspec
|
67
|
+
lib/dnsync.rb
|
68
|
+
lib/dnsync/answer.rb
|
69
|
+
lib/dnsync/cli.rb
|
70
|
+
lib/dnsync/dnsimple.rb
|
71
|
+
lib/dnsync/http_status.rb
|
72
|
+
lib/dnsync/nsone.rb
|
73
|
+
lib/dnsync/record.rb
|
74
|
+
lib/dnsync/record_identifier.rb
|
75
|
+
lib/dnsync/recurring_zone_updater.rb
|
76
|
+
lib/dnsync/zone.rb
|
77
|
+
lib/dnsync/zone_difference.rb
|
78
|
+
lib/dnsync/zone_updater.rb
|
79
|
+
test/record_identifier_test.rb
|
80
|
+
test/record_test.rb
|
81
|
+
test/test_helper.rb
|
82
|
+
]
|
83
|
+
# = MANIFEST =
|
84
|
+
|
85
|
+
## Test files will be grabbed from the file list. Make sure the path glob
|
86
|
+
## matches what you actually use.
|
87
|
+
s.test_files = s.files.select { |path| path =~ /^test\/.*\.rb/ }
|
88
|
+
end
|
@@ -0,0 +1,28 @@
|
|
1
|
+
module Dnsync
|
2
|
+
class Answer
|
3
|
+
include Comparable
|
4
|
+
|
5
|
+
attr_reader :content, :priority
|
6
|
+
|
7
|
+
def initialize(content, priority = nil)
|
8
|
+
unless content.present?
|
9
|
+
raise ArgumentError, 'content must be provided'
|
10
|
+
end
|
11
|
+
|
12
|
+
@content = content
|
13
|
+
@priority = priority
|
14
|
+
|
15
|
+
freeze
|
16
|
+
end
|
17
|
+
|
18
|
+
def <=>(other)
|
19
|
+
[ content, priority ] <=> [ other.content, other.priority ]
|
20
|
+
end
|
21
|
+
|
22
|
+
def hash
|
23
|
+
[ content, priority ].hash
|
24
|
+
end
|
25
|
+
|
26
|
+
alias_method :eql?, :==
|
27
|
+
end
|
28
|
+
end
|
data/lib/dnsync/cli.rb
ADDED
@@ -0,0 +1,207 @@
|
|
1
|
+
require 'optparse'
|
2
|
+
require 'configlet'
|
3
|
+
require 'pp'
|
4
|
+
require 'scrolls'
|
5
|
+
|
6
|
+
require 'dnsync/dnsimple'
|
7
|
+
require 'dnsync/nsone'
|
8
|
+
require 'dnsync/zone_difference'
|
9
|
+
require 'dnsync/zone_updater'
|
10
|
+
require 'dnsync/recurring_zone_updater'
|
11
|
+
require 'dnsync/http_status'
|
12
|
+
|
13
|
+
module Dnsync
|
14
|
+
class Cli
|
15
|
+
attr_reader :program_name
|
16
|
+
|
17
|
+
def initialize(argv)
|
18
|
+
@args = argv.dup
|
19
|
+
@program_name = File.basename($0)
|
20
|
+
end
|
21
|
+
|
22
|
+
def call
|
23
|
+
Configlet.prefix = 'dnsync'
|
24
|
+
Configlet.munge(:noop) { |v| v == "true" }
|
25
|
+
Configlet.munge(:monitor_frequency) { |v| v.present? ? v.to_i : v }
|
26
|
+
|
27
|
+
read_env_from_file(File.expand_path("~/.dnsync.env"))
|
28
|
+
read_env_from_file(File.expand_path("../../../.env", __FILE__))
|
29
|
+
read_env_from_file('.env')
|
30
|
+
|
31
|
+
opts = OptionParser.new do |opts|
|
32
|
+
opts.banner = "usage: #{program_name} [options] <command> [<args>]"
|
33
|
+
|
34
|
+
opts.separator ""
|
35
|
+
opts.separator commands_help
|
36
|
+
|
37
|
+
opts.separator ""
|
38
|
+
opts.separator "Options:"
|
39
|
+
|
40
|
+
opts.on("--dnsimple-email=EMAIL", "DNSimple email address") do |v|
|
41
|
+
Configlet[:dnsimple_email] = v
|
42
|
+
end
|
43
|
+
opts.on("--dnsimple-token=TOKEN", "DNSimple token") do |v|
|
44
|
+
Configlet[:dnsimple_token] = v
|
45
|
+
end
|
46
|
+
opts.on("--nsone-token=TOKEN", "NSONE token") do |v|
|
47
|
+
Configlet[:nsone_token] = v
|
48
|
+
end
|
49
|
+
opts.on("--domain=DOMAIN", "Domain to synchronize") do |v|
|
50
|
+
Configlet[:domain] = v
|
51
|
+
end
|
52
|
+
opts.on("--monitor-frequency=FREQUENCY", "Frequency to check DNSimple for updates") do |v|
|
53
|
+
Configlet[:monitor_frequency] = v
|
54
|
+
end
|
55
|
+
opts.on("--status-port=PORT", "Port to run status HTTP server on") do |v|
|
56
|
+
Configlet[:status_port] = v
|
57
|
+
end
|
58
|
+
opts.on("--noop", "Don't do any write operations") do |v|
|
59
|
+
Configlet[:noop] = v.to_s
|
60
|
+
end
|
61
|
+
opts.on("-h", "--help", "This help message") do
|
62
|
+
puts opts
|
63
|
+
exit(1)
|
64
|
+
end
|
65
|
+
end
|
66
|
+
@args = opts.order(@args)
|
67
|
+
|
68
|
+
case command = @args.shift
|
69
|
+
when 'dump'
|
70
|
+
dump
|
71
|
+
when 'diff'
|
72
|
+
diff
|
73
|
+
when 'sync'
|
74
|
+
sync
|
75
|
+
when 'monitor'
|
76
|
+
monitor
|
77
|
+
else
|
78
|
+
puts "#{program_name}: '#{command}' is not a command. see '#{program_name} --help'."
|
79
|
+
exit(1)
|
80
|
+
end
|
81
|
+
|
82
|
+
exit(0)
|
83
|
+
end
|
84
|
+
|
85
|
+
def commands_help
|
86
|
+
unindent(<<-EOF)
|
87
|
+
The available commands are:
|
88
|
+
sync Perform a one-time synchronization from DNSimple to NSONE
|
89
|
+
monitor Perform continual synchronization from DNSimple to NSONE
|
90
|
+
|
91
|
+
EOF
|
92
|
+
end
|
93
|
+
|
94
|
+
def dump
|
95
|
+
case command = @args.shift
|
96
|
+
when 'nsone'
|
97
|
+
nsone = Nsone.new(Configlet[:nsone_token], Configlet[:domain])
|
98
|
+
records = nsone.zone
|
99
|
+
else
|
100
|
+
dnsimple = Dnsimple.new(Configlet[:dnsimple_email],
|
101
|
+
Configlet[:dnsimple_token], Configlet[:domain])
|
102
|
+
records = dnsimple.zone
|
103
|
+
end
|
104
|
+
|
105
|
+
pp records
|
106
|
+
end
|
107
|
+
|
108
|
+
def diff
|
109
|
+
nsone = Nsone.new(Configlet[:nsone_token], Configlet[:domain])
|
110
|
+
dnsimple = Dnsimple.new(Configlet[:dnsimple_email],
|
111
|
+
Configlet[:dnsimple_token], Configlet[:domain])
|
112
|
+
|
113
|
+
diff = ZoneDifference.new(nsone.zone, dnsimple.zone,
|
114
|
+
%w(NS SOA))
|
115
|
+
|
116
|
+
puts " ---- added ---- "
|
117
|
+
pp diff.added
|
118
|
+
|
119
|
+
puts " ---- removed ---- "
|
120
|
+
pp diff.removed
|
121
|
+
|
122
|
+
puts " ---- changed ---- "
|
123
|
+
pp diff.changed
|
124
|
+
end
|
125
|
+
|
126
|
+
def sync
|
127
|
+
nsone = Nsone.new(Configlet[:nsone_token], Configlet[:domain])
|
128
|
+
dnsimple = Dnsimple.new(Configlet[:dnsimple_email],
|
129
|
+
Configlet[:dnsimple_token], Configlet[:domain])
|
130
|
+
|
131
|
+
nsone_zone, dnsimple_zone = nil
|
132
|
+
|
133
|
+
Scrolls.log(:zone => Configlet[:domain], :from => :nsone) do
|
134
|
+
nsone_zone = nsone.zone
|
135
|
+
end
|
136
|
+
|
137
|
+
Scrolls.log(:zone => Configlet[:domain], :from => :dnsimple) do
|
138
|
+
dnsimple_zone = dnsimple.zone
|
139
|
+
end
|
140
|
+
|
141
|
+
diff = ZoneDifference.new(nsone_zone, dnsimple_zone,
|
142
|
+
%w(NS SOA))
|
143
|
+
|
144
|
+
if Configlet[:noop]
|
145
|
+
puts "Would be: Adding: #{diff.added.length} Updating: #{diff.changed.length} Removing: #{diff.removed.length}"
|
146
|
+
else
|
147
|
+
updater = ZoneUpdater.new(diff, nsone)
|
148
|
+
|
149
|
+
Scrolls.log(:zone => Configlet[:domain], :action => :updating, :to => :nsone,
|
150
|
+
:adding => diff.added.length, :updating => diff.changed.length,
|
151
|
+
:removing => diff.removed.length) do
|
152
|
+
updater.call
|
153
|
+
end
|
154
|
+
end
|
155
|
+
end
|
156
|
+
|
157
|
+
def monitor
|
158
|
+
nsone = Nsone.new(Configlet[:nsone_token], Configlet[:domain])
|
159
|
+
dnsimple = Dnsimple.new(Configlet[:dnsimple_email],
|
160
|
+
Configlet[:dnsimple_token], Configlet[:domain])
|
161
|
+
|
162
|
+
updater = RecurringZoneUpdater.new(dnsimple, nsone,
|
163
|
+
Configlet[:monitor_frequency] || 10)
|
164
|
+
updater.start
|
165
|
+
|
166
|
+
if status_port = Configlet[:status_port]
|
167
|
+
puts "Starting status server on #{status_port}"
|
168
|
+
status = HttpStatus.new(status_port, updater)
|
169
|
+
status.start
|
170
|
+
end
|
171
|
+
|
172
|
+
rd, wr = IO.pipe
|
173
|
+
Thread.new do
|
174
|
+
# Wait for a signal
|
175
|
+
rd.read(1)
|
176
|
+
updater.stop
|
177
|
+
|
178
|
+
if status
|
179
|
+
status.stop
|
180
|
+
end
|
181
|
+
end
|
182
|
+
|
183
|
+
%w(QUIT HUP INT TERM).each do |sig|
|
184
|
+
Signal.trap(sig) { wr.write('x') }
|
185
|
+
end
|
186
|
+
|
187
|
+
updater.join
|
188
|
+
if status
|
189
|
+
status.join
|
190
|
+
end
|
191
|
+
end
|
192
|
+
|
193
|
+
private
|
194
|
+
def unindent(string)
|
195
|
+
indentation = string[/\A\s*/]
|
196
|
+
string.strip.gsub(/^#{indentation}/, "") + "\n"
|
197
|
+
end
|
198
|
+
|
199
|
+
def read_env_from_file(filename)
|
200
|
+
if File.exists?(filename)
|
201
|
+
IO.read(filename).split(/\n+/).each do |line|
|
202
|
+
ENV[$1] = $2 if line =~ /^([^#][^=]*)=(.+)$/
|
203
|
+
end
|
204
|
+
end
|
205
|
+
end
|
206
|
+
end
|
207
|
+
end
|