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) ⇒ Service

Returns a new instance of Service.



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

def initialize(tmp_path)
  self.tmp_path = tmp_path
end

Instance Attribute Details

#tmp_pathObject

Returns the value of attribute tmp_path.



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

def tmp_path
  @tmp_path
end

Instance Method Details

#restart!(start_opts = {}) ⇒ Object



55
56
57
58
# File 'lib/vagrant-dns/service.rb', line 55

def restart!(start_opts = {})
  stop!
  start!(start_opts)
end

#run!(run_options) ⇒ Object



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/vagrant-dns/service.rb', line 29

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

    registry = Registry.new(tmp_path).to_hash
    std_resolver = RubyDNS::Resolver.new(Async::DNS::System.nameservers)
    ttl = VagrantDNS::Config.ttl

    RubyDNS::run_server(VagrantDNS::Config.listen) do
      registry.each do |pattern, ip|
        match(pattern, Resolv::DNS::Resource::IN::A) do |transaction, match_data|
          transaction.respond!(ip, ttl: ttl)
        end
      end

      otherwise do |transaction|
        transaction.passthrough!(std_resolver) do |reply, reply_name|
          puts reply
          puts reply_name
        end
      end
    end
  end
end

#runoptsObject



72
73
74
75
76
77
78
79
80
# File 'lib/vagrant-dns/service.rb', line 72

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

#show_configObject



60
61
62
63
64
65
66
67
68
69
70
# File 'lib/vagrant-dns/service.rb', line 60

def show_config
  registry = Registry.new(tmp_path).to_hash

  if registry.any?
    registry.each do |pattern, ip|
      puts format("%s => %s", pattern.inspect, ip)
    end
  else
    puts "Configuration missing or empty."
  end
end

#start!(opts = {}) ⇒ Object



11
12
13
14
15
16
17
# File 'lib/vagrant-dns/service.rb', line 11

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

#status!Object



24
25
26
27
# File 'lib/vagrant-dns/service.rb', line 24

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

#stop!Object



19
20
21
22
# File 'lib/vagrant-dns/service.rb', line 19

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