class ApartmentAcmeClient::DnsApi::CheckDns

Check to see if a particular DNS record is present.

Attributes

dns_record[R]
root_domain[R]

Public Class Methods

new(root_domain, dns_record) click to toggle source
# File lib/apartment_acme_client/dns_api/check_dns.rb, line 8
def initialize(root_domain, dns_record)
  # ensure we only have the TLD, not a subdomain
  @root_domain = root_domain.split(".").last(2).join(".")
  @dns_record = dns_record
end

Public Instance Methods

check_dns(value) click to toggle source

Search DNS recodrs for any entries which are TXT and include the text 'value'

# File lib/apartment_acme_client/dns_api/check_dns.rb, line 16
def check_dns(value)
  valid = true

  nameservers.each do |nameserver|
    begin
      records = Resolv::DNS.open(nameserver: nameserver) do |dns|
        dns.getresources(
          dns_record,
          Resolv::DNS::Resource::IN::TXT
        )
      end
      records = records.map(&:strings).flatten
      valid = records.include?(value)
    rescue Resolv::ResolvError
      return false
    end
    return false unless valid
  end

  valid
end
nameservers() click to toggle source
# File lib/apartment_acme_client/dns_api/check_dns.rb, line 38
def nameservers
  return @nameservers if defined?(@nameservers)

  @nameservers = []
  Resolv::DNS.open(nameserver: '8.8.8.8') do |dns|
    while nameservers.empty?
      @nameservers = dns.getresources(
        root_domain,
        Resolv::DNS::Resource::IN::NS
      ).map(&:name).map(&:to_s)
    end
  end

  @nameservers
end
wait_for_present(value, timeout_seconds: 120) click to toggle source
# File lib/apartment_acme_client/dns_api/check_dns.rb, line 54
def wait_for_present(value, timeout_seconds: 120)
  time = 1
  until check_dns(value)
    puts "Waiting for DNS to update"
    sleep 1
    time += 1
    break if time > timeout_seconds
  end
end