From e1f00ed6c75724251c3e205a3a50c7a3a4bcb077 Mon Sep 17 00:00:00 2001 From: Raphaël Pinson Date: Fri, 12 Apr 2013 10:53:21 +0200 Subject: Rename shared-network as shared_network --- manifests/shared_network.pp | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) create mode 100644 manifests/shared_network.pp (limited to 'manifests/shared_network.pp') diff --git a/manifests/shared_network.pp b/manifests/shared_network.pp new file mode 100644 index 0000000..9a02a5b --- /dev/null +++ b/manifests/shared_network.pp @@ -0,0 +1,25 @@ +# == Definition: dhcp::shared-network +# Creates a shared-network +# +# Arguments: +# *$subnets* : subnet list to be included in the shared-network +# +# Warnings: +# - subnets must exists +# - subnets must have $is_shared set to true (default is false) +# +define dhcp::shared-network( + $ensure = present, + $subnets = [] +) { + + include dhcp::params + + concat::fragment {"shared-${name}": + ensure => $ensure, + target => "${dhcp::params::config_dir}/dhcpd.conf", + content => template('dhcp/shared-network.erb'), + require => Dhcp::Subnet[$subnets], + } + +} -- cgit v1.2.3 From b35e1322d0a791ab22cb2c385ec913492c75026d Mon Sep 17 00:00:00 2001 From: Raphaël Pinson Date: Fri, 12 Apr 2013 11:10:45 +0200 Subject: Rename dhcp::shared-network to dhcp::shared_network, add specs --- manifests/shared_network.pp | 15 ++++--- spec/defines/dhcp_shared_network_spec.rb | 71 ++++++++++++++++++++++++++++++++ templates/shared-network.erb | 9 ++-- 3 files changed, 87 insertions(+), 8 deletions(-) create mode 100644 spec/defines/dhcp_shared_network_spec.rb (limited to 'manifests/shared_network.pp') diff --git a/manifests/shared_network.pp b/manifests/shared_network.pp index 9a02a5b..38f14fb 100644 --- a/manifests/shared_network.pp +++ b/manifests/shared_network.pp @@ -8,17 +8,22 @@ # - subnets must exists # - subnets must have $is_shared set to true (default is false) # -define dhcp::shared-network( +define dhcp::shared_network( $ensure = present, - $subnets = [] + $subnets = [], ) { - include dhcp::params + include ::dhcp::params - concat::fragment {"shared-${name}": + validate_string($ensure) + validate_re($ensure, ['present', 'absent'], + "\$ensure must be either 'present' or 'absent', got '${ensure}'") + validate_array($subnets) + + concat::fragment {"dhcp-shared-${name}": ensure => $ensure, target => "${dhcp::params::config_dir}/dhcpd.conf", - content => template('dhcp/shared-network.erb'), + content => template("${module_name}/shared-network.erb"), require => Dhcp::Subnet[$subnets], } diff --git a/spec/defines/dhcp_shared_network_spec.rb b/spec/defines/dhcp_shared_network_spec.rb new file mode 100644 index 0000000..ff5a2c5 --- /dev/null +++ b/spec/defines/dhcp_shared_network_spec.rb @@ -0,0 +1,71 @@ +require 'spec_helper' + +describe 'dhcp::shared_network' do + let (:title) { 'My network' } + let (:facts) { { + :operatingsystem => 'Debian', + :osfamily => 'Debian', + :lsbdistcodename => 'squeeze' + } } + + context 'when passing wrong value for ensure' do + let (:params) { { + :ensure => 'running', + } } + + it 'should fail' do + expect { + should contain_concat__fragment('dhcp-shared-My network') + }.to raise_error(Puppet::Error, /\$ensure must be either 'present' or 'absent', got 'running'/) + end + end + + context 'when passing wrong type for subnets' do + let (:params) { { + :subnets => true, + } } + + it 'should fail' do + expect { + should contain_concat__fragment('dhcp-shared-My network') + }.to raise_error(Puppet::Error, /true is not an Array\./) + end + end + + context 'when passing no parameters' do + it { should contain_concat__fragment('dhcp-shared-My network').with( + :ensure => 'present', + :target => '/etc/dhcp/dhcpd.conf' + ).with_content( + /shared-network My network/ + ) + } + end + + context 'when passing wrong type for a subnet' do + let (:params) { { + :subnets => [true], + } } + + it 'should fail' do + expect { + should contain_concat__fragment('dhcp-shared-My network') + }.to raise_error(Puppet::Error, /true is not a string\./) + end + end + + context 'when passing wrong value for a subnet' do + let (:params) { { + :subnets => ['wrong value'], + } } + + it 'should fail' do + expect { + should contain_concat__fragment('dhcp-shared-My network') + }.to raise_error(Puppet::Error, /"wrong value" does not match/) + end + end + + context 'when passing subnets' do + end +end diff --git a/templates/shared-network.erb b/templates/shared-network.erb index 99a7aca..407106a 100644 --- a/templates/shared-network.erb +++ b/templates/shared-network.erb @@ -1,6 +1,9 @@ -#### dhcp::shared-network <%= name %> -shared-network <%= name %> { -<% subnets.each do |subnet| -%> +#### dhcp::shared_network <%= @name %> +shared-network <%= @name %> { +<% @subnets.each do |subnet| + scope.function_validate_string([subnet]) + scope.function_validate_re([subnet, '^([1-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])(\.([0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])){3}$']) +-%> include "<%= scope.lookupvar("dhcp::params::config_dir") %>/subnets/<%= subnet %>.conf"; <% end -%> } -- 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/shared_network.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