vagrant-rubydns 0.0.2 → 0.0.3

Sign up to get free protection for your applications and to get access to all the features.
data/README.md CHANGED
@@ -21,16 +21,26 @@ Bring up a machine that has a private network IP address and a hostname (see the
21
21
 
22
22
  $ vagrant up
23
23
 
24
- And you should be able to get your hostname from the local server:
24
+ And you should be able to get your hostname from your host:
25
25
 
26
26
  $ dig -p 10053 @localhost myhost.vagrant.dev
27
-
28
- If you shut down your guest, the entry will be gone! Any DNS queries that do not match will be passed through to an upstream DNS server, so this will be able to serve as the one-stop shop for your guests DNS needs.
27
+
28
+ You can also make this visible to the guest by using the provisioner, which will set `resolv.conf` and `iptables` rules such that DNS points at our server:
29
+
30
+ config.vm.provision :rubydns
31
+
32
+ If you shut down your guest, the entries associated with it will be removed.
33
+
34
+ You can add static host entries to the DNS server in your Vagrantfile like so:
35
+
36
+ config.rubydns.host 'myhost.example.com', '1.2.3.4'
37
+
38
+ Any DNS queries that do not match will be passed through to an upstream DNS server, so this will be able to serve as the one-stop shop for your guests' DNS needs.
29
39
 
30
40
  ## Work in Progress - Lots to do!
31
41
 
32
- * There's a stub provisioner in here - we'll use that to automatically point guests to our DNS server as they come up.
33
- * Lots of static values that need configurin' - config location, ports, TLD, etc.
42
+ * The provisioner assumes resolv.conf-based DNS and iptables-based firewall.
43
+ * Lots of static values that need configurin' - config location, ports, etc.
34
44
  * Tests tests tests.
35
45
 
36
46
  ## Contributing
data/examples/Vagrantfile CHANGED
@@ -20,4 +20,7 @@ Vagrant.configure("2") do |config|
20
20
  config.vm.network :private_network, ip: '172.16.32.111'
21
21
 
22
22
  config.vm.hostname = "myhost.vagrant.dev"
23
+
24
+ config.rubydns.host 'static1.example.com', '1.2.3.4'
25
+ config.rubydns.host 'static2.example.com', '2.3.4.5'
23
26
  end
@@ -4,8 +4,8 @@ rescue LoadError
4
4
  raise 'The Vagrant RubyDNS plugin must be run within Vagrant.'
5
5
  end
6
6
 
7
- require 'vagrant-rubydns/config'
8
7
  require 'vagrant-rubydns/plugin'
8
+ require 'vagrant-rubydns/store'
9
9
  require 'vagrant-rubydns/util'
10
10
  require 'vagrant-rubydns/version'
11
11
 
@@ -6,13 +6,22 @@ module VagrantRubydns
6
6
  end
7
7
 
8
8
  def call(env)
9
- hostname, ip_address = Util.host_and_ip(env[:machine])
10
-
11
- env[:ui].info "setting #{hostname} to #{ip_address} in in DNS"
9
+ setup_machine_dns(env)
10
+ setup_static_dns(env)
11
+ @app.call(env)
12
+ end
12
13
 
13
- Config.set(hostname, ip_address)
14
+ def setup_machine_dns(env)
15
+ hostname, ip_address = Util.host_and_ip(env[:machine])
16
+ env[:ui].info "[rubydns] adding machine entry: #{hostname} => #{ip_address}"
17
+ Store.set(hostname, ip_address)
18
+ end
14
19
 
15
- @app.call(env)
20
+ def setup_static_dns(env)
21
+ env[:global_config].rubydns.hosts.each do |hostname, ip_address|
22
+ env[:ui].info "[rubydns] adding static entry: #{hostname} => #{ip_address}"
23
+ Store.set hostname, ip_address
24
+ end
16
25
  end
17
26
  end
18
27
  end
@@ -6,13 +6,22 @@ module VagrantRubydns
6
6
  end
7
7
 
8
8
  def call(env)
9
- hostname = Util.hostname(env[:machine])
10
-
11
- env[:ui].info "removing #{hostname} from DNS"
9
+ teardown_static_dns(env)
10
+ teardown_machine_dns(env)
11
+ @app.call(env)
12
+ end
12
13
 
13
- Config.delete(hostname)
14
+ def teardown_machine_dns(env)
15
+ hostname = Util.hostname(env[:machine])
16
+ env[:ui].info "[rubydns] removing machine entry: #{hostname}"
17
+ Store.delete(hostname)
18
+ end
14
19
 
15
- @app.call(env)
20
+ def teardown_static_dns(env)
21
+ env[:global_config].rubydns.hosts.each do |hostname, _|
22
+ env[:ui].info "[rubydns] removing static entry: #{hostname}"
23
+ Store.delete hostname
24
+ end
16
25
  end
17
26
  end
18
27
  end
@@ -1,36 +1,18 @@
1
1
  module VagrantRubydns
2
- class Config
3
- CONFIG_FILE = Pathname('.vagrant_dns.json')
2
+ class Config < Vagrant.plugin('2', :config)
3
+ attr_accessor :hosts
4
4
 
5
- def self.set(key, value)
6
- write(current_config.merge(key => value))
5
+ def initialize
6
+ @hosts = {}
7
7
  end
8
8
 
9
- def self.delete(key)
10
- write(current_config.reject { |k, _| k == key })
9
+ def host(hostname, ip_address)
10
+ @hosts[hostname] = ip_address
11
11
  end
12
12
 
13
- def self.get(key)
14
- current_config[key]
15
- end
16
-
17
- def self.clear!
18
- write({})
19
- end
20
-
21
- protected
22
-
23
- def self.current_config
24
- if CONFIG_FILE.exist?
25
- JSON.parse(File.read(CONFIG_FILE))
26
- else
27
- {}
28
- end
29
- end
30
-
31
- def self.write(config)
32
- File.open(CONFIG_FILE, "w") do |f|
33
- f.write(JSON.pretty_generate(config))
13
+ def merge(other)
14
+ super.tap do |result|
15
+ result.hosts = @hosts.merge(other.hosts)
34
16
  end
35
17
  end
36
18
  end
@@ -7,6 +7,11 @@ module VagrantRubydns
7
7
  Command
8
8
  end
9
9
 
10
+ config 'rubydns' do
11
+ require_relative 'config'
12
+ Config
13
+ end
14
+
10
15
  provisioner 'rubydns' do
11
16
  require_relative 'provisioner'
12
17
  Provisioner
@@ -8,9 +8,6 @@ module VagrantRubydns
8
8
  end
9
9
 
10
10
  def provision
11
- # hostname, ip_address = Util.host_and_ip(@machine)
12
- # Config.set(hostname, ip_address)
13
-
14
11
  @machine.env.ui.info "setting up machine's DNS to point to our server"
15
12
 
16
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,15 +14,11 @@ 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.configfile
18
- Pathname('.vagrant_dns.json')
19
- end
20
-
21
17
  def self.run
22
18
  server = self
23
19
  RubyDNS::run_server(:listen => INTERFACES) do
24
- match(/vagrant.dev/, IN::A) do |transaction|
25
- ip = Config.get(transaction.name)
20
+ match(/.*/, IN::A) do |transaction|
21
+ ip = Store.get(transaction.name)
26
22
  if ip
27
23
  transaction.respond!(ip)
28
24
  else
@@ -0,0 +1,37 @@
1
+ module VagrantRubydns
2
+ class Store
3
+ STORE_FILE = Pathname('.vagrant_dns.json')
4
+
5
+ def self.set(key, value)
6
+ write(current_config.merge(key => value))
7
+ end
8
+
9
+ def self.delete(key)
10
+ write(current_config.reject { |k, _| k == key })
11
+ end
12
+
13
+ def self.get(key)
14
+ current_config[key]
15
+ end
16
+
17
+ def self.clear!
18
+ write({})
19
+ end
20
+
21
+ protected
22
+
23
+ def self.current_config
24
+ if STORE_FILE.exist?
25
+ JSON.parse(File.read(STORE_FILE))
26
+ else
27
+ {}
28
+ end
29
+ end
30
+
31
+ def self.write(config)
32
+ File.open(STORE_FILE, "w") do |f|
33
+ f.write(JSON.pretty_generate(config))
34
+ end
35
+ end
36
+ end
37
+ end
@@ -1,5 +1,5 @@
1
1
  module Vagrant
2
2
  module Rubydns
3
- VERSION = "0.0.2"
3
+ VERSION = "0.0.3"
4
4
  end
5
5
  end
data/test/test_helper.rb CHANGED
@@ -8,7 +8,7 @@ require 'support/fake_ui'
8
8
  require 'vagrant-rubydns'
9
9
 
10
10
  # must be called before minitest/autorun to ensure proper at_exit ordering
11
- MiniTest::Unit.after_tests { VagrantRubydns::Config.clear! }
11
+ MiniTest::Unit.after_tests { VagrantRubydns::Store.clear! }
12
12
 
13
13
  require 'minitest/autorun'
14
14
 
@@ -14,8 +14,8 @@ module VagrantRubydns
14
14
  env[:called].must_equal true
15
15
  end
16
16
 
17
- it "stores the machine's hostname => ip address in the config" do
18
- Config.clear!
17
+ it "stores the machine's hostname => ip address" do
18
+ Store.clear!
19
19
 
20
20
  app = Proc.new {}
21
21
  setup = Setup.new(app, nil)
@@ -23,7 +23,7 @@ module VagrantRubydns
23
23
  env = fake_environment_with_machine('somehost.vagrant.dev', '1.2.3.4')
24
24
  setup.call(env)
25
25
 
26
- Config.get('somehost.vagrant.dev').must_equal '1.2.3.4'
26
+ Store.get('somehost.vagrant.dev').must_equal '1.2.3.4'
27
27
  end
28
28
  end
29
29
  end
@@ -14,8 +14,8 @@ module VagrantRubydns
14
14
  env[:called].must_equal true
15
15
  end
16
16
 
17
- it "stores the machine's hostname => ip address in the config" do
18
- Config.set('somehost.vagrant.dev', '1.2.3.4')
17
+ it "stores the machine's hostname => ip address" do
18
+ Store.set('somehost.vagrant.dev', '1.2.3.4')
19
19
 
20
20
  app = Proc.new {}
21
21
  teardown = Teardown.new(app, nil)
@@ -23,7 +23,7 @@ module VagrantRubydns
23
23
  env = fake_environment_with_machine('somehost.vagrant.dev', '1.2.3.4')
24
24
  teardown.call(env)
25
25
 
26
- Config.get('somehost.vagrant.dev').must_equal nil
26
+ Store.get('somehost.vagrant.dev').must_equal nil
27
27
  end
28
28
  end
29
29
  end
@@ -0,0 +1,49 @@
1
+ require 'test_helper'
2
+
3
+ module VagrantRubydns
4
+ describe Store do
5
+ before { Store.clear! }
6
+
7
+ describe "set" do
8
+ it "sets the key to the value and makes it available for getting" do
9
+ Store.set('foo', 'bar')
10
+
11
+ Store.get('foo').must_equal 'bar'
12
+ end
13
+
14
+ it "allows updating keys that already exist" do
15
+ Store.set('foo', 'bar')
16
+ Store.set('foo', 'qux')
17
+
18
+ Store.get('foo').must_equal 'qux'
19
+ end
20
+ end
21
+
22
+ describe "get" do
23
+ it "returns nil for unset values" do
24
+ Store.get('notakey').must_equal nil
25
+ end
26
+
27
+ it "returns the latest set value (no caching)" do
28
+ Store.set('foo', 'first')
29
+ Store.get('foo').must_equal 'first'
30
+ Store.set('foo', 'second')
31
+ Store.get('foo').must_equal 'second'
32
+ Store.delete('foo')
33
+ Store.get('foo').must_equal nil
34
+ end
35
+ end
36
+
37
+ describe "delete" do
38
+ it "removes the key from the store" do
39
+ Store.set('now', 'you see me')
40
+
41
+ Store.get('now').must_equal 'you see me'
42
+
43
+ Store.delete('now')
44
+
45
+ Store.get('now').must_equal nil # you don't!
46
+ end
47
+ end
48
+ end
49
+ 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.2
4
+ version: 0.0.3
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -80,13 +80,14 @@ files:
80
80
  - lib/vagrant-rubydns/plugin.rb
81
81
  - lib/vagrant-rubydns/provisioner.rb
82
82
  - lib/vagrant-rubydns/server.rb
83
+ - lib/vagrant-rubydns/store.rb
83
84
  - lib/vagrant-rubydns/util.rb
84
85
  - lib/vagrant-rubydns/version.rb
85
86
  - test/support/fake_ui.rb
86
87
  - test/test_helper.rb
87
88
  - test/vagrant-rubydns/action/setup_test.rb
88
89
  - test/vagrant-rubydns/action/teardown_test.rb
89
- - test/vagrant-rubydns/config_test.rb
90
+ - test/vagrant-rubydns/store_test.rb
90
91
  - vagrant-rubydns.gemspec
91
92
  homepage: ''
92
93
  licenses:
@@ -118,5 +119,5 @@ test_files:
118
119
  - test/test_helper.rb
119
120
  - test/vagrant-rubydns/action/setup_test.rb
120
121
  - test/vagrant-rubydns/action/teardown_test.rb
121
- - test/vagrant-rubydns/config_test.rb
122
+ - test/vagrant-rubydns/store_test.rb
122
123
  has_rdoc:
@@ -1,49 +0,0 @@
1
- require 'test_helper'
2
-
3
- module VagrantRubydns
4
- describe Config do
5
- before { Config.clear! }
6
-
7
- describe "set" do
8
- it "sets the key to the value and makes it available for getting" do
9
- Config.set('foo', 'bar')
10
-
11
- Config.get('foo').must_equal 'bar'
12
- end
13
-
14
- it "allows updating keys that already exist" do
15
- Config.set('foo', 'bar')
16
- Config.set('foo', 'qux')
17
-
18
- Config.get('foo').must_equal 'qux'
19
- end
20
- end
21
-
22
- describe "get" do
23
- it "returns nil for unset values" do
24
- Config.get('notakey').must_equal nil
25
- end
26
-
27
- it "returns the latest set value (no caching)" do
28
- Config.set('foo', 'first')
29
- Config.get('foo').must_equal 'first'
30
- Config.set('foo', 'second')
31
- Config.get('foo').must_equal 'second'
32
- Config.delete('foo')
33
- Config.get('foo').must_equal nil
34
- end
35
- end
36
-
37
- describe "delete" do
38
- it "removes the key from the store" do
39
- Config.set('now', 'you see me')
40
-
41
- Config.get('now').must_equal 'you see me'
42
-
43
- Config.delete('now')
44
-
45
- Config.get('now').must_equal nil # you don't!
46
- end
47
- end
48
- end
49
- end