Class: VagrantDNS::Service

Inherits:
Object
  • Object
show all
Defined in:
lib/vagrant-dns/service.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(tmp_path, options = {}) ⇒ Service

Returns a new instance of Service.



5
6
7
8
# File 'lib/vagrant-dns/service.rb', line 5

def initialize(tmp_path, options = {})
  self.tmp_path = tmp_path
  self.options = options
end

Instance Attribute Details

#optionsObject

Returns the value of attribute options.



3
4
5
# File 'lib/vagrant-dns/service.rb', line 3

def options
  @options
end

#tmp_pathObject

Returns the value of attribute tmp_path.



3
4
5
# File 'lib/vagrant-dns/service.rb', line 3

def tmp_path
  @tmp_path
end

Instance Method Details

#config_fileObject



48
49
50
# File 'lib/vagrant-dns/service.rb', line 48

def config_file
  File.join(tmp_path, "config")
end

#restart!Object



36
37
38
39
# File 'lib/vagrant-dns/service.rb', line 36

def restart!
  stop!
  start!
end

#run!(run_options) ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/vagrant-dns/service.rb', line 20

def run!(run_options)
  Daemons.run_proc("vagrant-dns", run_options) do
    require 'rubydns'

    registry = YAML.load(File.read(config_file))

    RubyDNS::run_server(:listen => VagrantDNS::Config.listen) do
      registry.each do |pattern, ip|
        match(Regexp.new(pattern), :A) do |match_data, transaction|
          transaction.respond!(ip)
        end
      end
    end
  end
end

#runoptsObject



41
42
43
44
45
46
# File 'lib/vagrant-dns/service.rb', line 41

def runopts
  {:dir_mode => :normal, 
   :dir => File.join(tmp_path, "daemon"),
   :log_output => true,
   :log_dir => File.join(tmp_path, "daemon")}
end

#start!Object



10
11
12
13
# File 'lib/vagrant-dns/service.rb', line 10

def start!
  run_options = {:ARGV => ["start"]}.merge(options).merge(runopts)
  run!(run_options)
end

#stop!Object



15
16
17
18
# File 'lib/vagrant-dns/service.rb', line 15

def stop!
  run_options = {:ARGV => ["stop"]}.merge(options).merge(runopts)
  run!(run_options)
end