Skip to content

Commit

Permalink
allow dhcp::host to set "on EVENT" handlers
Browse files Browse the repository at this point in the history
This adds 3 new optional parameters to dhcp::host:

* on_commit
* on_release
* on_expiry

Each of these accepts an array of strings representing statements that
dhcpd is to perform when that event occurs for that host.

Signed-off-by: John Florian <jflorian@doubledog.org>
  • Loading branch information
John Florian committed Mar 10, 2019
1 parent e6701a0 commit a88dc6f
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 8 deletions.
15 changes: 14 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,20 @@ dhcp::host { 'server1':
ip => '10.0.1.51',
# Optionally override subnet/global settings for some hosts.
default_lease_time => 600,
max_lease_time => 900
max_lease_time => 900,
# Optionally declare event statements in any combination.
on_commit => [
'set ClientIP = binary-to-ascii(10, 8, ".", leased-address)',
'execute("/usr/local/bin/my_dhcp_helper.sh", ClientIP)'
],
on_release => [
'set ClientIP = binary-to-ascii(10, 8, ".", leased-address)',
'log(concat("Released IP: ", ClientIP))'
],
on_expiry => [
'set ClientIP = binary-to-ascii(10, 8, ".", leased-address)',
'log(concat("Expired IP: ", ClientIP))'
]
}
```

Expand Down
15 changes: 9 additions & 6 deletions manifests/host.pp
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,15 @@
define dhcp::host (
Stdlib::IP::Address $ip,
Dhcp::Mac $mac,
String $ddns_hostname = $name,
Hash $options = {},
String $comment = '',
Boolean $ignored = false,
Optional[Integer] $default_lease_time = undef,
Optional[Integer] $max_lease_time = undef,
String $ddns_hostname = $name,
Hash $options = {},
String $comment = '',
Boolean $ignored = false,
Optional[Integer] $default_lease_time = undef,
Optional[Integer] $max_lease_time = undef,
Optional[Array[String[1]]] $on_commit = [],
Optional[Array[String[1]]] $on_release = [],
Optional[Array[String[1]]] $on_expiry = [],
) {

$host = $name
Expand Down
26 changes: 25 additions & 1 deletion spec/defines/host_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,19 @@
let(:params) do
default_params.merge(
'default_lease_time' => 600,
'max_lease_time' => 900
'max_lease_time' => 900,
'on_commit' => [
'set ClientIP = binary-to-ascii(10, 8, ".", leased-address)',
'execute("/usr/local/bin/my_dhcp_helper.sh", ClientIP)'
],
'on_release' => [
'set ClientIP = binary-to-ascii(10, 8, ".", leased-address)',
'log(concat("Released IP: ", ClientIP))'
],
'on_expiry' => [
'set ClientIP = binary-to-ascii(10, 8, ".", leased-address)',
'log(concat("Expired IP: ", ClientIP))'
]
)
end

Expand All @@ -80,6 +92,18 @@
" ddns-hostname \"#{title}\";",
' default-lease-time 600;',
' max-lease-time 900;',
' on commit {',
' set ClientIP = binary-to-ascii(10, 8, ".", leased-address);',
' execute("/usr/local/bin/my_dhcp_helper.sh", ClientIP);',
' }',
' on release {',
' set ClientIP = binary-to-ascii(10, 8, ".", leased-address);',
' log(concat("Released IP: ", ClientIP));',
' }',
' on expiry {',
' set ClientIP = binary-to-ascii(10, 8, ".", leased-address);',
' log(concat("Expired IP: ", ClientIP));',
' }',
'}'
]
expect(content.split("\n")).to match_array(expected_lines)
Expand Down
21 changes: 21 additions & 0 deletions templates/dhcpd.host.erb
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,25 @@ host <%= @host %> {
option <%= option %> <%= @options[option] %>;
<% end -%>
<% end -%>
<% if not @on_commit.empty? -%>
on commit {
<% @on_commit.each do |statement| -%>
<%= statement %>;
<% end -%>
}
<% end -%>
<% if not @on_release.empty? -%>
on release {
<% @on_release.each do |statement| -%>
<%= statement %>;
<% end -%>
}
<% end -%>
<% if not @on_expiry.empty? -%>
on expiry {
<% @on_expiry.each do |statement| -%>
<%= statement %>;
<% end -%>
}
<% end -%>
}

0 comments on commit a88dc6f

Please sign in to comment.