Class: Rubber::Dns::Zerigo

Inherits:
Base
  • Object
show all
Defined in:
lib/rubber/dns/zerigo.rb

Instance Attribute Summary collapse

Attributes inherited from Base

#env

Instance Method Summary collapse

Methods inherited from Base

#destroy, #host_records_equal?, #setup_opts, #up_to_date, #update

Constructor Details

#initialize(env) ⇒ Zerigo

Returns a new instance of Zerigo.



11
12
13
14
15
# File 'lib/rubber/dns/zerigo.rb', line 11

def initialize(env)
  super(env)
  creds = { :provider => 'zerigo', :zerigo_email => env.email, :zerigo_token => env.token }
  @client = ::Fog::DNS.new(creds)
end

Instance Attribute Details

#clientObject

Returns the value of attribute client.



9
10
11
# File 'lib/rubber/dns/zerigo.rb', line 9

def client
  @client
end

Instance Method Details

#create_host_record(opts = {}) ⇒ Object



93
94
95
96
97
98
99
# File 'lib/rubber/dns/zerigo.rb', line 93

def create_host_record(opts = {})
  opts = setup_opts(opts, [:host, :data, :domain, :type, :ttl])
  zone = find_or_create_zone(opts[:domain])
  opts_to_hosts(opts).each do |host|
    zone.records.create(host)
  end
end

#destroy_host_record(opts = {}) ⇒ Object



101
102
103
104
105
106
107
# File 'lib/rubber/dns/zerigo.rb', line 101

def destroy_host_record(opts = {})
  opts = setup_opts(opts, [:host, :domain])

  find_hosts(opts).each do |h|
    h.destroy || raise("Failed to destroy #{h.hostname}")
  end
end

#find_host_records(opts = {}) ⇒ Object



81
82
83
84
85
86
87
88
89
90
91
# File 'lib/rubber/dns/zerigo.rb', line 81

def find_host_records(opts = {})
  hosts = find_hosts(opts)
  group = {}
  hosts.each do |h|
    key = "#{h.name}.#{h.domain} #{h.type}"
    group[key] ||= []
    group[key] << h
  end
  result = group.values.collect {|h| hosts_to_opts(h).merge(:domain => opts[:domain])}
  return result
end

#find_hosts(opts = {}) ⇒ Object



67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/rubber/dns/zerigo.rb', line 67

def find_hosts(opts = {})
  opts = setup_opts(opts, [:host, :domain])
  result = []
  zone = find_or_create_zone(opts[:domain])

  # TODO: revert this when zerigo fog gets fixed to allow parameters 
  # hosts = fqdn ? (zone.records.all(:name => fqdn) rescue []) : zone.records.all
  hosts = zone.records.all
  hosts = hosts.select {|h| name = h.name || ''; name == opts[:host] } if opts.has_key?(:host) && opts[:host] != '*'
  hosts = hosts.select {|h| h.type == opts[:type] } if opts.has_key?(:type) && opts[:type] != '*'
  
  return hosts
end

#find_or_create_zone(domain) ⇒ Object



59
60
61
62
63
64
65
# File 'lib/rubber/dns/zerigo.rb', line 59

def find_or_create_zone(domain)
  zone = @client.zones.all.find {|z| z.domain =~ /^#{domain}\.?/}
  if ! zone
    zone = @client.zones.create(:domain => domain)
  end
  return zone
end

#hosts_to_opts(hosts) ⇒ Object

multiple hosts with same name/type convert to a single rubber-dns.yml opts format



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/rubber/dns/zerigo.rb', line 18

def hosts_to_opts(hosts)
  opts = {}
  
  hosts.each do |host|
    opts[:host] ||= host.name || ''
    opts[:domain] ||= host.zone.domain
    opts[:type] ||= host.type
    opts[:ttl] ||= host.ttl.to_i if host.ttl
    
    opts[:data] ||= []
    if host.type =~ /MX/i
      opts[:data] << {:priority => host.priority, :value => host.value}
    else
      opts[:data] << host.value
    end
  end
  
  return opts
end

#opts_to_hosts(opts) ⇒ Object

a single rubber-dns.yml opts format converts to multiple hosts with same name/type



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/rubber/dns/zerigo.rb', line 39

def opts_to_hosts(opts)
  hosts = []
  
  opts[:data].each do |o|
    host = {}
    host[:name] = opts[:host]
    host[:type] =  opts[:type]
    host[:ttl] = opts[:ttl] if opts[:ttl]
    if o.kind_of?(Hash) && o[:priority]
      host[:priority] = o[:priority]
      host[:value] = o[:value]
    else
      host[:value] = o
    end
    hosts << host
  end
  
  return hosts
end

#update_host_record(old_opts = {}, new_opts = {}) ⇒ Object



109
110
111
112
113
114
115
116
117
118
# File 'lib/rubber/dns/zerigo.rb', line 109

def update_host_record(old_opts={}, new_opts={})
  old_opts = setup_opts(old_opts, [:host, :domain, :type])
  new_opts = setup_opts(new_opts, [:host, :domain, :type, :data])

  # Tricky to update existing hosts since zerigo needs a separate host
  # entry for multiple records of same type (MX, etc), so take the easy
  # way out and destroy/create instead of update
  destroy_host_record(old_opts)
  create_host_record(new_opts)
end