Class: VagrantAutoDNS::Daemon

Inherits:
RExec::Daemon::Base
  • Object
show all
Defined in:
lib/vagrant-autodns/daemon.rb

Constant Summary collapse

ProcessFile =
RExec::Daemon::ProcessFile
A_RECORD =
Resolv::DNS::Resource::IN::A
AAAA_RECORD =
Resolv::DNS::Resource::IN::AAAA
@@base_directory =
nil

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.db_fileObject

Returns the value of attribute db_file.



28
29
30
# File 'lib/vagrant-autodns/daemon.rb', line 28

def db_file
  @db_file
end

.enable_ipv6Object

Returns the value of attribute enable_ipv6.



29
30
31
# File 'lib/vagrant-autodns/daemon.rb', line 29

def enable_ipv6
  @enable_ipv6
end

.listen_ipObject

Returns the value of attribute listen_ip.



29
30
31
# File 'lib/vagrant-autodns/daemon.rb', line 29

def listen_ip
  @listen_ip
end

.listen_portObject

Returns the value of attribute listen_port.



29
30
31
# File 'lib/vagrant-autodns/daemon.rb', line 29

def listen_port
  @listen_port
end

.resolverObject

Returns the value of attribute resolver.



28
29
30
# File 'lib/vagrant-autodns/daemon.rb', line 28

def resolver
  @resolver
end

.working_directoryObject

Returns the value of attribute working_directory.



28
29
30
# File 'lib/vagrant-autodns/daemon.rb', line 28

def working_directory
  @working_directory
end

Class Method Details

.autodnsdbObject



93
94
95
96
# File 'lib/vagrant-autodns/daemon.rb', line 93

def self.autodnsdb
  return @autodnsdb if @autodnsdb.is_a?(AutoDNSDB)
  @autodnsdb = AutoDNSDB.new(autodnsdb_path)
end

.autodnsdb_pathObject



98
99
100
# File 'lib/vagrant-autodns/daemon.rb', line 98

def self.autodnsdb_path
  @autodnsdb_path ||= File.expand_path(File.join(working_directory, db_file))
end

.daemon_statusObject



73
74
75
# File 'lib/vagrant-autodns/daemon.rb', line 73

def self.daemon_status
  ProcessFile.status(self)
end

.ensure_runningObject



69
70
71
# File 'lib/vagrant-autodns/daemon.rb', line 69

def self.ensure_running
  restart unless running?
end

.ipv6_enabled?Boolean

Returns:

  • (Boolean)


89
90
91
# File 'lib/vagrant-autodns/daemon.rb', line 89

def self.ipv6_enabled?
  @enable_ipv6.nil? || @enable_ipv6
end

.pidObject



77
78
79
# File 'lib/vagrant-autodns/daemon.rb', line 77

def self.pid
  ProcessFile.recall(self)
end

.restartObject



64
65
66
67
# File 'lib/vagrant-autodns/daemon.rb', line 64

def self.restart
  stop unless stopped?
  start
end

.runObject



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/vagrant-autodns/daemon.rb', line 32

def self.run
  upstream = upstream_resolver
  db_lib = AutoDNSDB
  db_absolute_path = autodnsdb_path
  allow_ipv6 = ipv6_enabled?
  RubyDNS::run_server(:listen => listen_interface) do
    db = db_lib.new(db_absolute_path)
    match(//, A_RECORD) do |transaction|
      domain = transaction.name.to_s.encode('US-ASCII')
      record = db.find_record(domain)
      if record
        transaction.respond!(record['ip'])
      else
        transaction.passthrough!(upstream)
      end
    end

    match(//, AAAA_RECORD) do |transaction|
      if allow_ipv6
        transaction.passthrough!(upstream)
      else
        transaction.fail!(:NXDomain)
      end
    end

    # Default DNS handler
    otherwise do |transaction|
      transaction.passthrough!(upstream)
    end
  end
end

.running?Boolean

Returns:

  • (Boolean)


81
82
83
# File 'lib/vagrant-autodns/daemon.rb', line 81

def self.running?
  daemon_status == :running
end

.stopped?Boolean

Returns:

  • (Boolean)


85
86
87
# File 'lib/vagrant-autodns/daemon.rb', line 85

def self.stopped?
  daemon_status == :stopped
end