diff --git a/README.md b/README.md index 8e900fcc0..7711a3f00 100644 --- a/README.md +++ b/README.md @@ -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))' + ] } ``` diff --git a/manifests/host.pp b/manifests/host.pp index 9e25686cf..c57ffbe62 100644 --- a/manifests/host.pp +++ b/manifests/host.pp @@ -9,6 +9,9 @@ Boolean $ignored = false, Optional[Integer] $default_lease_time = undef, Optional[Integer] $max_lease_time = undef, + Array[String[1]] $on_commit = [], + Array[String[1]] $on_release = [], + Array[String[1]] $on_expiry = [], ) { $host = $name diff --git a/spec/defines/host_spec.rb b/spec/defines/host_spec.rb index 50efdba9e..69dfc82fc 100644 --- a/spec/defines/host_spec.rb +++ b/spec/defines/host_spec.rb @@ -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 @@ -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) diff --git a/templates/dhcpd.host.erb b/templates/dhcpd.host.erb index bd4b809b2..a9f94b500 100644 --- a/templates/dhcpd.host.erb +++ b/templates/dhcpd.host.erb @@ -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 -%> }