vagrant-rubydns 0.0.6 → 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/README.md +2 -5
- data/examples/Vagrantfile +0 -1
- data/lib/vagrant-rubydns.rb +12 -2
- data/lib/vagrant-rubydns/action/redirect_dns.rb +35 -0
- data/lib/vagrant-rubydns/action/setup.rb +14 -2
- data/lib/vagrant-rubydns/action/teardown.rb +18 -2
- data/lib/vagrant-rubydns/command.rb +36 -3
- data/lib/vagrant-rubydns/dependent_vms.rb +40 -0
- data/lib/vagrant-rubydns/plugin.rb +2 -5
- data/lib/vagrant-rubydns/server.rb +14 -3
- data/lib/vagrant-rubydns/store.rb +22 -10
- data/lib/vagrant-rubydns/version.rb +1 -1
- data/test/support/clear_dependent_vms.rb +10 -0
- data/test/support/disable_server_daemon.rb +37 -0
- data/test/support/fake_working_dir.rb +16 -0
- data/test/test_helper.rb +7 -5
- data/test/vagrant-rubydns/action/setup_test.rb +34 -9
- data/test/vagrant-rubydns/action/teardown_test.rb +50 -6
- data/test/vagrant-rubydns/dependent_vms_test.rb +33 -0
- data/test/vagrant-rubydns/store_test.rb +24 -18
- metadata +12 -3
- data/lib/vagrant-rubydns/provisioner.rb +0 -34
data/README.md
CHANGED
@@ -1,9 +1,10 @@
|
|
1
1
|
# Vagrant RubyDNS Plugin
|
2
2
|
|
3
|
+
Simple DNS that's visible on both the guest and the host.
|
4
|
+
|
3
5
|
Spins up a small RubyDNS server that you can point your VMs at, automatically
|
4
6
|
registers/deregisters IP addresseses of guests as they come up and down.
|
5
7
|
|
6
|
-
**Goal**: simple DNS that's visible on both the guest and the host
|
7
8
|
|
8
9
|
## Installation
|
9
10
|
|
@@ -13,10 +14,6 @@ Install under Vagrant (1.1 or later):
|
|
13
14
|
|
14
15
|
## Usage
|
15
16
|
|
16
|
-
Spin up the DNS server
|
17
|
-
|
18
|
-
$ vagrant rubydns
|
19
|
-
|
20
17
|
Enable the plugin in your `Vagrantfile`:
|
21
18
|
|
22
19
|
config.rubydns.enable
|
data/examples/Vagrantfile
CHANGED
data/lib/vagrant-rubydns.rb
CHANGED
@@ -4,9 +4,19 @@ rescue LoadError
|
|
4
4
|
raise 'The Vagrant RubyDNS plugin must be run within Vagrant.'
|
5
5
|
end
|
6
6
|
|
7
|
+
module VagrantRubydns
|
8
|
+
def self.working_dir
|
9
|
+
@working_dir ||= Pathname(File.expand_path('~/.vagrant_rubydns')).tap(&:mkpath)
|
10
|
+
end
|
11
|
+
|
12
|
+
def self.working_dir=(working_dir)
|
13
|
+
@working_dir = Pathname(working_dir).tap(&:mkpath)
|
14
|
+
end
|
15
|
+
end
|
16
|
+
|
7
17
|
require 'vagrant-rubydns/plugin'
|
8
18
|
require 'vagrant-rubydns/store'
|
19
|
+
require 'vagrant-rubydns/dependent_vms'
|
20
|
+
require 'vagrant-rubydns/server'
|
9
21
|
require 'vagrant-rubydns/util'
|
10
22
|
require 'vagrant-rubydns/version'
|
11
|
-
|
12
|
-
module VagrantRubydns; end
|
@@ -0,0 +1,35 @@
|
|
1
|
+
module VagrantRubydns
|
2
|
+
module Action
|
3
|
+
class RedirectDns
|
4
|
+
def initialize(app, env)
|
5
|
+
@app = app
|
6
|
+
end
|
7
|
+
|
8
|
+
def call(env)
|
9
|
+
@machine = env[:machine]
|
10
|
+
|
11
|
+
@machine.ui.info "setting up machine's DNS to point to our server"
|
12
|
+
|
13
|
+
redirect_dns('10.0.2.3', 53, '10.0.2.2', 10053)
|
14
|
+
end
|
15
|
+
|
16
|
+
def redirect_dns(original_server, original_port, target_server, target_port)
|
17
|
+
%w[tcp udp].each do |protocol|
|
18
|
+
rule = "OUTPUT -t nat -d #{original_server} -p #{protocol} --dport #{original_port} -j DNAT --to-destination #{target_server}:#{target_port}"
|
19
|
+
command = %Q(iptables -C #{rule} 2> /dev/null || iptables -A #{rule})
|
20
|
+
_run_command(command)
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
def _run_command(command)
|
25
|
+
@machine.communicate.sudo(command) do |data, type|
|
26
|
+
if [:stderr, :stdout].include?(type)
|
27
|
+
color = (type == :stdout) ? :green : :red
|
28
|
+
@machine.env.ui.info(data.chomp, :color => color, :prefix => false)
|
29
|
+
end
|
30
|
+
end
|
31
|
+
end
|
32
|
+
end
|
33
|
+
end
|
34
|
+
end
|
35
|
+
|
@@ -7,22 +7,34 @@ module VagrantRubydns
|
|
7
7
|
|
8
8
|
def call(env)
|
9
9
|
if env[:global_config].rubydns.enabled?
|
10
|
+
DependentVMs.add(env[:machine])
|
11
|
+
start_server_if_necessary(env)
|
10
12
|
setup_machine_dns(env)
|
11
13
|
setup_static_dns(env)
|
14
|
+
env[:machine].config.vm.provision :rubydns
|
12
15
|
end
|
13
16
|
@app.call(env)
|
14
17
|
end
|
15
18
|
|
19
|
+
def start_server_if_necessary(env)
|
20
|
+
if Server.running?
|
21
|
+
env[:ui].info "[rubydns] dns server already running"
|
22
|
+
else
|
23
|
+
env[:ui].info "[rubydns] starting dns server"
|
24
|
+
Server.start
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
16
28
|
def setup_machine_dns(env)
|
17
29
|
hostname, ip_address = Util.host_and_ip(env[:machine])
|
18
30
|
env[:ui].info "[rubydns] adding machine entry: #{hostname} => #{ip_address}"
|
19
|
-
Store.set(hostname, ip_address)
|
31
|
+
Store.hosts.set(hostname, ip_address)
|
20
32
|
end
|
21
33
|
|
22
34
|
def setup_static_dns(env)
|
23
35
|
env[:global_config].rubydns.hosts.each do |hostname, ip_address|
|
24
36
|
env[:ui].info "[rubydns] adding static entry: #{hostname} => #{ip_address}"
|
25
|
-
Store.set hostname, ip_address
|
37
|
+
Store.hosts.set hostname, ip_address
|
26
38
|
end
|
27
39
|
end
|
28
40
|
end
|
@@ -9,20 +9,36 @@ module VagrantRubydns
|
|
9
9
|
if env[:global_config].rubydns.enabled?
|
10
10
|
teardown_static_dns(env)
|
11
11
|
teardown_machine_dns(env)
|
12
|
+
|
13
|
+
DependentVMs.remove(env[:machine])
|
14
|
+
stop_server_if_necessary(env)
|
12
15
|
end
|
13
16
|
@app.call(env)
|
14
17
|
end
|
15
18
|
|
19
|
+
def stop_server_if_necessary(env)
|
20
|
+
if Server.running?
|
21
|
+
if DependentVMs.none?
|
22
|
+
env[:ui].info "[rubydns] no dependent vms left, stopping dns server"
|
23
|
+
Server.stop
|
24
|
+
else
|
25
|
+
env[:ui].info "[rubydns] there are dependent vms left, leaving dns server"
|
26
|
+
end
|
27
|
+
else
|
28
|
+
env[:ui].info "[rubydns] dns server already stopped"
|
29
|
+
end
|
30
|
+
end
|
31
|
+
|
16
32
|
def teardown_machine_dns(env)
|
17
33
|
hostname = Util.hostname(env[:machine])
|
18
34
|
env[:ui].info "[rubydns] removing machine entry: #{hostname}"
|
19
|
-
Store.delete(hostname)
|
35
|
+
Store.hosts.delete(hostname)
|
20
36
|
end
|
21
37
|
|
22
38
|
def teardown_static_dns(env)
|
23
39
|
env[:global_config].rubydns.hosts.each do |hostname, _|
|
24
40
|
env[:ui].info "[rubydns] removing static entry: #{hostname}"
|
25
|
-
Store.delete hostname
|
41
|
+
Store.hosts.delete hostname
|
26
42
|
end
|
27
43
|
end
|
28
44
|
end
|
@@ -1,9 +1,42 @@
|
|
1
1
|
module VagrantRubydns
|
2
2
|
class Command < Vagrant.plugin('2', :command)
|
3
|
+
DAEMON_COMMANDS = %w(start stop restart status)
|
4
|
+
DEPENDENT_VM_COMMADNS = %w(dependentvms)
|
5
|
+
|
3
6
|
def execute
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
+
ARGV.shift # flush rubydns from ARGV, RExec wants to use it for daemon commands
|
8
|
+
|
9
|
+
command = ARGV.first
|
10
|
+
if DAEMON_COMMANDS.include?(command)
|
11
|
+
Server.daemonize
|
12
|
+
elsif command == 'dependentvms'
|
13
|
+
if DependentVMs.any?
|
14
|
+
@env.ui.info(DependentVMs.list.map { |dvm| " - #{dvm}" }.join("\n"))
|
15
|
+
else
|
16
|
+
@env.ui.info("No dependent VMs")
|
17
|
+
end
|
18
|
+
else
|
19
|
+
boom("'#{command}' is not a command")
|
20
|
+
end
|
21
|
+
|
22
|
+
0 # happy exit code
|
23
|
+
end
|
24
|
+
|
25
|
+
def boom(msg)
|
26
|
+
raise Vagrant::Errors::CLIInvalidOptions, :help => usage(msg)
|
27
|
+
end
|
28
|
+
|
29
|
+
def usage(msg); <<-EOS.gsub(/^ /, '')
|
30
|
+
ERROR: #{msg}
|
31
|
+
|
32
|
+
vagrant rubydns <command>
|
33
|
+
|
34
|
+
commands:
|
35
|
+
{start|stop|restart|status}
|
36
|
+
control the rubydns server daemon
|
37
|
+
dependentvms
|
38
|
+
list vms currently dependent on the rubydns server
|
39
|
+
EOS
|
7
40
|
end
|
8
41
|
end
|
9
42
|
end
|
@@ -0,0 +1,40 @@
|
|
1
|
+
require 'fileutils'
|
2
|
+
|
3
|
+
#
|
4
|
+
# Keep track of dependent VMs.
|
5
|
+
#
|
6
|
+
# Poor man's race condition defense - touch and rm files in a directory and count them.
|
7
|
+
#
|
8
|
+
module VagrantRubydns
|
9
|
+
class DependentVMs
|
10
|
+
extend Enumerable
|
11
|
+
|
12
|
+
def self.each(&block)
|
13
|
+
(dir.directory? ? dir.children : []).each(&block)
|
14
|
+
end
|
15
|
+
|
16
|
+
def self.add(machine)
|
17
|
+
FileUtils.touch(file_for(machine))
|
18
|
+
end
|
19
|
+
|
20
|
+
def self.remove(machine)
|
21
|
+
file_for(machine).tap { |f| f.delete if f.exist? }
|
22
|
+
end
|
23
|
+
|
24
|
+
def self.list
|
25
|
+
self.map { |path| path.basename.to_s }
|
26
|
+
end
|
27
|
+
|
28
|
+
def self.clear!
|
29
|
+
dir.rmtree
|
30
|
+
end
|
31
|
+
|
32
|
+
def self.file_for(machine)
|
33
|
+
dir.join(Util.hostname(machine))
|
34
|
+
end
|
35
|
+
|
36
|
+
def self.dir
|
37
|
+
VagrantRubydns.working_dir.join('dependent_vms').tap(&:mkpath)
|
38
|
+
end
|
39
|
+
end
|
40
|
+
end
|
@@ -12,14 +12,11 @@ module VagrantRubydns
|
|
12
12
|
Config
|
13
13
|
end
|
14
14
|
|
15
|
-
provisioner 'rubydns' do
|
16
|
-
require_relative 'provisioner'
|
17
|
-
Provisioner
|
18
|
-
end
|
19
|
-
|
20
15
|
action_hook 'rubydns_setup', :machine_action_up do |hook|
|
21
16
|
require_relative 'action/setup'
|
17
|
+
require_relative 'action/redirect_dns'
|
22
18
|
hook.before(VagrantPlugins::ProviderVirtualBox::Action::Boot, Action::Setup)
|
19
|
+
hook.after(VagrantPlugins::ProviderVirtualBox::Action::Boot, Action::RedirectDns)
|
23
20
|
end
|
24
21
|
|
25
22
|
action_hook 'rubydns_teardown', :machine_action_halt do |hook|
|
@@ -1,7 +1,7 @@
|
|
1
1
|
require 'rubydns'
|
2
2
|
|
3
3
|
module VagrantRubydns
|
4
|
-
class Server
|
4
|
+
class Server < RExec::Daemon::Base
|
5
5
|
|
6
6
|
INTERFACES = [
|
7
7
|
[:udp, "0.0.0.0", 10053],
|
@@ -14,13 +14,24 @@ module VagrantRubydns
|
|
14
14
|
@upstream ||= RubyDNS::Resolver.new([[:udp, "8.8.8.8", 53], [:tcp, "8.8.8.8", 53]])
|
15
15
|
end
|
16
16
|
|
17
|
+
def self.logfile
|
18
|
+
VagrantRubydns.working_dir.join('server.log')
|
19
|
+
end
|
20
|
+
|
21
|
+
# For RExec
|
22
|
+
@@base_directory = VagrantRubydns.working_dir
|
23
|
+
|
24
|
+
def self.running?
|
25
|
+
RExec::Daemon::ProcessFile.status(self) == :running
|
26
|
+
end
|
27
|
+
|
17
28
|
def self.run
|
18
29
|
server = self
|
19
30
|
RubyDNS::run_server(:listen => INTERFACES) do
|
20
|
-
logger.level = Logger::INFO
|
31
|
+
self.logger.level = Logger::INFO
|
21
32
|
|
22
33
|
match(/.*/, IN::A) do |transaction|
|
23
|
-
ip = Store.get(transaction.name)
|
34
|
+
ip = Store.hosts.get(transaction.name)
|
24
35
|
if ip
|
25
36
|
transaction.respond!(ip)
|
26
37
|
else
|
@@ -1,35 +1,47 @@
|
|
1
1
|
module VagrantRubydns
|
2
2
|
class Store
|
3
|
-
|
3
|
+
def self.hosts
|
4
|
+
@hosts ||= new(VagrantRubydns.working_dir.join('hosts.json'))
|
5
|
+
end
|
6
|
+
|
7
|
+
attr_accessor :backing_file
|
8
|
+
|
9
|
+
def initialize(backing_file)
|
10
|
+
@backing_file = Pathname(backing_file)
|
11
|
+
end
|
4
12
|
|
5
|
-
def
|
13
|
+
def set(key, value)
|
6
14
|
write(current_config.merge(key => value))
|
7
15
|
end
|
8
16
|
|
9
|
-
def
|
17
|
+
def delete(key)
|
10
18
|
write(current_config.reject { |k, _| k == key })
|
11
19
|
end
|
12
20
|
|
13
|
-
def
|
21
|
+
def get(key)
|
14
22
|
current_config[key]
|
15
23
|
end
|
16
24
|
|
17
|
-
def
|
25
|
+
def clear!
|
18
26
|
write({})
|
19
27
|
end
|
20
28
|
|
21
29
|
protected
|
22
30
|
|
23
|
-
def
|
24
|
-
if
|
25
|
-
|
31
|
+
def current_config
|
32
|
+
if backing_file.exist?
|
33
|
+
begin
|
34
|
+
JSON.parse(File.read(backing_file))
|
35
|
+
rescue JSON::ParserError
|
36
|
+
{}
|
37
|
+
end
|
26
38
|
else
|
27
39
|
{}
|
28
40
|
end
|
29
41
|
end
|
30
42
|
|
31
|
-
def
|
32
|
-
File.open(
|
43
|
+
def write(config)
|
44
|
+
File.open(backing_file, "w") do |f|
|
33
45
|
f.write(JSON.pretty_generate(config))
|
34
46
|
end
|
35
47
|
end
|
@@ -0,0 +1,37 @@
|
|
1
|
+
class VagrantRubydns::Server < RExec::Daemon::Base
|
2
|
+
def self.start
|
3
|
+
@start_count += 1
|
4
|
+
end
|
5
|
+
|
6
|
+
def self.stop
|
7
|
+
@stop_count += 1
|
8
|
+
end
|
9
|
+
|
10
|
+
def self.running?
|
11
|
+
@start_count > 0
|
12
|
+
end
|
13
|
+
|
14
|
+
def self.clear!
|
15
|
+
@start_count = 0
|
16
|
+
@stop_count = 0
|
17
|
+
end
|
18
|
+
|
19
|
+
def self.start_count
|
20
|
+
@start_count
|
21
|
+
end
|
22
|
+
|
23
|
+
def self.stop_count
|
24
|
+
@stop_count
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
28
|
+
module FakeServerHooks
|
29
|
+
def setup
|
30
|
+
super
|
31
|
+
VagrantRubydns::Server.clear!
|
32
|
+
end
|
33
|
+
end
|
34
|
+
|
35
|
+
class MiniTest::Spec
|
36
|
+
include FakeServerHooks
|
37
|
+
end
|
@@ -0,0 +1,16 @@
|
|
1
|
+
module FakeWorkingDirHooks
|
2
|
+
def setup
|
3
|
+
super
|
4
|
+
VagrantRubydns.working_dir = '/tmp/vagrant_rubydns_test_working_dir'
|
5
|
+
end
|
6
|
+
|
7
|
+
def teardown
|
8
|
+
super
|
9
|
+
VagrantRubydns.working_dir.rmtree if VagrantRubydns.working_dir.directory?
|
10
|
+
end
|
11
|
+
end
|
12
|
+
|
13
|
+
class MiniTest::Spec
|
14
|
+
include FakeWorkingDirHooks
|
15
|
+
end
|
16
|
+
|
data/test/test_helper.rb
CHANGED
@@ -3,13 +3,8 @@ $:.push(File.expand_path('../../lib', __FILE__))
|
|
3
3
|
require 'bundler/setup'
|
4
4
|
require 'minitest/spec'
|
5
5
|
|
6
|
-
require 'support/fake_ui'
|
7
|
-
|
8
6
|
require 'vagrant-rubydns'
|
9
7
|
|
10
|
-
# must be called before minitest/autorun to ensure proper at_exit ordering
|
11
|
-
MiniTest::Unit.after_tests { VagrantRubydns::Store.clear! }
|
12
|
-
|
13
8
|
require 'minitest/autorun'
|
14
9
|
|
15
10
|
def fake_environment(extras={})
|
@@ -43,3 +38,10 @@ def fake_environment_with_machine(hostname, ip)
|
|
43
38
|
|
44
39
|
{ machine: machine, ui: FakeUI, global_config: env.config_global }
|
45
40
|
end
|
41
|
+
|
42
|
+
# order is important on these
|
43
|
+
require 'support/fake_working_dir'
|
44
|
+
require 'support/clear_dependent_vms'
|
45
|
+
|
46
|
+
require 'support/fake_ui'
|
47
|
+
require 'support/disable_server_daemon'
|
@@ -6,39 +6,64 @@ module VagrantRubydns
|
|
6
6
|
describe Setup do
|
7
7
|
it "calls the next app in the chain" do
|
8
8
|
env = fake_environment(called: false)
|
9
|
-
|
10
9
|
app = lambda { |e| e[:called] = true }
|
11
|
-
|
12
10
|
setup = Setup.new(app, nil)
|
11
|
+
|
13
12
|
setup.call(env)
|
14
13
|
|
15
14
|
env[:called].must_equal true
|
16
15
|
end
|
17
16
|
|
18
17
|
it "stores the machine's hostname => ip address" do
|
19
|
-
Store.clear!
|
20
|
-
|
21
18
|
app = Proc.new {}
|
22
19
|
setup = Setup.new(app, nil)
|
20
|
+
env = fake_environment_with_machine('somehost.vagrant.dev', '1.2.3.4')
|
21
|
+
|
22
|
+
setup.call(env)
|
23
23
|
|
24
|
+
Store.hosts.get('somehost.vagrant.dev').must_equal '1.2.3.4'
|
25
|
+
end
|
26
|
+
|
27
|
+
it "records the booting host as a dependent VM" do
|
28
|
+
app = Proc.new {}
|
29
|
+
setup = Setup.new(app, nil)
|
24
30
|
env = fake_environment_with_machine('somehost.vagrant.dev', '1.2.3.4')
|
31
|
+
|
25
32
|
setup.call(env)
|
26
33
|
|
27
|
-
|
34
|
+
DependentVMs.list.must_equal %w[somehost.vagrant.dev]
|
28
35
|
end
|
29
36
|
|
30
|
-
it "
|
31
|
-
|
37
|
+
it "starts the rubydns server if it's not already started" do
|
38
|
+
app = Proc.new {}
|
39
|
+
setup = Setup.new(app, nil)
|
40
|
+
env = fake_environment_with_machine('somehost.vagrant.dev', '1.2.3.4')
|
41
|
+
|
42
|
+
setup.call(env)
|
43
|
+
|
44
|
+
Server.running?.must_equal true
|
45
|
+
end
|
32
46
|
|
47
|
+
it "does not attempt to start the server if it's already up" do
|
33
48
|
app = Proc.new {}
|
34
49
|
setup = Setup.new(app, nil)
|
50
|
+
env = fake_environment_with_machine('somehost.vagrant.dev', '1.2.3.4')
|
51
|
+
|
52
|
+
Server.start
|
53
|
+
setup.call(env)
|
35
54
|
|
55
|
+
Server.start_count.must_equal 1
|
56
|
+
end
|
57
|
+
|
58
|
+
it "does nothing if it is not enabled via config" do
|
59
|
+
app = Proc.new {}
|
60
|
+
setup = Setup.new(app, nil)
|
36
61
|
env = fake_environment_with_machine('somehost.vagrant.dev', '1.2.3.4')
|
37
|
-
env[:global_config].rubydns.disable
|
38
62
|
|
63
|
+
env[:global_config].rubydns.disable
|
39
64
|
setup.call(env)
|
40
65
|
|
41
|
-
Store.get('somehost.vagrant.dev').must_equal nil
|
66
|
+
Store.hosts.get('somehost.vagrant.dev').must_equal nil
|
42
67
|
end
|
43
68
|
end
|
44
69
|
end
|
@@ -6,31 +6,75 @@ module VagrantRubydns
|
|
6
6
|
describe Teardown do
|
7
7
|
it "calls the next app in the chain" do
|
8
8
|
env = fake_environment(called: false)
|
9
|
-
|
10
9
|
app = lambda { |e| e[:called] = true }
|
11
|
-
|
12
10
|
teardown = Teardown.new(app, nil)
|
11
|
+
|
13
12
|
teardown.call(env)
|
14
13
|
|
15
14
|
env[:called].must_equal true
|
16
15
|
end
|
17
16
|
|
18
17
|
it "clears the machine's hostname => ip address" do
|
19
|
-
|
18
|
+
app = Proc.new {}
|
19
|
+
teardown = Teardown.new(app, nil)
|
20
|
+
env = fake_environment_with_machine('somehost.vagrant.dev', '1.2.3.4')
|
21
|
+
|
22
|
+
Store.hosts.set('somehost.vagrant.dev', '1.2.3.4')
|
23
|
+
teardown.call(env)
|
24
|
+
|
25
|
+
Store.hosts.get('somehost.vagrant.dev').must_equal nil
|
26
|
+
end
|
20
27
|
|
28
|
+
it "removes the machine as a dependent VM" do
|
21
29
|
app = Proc.new {}
|
22
30
|
teardown = Teardown.new(app, nil)
|
31
|
+
env = fake_environment_with_machine('somehost.vagrant.dev', '1.2.3.4')
|
23
32
|
|
33
|
+
DependentVMs.add(env[:machine])
|
34
|
+
teardown.call(env)
|
35
|
+
|
36
|
+
DependentVMs.list.must_equal []
|
37
|
+
end
|
38
|
+
|
39
|
+
it "stops the rubydns server when there are no dependent machines left" do
|
40
|
+
app = Proc.new {}
|
41
|
+
teardown = Teardown.new(app, nil)
|
24
42
|
env = fake_environment_with_machine('somehost.vagrant.dev', '1.2.3.4')
|
43
|
+
|
44
|
+
Server.start
|
45
|
+
teardown.call(env)
|
46
|
+
|
47
|
+
Server.stop_count.must_equal 1
|
48
|
+
end
|
49
|
+
|
50
|
+
it "leaves the rubydns server when other dependent vms exist" do
|
51
|
+
app = Proc.new {}
|
52
|
+
teardown = Teardown.new(app, nil)
|
53
|
+
env = fake_environment_with_machine('somehost.vagrant.dev', '1.2.3.4')
|
54
|
+
|
55
|
+
other_env = fake_environment_with_machine('otherhost.vagrant.dev', '1.2.3.4')
|
56
|
+
DependentVMs.add(other_env[:machine])
|
57
|
+
|
58
|
+
Server.start
|
59
|
+
teardown.call(env)
|
60
|
+
|
61
|
+
Server.stop_count.must_equal 0
|
62
|
+
end
|
63
|
+
|
64
|
+
it "leaves the server alone if it's not running" do
|
65
|
+
app = Proc.new {}
|
66
|
+
teardown = Teardown.new(app, nil)
|
67
|
+
env = fake_environment_with_machine('somehost.vagrant.dev', '1.2.3.4')
|
68
|
+
|
25
69
|
teardown.call(env)
|
26
70
|
|
27
|
-
|
71
|
+
Server.stop_count.must_equal 0
|
28
72
|
end
|
29
73
|
|
30
74
|
it "does nothing when rubydns is disabled" do
|
31
75
|
# somewhat unrealistic since this entry shouldn't be there if it was
|
32
76
|
# disabled in the first place, but oh well
|
33
|
-
Store.set('somehost.vagrant.dev', '1.2.3.4')
|
77
|
+
Store.hosts.set('somehost.vagrant.dev', '1.2.3.4')
|
34
78
|
|
35
79
|
app = Proc.new {}
|
36
80
|
teardown = Teardown.new(app, nil)
|
@@ -40,7 +84,7 @@ module VagrantRubydns
|
|
40
84
|
|
41
85
|
teardown.call(env)
|
42
86
|
|
43
|
-
Store.get('somehost.vagrant.dev').must_equal '1.2.3.4'
|
87
|
+
Store.hosts.get('somehost.vagrant.dev').must_equal '1.2.3.4'
|
44
88
|
end
|
45
89
|
end
|
46
90
|
end
|
@@ -0,0 +1,33 @@
|
|
1
|
+
require 'test_helper'
|
2
|
+
|
3
|
+
module VagrantRubydns
|
4
|
+
describe DependentVMs do
|
5
|
+
describe "any?" do
|
6
|
+
it "reports false when nothing has happened" do
|
7
|
+
DependentVMs.any?.must_equal false
|
8
|
+
end
|
9
|
+
|
10
|
+
it "reports true once a machine has been added" do
|
11
|
+
env = fake_environment_with_machine('recordme.example.dev', '1.2.3.4')
|
12
|
+
DependentVMs.add(env[:machine])
|
13
|
+
DependentVMs.any?.must_equal true
|
14
|
+
end
|
15
|
+
|
16
|
+
it "reports false if a machine has been added then removed" do
|
17
|
+
env = fake_environment_with_machine('recordme.example.dev', '1.2.3.4')
|
18
|
+
DependentVMs.add(env[:machine])
|
19
|
+
DependentVMs.remove(env[:machine])
|
20
|
+
DependentVMs.any?.must_equal false
|
21
|
+
end
|
22
|
+
|
23
|
+
it "reports true if not all machines have been removed" do
|
24
|
+
first_env = fake_environment_with_machine('recordme.example.dev', '1.2.3.4')
|
25
|
+
second_env = fake_environment_with_machine('alsome.example.dev', '2.3.4.5')
|
26
|
+
DependentVMs.add(first_env[:machine])
|
27
|
+
DependentVMs.add(second_env[:machine])
|
28
|
+
DependentVMs.remove(first_env[:machine])
|
29
|
+
DependentVMs.any?.must_equal true
|
30
|
+
end
|
31
|
+
end
|
32
|
+
end
|
33
|
+
end
|
@@ -2,47 +2,53 @@ require 'test_helper'
|
|
2
2
|
|
3
3
|
module VagrantRubydns
|
4
4
|
describe Store do
|
5
|
-
before {
|
6
|
-
|
5
|
+
before {
|
6
|
+
@store = Store.new(Tempfile.new(%w[rubydns_test_store .json]))
|
7
|
+
}
|
8
|
+
|
9
|
+
after {
|
10
|
+
@store.backing_file.unlink
|
11
|
+
}
|
12
|
+
|
7
13
|
describe "set" do
|
8
14
|
it "sets the key to the value and makes it available for getting" do
|
9
|
-
|
15
|
+
@store.set('foo', 'bar')
|
10
16
|
|
11
|
-
|
17
|
+
@store.get('foo').must_equal 'bar'
|
12
18
|
end
|
13
19
|
|
14
20
|
it "allows updating keys that already exist" do
|
15
|
-
|
16
|
-
|
21
|
+
@store.set('foo', 'bar')
|
22
|
+
@store.set('foo', 'qux')
|
17
23
|
|
18
|
-
|
24
|
+
@store.get('foo').must_equal 'qux'
|
19
25
|
end
|
20
26
|
end
|
21
27
|
|
22
28
|
describe "get" do
|
23
29
|
it "returns nil for unset values" do
|
24
|
-
|
30
|
+
@store.get('notakey').must_equal nil
|
25
31
|
end
|
26
32
|
|
27
33
|
it "returns the latest set value (no caching)" do
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
+
@store.set('foo', 'first')
|
35
|
+
@store.get('foo').must_equal 'first'
|
36
|
+
@store.set('foo', 'second')
|
37
|
+
@store.get('foo').must_equal 'second'
|
38
|
+
@store.delete('foo')
|
39
|
+
@store.get('foo').must_equal nil
|
34
40
|
end
|
35
41
|
end
|
36
42
|
|
37
43
|
describe "delete" do
|
38
44
|
it "removes the key from the store" do
|
39
|
-
|
45
|
+
@store.set('now', 'you see me')
|
40
46
|
|
41
|
-
|
47
|
+
@store.get('now').must_equal 'you see me'
|
42
48
|
|
43
|
-
|
49
|
+
@store.delete('now')
|
44
50
|
|
45
|
-
|
51
|
+
@store.get('now').must_equal nil # you don't!
|
46
52
|
end
|
47
53
|
end
|
48
54
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: vagrant-rubydns
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0
|
4
|
+
version: 0.1.0
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2013-
|
12
|
+
date: 2013-05-07 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: rubydns
|
@@ -73,20 +73,25 @@ files:
|
|
73
73
|
- Rakefile
|
74
74
|
- examples/Vagrantfile
|
75
75
|
- lib/vagrant-rubydns.rb
|
76
|
+
- lib/vagrant-rubydns/action/redirect_dns.rb
|
76
77
|
- lib/vagrant-rubydns/action/setup.rb
|
77
78
|
- lib/vagrant-rubydns/action/teardown.rb
|
78
79
|
- lib/vagrant-rubydns/command.rb
|
79
80
|
- lib/vagrant-rubydns/config.rb
|
81
|
+
- lib/vagrant-rubydns/dependent_vms.rb
|
80
82
|
- lib/vagrant-rubydns/plugin.rb
|
81
|
-
- lib/vagrant-rubydns/provisioner.rb
|
82
83
|
- lib/vagrant-rubydns/server.rb
|
83
84
|
- lib/vagrant-rubydns/store.rb
|
84
85
|
- lib/vagrant-rubydns/util.rb
|
85
86
|
- lib/vagrant-rubydns/version.rb
|
87
|
+
- test/support/clear_dependent_vms.rb
|
88
|
+
- test/support/disable_server_daemon.rb
|
86
89
|
- test/support/fake_ui.rb
|
90
|
+
- test/support/fake_working_dir.rb
|
87
91
|
- test/test_helper.rb
|
88
92
|
- test/vagrant-rubydns/action/setup_test.rb
|
89
93
|
- test/vagrant-rubydns/action/teardown_test.rb
|
94
|
+
- test/vagrant-rubydns/dependent_vms_test.rb
|
90
95
|
- test/vagrant-rubydns/store_test.rb
|
91
96
|
- vagrant-rubydns.gemspec
|
92
97
|
homepage: ''
|
@@ -115,9 +120,13 @@ signing_key:
|
|
115
120
|
specification_version: 3
|
116
121
|
summary: a simple dns server for vagrant guests
|
117
122
|
test_files:
|
123
|
+
- test/support/clear_dependent_vms.rb
|
124
|
+
- test/support/disable_server_daemon.rb
|
118
125
|
- test/support/fake_ui.rb
|
126
|
+
- test/support/fake_working_dir.rb
|
119
127
|
- test/test_helper.rb
|
120
128
|
- test/vagrant-rubydns/action/setup_test.rb
|
121
129
|
- test/vagrant-rubydns/action/teardown_test.rb
|
130
|
+
- test/vagrant-rubydns/dependent_vms_test.rb
|
122
131
|
- test/vagrant-rubydns/store_test.rb
|
123
132
|
has_rdoc:
|
@@ -1,34 +0,0 @@
|
|
1
|
-
module VagrantRubydns
|
2
|
-
class Provisioner < Vagrant.plugin('2', :provisioner)
|
3
|
-
def initialize(machine, config)
|
4
|
-
super
|
5
|
-
end
|
6
|
-
|
7
|
-
def configure(root_config)
|
8
|
-
end
|
9
|
-
|
10
|
-
def provision
|
11
|
-
@machine.env.ui.info "setting up machine's DNS to point to our server"
|
12
|
-
|
13
|
-
redirect_dns_to_unpriviledged_port_tcp = 'OUTPUT -t nat -d 10.0.2.2 -p tcp --dport 53 -j DNAT --to-destination 10.0.2.2:10053'
|
14
|
-
command = %Q(iptables -C #{redirect_dns_to_unpriviledged_port_tcp} 2> /dev/null || iptables -A #{redirect_dns_to_unpriviledged_port_tcp})
|
15
|
-
_run_command(command)
|
16
|
-
|
17
|
-
redirect_dns_to_unpriviledged_port_udp = 'OUTPUT -t nat -d 10.0.2.2 -p udp --dport 53 -j DNAT --to-destination 10.0.2.2:10053'
|
18
|
-
command = %Q(iptables -C #{redirect_dns_to_unpriviledged_port_udp} 2> /dev/null || iptables -A #{redirect_dns_to_unpriviledged_port_udp})
|
19
|
-
_run_command(command)
|
20
|
-
|
21
|
-
command = %q(sed -i'' -e 's/10.0.2.3/10.0.2.2/' /etc/resolv.conf)
|
22
|
-
_run_command(command)
|
23
|
-
end
|
24
|
-
|
25
|
-
def _run_command(command)
|
26
|
-
@machine.communicate.sudo(command) do |data, type|
|
27
|
-
if [:stderr, :stdout].include?(type)
|
28
|
-
color = (type == :stdout) ? :green : :red
|
29
|
-
@machine.env.ui.info(data.chomp, :color => color, :prefix => false)
|
30
|
-
end
|
31
|
-
end
|
32
|
-
end
|
33
|
-
end
|
34
|
-
end
|