Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 18 additions & 13 deletions recipes/default.rb
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ class Chef::Recipe::NameServer
# end
end

include_recipe('bind9-reversezones::reverse_zones')

template File.join(node[:bind9][:config_path], node[:bind9][:options_file]) do
source "named.conf.options.erb"
Expand All @@ -94,7 +95,7 @@ class Chef::Recipe::NameServer
group node[:bind9][:user]
mode 0644
variables({
:zonefiles => search(:zones)
:zonefiles => search(:zones) + search(:reversezones)
})
notifies :restart, "service[bind9]"
end
Expand All @@ -119,16 +120,17 @@ class Chef::Recipe::NameServer
end

search(:zones).each do |zone|
#unless zone['autodomain'].nil? || zone['autodomain'] == ''
# search(:node, "domain:#{zone['autodomain']}").each do |host|
# next if host['ipaddress'] == '' || host['ipaddress'].nil?
# zone['zone_info']['records'].push( {
# "name" => host['hostname'],
# "type" => "A",
# "ip" => host['ipaddress']
# })
# end
#end
Chef::Log.info("Got zone #{zone[:domain]}")
unless zone['autodomain'].nil? || zone['autodomain'] == ''
search(:node, "domain:#{zone['autodomain']}").each do |host|
next if host['ipaddress'] == '' || host['ipaddress'].nil?
zone['zone_info']['records'].push( {
"name" => host['hostname'],
"type" => "A",
"ip" => host['ipaddress']
})
end
end

template File.join(node[:bind9][:zones_path], zone['domain']) do
source File.join(node[:bind9][:zones_path], "#{zone['domain']}.erb")
Expand All @@ -155,9 +157,12 @@ class Chef::Recipe::NameServer
:global_ttl => zone['zone_info']['global_ttl'],
:nameserver => zone['zone_info']['nameserver'],
:mail_exchange => zone['zone_info']['mail_exchange'],
:records => zone['zone_info']['records']
:records => zone['zone_info']['records'].sort do |a, b|
a['ip'] <=> b['ip'] if a['name'] == b['name']
a['name'] <=> b['name']
end
})
notifies :create, resources(:template => File.join(node[:bind9][:zones_path], zone['domain'])), :immediately
notifies :create, resources(template: File.join(node[:bind9][:zones_path], zone[:domain])), :immediately
end
end

Expand Down
77 changes: 77 additions & 0 deletions recipes/reverse_zones.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
# Cookbook Name:: bind9-reversezones
# Recipe:: reversezones
#
# Copyright 2013, Arnold Krille
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#


directory node[:bind9][:zones_path] do
owner node[:bind9][:user]
group node[:bind9][:user]
mode 0744
recursive true
not_if { ::File.directory?(node[:bind9][:zones_path]) or ::File.symlink?(node[:bind9][:zones_path]) }
end

search(:reversezones).each do |zone|
Chef::Log.info("Got reverse-zone #{zone[:domain]}")
unless zone['autodomain'].nil? || zone['autodomain'] == ''
zoneip = zone['domain'].scan(/[0-9]+/).join('.')
search(:node, "ipaddress:#{zone['autodomain']}*").each do |host|
next if host['ipaddress'] == '' || host['ipaddress'].nil?
zone['zone_info']['records'].push( {
"name" => host['fqdn'] || "#{host['name']}.#{host['domain']}",
"type" => "PTR",
"ip" => host['ipaddress'].scan(/[0-9]{1,3}/).reverse().join('.').sub!(/\.#{zoneip}$/, '')
})
end
end
if not zone['domain'].end_with?('.IN-ADDR.ARPA')
zone['domain'] += '.IN-ADDR.ARPA'
end

template File.join(node[:bind9][:zones_path], zone['domain']) do
source File.join(node[:bind9][:zones_path], "#{zone['domain']}.erb")
local true
owner node[:bind9][:user]
group node[:bind9][:user]
mode 0644
notifies :restart, "service[bind9]"
variables({
:serial => zone['zone_info']['serial'] || Time.new.strftime("%Y%m%d%H%M%S")
})
action :nothing
end

template File.join(node[:bind9][:zones_path], "#{zone['domain']}.erb") do
source "reverse_zonefile.erb"
owner node[:bind9][:user]
group node[:bind9][:user]
mode 0644
variables({
:domain => zone['domain'],
:soa => zone['zone_info']['soa'],
:contact => zone['zone_info']['contact'],
:global_ttl => zone['zone_info']['global_ttl'],
:nameserver => zone['zone_info']['nameserver'],
:mail_exchange => zone['zone_info']['mail_exchange'],
:records => zone['zone_info']['records'].sort do |a, b|
a['name'] <=> b['name'] if a['ip'] == b['ip']
a['ip'] <=> b['ip']
end
})
notifies :create, resources(:template => File.join(node[:bind9][:zones_path], zone['domain'])), :immediately
end
end
21 changes: 21 additions & 0 deletions templates/default/reverse_zonefile.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
$TTL <%= @global_ttl %>
@ IN SOA <%= @soa %> <%= @contact %> (
<%%= @serial %> ; serial [yyyyMMddNN]
4H ; refresh
30M ; retry
1W ; expiry
1D ; minimum
)

IN NS <%= @soa %>
<% @nameserver.each do |ns| -%>
IN NS <%= ns %>
<% end %>

<% @mail_exchange.each do |mx| -%>
IN MX <%= mx['priority'] %> <%= mx['host'] %>
<% end %>

<% @records.each do |record| -%>
<%= "%-20s %5s IN %5s %s." % [record['ip'],record['ttl'],record['type'],record['name']] %>
<% end %>