Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update zone-serial only on changing zone-records (sed version) #45

Merged
merged 8 commits into from
Apr 22, 2014
Merged
Show file tree
Hide file tree
Changes from 4 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
6 changes: 4 additions & 2 deletions manifests/record.pp
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,12 @@
$order = 9
) {

$zone_file = "/etc/bind/zones/db.${zone}"
$cfg_dir = $dns::server::params::cfg_dir

$zone_file_stage = "${cfg_dir}/zones/db.${zone}.stage"

concat::fragment{"db.${zone}.${name}.record":
target => $zone_file,
target => $zone_file_stage,
order => $order,
content => template("${module_name}/zone_record.erb")
}
Expand Down
38 changes: 27 additions & 11 deletions manifests/zone.pp
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
define dns::zone (
$soa = "${::fqdn}.",
$soa_email = "root.${::fqdn}.",
$serial = false,
$zone_ttl = '604800',
$zone_refresh = '604800',
$zone_retry = '86400',
Expand All @@ -16,44 +15,61 @@
$ensure = present
) {

validate_array($allow_transfer)
include dns::server::params
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I use external variable $dns::server::params::cfg_dir and without this include tests don't pass.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use

let(:pre_condition) { 'include dns::server::params' }

In your test block instead.


$zone_serial = $serial ? {
false => inline_template('<%= Time.now.to_i %>'),
default => $serial
}
$cfg_dir = $dns::server::params::cfg_dir

validate_array($allow_transfer)

$zone = $reverse ? {
true => "${name}.in-addr.arpa",
default => $name
}

$zone_file = "/etc/bind/zones/db.${name}"
$zone_file = "${cfg_dir}/zones/db.${name}"
$zone_file_stage = "${zone_file}.stage"

if $ensure == absent {
file { $zone_file:
ensure => absent,
}
} else {
# Zone Database
concat { $zone_file:

# Create "fake" zone file without zone-serial
concat { $zone_file_stage:
owner => 'bind',
group => 'bind',
mode => '0644',
require => [Class['concat::setup'], Class['dns::server']],
notify => Class['dns::server::service']
notify => Exec["bump-${zone}-serial"]
}
concat::fragment{"db.${name}.soa":
target => $zone_file,
target => $zone_file_stage,
order => 1,
content => template("${module_name}/zone_file.erb")
}

# Generate real zone from stage file through replacement _SERIAL_ template
# to current timestamp. A real zone file will be updated only at change of
# the stage file, thanks to this serial is updated only in case of need.
$zone_serial = inline_template('<%= Time.now.to_i %>')
exec { "bump-${zone}-serial":
command => "sed '8s/_SERIAL_/${zone_serial}/' ${zone_file_stage} > ${zone_file}",
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I change only line num 8 (sed '8s/...'). It is safe, but demands care with a zone_file.erb template.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That seems ok to me.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I checked code by lint and corrected all warnings except this line (correct version not committed now). Warning for this line:

manifests/zone.pp:56:80chars:WARNING:line has more than 80 characters

There is a need to correct this warning?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Eh, its ok.

path => ['/bin', '/sbin', '/usr/bin', '/usr/sbin'],
refreshonly => true,
provider => posix,
user => 'bind',
group => 'bind',
require => Class['dns::server::install'],
notify => Class['dns::server::service'],
}
}

# Include Zone in named.conf.local
concat::fragment{"named.conf.local.${name}.include":
ensure => $ensure,
target => '/etc/bind/named.conf.local',
target => "${cfg_dir}/named.conf.local",
order => 3,
content => template("${module_name}/zone.erb")
}
Expand Down
17 changes: 15 additions & 2 deletions spec/defines/dns_zone_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
let(:title) { 'test.com' }

context 'passing something other than an array' do
let :facts do { :concat_basedir => '/dne', } end
let :facts do { :osfamily => 'Debian', :concat_basedir => '/dne' } end
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

include dns::server::params in zone.pp require fact osfamily

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That is fine.

let :params do { :allow_transfer => '127.0.0.1' } end

it 'should fail input validation' do
Expand All @@ -13,7 +13,7 @@
end

context 'passing an array to data' do
let :facts do { :concat_basedir => '/dne', } end
let :facts do { :osfamily => 'Debian', :concat_basedir => '/dne' } end
let :params do
{ :allow_transfer => [ '192.0.2.0', '2001:db8::/32' ] }
end
Expand All @@ -36,6 +36,19 @@
with_content(/2001:db8::\/32/)
}

it {
should contain_concat('/etc/bind/zones/db.test.com.stage')
}

it { should contain_concat__fragment('db.test.com.soa').
with_content(/_SERIAL_/)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is a method to check only the 8th line for pattern?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not that I know of, but I'm pretty happy with this part.

If someone breaks the method of bumping serials, this test will complain.

}

it {
should contain_exec('bump-test.com-serial').
with_refreshonly('true')
}

end

end
Expand Down
2 changes: 1 addition & 1 deletion templates/zone_file.erb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
$ORIGIN <%= @zone %>.
$TTL <%= @zone_ttl %>
@ IN SOA <%= @soa %>. <%= @soa_email %>. (
<%= @zone_serial %> ; Serial
_SERIAL_ ; Serial<%# Be careful at change of number of this line. It is used in zone.pp/Exec[bump-${zone}-serial]. %>
<%= @zone_refresh %> ; Refresh
<%= @zone_retry %> ; Retry
<%= @zone_expire %> ; Expire
Expand Down