Skip to content
Browse files

Forked from zleslie's dhcp module, reworked for theforeman

  • Loading branch information...
0 parents commit 2f1bbd2ba400514860f6af4f1955157789110766 Greg Sutcliffe committed Mar 31, 2012
53 README.md
@@ -0,0 +1,53 @@
+# DHCP module for Puppet
+
+DHCP module for theforeman. Based on original DHCP module by ZLeslie, thanks
+to him for the original work.
+
+Installs and manages a DHCP server.
+
+## Features
+* Multiple subnet support
+* Host reservations
+* Secure dynamic DNS updates when combined with Bind
+
+## Usage
+Define the server and the zones it will be responsible for.
+
+ class { 'dhcp':
+ dnsdomain => [
+ 'dc1.example.net',
+ '1.0.10.in-addr.arpa',
+ ],
+ nameservers => ['10.0.1.20'],
+ ntpservers => ['us.pool.ntp.org'],
+ interfaces => ['eth0'],
+ dnsupdatekey => "/etc/bind/keys.d/$ddnskeyname",
+ require => Bind::Key[ $ddnskeyname ],
+ pxeserver => '10.0.1.50',
+ pxefilename => 'pxelinux.0',
+ }
+
+### dhcp::pool
+Define the pool attributes
+
+ dhcp::pool{ 'ops.dc1.example.net':
+ network => '10.0.1.0',
+ mask => '255.255.255.0',
+ range => '10.0.1.100 10.0.1.200',
+ gateway => '10.0.1.1',
+ }
+
+
+### dhcp::host
+Create host reservations.
+
+ dhcp::host {
+ 'server1': mac => "00:50:56:00:00:01", ip => "10.0.1.51";
+ 'server2': mac => "00:50:56:00:00:02", ip => "10.0.1.52";
+ 'server3': mac => "00:50:56:00:00:03", ip => "10.0.1.53";
+ }
+
+## Contributors
+Zach Leslie <zach.leslie@gmail.com>
+Ben Hughes <git@mumble.org.uk>
+Greg Sutcliffe <greg.sutcliffe@gmail.com>
51 files/dhcpd.hosts
@@ -0,0 +1,51 @@
+host vs1.zlan { # eth
+ hardware ethernet 08:00:27:d4:ba:f7;
+ fixed-address 10.210.18.31;
+}
+host vs2.zlan { # eth
+ hardware ethernet 08:00:27:45:8c:7b;
+ fixed-address 10.210.18.32;
+}
+
+host carbon.wifi.zlan { # wifi
+ hardware ethernet 00:1f:f3:fa:88:e2;
+ fixed-address 10.210.18.50;
+}
+host carbon.zlan { # eth
+ hardware ethernet 00:1f:f3:45:18:5b;
+ fixed-address 10.210.18.50;
+}
+host wks2.zlan { # eth
+ hardware ethernet 00:26:bb:4c:66:e6;
+ fixed-address 10.210.18.122;
+}
+
+host lt1.wifi.zlan { #wifi
+ hardware ethernet f8:1e:df:e6:82:47;
+ fixed-address 10.210.18.125;
+}
+host lt1.zlan { #eth
+ hardware ethernet 7c:6d:62:8f:e6:3f;
+ fixed-address 10.210.18.125;
+}
+host lt2.zlan { #wifi
+ hardware ethernet 00:18:de:37:27:98;
+ fixed-address 10.210.18.126;
+}
+host zachbook { #wifi
+ hardware ethernet 60:33:4b:2a:ba:b6;
+ fixed-address 10.210.18.127;
+}
+host ps3.zlan { #wifi
+ hardware ethernet 00:1f:a7:1a:16:6e;
+ fixed-address 10.210.18.130;
+}
+host touch1.zlan {
+ hardware ethernet 90:27:e4:66:9d:c3;
+ fixed-address 10.210.18.131;
+}
+host touch2.zlan {
+ hardware ethernet 00:22:41:61:56:26;
+ fixed-address 10.210.18.132;
+}
+
33 files/dhcpd.pools
@@ -0,0 +1,33 @@
+
+#subnet 10.132.6.0 netmask 255.255.255.0 {
+#
+# pool {
+# failover peer "dhcp-failover";
+# deny dynamic bootp clients;
+# range 10.132.6.21 10.132.6.239 ;
+# }
+
+# option subnet-mask 255.255.255.0;
+# option domain-name "eng.vmware.com";
+# option routers 10.132.6.253;
+# default-lease-time 1800;
+# max-lease-time 7200;
+
+#}
+
+#################################
+# 10.210.18.0/24
+#################################
+
+subnet 10.210.18.0 netmask 255.255.255.0 {
+ pool
+ {
+ #failover peer "dhcp-failover";
+ range 10.210.18.175 10.210.18.199;
+ }
+ option subnet-mask 255.255.255.0;
+ option routers 10.210.18.253;
+
+}
+
+
25 manifests/disable.pp
@@ -0,0 +1,25 @@
+class dhcp::disable {
+ include dhcp::params
+
+ $dhcp_dir = $dhcp::params::dhcp_dir
+ $dnsdomain = $dhcp::params::dnsdomain
+ $nameservers = $dhcp::params::nameservers
+ $ntpserver = $dhcp::params::ntpserver
+ $pxeserver = $dhcp::params::pxeserver
+ $filename = $dhcp::params::filename
+ $logfacility = $dhcp::params::logfacility
+
+ package {
+ "isc-dhcp-server":
+ ensure => absent;
+ }
+ service {
+ "isc-dhcp-server":
+ enable => false,
+ ensure => "stopped",
+ hasstatus => true,
+ require => Package["isc-dhcp-server"];
+ }
+
+}
+
18 manifests/host.pp
@@ -0,0 +1,18 @@
+define dhcp::host (
+ $ip,
+ $mac,
+ $comment=''
+ ) {
+
+ $host = $name
+ include dhcp::params
+
+ $dhcp_dir = $dhcp::params::dhcp_dir
+
+ concat::fragment { "dhcp_host_${name}":
+ target => "${dhcp_dir}/dhcpd.hosts",
+ content => template("dhcp/dhcpd.host.erb"),
+ order => 10,
+ }
+}
+
95 manifests/init.pp
@@ -0,0 +1,95 @@
+class dhcp (
+ $dnsdomain,
+ $nameservers,
+ $ntpservers,
+ $interfaces = undef,
+ $interface = "NOTSET",
+ $dnsupdatekey = undef,
+ $pxeserver = undef,
+ $pxefilename = undef,
+ $logfacility = 'local7',
+ $dhcp_monitor = true
+) {
+
+ include dhcp::params
+
+ $dhcp_dir = $dhcp::params::dhcp_dir
+ $packagename = $dhcp::params::packagename
+ $servicename = $dhcp::params::servicename
+
+ # Incase people set interface instead of interfaces work around
+ # that. If they set both, use interfaces and the user is a unwise
+ # and deserves what they get.
+ if $interface != "NOTSET" and $interfaces == undef {
+ $dhcp_interfaces = [ $interface ]
+ } elsif $interface == "NOTSET" and $interfaces == undef {
+ fail ("You need to set \$interfaces in $module_name")
+ } else {
+ $dhcp_interfaces = $interfaces
+ }
+
+ package {
+ "$packagename":
+ ensure => installed,
+ provider => $operatingsystem ? {
+ default => undef,
+ darwin => macports
+ }
+ }
+
+ # Only debian and ubuntu have this style of defaults for startup.
+ case $operatingsystem {
+ 'debian','ubuntu': {
+ file{ '/etc/default/isc-dhcp-server':
+ ensure => present,
+ owner => 'root',
+ group => 'root',
+ mode => '0644',
+ before => Package[$packagename],
+ notify => Service[$servicename],
+ content => template('dhcp/debian/default_isc-dhcp-server'),
+ }
+ }
+ }
+
+# file { "${dhcp_dir}/dhcpd.conf":
+# owner => root,
+# group => 0,
+# mode => 644,
+# require => Package[$packagename],
+# }
+
+ include concat::setup
+
+ concat { "${dhcp_dir}/dhcpd.conf":
+
+ }
+ concat::fragment { 'dhcp-conf-main':
+ target => "${dhcp_dir}/dhcpd.conf",
+ content => template("dhcp/dhcpd.conf.erb"),
+ order => 01,
+ owner => root,
+ group => 0,
+ mode => 644,
+ require => Package[$packagename],
+ }
+
+ concat { "${dhcp_dir}/dhcpd.hosts": }
+ concat::fragment { 'dhcp-hosts-header':
+ target => "${dhcp_dir}/dhcpd.hosts",
+ content => "# static DHCP hosts\n",
+ order => 01,
+ }
+
+ service {
+ "$servicename":
+ enable => "true",
+ ensure => "running",
+ hasstatus => true,
+ subscribe => [Concat["${dhcp_dir}/dhcpd.hosts"], File["${dhcp_dir}/dhcpd.conf"]],
+ require => Package["$packagename"];
+ }
+
+ if $dhcp_monitor == true { include dhcp::monitor }
+
+}
6 manifests/monitor.pp
@@ -0,0 +1,6 @@
+# ------------
+# Monitoring
+# ------------
+class dhcp::monitor {
+ include munin::dhcp
+}
20 manifests/params.pp
@@ -0,0 +1,20 @@
+class dhcp::params {
+
+ $dhcp_dir = $operatingsystem ? {
+ debian => "/etc/dhcp",
+ ubuntu => "/etc/dhcp3",
+ darwin => "/opt/local/etc/dhcp",
+ default => "/etc",
+ }
+
+ $packagename = $operatingsystem ? {
+ darwin => "dhcp",
+ default => "isc-dhcp-server",
+ }
+
+ $servicename = $operatingsystem ? {
+ darwin => "org.macports.dhcpd",
+ default => "isc-dhcp-server",
+ }
+
+}
18 manifests/pool.pp
@@ -0,0 +1,18 @@
+define dhcp::pool (
+ $network,
+ $mask,
+ $range,
+ $gateway
+ ) {
+
+ include dhcp::params
+
+ $dhcp_dir = $dhcp::params::dhcp_dir
+
+ concat::fragment { "dhcp_pool_${name}":
+ target => "${dhcp_dir}/dhcpd.conf",
+ content => template("dhcp/dhcpd.pool.erb"),
+ order => 70,
+ }
+}
+
11 templates/debian/default_isc-dhcp-server
@@ -0,0 +1,11 @@
+# Defaults for dhcp initscript
+# sourced by /etc/init.d/dhcp
+# installed at /etc/default/isc-dhcp-server by the maintainer scripts
+
+#
+# This is a POSIX shell fragment
+#
+
+# On what interfaces should the DHCP server (dhcpd) serve DHCP requests?
+# Separate multiple interfaces with spaces, e.g. "eth0 eth1".
+INTERFACES="<%= dhcp_interfaces.join(' ') %>"
44 templates/dhcpd.conf.erb
@@ -0,0 +1,44 @@
+# dhcpd.conf
+omapi-port 7911;
+
+default-lease-time 600;
+max-lease-time 7200;
+
+<% if has_variable?( 'dnsupdatekey' ) and dnsupdatekey != :undef -%>
+ddns-updates on;
+ddns-update-style interim;
+update-static-leases on;
+use-host-decl-names on;
+
+# Key from bind
+include "<%= dnsupdatekey %>";
+<% dnsdomain.each do |dom| -%>
+zone <%= dom %>. {
+ primary <%= nameservers.first %>;
+ key <%= dnsupdatekey.split('/').last %>;
+}
+<% end -%>
+<% else %>
+ddns-update-style none;
+<% end -%>
+
+option domain-name "<%= dnsdomain.first %>";
+option domain-name-servers <%= nameservers.join( ', ') %>;
+option ntp-servers <%= ntpservers.join( ', ') %>;
+
+allow booting;
+allow bootp;
+
+option fqdn.no-client-update on; # set the "O" and "S" flag bits
+option fqdn.rcode2 255;
+option pxegrub code 150 = text ;
+
+<% if has_variable?( 'pxeserver' ) and has_variable?( 'pxefilename' ) then -%>
+# PXE Handoff.
+next-server <%= pxeserver %>;
+filename "<%= pxefilename %>";
+<% end -%>
+
+log-facility <%= logfacility %>;
+
+include "<%= dhcp_dir %>/dhcpd.hosts";
5 templates/dhcpd.host.erb
@@ -0,0 +1,5 @@
+host <%= host %> {
+ hardware ethernet <%= mac %>;
+ fixed-address <%= ip %>;
+ ddns-hostname "<%= name %>";
+}
13 templates/dhcpd.pool.erb
@@ -0,0 +1,13 @@
+#################################
+# <%= name network mask %>
+#################################
+subnet <%= network %> netmask <%= mask %> {
+ pool
+ {
+ range <%= range %>;
+ }
+
+ option subnet-mask <%= mask %>;
+ option routers <%= gateway %>;
+}
+
29 tests/init.pp
@@ -0,0 +1,29 @@
+$ddnskeyname = 'dhcp_updater'
+
+class { 'dhcp':
+ dnsdomain => [
+ 'example.com',
+ '1.1.10.in-addr.arpa',
+ ],
+ nameservers => ['10.1.1.10'],
+ ntpservers => ['us.pool.ntp.org'],
+ interfaces => ['eth0'],
+ dnsupdatekey => "/etc/bind/keys.d/$ddnskeyname",
+ require => Bind::Key[ $ddnskeyname ],
+ pxeserver => '10.1.1.5',
+ pxefilename => 'pxelinux.0',
+}
+
+dhcp::pool{ 'example.com':
+ network => '10.1.1.0',
+ mask => '255.255.255.0',
+ range => '10.1.1.100 10.1.1.200',
+ gateway => '10.1.1.1',
+}
+
+dhcp::host {
+ 'gateway':
+ mac => "00:11:22:33:44:55",
+ ip => "10.1.1.1",
+}
+

0 comments on commit 2f1bbd2

Please sign in to comment.
Something went wrong with that request. Please try again.