Class: Rubber::Dns::Aws
- Inherits:
-
Base
- Object
- Base
- Rubber::Dns::Aws
show all
- Defined in:
- lib/rubber/dns/aws.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) ⇒ Aws
Returns a new instance of Aws.
11
12
13
14
15
|
# File 'lib/rubber/dns/aws.rb', line 11
def initialize(env)
super(env)
creds = { :provider => 'aws', :aws_access_key_id => env.access_key, :aws_secret_access_key => env.access_secret }
@client = ::Fog::DNS.new(creds)
end
|
Instance Attribute Details
#client ⇒ Object
Returns the value of attribute client.
9
10
11
|
# File 'lib/rubber/dns/aws.rb', line 9
def client
@client
end
|
Instance Method Details
#all_hosts(zone) ⇒ Object
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
|
# File 'lib/rubber/dns/aws.rb', line 110
def all_hosts(zone)
hosts = []
opts = {}
has_more = true
while has_more
all_hosts = zone.records.all(opts)
has_more = all_hosts.is_truncated
opts = {:name => all_hosts.next_record_name,
:type => all_hosts.next_record_type
}
hosts.concat(all_hosts)
end
return hosts
end
|
#create_host_record(opts = {}) ⇒ Object
155
156
157
158
159
|
# File 'lib/rubber/dns/aws.rb', line 155
def create_host_record(opts = {})
opts = setup_opts(opts, [:host, :data, :domain, :type, :ttl])
zone = find_or_create_zone(opts[:domain])
zone.records.create(opts_to_host(opts))
end
|
#denormalize_name(name, domain) ⇒ Object
30
31
32
33
34
35
36
37
38
39
40
41
|
# File 'lib/rubber/dns/aws.rb', line 30
def denormalize_name(name, domain)
if ! name || name.strip.empty?
name = "#{domain}"
else
name = "#{name}.#{domain}"
end
name = "#{name}."
domain = "#{domain}."
return name, domain
end
|
#destroy_host_record(opts = {}) ⇒ Object
161
162
163
164
165
166
167
|
# File 'lib/rubber/dns/aws.rb', line 161
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
148
149
150
151
152
153
|
# File 'lib/rubber/dns/aws.rb', line 148
def find_host_records(opts = {})
hosts = find_hosts(opts)
result = hosts.collect {|h| host_to_opts(h).merge(:domain => opts[:domain]) }
result
end
|
#find_hosts(opts = {}) ⇒ Object
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
|
# File 'lib/rubber/dns/aws.rb', line 128
def find_hosts(opts = {})
opts = setup_opts(opts, [:host, :domain])
zone = find_or_create_zone(opts[:domain])
host = opts_to_host(opts)
if opts[:host] && opts[:host] != '*'
found_host = zone.records.all(:name => host[:name], :type => host[:type], :max_items => 1).first
found_host = nil if found_host && found_host.name != "#{host[:name]}." && found_host.type != host[:type]
hosts = Array(found_host)
else
hosts = all_hosts(zone)
end
hosts = hosts.select {|h| h.name == host[:name] } if opts.has_key?(:host) && opts[:host] != '*'
hosts = hosts.select {|h| h.type == host[:type] } if opts.has_key?(:type) && opts[:type] != '*'
hosts
end
|
#find_or_create_zone(domain) ⇒ Object
102
103
104
105
106
107
108
|
# File 'lib/rubber/dns/aws.rb', line 102
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
|
#host_to_opts(host) ⇒ Object
Convert from fog/aws model to rubber option hash that represents a dns record
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
|
# File 'lib/rubber/dns/aws.rb', line 44
def host_to_opts(host)
opts = {}
opts[:host] ||= host.name || ''
opts[:domain] ||= host.zone.domain
opts[:host], opts[:domain] = normalize_name(opts[:host], opts[:domain])
opts[:type] ||= host.type
opts[:ttl] ||= host.ttl.to_i if host.ttl
opts[:data] ||= []
if host.type =~ /MX/i
host.value.each do |val|
parts = val.split(" ")
opts[:data] << {'priority' => parts[0], 'value' => parts[1]}
end
elsif ! host.alias_target.nil?
opts[:data] << {
'hosted_zone_id' => host.alias_target['HostedZoneId'],
'dns_name' => host.alias_target['DNSName'].split('.')[0..-1].join('.')
}
opts.delete(:ttl)
else
opts[:data].concat(Array(host.value))
end
return opts
end
|
#normalize_name(name, domain) ⇒ Object
17
18
19
20
21
22
23
24
25
26
27
28
|
# File 'lib/rubber/dns/aws.rb', line 17
def normalize_name(name, domain)
domain = domain.gsub(/\.$/, "")
if name
name = name.gsub(/\.$/, "")
name = name.gsub(/.?#{domain}$/, "")
name = name.gsub("\\052", "*")
end
return name, domain
end
|
#opts_to_host(opts) ⇒ Object
Convert from rubber option hash that represents a dns record to fog/aws model
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
|
# File 'lib/rubber/dns/aws.rb', line 77
def opts_to_host(opts)
host = {}
host[:name], domain = denormalize_name(opts[:host], opts[:domain])
host[:type] = opts[:type]
host[:ttl] = opts[:ttl] if opts[:ttl]
if opts[:data]
if host[:type] =~ /MX/i
host[:value] = opts[:data].collect {|o| "#{o[:priority]} #{o[:value]}"}
elsif opts[:data].first.is_a?(Hash)
host[:alias_target] = opts[:data].first
host.delete(:value)
host.delete(:ttl)
else
host[:value] = opts[:data]
end
end
return host
end
|
#update_host_record(old_opts = {}, new_opts = {}) ⇒ Object
169
170
171
172
173
174
175
176
177
|
# File 'lib/rubber/dns/aws.rb', line 169
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])
new_host = opts_to_host(new_opts)
host = find_hosts(old_opts).first
result = host.modify(new_host)
result || raise("Failed to update host #{host.name}, #{host.errors.full_messages.join(', ')}")
end
|