Module: Fum::DNS

Included in:
Commands::Launch, Commands::List, Commands::Repair, Commands::Status, StageAnalyzer
Defined in:
lib/fum/dns.rb

Overview

Mixin for common DNS methods

Instance Method Summary collapse

Instance Method Details

#dnsObject



8
9
10
# File 'lib/fum/dns.rb', line 8

def dns
  Fog::DNS[:AWS]
end

#dns_names_equal(a, b) ⇒ Object

Returns true if the specified dns names equal, ignoring any trailing “.”



130
131
132
# File 'lib/fum/dns.rb', line 130

def dns_names_equal(a, b)
  ensure_trailing_dot(a) == ensure_trailing_dot(b)
end

#ensure_trailing_dot(name) ⇒ Object

Appends a trailing . to the given name if it doesn’t have one



124
125
126
127
# File 'lib/fum/dns.rb', line 124

def ensure_trailing_dot(name)
  name = "#{name}." unless name.nil? || name.end_with?(".")
  name
end

#find_records(zone, name) ⇒ Object



112
113
114
115
116
117
118
119
120
121
# File 'lib/fum/dns.rb', line 112

def find_records(zone, name)
  existing_records = zone.records.all({:name => name})

  matching = []
  types = ['A', 'AAAA', 'CNAME']
  existing_records.each { |record|
    matching << record if record.name == name && types.include?(record.type)
  }
  matching
end

#update_zone(zone_decl, hosted_zone_name_id, dns_name, env_cname, options) ⇒ Object



29
30
31
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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
# File 'lib/fum/dns.rb', line 29

def update_zone(zone_decl, hosted_zone_name_id, dns_name, env_cname, options)
  dns = Fog::DNS[:aws]

  zone = dns.zones.all({:domain => zone_decl.name}).shift
  die "Could not find zone #{zone_decl.name} in account." unless zone

  puts "Updating records in zone #{zone.domain}"
  create_list = []
  modify_list = []

  zone_decl.records.each { |record|
    fqdn = "#{record[:name]}.#{zone_decl.name}."

    create_opts = {
        :name => fqdn,
        :type => record[:type]
    }

    case record[:type]
      when 'CNAME'
        create_opts[:value] = case record[:target]
                                when :elb
                                  ensure_trailing_dot(dns_name)
                                when :env
                                  ensure_trailing_dot(env_cname)
                              end
      when 'A'
        create_opts[:alias_target] = {
            :hosted_zone_id => hosted_zone_name_id,
            :dns_name => dns_name
        }
      else
        raise RuntimeError, "Unknown type #{record[:type]}"
    end

    existing = find_records(zone, fqdn)

    if existing.length > 1
      # We do not currently handle this case, which would occur if AAAA records exist or weighted/latency records used.
      puts "Cannot update record #{fqdn} in zone #{zone} because more than one A, AAAA, or CNAME record already exists."
    end

    if existing.length == 0
      create_list << create_opts
    else
      modify_list << {
          :record => existing.shift,
          :create_opts => create_opts
      }
    end

  }

  # We do not currently do this atomically, but one record at a time.  Should probably move to atomic at some
  # point.

  new_records = []

  create_list.each { |create_options|
    puts "Creating #{create_options[:type]} record with name #{create_options[:name]} in zone #{zone.domain}"
    unless options[:noop]
      new_records << zone.records.create(create_options)
    end
  }

  modify_list.each { |record|
    puts "Updating #{record[:create_opts][:type]} record with name #{record[:create_opts][:name]} in zone #{zone.domain}"
    unless options[:noop]
      record[:record].modify(record[:create_opts])
      new_records << record[:record]
    end
  }

  puts "Waiting for DNS records to sync in zone #{zone.domain}..."
  # Wait for records to be ready.
  new_records.each { |record|
    record.wait_for { record.ready? }
  }
  puts "Updated records are now in sync in zone #{zone.domain}"

end

#update_zones(stage_decl, env, options) ⇒ Object



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/fum/dns.rb', line 12

def update_zones(stage_decl, env, options)
  zones = stage_decl.zones

  hosted_zone_name_id = nil
  dns_name = nil

  unless options[:noop]
    lb = env.load_balancer

    hosted_zone_name_id = lb.hosted_zone_name_id
    dns_name = lb.dns_name
  end

  zones.each { |zone| update_zone(zone, hosted_zone_name_id, dns_name, env.cname, options) }

end