vagrant-dns 0.1.0 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,14 @@
1
+ # v0.2.0
2
+
3
+ * Deactivate ipv6 support (was broken anyways)
4
+ * Add multiple patterns support
5
+ * Introduces the `tlds` config key, for managing multiple tlds. `tld` is still supported
6
+ * --ontop option to run the dns server on top
7
+ * removed the logger config in favor of a default logger
8
+ * Uses one central server now
9
+ * The DNS server is now reconfigured on every run
10
+
11
+ # v0.1.0
12
+
13
+ * Initial Release
14
+ * Support for A and AAAA configuration
data/README.md CHANGED
@@ -14,7 +14,7 @@ otherwise, use:
14
14
 
15
15
  ## Usage
16
16
 
17
- In addition to your networking config, configure a toplevel domain and a `host_name` for your machine. Optionally configure a logger and the listen options for the DNS server, this setting is global:
17
+ In addition to your networking config, configure a toplevel domain and a `host_name` for your machine. Optionally, configure a set of free matching patterns. Global configuration options can be given through the `VagrantDNS::Config` object:
18
18
 
19
19
  ```ruby
20
20
  Vagrant::Config.run do |config|
@@ -23,12 +23,14 @@ Vagrant::Config.run do |config|
23
23
  config.dns.tld = "dev"
24
24
 
25
25
  config.vm.host_name = "machine"
26
+
27
+ config.dns.patterns = [/^.*mysite.dev$/, /^.*myothersite.dev$/]
28
+
26
29
  config.vm.network :hostonly, "33.33.33.60"
27
30
  end
28
31
 
29
32
  # optional
30
33
  VagrantDNS::Config.logger = Logger.new("dns.log")
31
- VagrantDNS::Config.listen = [[:udp, "0.0.0.0", 5300]]
32
34
  ```
33
35
 
34
36
  Then, register the DNS server as a resolver:
@@ -37,7 +39,7 @@ Then, register the DNS server as a resolver:
37
39
  $ sudo vagrant dns --install
38
40
  ```
39
41
 
40
- On OS X, this will create a file `/etc/resolver/dev`, which tells OS X to resolve the TLD `.dev` by using the resolver given in this file.
42
+ On OS X, this will create a file `/etc/resolver/dev`, which tells OS X to resolve the TLD `.dev` by using the nameserver given in this file. You will have to rerun --install every time a tld is added.
41
43
 
42
44
  You can delete this file by running:
43
45
 
@@ -65,11 +67,22 @@ Finally, stop the server using:
65
67
  $ vagrant dns --stop
66
68
  ```
67
69
 
68
- # Issues
70
+ The DNS server will start automatically once the first VM is started.
71
+
72
+ ## VM options
73
+
74
+ * `vm.dns.tld`: Set the tld for the given virtual machine. No default.
75
+ * `vm.dns.tlds`: Set multiple tlds. Default: `[tld]`
76
+ * `vm.dns.patterns`: A list of domain patterns to match. Defaults to `[/^.*{host_name}.{tld}$/]`
77
+
78
+ ## Global Options
79
+
80
+ * `VagrantDNS::Config.listen`: an Array of Arrays describing interfaces to bind to. Defaults to `[[:udp, "127.0.0.1", 5300]]`.
81
+ * `VagrantDNS::Config.auto_run`: (re)start and reconfigure the server every time a machine is started. On by default.
82
+
83
+ ## Issues
69
84
 
70
- * Currently, you need one TLD per environment
71
- * Only A and AAAA records at the moment
72
- * Only one record per machine ("hostname.tld")
73
- * Advanced customization of record rules is not supported at the moment
85
+ * A records only
86
+ * no ipv6 support
74
87
  * OS X only
75
88
  * Alpha code
@@ -9,12 +9,13 @@ Vagrant::Config.run do |config|
9
9
 
10
10
  # Every Vagrant virtual environment requires a box to build off of.
11
11
  config.vm.box = "base"
12
-
12
+
13
13
  config.dns.tld = "dev"
14
-
14
+ config.dns.patterns = /^.*machine.dev$/
15
+
15
16
  config.vm.host_name = "machine"
16
17
  config.vm.network :hostonly, "33.33.33.60"
17
18
 
18
- VagrantDNS::Config.logger = Logger.new("dns.log")
19
+
19
20
  VagrantDNS::Config.listen = [[:udp, "0.0.0.0", 5300]]
20
21
  end
@@ -1,7 +1,13 @@
1
1
  require "vagrant-dns/version"
2
2
  require "vagrant-dns/command"
3
3
  require "vagrant-dns/config"
4
+ require "vagrant-dns/service"
5
+ require "vagrant-dns/installers/mac"
6
+ require "vagrant-dns/restart_middleware"
7
+ require "vagrant-dns/configurator"
4
8
 
