Class: Proxy::Dns::Dnsmasq::Openwrt

Inherits:
Record
  • Object
show all
Defined in:
lib/smart_proxy_dns_dnsmasq/backend/openwrt.rb

Defined Under Namespace

Classes: DSL

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from Record

#do_create, #do_remove

Constructor Details

#initialize(config, reload_cmd, dns_ttl) ⇒ Openwrt

Returns a new instance of Openwrt.



7
8
9
10
11
12
13
# File 'lib/smart_proxy_dns_dnsmasq/backend/openwrt.rb', line 7

def initialize(config, reload_cmd, dns_ttl)
  @config_file = config
  @reload_cmd = reload_cmd
  @dirty = false

  super(dns_ttl)
end

Instance Attribute Details

#config_fileObject (readonly)

Returns the value of attribute config_file.



5
6
7
# File 'lib/smart_proxy_dns_dnsmasq/backend/openwrt.rb', line 5

def config_file
  @config_file
end

#dirtyObject (readonly)

Returns the value of attribute dirty.



5
6
7
# File 'lib/smart_proxy_dns_dnsmasq/backend/openwrt.rb', line 5

def dirty
  @dirty
end

#reload_cmdObject (readonly)

Returns the value of attribute reload_cmd.



5
6
7
# File 'lib/smart_proxy_dns_dnsmasq/backend/openwrt.rb', line 5

def reload_cmd
  @reload_cmd
end

Instance Method Details

#add_cname(name, canonical) ⇒ Object



45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/smart_proxy_dns_dnsmasq/backend/openwrt.rb', line 45

def add_cname(name, canonical)
  found = find_type(:cname, :name, name)
  return true if found && found.options[:target] == canonical

  c = found
  c = DSL::Config.new :cname unless c
  c.options[:cname] = name
  c.options[:target] = canonical

  configuration << c unless found
  @dirty = true
end

#add_entry(type, fqdn, ip) ⇒ Object

Raises:

  • (Proxy::Dns::Error)


23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/smart_proxy_dns_dnsmasq/backend/openwrt.rb', line 23

def add_entry(type, fqdn, ip)
  raise Proxy::Dns::Error, "OpenWRT UCI can't manage IPv6 entries" if type == 'AAAA' || type == 'PTR' && IPAddr.new(ip).ipv6?
  found = find_type(:domain, :name, fqdn)
  return true if found && found.options[:ip] == ip

  h = found
  h = DSL::Config.new :domain unless h
  h.options[:name] = fqdn
  h.options[:ip] = ip

  configuration << h unless found
  @dirty = true
end

#remove_cname(name) ⇒ Object



58
59
60
61
62
63
# File 'lib/smart_proxy_dns_dnsmasq/backend/openwrt.rb', line 58

def remove_cname(name)
  return true unless c = find_type(:cname, :name, name)

  configuration.delete c
  @dirty = true
end

#remove_entry(type, fqdn = nil, ip = nil) ⇒ Object

Raises:

  • (Proxy::Dns::Error)


37
38
39
40
41
42
43
# File 'lib/smart_proxy_dns_dnsmasq/backend/openwrt.rb', line 37

def remove_entry(type, fqdn = nil, ip = nil)
  raise Proxy::Dns::Error, "OpenWRT UCI can't manage IPv6 entries" if type == 'AAAA' || type == 'PTR' && IPAddr.new(ip).ipv6?
  return true unless h = find_type(:domain, fqdn && :name || :ip, fqdn || ip)

  configuration.delete h
  @dirty = true
end

#update!Object



15
16
17
18
19
20
21
# File 'lib/smart_proxy_dns_dnsmasq/backend/openwrt.rb', line 15

def update!
  return unless @dirty
  @dirty = false

  File.write(@config_file, "\n" + configuration.join("\n") + "\n")
  system(@reload_cmd)
end