Module: Bosh::Director::DnsHelper
- Included in:
- Api::Controllers::BaseController, Config, Bosh::Director::DeploymentPlan::DeploymentSpecParser, Bosh::Director::DeploymentPlan::DnsBinder, Bosh::Director::DeploymentPlan::DynamicNetwork, Bosh::Director::DeploymentPlan::Instance, Bosh::Director::DeploymentPlan::JobSpecParser, Bosh::Director::DeploymentPlan::ManualNetwork, Bosh::Director::DeploymentPlan::Network, Bosh::Director::DeploymentPlan::NetworkSubnet, Bosh::Director::DeploymentPlan::Planner, InstanceDeleter, InstanceUpdater, Jobs::DeleteDeployment
- Defined in:
- lib/bosh/director/dns_helper.rb
Constant Summary collapse
- SOA =
primary_ns contact serial refresh retry expire minimum
"localhost hostmaster@localhost 0 10800 604800 30"
- TTL_5M =
300
- TTL_4H =
3600 * 4
Instance Method Summary collapse
-
#add_default_dns_server(servers) ⇒ Object
add default dns server to an array of dns servers.
- #canonical(string) ⇒ Object
-
#default_dns_server ⇒ Object
returns the default DNS server.
-
#delete_dns_records(record_pattern, domain_id = nil) ⇒ Object
deletes all DNS records matching the pattern.
- #delete_empty_domain(domain) ⇒ Object
-
#dns_domain_name ⇒ Object
returns the DNS domain name.
-
#dns_ns_record ⇒ Object
returns the DNS name server record.
-
#dns_servers(network, spec, add_default_dns = true) ⇒ Object
build a list of dns servers to use.
-
#flush_dns_cache ⇒ Object
Purge cached DNS records.
- #invalid_dns(network, reason) ⇒ Object
-
#reverse_domain(ip) ⇒ String
Reverse dns domain name for an IP.
-
#reverse_host(ip) ⇒ String
Reverse dns name for an IP used for a PTR record.
-
#update_dns_a_record(domain, name, ip_address) ⇒ Object
create/update DNS A record.
-
#update_dns_ptr_record(name, ip_address) ⇒ Object
create/update DNS PTR records (for reverse lookups).
Instance Method Details
#add_default_dns_server(servers) ⇒ Object
add default dns server to an array of dns servers
63 64 65 66 67 68 69 70 71 72 73 |
# File 'lib/bosh/director/dns_helper.rb', line 63 def add_default_dns_server(servers) return servers unless Config.dns_enabled? default_server = default_dns_server if default_server && default_server != "127.0.0.1" (servers ||= []) << default_server servers.uniq! end servers end |
#canonical(string) ⇒ Object
21 22 23 24 25 26 27 28 29 30 31 32 33 |
# File 'lib/bosh/director/dns_helper.rb', line 21 def canonical(string) # a-z, 0-9, -, case insensitive, and must start with a letter string = string.downcase.gsub(/_/, "-").gsub(/[^a-z0-9-]/, "") if string =~ /^(\d|-)/ raise DnsInvalidCanonicalName, "Invalid DNS canonical name `#{string}', must begin with a letter" end if string =~ /-$/ raise DnsInvalidCanonicalName, "Invalid DNS canonical name `#{string}', can't end with a hyphen" end string end |
#default_dns_server ⇒ Object
returns the default DNS server
58 59 60 |
# File 'lib/bosh/director/dns_helper.rb', line 58 def default_dns_server Config.dns["server"] if Config.dns end |
#delete_dns_records(record_pattern, domain_id = nil) ⇒ Object
deletes all DNS records matching the pattern
143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 |
# File 'lib/bosh/director/dns_helper.rb', line 143 def delete_dns_records(record_pattern, domain_id=nil) records = Models::Dns::Record.filter(:name.like(record_pattern)) if domain_id records = records.filter(:domain_id => domain_id) end # delete A records and collect all IPs for later ips = [] records.each do |record| ips << record.content @logger.info("Deleting DNS record: #{record.name}") record.destroy end # delete PTR records from IP list ips.each do |ip| records = Models::Dns::Record.filter(:name.like(reverse_host(ip))) records.each do |record| @logger.info("Deleting reverse DNS record: #{record.name}") record.destroy end end # see if any of the reverse domains are empty and should be deleted ips.each do |ip| reverse = reverse_domain(ip) rdomain = Models::Dns::Domain.filter(:name => reverse, :type => "NATIVE") rdomain.each do |domain| delete_empty_domain(domain) end end end |
#delete_empty_domain(domain) ⇒ Object
177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 |
# File 'lib/bosh/director/dns_helper.rb', line 177 def delete_empty_domain(domain) # If the count is 2, it means we only have the NS & SOA record # and the domain is "empty" and can be deleted if domain.records.size == 2 @logger.info("Deleting empty reverse domain #{domain.name}") # Since DNS domain can be deleted by multiple threads # it's possible for database to return 0 rows modified result. # In this specific case that's a valid return value # but Sequel usually considers that an error. # ('Attempt to delete object did not result in a single row modification') domain.require_modification = false # Cascaded - all records are removed domain.destroy end end |
#dns_domain_name ⇒ Object
returns the DNS domain name
76 77 78 |
# File 'lib/bosh/director/dns_helper.rb', line 76 def dns_domain_name Config.dns_domain_name end |
#dns_ns_record ⇒ Object
returns the DNS name server record
81 82 83 |
# File 'lib/bosh/director/dns_helper.rb', line 81 def dns_ns_record "ns.#{dns_domain_name}" end |
#dns_servers(network, spec, add_default_dns = true) ⇒ Object
build a list of dns servers to use
36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 |
# File 'lib/bosh/director/dns_helper.rb', line 36 def dns_servers(network, spec, add_default_dns = true) servers = nil dns_property = safe_property(spec, "dns", :class => Array, :optional => true) if dns_property servers = [] dns_property.each do |dns| dns = NetAddr::CIDR.create(dns) unless dns.size == 1 invalid_dns(network, "must be a single IP") end servers << dns.ip end end return servers unless add_default_dns add_default_dns_server(servers) end |
#flush_dns_cache ⇒ Object
Purge cached DNS records
204 205 206 207 208 209 210 211 212 213 214 |
# File 'lib/bosh/director/dns_helper.rb', line 204 def flush_dns_cache flush_command = Config.dns['flush_command'] if flush_command && !flush_command.empty? stdout, stderr, status = Open3.capture3(flush_command) if status == 0 @logger.debug("Flushed #{stdout.chomp} records from DNS cache") else @logger.warn("Failed to flush DNS cache: #{stderr.chomp}") end end end |
#invalid_dns(network, reason) ⇒ Object
198 199 200 201 |
# File 'lib/bosh/director/dns_helper.rb', line 198 def invalid_dns(network, reason) raise NetworkInvalidDns, "Invalid DNS for network `#{network}': #{reason}" end |
#reverse_domain(ip) ⇒ String
Returns reverse dns domain name for an IP.
11 12 13 |
# File 'lib/bosh/director/dns_helper.rb', line 11 def reverse_domain(ip) reverse(ip, 2) end |
#reverse_host(ip) ⇒ String
Returns reverse dns name for an IP used for a PTR record.
17 18 19 |
# File 'lib/bosh/director/dns_helper.rb', line 17 def reverse_host(ip) reverse(ip, 3) end |
#update_dns_a_record(domain, name, ip_address) ⇒ Object
create/update DNS A record
86 87 88 89 90 91 92 93 94 95 96 97 |
# File 'lib/bosh/director/dns_helper.rb', line 86 def update_dns_a_record(domain, name, ip_address) record = Models::Dns::Record.find(:domain_id => domain.id, :name => name) if record.nil? record = Models::Dns::Record.new(:domain_id => domain.id, :name => name, :type => "A", :ttl => TTL_5M) end record.content = ip_address record.change_date = Time.now.to_i record.save end |
#update_dns_ptr_record(name, ip_address) ⇒ Object
create/update DNS PTR records (for reverse lookups)
100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 |
# File 'lib/bosh/director/dns_helper.rb', line 100 def update_dns_ptr_record(name, ip_address) reverse_domain = reverse_domain(ip_address) reverse_host = reverse_host(ip_address) rdomain = Models::Dns::Domain.safe_find_or_create(:name => reverse_domain, :type => "NATIVE") Models::Dns::Record.find_or_create(:domain_id => rdomain.id, :name => reverse_domain, :type =>'SOA', :content => SOA, :ttl => TTL_4H) Models::Dns::Record.find_or_create(:domain_id => rdomain.id, :name => reverse_domain, :type =>'NS', :ttl => TTL_4H, :content => dns_ns_record) record = Models::Dns::Record.find(:content => name, :type =>'PTR') # delete the record if the IP address changed if record && record.name != reverse_host id = record.domain_id record.destroy record = nil # delete the domain if the domain id changed and it's empty if id != rdomain.id delete_empty_domain(Models::Dns::Domain[id]) end end unless record record = Models::Dns::Record.new(:domain_id => rdomain.id, :name => reverse_host, :type =>'PTR', :ttl => TTL_5M) end record.content = name record.change_date = Time.now.to_i record.save end |