vagrant-rubydns 0.0.1 → 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore CHANGED
@@ -17,3 +17,4 @@ test/version_tmp
17
17
  tmp
18
18
 
19
19
  .vagrant
20
+ .vagrant_dns.json
data/Rakefile CHANGED
@@ -1 +1,10 @@
1
- require "bundler/gem_tasks"
1
+ require 'bundler/gem_tasks'
2
+
3
+ require 'rake/testtask'
4
+
5
+ Rake::TestTask.new do |t|
6
+ t.pattern = "test/**/*_test.rb"
7
+ t.libs << 'test'
8
+ end
9
+
10
+ task :default => :test
@@ -4,8 +4,8 @@
4
4
  # If you're doing development and want to run this locally you can simply
5
5
  # `bundle install`, then prefix all your vagrant commands with `bundle exec`, like so:
6
6
  #
7
- # bundle exec vagrant up
8
- # bundle exce vagrant halt
7
+ # VAGRANT_CWD=examples bundle exec vagrant up
8
+ # VAGRANT_CWD=examples bundle exce vagrant halt
9
9
  #
10
10
 
11
11
  # This line is unnecessary when the plugin is installed normally; it's just
@@ -6,35 +6,14 @@ module VagrantRubydns
6
6
  end
7
7
 
8
8
  def call(env)
9
- @env = env
9
+ hostname, ip_address = Util.host_and_ip(env[:machine])
10
10
 
11
- configfile = ".vagrant_dns.json"
11
+ env[:ui].info "setting #{hostname} to #{ip_address} in in DNS"
12
12
 
13
- config = JSON.parse(File.read(configfile))
14
-
15
- config[hostname] = ip_address
16
-
17
- File.open(configfile, "w") do |f|
18
- f.write(JSON.pretty_generate(config))
19
- end
13
+ Config.set(hostname, ip_address)
20
14
 
21
15
  @app.call(env)
22
16
  end
23
-
24
- def hostname
25
- @env[:machine].config.vm.hostname
26
- end
27
-
28
- def ip_address
29
- @env[:machine].config.vm.networks.each do |type, options|
30
- if type == :private_network && options[:ip].is_a?(String)
31
- return options[:ip]
32
- end
33
- end
34
-
35
- nil
36
- end
37
-
38
17
  end
39
18
  end
40
19
  end
@@ -3,29 +3,17 @@ module VagrantRubydns
3
3
  class Teardown
4
4
  def initialize(app, env)
5
5
  @app = app
6
- puts "I AM RUBYDNS TEARDOWN init"
7
6
  end
8
7
 
9
8
  def call(env)
10
- @env = env
9
+ hostname = Util.hostname(env[:machine])
11
10
 
12
- configfile = ".vagrant_dns.json"
11
+ env[:ui].info "removing #{hostname} from DNS"
13
12
 
14
- config = JSON.parse(File.read(configfile))
15
-
16
- config.delete hostname
17
-
18
- File.open(configfile, "w") do |f|
19
- f.write(JSON.pretty_generate(config))
20
- end
13
+ Config.delete(hostname)
21
14
 
22
15
  @app.call(env)
23
16
  end
24
-
25
- def hostname
26
- @env[:machine].config.vm.hostname
27
- end
28
-
29
17
  end
30
18
  end
31
19
  end
@@ -1,7 +1,6 @@
1
1
  module VagrantRubydns
2
2
  class Command < Vagrant.plugin('2', :command)
3
3
  def execute
4
- puts "i am rubydns command"
5
4
  require_relative 'server'
6
5
  Server.run
7
6
  0
@@ -0,0 +1,37 @@
1
+ module VagrantRubydns
2
+ class Config
3
+ CONFIG_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 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))
34
+ end
35
+ end
36
+ end
37
+ end
@@ -1,14 +1,14 @@
1
1
  module VagrantRubydns
2
2
  class Plugin < Vagrant.plugin('2')
3
- name "vagrant-rubydns"
3
+ name 'vagrant-rubydns'
4
4
 
5
- command "rubydns" do
6
- require_relative "command"
5
+ command 'rubydns' do
6
+ require_relative 'command'
7
7
  Command
8
8
  end
9
9
 
10
- provisioner "rubydns" do
11
- require_relative "provisioner"
10
+ provisioner 'rubydns' do
11
+ require_relative 'provisioner'
12
12
  Provisioner
13
13
  end
14
14
 
@@ -7,19 +7,31 @@ module VagrantRubydns
7
7
  def configure(root_config)
8
8
  end
9
9
 
