Class: Reconciler

Inherits:
Object
  • Object
show all
Defined in:
lib/reconciler.rb

Instance Method Summary collapse

Constructor Details

#initialize(zone, ip, existing, desired, setup = {}) ⇒ Reconciler

Returns a new instance of Reconciler.



2
3
4
# File 'lib/reconciler.rb', line 2

def initialize(zone, ip, existing, desired, setup={})
  @zone, @ip, @existing, @desired, @setup = zone, ip, existing, desired, setup
end

Instance Method Details

#cru_by_data(type, name, keepers, desired) ⇒ Object

MX, SRV, TXT



158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
# File 'lib/reconciler.rb', line 158

def cru_by_data(type, name, keepers, desired)
  type = type.to_s.upcase
  
  keepers.each do |r|
    unless r.aux == desired[r.data]
      log "> changing record: #{type} #{r.name} #{r.data} from #{r.aux} to #{desired[r.data]}"
      r.aux = desired[r.data]
      r.save
    end
    desired.delete(r.data)
  end
  
  desired.each_pair do |data, aux|
    log "> creating record: #{type} #{name} (#{data} / #{aux})"
    r = @zone.new_record(:record_type => type, :name => name, :data => data, :aux => aux)
    r.save
  end
  
end

#crud_by_name(type, records, desired) ⇒ Object

A, CNAME



132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
# File 'lib/reconciler.rb', line 132

def crud_by_name(type, records, desired)
  type = type.to_s.upcase
  records.each do |record|
    if desired[record.name] == record.data
      desired.delete(record.name)
      
    elsif desired.has_key?(record.name)
      log "> changing record: #{type} #{record.name} from #{record.data} to #{desired[record.name]}"
      record.data = desired[record.name]
      desired.delete(record.name)
      record.save
      
    else
      log "> removing record: #{type} #{record.name} (#{record.data})"
      record.destroy
    end
  end
  (desired || {}).each_pair do |name, data|
    log "> creating record: #{type} #{name} (#{data})"
    record = @zone.new_record(:record_type => type, :name => name, :data => data)
    record.save
  end
  
end

#destroy_records(records = []) ⇒ Object



124
125
126
127
128
129
# File 'lib/reconciler.rb', line 124

def destroy_records(records=[])
  records.each do |r|
    log "> removing record: #{r.kind} #{r.name} #{r.data}"
    r.destroy
  end
end

#log(msg, level = :info) ⇒ Object



6
7
8
# File 'lib/reconciler.rb', line 6

def log(msg, level=:info)
  $LOG.send(level, msg) if $LOG
end

#process!Object



10
11
12
13
14
15
16
17
# File 'lib/reconciler.rb', line 10

def process!
  process_a_records
  process_cname_records
  process_mx_records
  process_srv_records
  process_txt_records
  process_ns_records
end

#process_a_recordsObject



19
20
21
22
23
24
25
26
27
28
# File 'lib/reconciler.rb', line 19

def process_a_records
  a_records = @existing.select {|r| r.a? }
  
  desired = @desired['a'] ||= {}
  desired[@zone.origin] = 'this'
  desired['*'] ||= 'this'
  desired.each_pair {|k,v| desired[k] = @ip if v == "this" }
  
  crud_by_name(:a, a_records, desired)
end

#process_cname_recordsObject



30
31
32
33
34
# File 'lib/reconciler.rb', line 30

def process_cname_records
  cnames = @existing.select {|r| r.cname? }
  desired = @desired['cname'] || {}
  crud_by_name(:cname, cnames, desired)
end

#process_mx_recordsObject



51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/reconciler.rb', line 51

def process_mx_records
  mx = @existing.select {|r| r.mx? }
  desired = @desired['mx'] || {}
  
  if @desired['goog']
    desired = {
      'aspmx.l.google.com.' => 1, 'alt1.aspmx.l.google.com.' => 5, 'alt2.aspmx.l.google.com.' => 5, 
      'aspmx2.googlemail.com.' => 10, 'aspmx3.googlemail.com.' => 10, 'aspmx4.googlemail.com.' => 10,
      'aspmx5.googlemail.com.' => 10
    }
  end
  
  if desired.kind_of?(String)
    desired = {desired => 5}
  end
    
  keepers = mx.select {|r| desired.has_key?(r.data) }
  
  cru_by_data :mx, @zone.origin, keepers, desired
  
  mx -= keepers
  destroy_records mx
end

#process_ns_recordsObject



36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/reconciler.rb', line 36

def process_ns_records
  ns = @existing.select {|r| r.ns? }
  (1..3).to_a.each do |i|
    unless ns.any? {|r| r.data == "ns#{i}.slicehost.net." }
      log "> creating NS record ns#{i}.slicehost.net."
      record = @zone.new_record(:record_type => "NS", :name => @zone.origin, :data => "ns#{i}.slicehost.net.")
      record.save
    else
      r = ns.detect {|r| r.data == "ns#{i}.slicehost.net." }
      ns.delete(r)
    end
  end
  destroy_records ns
end

#process_srv_recordsObject



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
# File 'lib/reconciler.rb', line 75

def process_srv_records
  srv = @existing.select {|r| r.srv? }
  desired = @desired['srv'] || {}
  
  if @desired['goog'] && @desired['goog'] != 'mail'
    desired.update({ 
      "_xmpp-server._tcp.#{@zone.origin}" => {
        '0 5269 xmpp-server.l.google.com.' => 5, '0 5269 xmpp-server1.l.google.com.' => 20,
        '0 5269 xmpp-server2.l.google.com.' => 20, '0 5269 xmpp-server3.l.google.com.' => 20,
        '0 5269 xmpp-server4.l.google.com.' => 20
      },
      "_jabber._tcp.#{@zone.origin}" => {
        '0 5269 xmpp-server.l.google.com.' => 5, '0 5269 xmpp-server1.l.google.com.' => 20,
        '0 5269 xmpp-server2.l.google.com.' => 20, '0 5269 xmpp-server3.l.google.com.' => 20,
        '0 5269 xmpp-server4.l.google.com.' => 20
      },
      "_xmpp-client._tcp.#{@zone.origin}" => {
        '0 5222 talk.l.google.com.' => 5, '0 5222 talk1.l.google.com.' => 20, 
        '0 5222 talk2.l.google.com.' => 20
      }
    })
  end
  
  desired.each_pair do |name, data_aux_pairs|
    keepers = srv.select {|r| r.name == name }.select {|r| data_aux_pairs.has_key?(r.data) }
    srv -= keepers
    cru_by_data(:srv, name, keepers, data_aux_pairs)
  end
  
  destroy_records srv
end

#process_txt_recordsObject



107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
# File 'lib/reconciler.rb', line 107

def process_txt_records
  txt = @existing.select {|r| r.txt? }
  desired = @desired['txt'] || []

  if @desired['goog'] && ! desired.any? {|t| t.match(/^v=spf1/)}
    desired << "v=spf1 include:aspmx.googlemail.com ~all"
  end

  desired = desired.inject({}) {|memo, value| memo.update({value => 0}) }
  
  keepers = txt.select {|r| desired.include?(r.data) }
  txt -= keepers
  cru_by_data(:txt, @zone.origin, keepers, desired)
  
  destroy_records txt
end