5
9
  Vagrant.config_keys.register(:dns) { VagrantDNS::Config }
6
10
 
7
- Vagrant.commands.register(:dns) { VagrantDNS::Command }
11
+ Vagrant.commands.register(:dns) { VagrantDNS::Command }
12
+
13
+ Vagrant.actions[:start].insert Vagrant::Action::VM::Customize, VagrantDNS::RestartMiddleware
@@ -1,5 +1,6 @@
1
1
  require 'optparse'
2
2
  require 'daemons'
3
+ require 'rbconfig'
3
4
 
4
5
  module VagrantDNS
5
6
 
@@ -10,7 +11,7 @@ module VagrantDNS
10
11
  def execute
11
12
  options = {}
12
13
  opts = OptionParser.new do |opts|
13
- opts.banner = "Usage: vagrant dns [vm-name] [-i|--install] [-u|--uninstall] [-s|--start] [-S|--stop]"
14
+ opts.banner = "Usage: vagrant dns [vm-name] [-i|--install] [-u|--uninstall] [-s|--start] [-S|--stop] [-r|--restart] [-o|--ontop]"
14
15
  opts.separator ""
15
16
 
16
17
  opts.on("--install", "-i", "Install DNS config for machine domain") do
@@ -28,7 +29,14 @@ module VagrantDNS
28
29
  opts.on("--stop", "-s", "Stop the DNS service") do
29
30
  options[:stop] = true
30
31
  end
31
-
32
+
33
+ opts.on("--restart", "-r", "Restart the DNS service") do
34
+ options[:restart] = true
35
+ end
36
+
37
+ opts.on("--ontop", "-o", "Start the DNS service on top. Debugging only, this blocks Vagrant!") do
38
+ options[:ontop] = true
39
+ end
32
40
  end
33
41
 
34
42
  argv = parse_options(opts)
@@ -36,75 +44,34 @@ module VagrantDNS
36
44
 
37
45
  dns_options = []
38
46
 
39
- if argv.empty?
40
- with_target_vms(nil) { |vm| dns_options << get_dns_options(vm) }
41
- else
42
- argv.each do |vm_name|
43
- with_target_vms(vm_name) { |vm| dns_options << get_dns_options(vm) }
44
- end
45
- end
47
+ vms = argv unless argv.empty?
48
+ tmp_path = File.join(@env.tmp_path, "dns")
49
+
50
+ with_target_vms(vms) { |vm| VagrantDNS::Configurator.new(vm, tmp_path).run! }
51
+
52
+ service = VagrantDNS::Service.new(tmp_path, options)
46
53
 
47
54
  if options[:start]
48
- run_options = {:ARGV => ["start"]}
55
+ service.start!
49
56
  elsif options[:stop]
50
- run_options = {:ARGV => ["stop"]}
57
+ service.stop!
58
+ elsif options[:restart]
59
+ service.restart!
51
60
  end
52
-
53
- if options[:start] || options[:stop]
54
- Daemons.run_proc("dnsserver.rb", run_options) do
55
- require 'rubydns'
56
-
57
- RubyDNS::run_server(:listen => VagrantDNS::Config.listen) do
58
- self.logger = VagrantDNS::Config.logger if VagrantDNS::Config.logger
59
-
60
- dns_options.each do |opts|
61
- pattern = /^.*#{opts[:host_name]}.#{opts[:tld]}$/
62
-
63
- network = opts[:networks].first
64
- ip = network.last.first
65
61
 
66
- action = lambda { |match_data, transaction| transaction.respond!(ip) }
67
-
68
- match(pattern, :A, &action)
69
- match(pattern, :AAAA, &action)
70
- end
71
- end
72
- end
73
- end
74
-
75
- if options[:install]
76
- require 'fileutils'
77
- dns_options.each do |opts|
78
- port = VagrantDNS::Config.listen.first.last
79
- tld = opts[:tld]
80
- contents = <<-FILE
81
- nameserver 127.0.0.1
82
- port #{port}
83
- FILE
84
- FileUtils.mkdir_p("/etc/resolver")
85
- File.open(File.join("/etc/resolver", tld), "w") do |f|
86
- f << contents
62
+ if options[:install] || options[:uninstall]
63
+ if RbConfig::CONFIG["host_os"].match /darwin/
64
+ installer = VagrantDNS::Installers::Mac.new(tmp_path)
65
+
66
+ if options[:install]
67
+ installer.install!
68
+ elsif options[:uninstall]
69
+ installer.uninstall!
87
70
  end
71
+ else
72
+ raise 'installing and uninstalling is only supported on Mac OS X at the moment.'
88
73
  end
89
74
  end
90
-
91
- if options[:uninstall]
92
- require 'fileutils'
93
-
94
- dns_options.each do |opts|
95
- tld = opts[:tld]
96
- FileUtils.rm(File.join("/etc/resolver", tld))
97
- end
98
- end
99
- end
100
-
101
- protected
102
-
103
- def get_dns_options(vm)
104
- dns_options = vm.config.dns.to_hash
105
- dns_options[:host_name] = vm.config.vm.host_name
106
- dns_options[:networks] = vm.config.vm.networks
107
- dns_options
108
75
  end
109
76
  end
110
77
  end
@@ -1,26 +1,43 @@
1
1
  require 'logger'
2
2
 
3
3
  module VagrantDNS
4
-
5
4
  class Config < Vagrant::Config::Base
6
5
  class << self
7
- attr_accessor :listen, :logger
8
-
6
+ attr_accessor :listen, :logger, :ipv4only, :auto_run
7
+
9
8
  def listen
10
9
  @listen ||= [[:udp, "127.0.0.1", 5300]]
11
10
  end
11
+
12
+ def ipv4only
13
+ @ipv4only || @ipv4only.nil?
14
+ end
15
+
16
+ def auto_run
17
+ return true if @auto_run.nil?
18
+ @auto_run
19
+ end
20
+ end
21
+
22
+ attr_accessor :records, :tlds, :ipv4only, :patterns
23
+
24
+ def pattern=(pattern)
25
+ self.patterns = pattern
12
26
  end
13
27
 
14
- attr_accessor :records, :tld
15
-
28
+ def tld=(tld)
29
+ @tlds = Array(tld)
30
+ end
31
+
16
32
  # explicit hash, to get symbols in hash keys
17
33
  def to_hash
18
34
  {
35
+ :patterns => (patterns ? Array(patterns) : patterns),
19
36
  :records => records,
20
- :tld => tld
37
+ :tlds => tlds,
38
+ :ipv4only => ipv4only
21
39
  }
22
40
  end
23
-
24
41
  end
25
42
  end
26
43
 
@@ -0,0 +1,75 @@
1
+ require 'fileutils'
2
+ require 'yaml'
3
+
4
+ module VagrantDNS
5
+ class Configurator
6
+ attr_accessor :vm, :tmp_path
7
+
8
+ def initialize(vm, tmp_path)
9
+ @vm = vm
10
+ @tmp_path = tmp_path
11
+ end
12
+
13
+ def run!
14
+ regenerate_resolvers!
15
+ ensure_deamon_env!
16
+ register_patterns!
17
+ end
18
+
19
+ private
20
+ def regenerate_resolvers!
21
+ FileUtils.mkdir_p(resolver_folder)
22
+ port = VagrantDNS::Config.listen.first.last
23
+ tlds = dns_options(vm)[:tlds]
24
+
25
+ tlds.each do |tld|
26
+ File.open(File.join(resolver_folder, tld), "w") do |f|
27
+ f << resolver_file(port)
28
+ end
29
+ end
30
+ end
31
+
32
+ def register_patterns!
33
+ registry = YAML.load(File.read(config_file)) if File.exists?(config_file)
34
+ registry ||= {}
35
+ opts = dns_options(vm)
36
+ patterns = opts[:patterns] || opts[:tlds].map { |tld| /^.*#{opts[:host_name]}.#{tld}$/ }
37
+ network = opts[:networks].first
38
+ ip = network.last.first
39
+
40
+ patterns.each do |p|
41
+ p = p.source if p.respond_to? :source # Regexp#to_s is unusable
42
+ registry[p] = ip
43
+ end
44
+
45
+ File.open(config_file, "w") { |f| f << YAML.dump(registry) }
46
+ end
47
+
48
+ def dns_options(vm)
49
+ dns_options = vm.config.dns.to_hash
50
+ dns_options[:host_name] = vm.config.vm.host_name
51
+ dns_options[:networks] = vm.config.vm.networks
52
+ dns_options
53
+ end
54
+
55
+ def resolver_file(port)
56
+ contents = <<-FILE
57
+ # this file is generated by vagrant-dns
58
+ nameserver 127.0.0.1
59
+ port #{port}
60
+ FILE
61
+ end
62
+
63
+ def resolver_folder
64
+ File.join(tmp_path, "resolver")
65
+ end
66
+
67
+ def ensure_deamon_env!
68
+ FileUtils.mkdir_p(File.join(tmp_path, "daemon"))
69
+ end
70
+
71
+ def config_file
72
+ File.join(tmp_path, "config")
73
+ end
74
+ end
75
+ end
@@ -0,0 +1,38 @@
1
+ module VagrantDNS
2
+ module Installers
3
+ class Mac
4
+ attr_accessor :tmp_path, :install_path
5
+
6
+ def initialize(tmp_path, install_path = "/etc/resolver")
7
+ self.tmp_path, self.install_path = tmp_path, install_path
8
+ end
9
+
10
+ def install!
11
+ require 'fileutils'
12
+
13
+ registered_resolvers.each do |r|
14
+ FileUtils.ln_s(registered_resolvers, "/etc/resolver", :force => true)
15
+ end
16
+ rescue Errno::EACCES => e
17
+ warn "vagrant-dns needs superuser access to manipulate DNS settings"
18
+ raise e
19
+ end
20
+
21
+ def uninstall!
22
+ require 'fileutils'
23
+
24
+ registered_resolvers.each do |r|
25
+ resolver = File.join("/etc/resolver", File.basename(r))
26
+ FileUtils.rm_f(resolver)
27
+ end
28
+ rescue Errno::EACCES => e
29
+ warn "vagrant-dns needs superuser access to manipulate DNS settings"
30
+ raise e
31
+ end
32
+
33
+ def registered_resolvers
34
+ Dir[File.join(tmp_path, "resolver", "*")]
35
+ end
36
+ end
37
+ end
38
+ end
@@ -0,0 +1,19 @@
1
+ module VagrantDNS
2
+ class RestartMiddleware
3
+ def initialize(app, env)
4
+ @app = app
5
+ @env = env
6
+ end
7
+
8
+ def call(env)
9
+ if VagrantDNS::Config.auto_run
10
+ tmp_path = File.join(env[:vm].env.tmp_path, "dns")
11
+ VagrantDNS::Configurator.new(env[:vm], tmp_path).run!
12
+ VagrantDNS::Service.new(tmp_path).restart!
13
+ env[:ui].info "Restarted DNS Service"
14
+ end
15
+
16
+ @app.call(env)
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,52 @@
1
+ module VagrantDNS
2
+ class Service
3
+ attr_accessor :tmp_path, :options
4
+
5
+ def initialize(tmp_path, options = {})
6
+ self.tmp_path = tmp_path
7
+ self.options = options
8
+ end
9
+
10
+ def start!
11
+ run_options = {:ARGV => ["start"]}.merge(options).merge(runopts)
12
+ run!(run_options)
13
+ end
14
+
15
+ def stop!
16
+ run_options = {:ARGV => ["stop"]}.merge(options).merge(runopts)
17
+ run!(run_options)
18
+ end
19
+
20
+ def run!(run_options)
21
+ Daemons.run_proc("vagrant-dns", run_options) do
22
+ require 'rubydns'
23
+
24
+ registry = YAML.load(File.read(config_file))
25
+
26
+ RubyDNS::run_server(:listen => VagrantDNS::Config.listen) do
27
+ registry.each do |pattern, ip|
28
+ match(Regexp.new(pattern), :A) do |match_data, transaction|
29
+ transaction.respond!(ip)
30
+ end
31
+ end
32
+ end
33
+ end
34
+ end
35
+
36
+ def restart!
37
+ stop!
38
+ start!
39
+ end
40
+
41
+ def runopts
42
+ {:dir_mode => :normal,
43
+ :dir => File.join(tmp_path, "daemon"),
44
+ :log_output => true,
45
+ :log_dir => File.join(tmp_path, "daemon")}
46
+ end
47
+
48
+ def config_file
49
+ File.join(tmp_path, "config")
50
+ end
51
+ end
52
+ end
@@ -1,5 +1,5 @@
1
1
  module Vagrant
2
2
  module Dns
3
- VERSION = "0.1.0"
3
+ VERSION = "0.2.0"
4
4
  end
5
5
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: vagrant-dns
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.2.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: 2012-04-28 00:00:00.000000000 Z
12
+ date: 2012-04-29 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: vagrant
@@ -68,6 +68,7 @@ extensions: []
68
68
  extra_rdoc_files: []
69
69
  files:
70
70
  - .gitignore
71
+ - CHANGELOG.md
71
72
  - Gemfile
72
73
  - LICENSE
73
74
  - README.md
@@ -76,6 +77,10 @@ files:
76
77
  - lib/vagrant-dns.rb
77
78
  - lib/vagrant-dns/command.rb
78
79
  - lib/vagrant-dns/config.rb
80
+ - lib/vagrant-dns/configurator.rb
81
+ - lib/vagrant-dns/installers/mac.rb
82
+ - lib/vagrant-dns/restart_middleware.rb
83
+ - lib/vagrant-dns/service.rb
79
84
  - lib/vagrant-dns/version.rb
80
85
  - lib/vagrant_init.rb
81
86
  - vagrant-dns.gemspec