10
- def ip_addresses
11
- @machine.config.vm.networks.map { |type, params|
12
- params if type == :private_network
13
- }.compact.map { |params| params[:ip] }
14
- end
10
+ def provision
11
+ # hostname, ip_address = Util.host_and_ip(@machine)
12
+ # Config.set(hostname, ip_address)
13
+
14
+ @machine.env.ui.info "setting up machine's DNS to point to our server"
15
15
 
16
- def hostname
17
- @machine.config.vm.hostname
16
+ 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'
17
+ command = %Q(iptables -C #{redirect_dns_to_unpriviledged_port_tcp} 2> /dev/null || iptables -A #{redirect_dns_to_unpriviledged_port_tcp})
18
+ _run_command(command)
19
+
20
+ 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'
21
+ command = %Q(iptables -C #{redirect_dns_to_unpriviledged_port_udp} 2> /dev/null || iptables -A #{redirect_dns_to_unpriviledged_port_udp})
22
+ _run_command(command)
23
+
24
+ command = %q(sed -i'' -e 's/10.0.2.3/10.0.2.2/' /etc/resolv.conf)
25
+ _run_command(command)
18
26
  end
19
27
 
20
- def provision
21
- puts "hi i am rubydns provisioner"
22
- puts "i wanna set #{ip_addresses.inspect} to #{hostname}"
28
+ def _run_command(command)
29
+ @machine.communicate.sudo(command) do |data, type|
30
+ if [:stderr, :stdout].include?(type)
31
+ color = (type == :stdout) ? :green : :red
32
+ @machine.env.ui.info(data.chomp, :color => color, :prefix => false)
33
+ end
34
+ end
23
35
  end
24
36
  end
25
37
  end
@@ -18,21 +18,13 @@ module VagrantRubydns
18
18
  Pathname('.vagrant_dns.json')
19
19
  end
20
20
 
21
- def self.entries
22
- if configfile.exist?
23
- JSON.parse(File.read('.vagrant_dns.json'))
24
- else
25
- {}
26
- end
27
- end
28
-
29
21
  def self.run
30
- # Start the RubyDNS server
31
22
  server = self
32
23
  RubyDNS::run_server(:listen => INTERFACES) do
33
24
  match(/vagrant.dev/, IN::A) do |transaction|
34
- if server.entries.has_key? transaction.name
35
- transaction.respond!(server.entries[transaction.name])
25
+ ip = Config.get(transaction.name)
26
+ if ip
27
+ transaction.respond!(ip)
36
28
  else
37
29
  transaction.passthrough!(server.upstream)
38
30
  end
@@ -0,0 +1,25 @@
1
+ module VagrantRubydns
2
+ module Util
3
+ def self.host_and_ip(machine)
4
+ [hostname(machine), ip_address(machine)]
5
+ end
6
+
7
+ def self.hostname(machine)
8
+ return nil unless machine
9
+
10
+ machine.config.vm.hostname
11
+ end
12
+
13
+ def self.ip_address(machine)
14
+ return nil unless machine
15
+
16
+ machine.config.vm.networks.each do |type, options|
17
+ if type == :private_network && options[:ip].is_a?(String)
18
+ return options[:ip]
19
+ end
20
+ end
21
+
22
+ nil
23
+ end
24
+ end
25
+ end
@@ -1,5 +1,5 @@
1
1
  module Vagrant
2
2
  module Rubydns
3
- VERSION = "0.0.1"
3
+ VERSION = "0.0.2"
4
4
  end
5
5
  end
@@ -1,12 +1,12 @@
1
1
  begin
2
- require "vagrant"
2
+ require 'vagrant'
3
3
  rescue LoadError
4
- raise "The Vagrant RubyDNS plugin must be run within Vagrant."
4
+ raise 'The Vagrant RubyDNS plugin must be run within Vagrant.'
5
5
  end
6
6
 
7
- require "vagrant-rubydns/version"
8
- require "vagrant-rubydns/plugin"
9
-
10
- module VagrantRubydns
11
- end
7
+ require 'vagrant-rubydns/config'
8
+ require 'vagrant-rubydns/plugin'
9
+ require 'vagrant-rubydns/util'
10
+ require 'vagrant-rubydns/version'
12
11
 
12
+ module VagrantRubydns; end
@@ -0,0 +1,4 @@
1
+ class FakeUI
2
+ def self.info(*args)
3
+ end
4
+ end
@@ -0,0 +1,38 @@
1
+ $:.push(File.expand_path('../../lib', __FILE__))
2
+
3
+ require 'bundler/setup'
4
+ require 'minitest/spec'
5
+
6
+ require 'support/fake_ui'
7
+
8
+ require 'vagrant-rubydns'
9
+
10
+ # must be called before minitest/autorun to ensure proper at_exit ordering
11
+ MiniTest::Unit.after_tests { VagrantRubydns::Config.clear! }
12
+
13
+ require 'minitest/autorun'
14
+
15
+ def fake_environment_with_machine(hostname, ip)
16
+ provider_cls = Class.new do
17
+ def initialize(machine)
18
+ end
19
+ end
20
+
21
+ env = Vagrant::Environment.new
22
+
23
+ machine = Vagrant::Machine.new(
24
+ 'fake_machine',
25
+ 'fake_provider',
26
+ provider_cls,
27
+ 'provider_config',
28
+ env.config_global,
29
+ Pathname('data_dir'),
30
+ 'box',
31
+ env
32
+ )
33
+
34
+ machine.config.vm.hostname = hostname
35
+ machine.config.vm.network :private_network, ip: ip
36
+
37
+ { machine: machine, ui: FakeUI }
38
+ end
@@ -0,0 +1,30 @@
1
+ require 'test_helper'
2
+ require 'vagrant-rubydns/action/setup'
3
+
4
+ module VagrantRubydns
5
+ module Action
6
+ describe Setup do
7
+ it "calls the next app in the chain" do
8
+ env = {called: false, ui: FakeUI}
9
+ app = lambda { |e| e[:called] = true }
10
+
11
+ setup = Setup.new(app, nil)
12
+ setup.call(env)
13
+
14
+ env[:called].must_equal true
15
+ end
16
+
17
+ it "stores the machine's hostname => ip address in the config" do
18
+ Config.clear!
19
+
20
+ app = Proc.new {}
21
+ setup = Setup.new(app, nil)
22
+
23
+ env = fake_environment_with_machine('somehost.vagrant.dev', '1.2.3.4')
24
+ setup.call(env)
25
+
26
+ Config.get('somehost.vagrant.dev').must_equal '1.2.3.4'
27
+ end
28
+ end
29
+ end
30
+ end
@@ -0,0 +1,31 @@
1
+ require 'test_helper'
2
+ require 'vagrant-rubydns/action/teardown'
3
+
4
+ module VagrantRubydns
5
+ module Action
6
+ describe Teardown do
7
+ it "calls the next app in the chain" do
8
+ env = {called: false, ui: FakeUI}
9
+ app = lambda { |e| e[:called] = true }
10
+
11
+ teardown = Teardown.new(app, nil)
12
+ teardown.call(env)
13
+
14
+ env[:called].must_equal true
15
+ end
16
+
17
+ it "stores the machine's hostname => ip address in the config" do
18
+ Config.set('somehost.vagrant.dev', '1.2.3.4')
19
+
20
+ app = Proc.new {}
21
+ teardown = Teardown.new(app, nil)
22
+
23
+ env = fake_environment_with_machine('somehost.vagrant.dev', '1.2.3.4')
24
+ teardown.call(env)
25
+
26
+ Config.get('somehost.vagrant.dev').must_equal nil
27
+ end
28
+ end
29
+ end
30
+ end
31
+
@@ -0,0 +1,49 @@
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
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.1
4
+ version: 0.0.2
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -71,15 +71,22 @@ files:
71
71
  - LICENSE.txt
72
72
  - README.md
73
73
  - Rakefile
74
- - Vagrantfile
74
+ - examples/Vagrantfile
75
75
  - lib/vagrant-rubydns.rb
76
76
  - lib/vagrant-rubydns/action/setup.rb
77
77
  - lib/vagrant-rubydns/action/teardown.rb
78
78
  - lib/vagrant-rubydns/command.rb
79
+ - lib/vagrant-rubydns/config.rb
79
80
  - lib/vagrant-rubydns/plugin.rb
80
81
  - lib/vagrant-rubydns/provisioner.rb
81
82
  - lib/vagrant-rubydns/server.rb
83
+ - lib/vagrant-rubydns/util.rb
82
84
  - lib/vagrant-rubydns/version.rb
85
+ - test/support/fake_ui.rb
86
+ - test/test_helper.rb
87
+ - test/vagrant-rubydns/action/setup_test.rb
88
+ - test/vagrant-rubydns/action/teardown_test.rb
89
+ - test/vagrant-rubydns/config_test.rb
83
90
  - vagrant-rubydns.gemspec
84
91
  homepage: ''
85
92
  licenses:
@@ -106,5 +113,10 @@ rubygems_version: 1.8.23
106
113
  signing_key:
107
114
  specification_version: 3
108
115
  summary: a simple dns server for vagrant guests
109
- test_files: []
116
+ test_files:
117
+ - test/support/fake_ui.rb
118
+ - test/test_helper.rb
119
+ - test/vagrant-rubydns/action/setup_test.rb
120
+ - test/vagrant-rubydns/action/teardown_test.rb
121
+ - test/vagrant-rubydns/config_test.rb
110
122
  has_rdoc: