From 745b2537e79eca59f8e9f2f15230482fcdf140aa Mon Sep 17 00:00:00 2001 From: Raphaël Pinson Date: Thu, 11 Apr 2013 11:54:50 +0200 Subject: Refactor module * Use a top dhcp class with a $server boolean param * Refactor the dhcp::class to use packages/config/service classes * Don't use inheritance, put everything in dhcp::params --- manifests/server.pp | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) (limited to 'manifests/server.pp') diff --git a/manifests/server.pp b/manifests/server.pp index 6bdf51f..779b305 100644 --- a/manifests/server.pp +++ b/manifests/server.pp @@ -33,7 +33,7 @@ node "dhcp.toto.ltd" { */ class dhcp::server { - case $::operatingsystem { - Debian: { include dhcp::server::debian } - } + class { '::dhcp::server::packages': } -> + class { '::dhcp::server::config': } ~> + class { '::dhcp::server::service': } } -- cgit v1.2.3 From 90aa04648bf2e79890e47b8f84df8903f9958a12 Mon Sep 17 00:00:00 2001 From: Raphaël Pinson Date: Thu, 11 Apr 2013 11:58:37 +0200 Subject: Use # as comments --- manifests/server.pp | 65 +++++++++++++++++++++++++---------------------------- 1 file changed, 31 insertions(+), 34 deletions(-) (limited to 'manifests/server.pp') diff --git a/manifests/server.pp b/manifests/server.pp index 779b305..63e9f37 100644 --- a/manifests/server.pp +++ b/manifests/server.pp @@ -1,37 +1,34 @@ -/* - -= Class: dhcp::server -Simple OS wrapper. Include this to install a dhcp server on your host. - -Requires: - module "common": git://github.com/camptocamp/puppet-common.git - -facultative argument: - *$dhcpd_ddns_update* : ddns-update-style option (default to none) - *$dhcpd_authoritative* : set it if you want that your DHCP server is - authoritative (default to no) - *$dhcpd_opts* : any other DHCPD valid options - -Example: -node "dhcp.toto.ltd" { - $dhcpd_opts = ['domain-name "toto.ltd"', "domain-name-servers 192.168.21.1"] - include dhcp::server - - dhcp::subnet {"10.27.20.0": - ensure => present, - broadcast => "10.27.20.255", - other_opts => ['filename "pxelinux.0";', 'next-server 10.27.10.1;'], - } - - dhcp::host {"titi-eth0": - ensure => present, - mac => "0e:18:fa:fe:d9:00", - subnet => "10.27.20.0", - fixed_address => "10.27.10.52", - } -} -*/ - +# = Class: dhcp::server +# Simple OS wrapper. Include this to install a dhcp server on your host. +# +# Requires: +# module "common": git://github.com/camptocamp/puppet-common.git +# +# facultative argument: +# *$dhcpd_ddns_update* : ddns-update-style option (default to none) +# *$dhcpd_authoritative* : set it if you want that your DHCP server is +# authoritative (default to no) +# *$dhcpd_opts* : any other DHCPD valid options +# +# Example: +# node "dhcp.toto.ltd" { +# $dhcpd_opts = ['domain-name "toto.ltd"', "domain-name-servers 192.168.21.1"] +# include dhcp::server +# +# dhcp::subnet {"10.27.20.0": +# ensure => present, +# broadcast => "10.27.20.255", +# other_opts => ['filename "pxelinux.0";', 'next-server 10.27.10.1;'], +# } +# +# dhcp::host {"titi-eth0": +# ensure => present, +# mac => "0e:18:fa:fe:d9:00", +# subnet => "10.27.20.0", +# fixed_address => "10.27.10.52", +# } +# } +# class dhcp::server { class { '::dhcp::server::packages': } -> class { '::dhcp::server::config': } ~> -- cgit v1.2.3 From a3158aaca4070a4de89b49d687854d72df8741fe Mon Sep 17 00:00:00 2001 From: Raphaël Pinson Date: Thu, 11 Apr 2013 12:37:03 +0200 Subject: Fix trailing whitespaces --- manifests/server.pp | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) (limited to 'manifests/server.pp') diff --git a/manifests/server.pp b/manifests/server.pp index 63e9f37..ceffa14 100644 --- a/manifests/server.pp +++ b/manifests/server.pp @@ -1,26 +1,26 @@ # = Class: dhcp::server # Simple OS wrapper. Include this to install a dhcp server on your host. -# +# # Requires: # module "common": git://github.com/camptocamp/puppet-common.git -# +# # facultative argument: # *$dhcpd_ddns_update* : ddns-update-style option (default to none) # *$dhcpd_authoritative* : set it if you want that your DHCP server is # authoritative (default to no) # *$dhcpd_opts* : any other DHCPD valid options -# +# # Example: # node "dhcp.toto.ltd" { # $dhcpd_opts = ['domain-name "toto.ltd"', "domain-name-servers 192.168.21.1"] # include dhcp::server -# +# # dhcp::subnet {"10.27.20.0": # ensure => present, # broadcast => "10.27.20.255", # other_opts => ['filename "pxelinux.0";', 'next-server 10.27.10.1;'], # } -# +# # dhcp::host {"titi-eth0": # ensure => present, # mac => "0e:18:fa:fe:d9:00", -- cgit v1.2.3 From 65bea20e0d8e61de7519bb1ab0677597500aaac7 Mon Sep 17 00:00:00 2001 From: Raphaël Pinson Date: Thu, 11 Apr 2013 14:25:15 +0200 Subject: Get rid of unscoped variables in dhcp::server, fix template --- manifests/init.pp | 9 +++++- manifests/server.pp | 20 +++++++----- manifests/server/config.pp | 4 +++ spec/classes/dhcp_spec.rb | 68 +++++++++++++++++++++++++++++++++++++++++ templates/dhcpd.conf.debian.erb | 16 +++------- 5 files changed, 97 insertions(+), 20 deletions(-) (limited to 'manifests/server.pp') diff --git a/manifests/init.pp b/manifests/init.pp index 8a14b13..e1e05b3 100644 --- a/manifests/init.pp +++ b/manifests/init.pp @@ -1,7 +1,14 @@ class dhcp ( $server = true, + $server_ddns_update = undef, + $server_authoritative = undef, + $server_opts = undef, ) { if $server { - class { '::dhcp::server': } + class { '::dhcp::server': + ddns_update => $server_ddns_update, + authoritative => $server_authoritative, + opts => $server_opts, + } } } diff --git a/manifests/server.pp b/manifests/server.pp index ceffa14..d83c26f 100644 --- a/manifests/server.pp +++ b/manifests/server.pp @@ -5,15 +5,17 @@ # module "common": git://github.com/camptocamp/puppet-common.git # # facultative argument: -# *$dhcpd_ddns_update* : ddns-update-style option (default to none) -# *$dhcpd_authoritative* : set it if you want that your DHCP server is -# authoritative (default to no) -# *$dhcpd_opts* : any other DHCPD valid options +# *$ddns_update* : ddns-update-style option (default to none) +# *$authoritative* : set it if you want that your DHCP server is +# authoritative (default to no) +# *$opts* : any other DHCPD valid options # # Example: # node "dhcp.toto.ltd" { -# $dhcpd_opts = ['domain-name "toto.ltd"', "domain-name-servers 192.168.21.1"] -# include dhcp::server +# class { 'dhcp::server': +# opts => ['domain-name "toto.ltd"', +# 'domain-name-servers 192.168.21.1'], +# } # # dhcp::subnet {"10.27.20.0": # ensure => present, @@ -29,7 +31,11 @@ # } # } # -class dhcp::server { +class dhcp::server ( + $ddns_update = 'none', + $authoritative = false, + $opts = [], +) { class { '::dhcp::server::packages': } -> class { '::dhcp::server::config': } ~> class { '::dhcp::server::service': } diff --git a/manifests/server/config.pp b/manifests/server/config.pp index 4898a60..0c1a74a 100644 --- a/manifests/server/config.pp +++ b/manifests/server/config.pp @@ -11,6 +11,10 @@ class dhcp::server::config { validate_string($dhcp::params::server_template) validate_re($dhcp::params::server_template, '^\S+$') + validate_string($dhcp::server::ddns_update) + validate_bool($dhcp::server::authoritative) + validate_array($dhcp::server::opts) + concat {"${dhcp::params::config_dir}/dhcpd.conf": owner => root, group => root, diff --git a/spec/classes/dhcp_spec.rb b/spec/classes/dhcp_spec.rb index c833c52..ce9228e 100644 --- a/spec/classes/dhcp_spec.rb +++ b/spec/classes/dhcp_spec.rb @@ -34,6 +34,13 @@ describe 'dhcp' do :mode => '0644' ) } + it { should contain_concat__fragment('00.dhcp.server.base').with( + :ensure => 'present', + :target => '/etc/dhcp3/dhcpd.conf', + :content => /log-facility/ + ).with_content(/ddns-update-style none;/).with_content(/#authoritative/) + } + # Service it { should contain_service('dhcpd').with( :ensure => 'running', @@ -62,6 +69,13 @@ describe 'dhcp' do :mode => '0644' ) } + it { should contain_concat__fragment('00.dhcp.server.base').with( + :ensure => 'present', + :target => '/etc/dhcp/dhcpd.conf', + :content => /log-facility/ + ).with_content(/ddns-update-style none;/).with_content(/#authoritative/) + } + # Service it { should contain_service('dhcpd').with( :ensure => 'running', @@ -70,4 +84,58 @@ describe 'dhcp' do :pattern => '/usr/sbin/dhcpd' ) } end + + context 'When passing ddns_update' do + let (:facts) { { + :operatingsystem => 'Debian', + :osfamily => 'Debian', + :lsbdistcodename => 'squeeze' + } } + let (:params) { { + :server_ddns_update => 'foo' + } } + + it { should contain_concat__fragment('00.dhcp.server.base').with( + :ensure => 'present', + :target => '/etc/dhcp/dhcpd.conf', + :content => /log-facility/ + ).with_content(/ddns-update-style foo;/).with_content(/#authoritative/) + } + end + + context 'When passing authoritative' do + let (:facts) { { + :operatingsystem => 'Debian', + :osfamily => 'Debian', + :lsbdistcodename => 'squeeze' + } } + let (:params) { { + :server_authoritative => true + } } + + it { should contain_concat__fragment('00.dhcp.server.base').with( + :ensure => 'present', + :target => '/etc/dhcp/dhcpd.conf', + :content => /log-facility/ + ).with_content(/ddns-update-style none;/).with_content(/[^#]authoritative/) + } + end + + context 'When passing opts' do + let (:facts) { { + :operatingsystem => 'Debian', + :osfamily => 'Debian', + :lsbdistcodename => 'squeeze' + } } + let (:params) { { + :server_opts => ['foo', 'bar', 'baz'] + } } + + it { should contain_concat__fragment('00.dhcp.server.base').with( + :ensure => 'present', + :target => '/etc/dhcp/dhcpd.conf', + :content => /log-facility/ + ).with_content(/ddns-update-style none;/).with_content(/#authoritative/).with_content(/foo;\nbar;\nbaz;\n/) + } + end end diff --git a/templates/dhcpd.conf.debian.erb b/templates/dhcpd.conf.debian.erb index ee95bee..7dfd168 100644 --- a/templates/dhcpd.conf.debian.erb +++ b/templates/dhcpd.conf.debian.erb @@ -4,22 +4,14 @@ # attempt to do a DNS update when a lease is confirmed. We default to the # behavior of the version 2 packages ('none', since DHCP v2 didn't # have support for DDNS.) -<% if has_variable?('dhcpd_ddns_update') -%> -ddns-update-style <%=dhcpd_ddns_update%>; -<% else -%> -ddns-update-style none; -<% end -%> +ddns-update-style <%= @ddns_update %>; # If this DHCP server is the official DHCP server for the local # network, the authoritative directive should be uncommented. -<% if has_variable?('dhcpd_authoritative') -%> -authoritative; -<% else -%> -#authoritative; -<% end -%> +<% unless @authoritative -%>#<%- end -%>authoritative; -<% if has_variable?('dhcpd_opts') and not dhcpd_opts.empty? -%> -<%= dhcpd_opts.join(";\n") %> +<% unless @opts.empty? -%> +<%= @opts.join(";\n") %>; <% end -%> # Use this to send dhcp log messages to a different log file (you also -- cgit v1.2.3 From 2b4ea8662914c9652cfde1834bbfdc6d25e45209 Mon Sep 17 00:00:00 2001 From: Raphaël Pinson Date: Thu, 11 Apr 2013 14:37:04 +0200 Subject: Doc in dhcp::server --- manifests/server.pp | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) (limited to 'manifests/server.pp') diff --git a/manifests/server.pp b/manifests/server.pp index d83c26f..1c77cf2 100644 --- a/manifests/server.pp +++ b/manifests/server.pp @@ -5,10 +5,10 @@ # module "common": git://github.com/camptocamp/puppet-common.git # # facultative argument: -# *$ddns_update* : ddns-update-style option (default to none) -# *$authoritative* : set it if you want that your DHCP server is -# authoritative (default to no) -# *$opts* : any other DHCPD valid options +# *$ddns_update* : ddns-update-style option (defaults to 'none') +# *$authoritative* : a boolean setting whether the DHCP server is +# authoritative (defaults to false) +# *$opts* : an array of DHCPD valid options # # Example: # node "dhcp.toto.ltd" { -- cgit v1.2.3 From 928dbaa62bcc37092ddec36b1f715365576eae6d Mon Sep 17 00:00:00 2001 From: Raphaël Pinson Date: Fri, 12 Apr 2013 14:11:44 +0200 Subject: doc --- README.md | 8 +++++++ manifests/hosts.pp | 38 +++++++++++++++++++++++++------ manifests/init.pp | 22 ++++++++++++++++++ manifests/server.pp | 54 +++++++++++++++++++++++---------------------- manifests/shared_network.pp | 20 ++++++++++++----- manifests/subnet.pp | 34 ++++++++++++++-------------- 6 files changed, 120 insertions(+), 56 deletions(-) (limited to 'manifests/server.pp') diff --git a/README.md b/README.md index 7a54b86..eb129db 100644 --- a/README.md +++ b/README.md @@ -9,6 +9,14 @@ This module is provided by [Camptocamp](http://www.camptocamp.com/) * dhcp * dhcp::server +### dhcp + +The `dhcp` class is a wrapper around `dhcp::server`: + + include ::dhcp + +### dhcp::server + ## Definitions * dhcp::hosts diff --git a/manifests/hosts.pp b/manifests/hosts.pp index 9e2b284..1767a90 100644 --- a/manifests/hosts.pp +++ b/manifests/hosts.pp @@ -2,13 +2,13 @@ # # Creates a dhcp configuration for given hosts # -# Arguments -# $template: dhcp host template - default: 'dhcp/host.conf.erb' -# $global_options: an array of global options for the whole bunch of hosts. -# you may override it per host, setting the host "options" -# directly in the hash. -# $subnet: targeted subnet -# $hash_data: hash containing data - default form: +# Parameters: +# ['template'] - DHCP host template - default: 'dhcp/host.conf.erb' +# ['global_options'] - An array of global options for the whole bunch of +# hosts. You may override it per host, setting the +# host "options" directly in the hash. +# ['subnet'] - Targeted subnet +# ['hash_data'] - Hash containing data - default form: # { # => { # options => ['opt1', 'opt2'], @@ -33,6 +33,30 @@ # …, # } # +# Sample usage: +# ::dhcp::hosts { 'workstations': +# subnet => '192.168.1.0', +# 'hash_data' => { +# 'host1' => { +# 'interfaces' => { +# 'eth0' => '00:11:22:33:44:55', +# 'wlan0' => '00:aa:bb:44:55:ff', +# }, +# }, +# 'host2' => { +# 'interfaces' => { +# 'eth1' => '00:11:af:33:44:55', +# }, +# 'fixed_address' => 'foo.example.com', +# 'options' => ['opt1'], +# }, +# }, +# } +# +# Requires: +# - puppetlabs/stdlib +# - ripienaar/concat +# define dhcp::hosts ( $hash_data, $subnet, diff --git a/manifests/init.pp b/manifests/init.pp index e1e05b3..9af496f 100644 --- a/manifests/init.pp +++ b/manifests/init.pp @@ -1,3 +1,25 @@ +# Class: dhcp +# +# This class provides a simple way to install a DHCP server +# It will install and configure the necessary packages. +# +# Parameters: +# ['server'] - Whether to install the DHCP server +# (default: true) +# ['server_ddns_update'] - Set ddns_update on dhcp::server +# ['server_authoritative'] - Set authoritative on dhcp::server +# ['server_opts'] - Set opts for dhcp::server +# +# Actions: +# - Deploys a DHCP server +# +# Sample usage: +# include ::dhcp +# +# Requires: +# - puppetlabs/stdlib +# - ripienaar/concat +# class dhcp ( $server = true, $server_ddns_update = undef, diff --git a/manifests/server.pp b/manifests/server.pp index 1c77cf2..a44cfe1 100644 --- a/manifests/server.pp +++ b/manifests/server.pp @@ -1,35 +1,37 @@ -# = Class: dhcp::server -# Simple OS wrapper. Include this to install a dhcp server on your host. +# Class: dhcp::server # -# Requires: -# module "common": git://github.com/camptocamp/puppet-common.git +# Installs and configures a DHCP server. # -# facultative argument: -# *$ddns_update* : ddns-update-style option (defaults to 'none') -# *$authoritative* : a boolean setting whether the DHCP server is -# authoritative (defaults to false) -# *$opts* : an array of DHCPD valid options +# Parameters: +# ['ddns_update'] : ddns-update-style option (defaults to 'none') +# ['authoritative'] : a boolean setting whether the DHCP server is +# authoritative (defaults to false) +# ['opts'] : an array of DHCPD valid options # -# Example: -# node "dhcp.toto.ltd" { -# class { 'dhcp::server': -# opts => ['domain-name "toto.ltd"', -# 'domain-name-servers 192.168.21.1'], -# } +# Sample usage: +# node "dhcp.toto.ltd" { +# class { 'dhcp::server': +# opts => ['domain-name "toto.ltd"', +# 'domain-name-servers 192.168.21.1'], +# } # -# dhcp::subnet {"10.27.20.0": -# ensure => present, -# broadcast => "10.27.20.255", -# other_opts => ['filename "pxelinux.0";', 'next-server 10.27.10.1;'], -# } +# dhcp::subnet {"10.27.20.0": +# ensure => present, +# broadcast => "10.27.20.255", +# other_opts => ['filename "pxelinux.0";', 'next-server 10.27.10.1;'], +# } # -# dhcp::host {"titi-eth0": -# ensure => present, -# mac => "0e:18:fa:fe:d9:00", -# subnet => "10.27.20.0", -# fixed_address => "10.27.10.52", +# dhcp::host {"titi-eth0": +# ensure => present, +# mac => "0e:18:fa:fe:d9:00", +# subnet => "10.27.20.0", +# fixed_address => "10.27.10.52", +# } # } -# } +# +# Requires: +# - puppetlabs/stdlib +# - ripienaar/concat # class dhcp::server ( $ddns_update = 'none', diff --git a/manifests/shared_network.pp b/manifests/shared_network.pp index 38f14fb..766f432 100644 --- a/manifests/shared_network.pp +++ b/manifests/shared_network.pp @@ -1,12 +1,22 @@ -# == Definition: dhcp::shared-network +# Definition: dhcp::shared-network +# # Creates a shared-network # -# Arguments: -# *$subnets* : subnet list to be included in the shared-network +# Parameters: +# ['subnets'] - An array of subnets to be included in the shared-network. +# +# Sample usage: +# ::dhcp::shared_network { 'office': +# subnets => ['192.168.1.0', '192.168.2.0'], +# } +# +# Requires: +# - puppetlabs/stdlib +# - ripienaar/concat # # Warnings: -# - subnets must exists -# - subnets must have $is_shared set to true (default is false) +# - subnets must exists +# - subnets must have $is_shared set to true (default is false) # define dhcp::shared_network( $ensure = present, diff --git a/manifests/subnet.pp b/manifests/subnet.pp index b9826da..b70a233 100644 --- a/manifests/subnet.pp +++ b/manifests/subnet.pp @@ -1,29 +1,27 @@ -# = Definition: dhcp::subnet -# Creates a subnet -# -# Arguments: -# *$broadcast* : subnet broadcast (mandatory) -# *$netmask* : subnet netmask (if not set, takes eth0 netmask) -# *$routers* : subnet routers (array) (if not set, takes eth0 IP) -# *$subnet_mask* : netmask sent to dhcp guests (if not set, takes -# $netmask, or netmask_eth0) -# *$domain_name* : subnet domain name (if not set, takes server domain) -# *$other_opts* : any other DHCPD option, as an array -# *$is_shared* : whether it's part of a shared network or not. Default: false +# Definition: dhcp::subnet # -# Example: +# Creates a subnet # -# node "dhcp.domain.ltd" { -# $dhcpd_domain_name = 'domain.ltd' -# $dhcpd_dns_servers = '10.27.21.1, 10.26.21.1' -# include dhcp +# Parameters: +# ['broadcast'] : subnet broadcast (mandatory) +# ['netmask'] : subnet netmask +# (default: $::netmask_eth0) +# ['routers'] : An array of subnet routers +# (default: $::netmask) +# ['subnet_mask'] : netmask sent to dhcp guests +# (default: the value of $netmask) +# ['domain_name'] : subnet domain name +# (default: $::domain) +# ['other_opts'] : An array of additional DHCPD options +# ['is_shared'] : whether it's part of a shared network or not +# (default: false) # +# Sample usage: # dhcp::subnet {"10.27.20.0": # ensure => present, # broadcast => "10.27.20.255", # other_opts => ['filename "pxelinux.0";', 'next-server 10.27.10.1;'], # } -# } # define dhcp::subnet( $broadcast, -- cgit v1.2.3