Class: ApartmentAcmeClient::DnsApi::CheckDns

Inherits:
Object
  • Object
show all
Defined in:
lib/apartment_acme_client/dns_api/check_dns.rb

Overview

Check to see if a particular DNS record is present.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(root_domain, dns_record) ⇒ CheckDns

Returns a new instance of CheckDns.



10
11
12
13
14
# File 'lib/apartment_acme_client/dns_api/check_dns.rb', line 10

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

Instance Attribute Details

#dns_recordObject (readonly)

Returns the value of attribute dns_record.



8
9
10
# File 'lib/apartment_acme_client/dns_api/check_dns.rb', line 8

def dns_record
  @dns_record
end

#root_domainObject (readonly)

Returns the value of attribute root_domain.



8
9
10
# File 'lib/apartment_acme_client/dns_api/check_dns.rb', line 8

def root_domain
  @root_domain
end

Instance Method Details

#check_dns(value) ⇒ Object

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



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/apartment_acme_client/dns_api/check_dns.rb', line 18

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

#nameserversObject



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/apartment_acme_client/dns_api/check_dns.rb', line 40

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



56
57
58
59
60
61
62
63
64
# File 'lib/apartment_acme_client/dns_api/check_dns.rb', line 56

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