Pro Puppet (44 page)

Read Pro Puppet Online

Authors: Jeffrey McCune James Turnbull

BOOK: Pro Puppet
3.85Mb size Format: txt, pdf, ePub

Here, the operator uses
puppet apply -e
to evaluate a single statement from the command line. The operator makes sure the class is idempotent by running Puppet a second time with the same command. Finally, to make sure the service can be easily stopped, he changes the
ensure
parameter. Once the module has been tested on a Debian system, the operator makes sure the module can properly stop the service, as shown in
Listing 8-13
.

Listing 8-13.
Testing to ensure that the NTP service can be stopped

$ puppet apply --verbose -e 'class { ntp: ensure => stopped }'
info: Applying configuration version '1298492670'
notice: /Stage[main]/Ntp/Service[ntp]/ensure: ensure changed 'running' to 'stopped'
$ puppet apply --verbose -e 'class { ntp: ensure => stopped }'
info: Applying configuration version '1298492677'

These two commands leave the package installed and configured, but stop the service. The operator has verified that his new module works well on a Debian-based system. The last step before publishing his module to the Forge is to add Enterprise Linux support for the module. By using conditionals and variables for the package, file and service resources, he's able to easily modify the existing class to support Enterprise Linux.

Adding Enterprise Linux Support to the NTP Module

Once the NTP module has been tested on Debian-based systems, the operator needs to make sure the module also works well on Enterprise Linux systems. First, the operator exercises the logic preventing the module from running on unsupported operating systems. On an Enterprise Linux system, as shown in
Listing 8-14
, he installs the module as normal.

Listing 8-14.
Installing the NTP module on Enterprise Linux

$ facter operatingsystem
CentOS
$ puppet-module install ~/src/modules/operator-ntp/pkg/operator-ntp-0.0.1.tar.gz
Installed "operator-ntp-0.0.1" into directory: operator-ntp
$ ln -s operator-ntp ntp
$ cd ~

Once installed on the Enterprise Linux system, the operator uses the same commands he used on the Debian system to test the newly-developed NTP module, shown in
Listing 8-15
.

Listing 8-15.
Initial test of the NTP module on Enterprise Linux

# puppet apply --verbose -e 'class { ntp: ensure => running }'
info: Applying configuration version '1298493317'
notice: The ntp module is not supported on CentOS
notice: /Stage[main]/Ntp/Notify[ntp_unsupported]/message: defined 'message' as 'The ntp module is not supported on CentOS'

The operator expects to receive this message because Enterprise Linux support has not yet been developed. Let's see how he modifies the module to support both Debian and Enterprise Linux systems. In
Listing 8-16
, he installs the NTP package to obtain a template of the NTP configuration file and copies it into the templates directory of the module. Then, he checks the service name to see if it matches the Debian service name or not.

Listing 8-16.
Obtaining the NTP service name and configuration for Enterprise Linux

$ yum install ntp

Installed:
  ntp.x86_64 0:4.2.2p1-9.el5.centos.2.1
$ cd ~/src/modules/operator-ntp/
$ cp /etc/ntp.conf ./templates/ntp.conf.el.erb
$ git add ./templates/ntp.conf.el.erb
$ chkconfig --list | grep ntp
ntpd            0:off   1:off   2:off   3:off   4:off   5:off   6:off

The operator notices the name of the service on Enterprise Linux-based systems is “ntpd,” which differs slightly from the “ntp” on Debian-based systems. Luckily, Puppet handles these minor differences with ease. Let's see how, in
Listing 8-17
, he modifies the NTP class to handle this difference and manage the NTP service.

Listing 8-17.
Extending the NTP class to support Enterprise Linux and Debian

$ git diff
diff --git a/Modulefile b/Modulefile
index 180cb31..cd60026 100644
--- a/Modulefile
+++ b/Modulefile
@@ -1,10 +1,10 @@
 name    'operator-ntp'
-version '0.0.1'
+version '0.0.2'
source 'UNKNOWN'
 author 'Example.com Operator'
 license 'UNKNOWN'
-summary 'UNKNOWN'
-description 'UNKNOWN'
+summary 'NTP Module'
+description 'NTP Module for Debian, Ubuntu, CentOS, RHEL, OEL'
 project_page 'UNKNOWN'
 ## Add dependencies, if any:
diff --git a/manifests/init.pp b/manifests/init.pp
index 622b216..ee655f7 100644
--- a/manifests/init.pp
+++ b/manifests/init.pp
@@ -30,10 +30,7 @@
 #   }
 #
 # [Remember: No empty lines between comments and class definition]
-class ntp($servers=[ "0.debian.pool.ntp.org iburst",
-                     "1.debian.pool.ntp.org iburst",
-                     "2.debian.pool.ntp.org iburst",
-                     "3.debian.pool.ntp.org iburst",],
+class ntp($servers="UNSET",
           $ensure="running",
           $autoupdate=false
 ) {
@@ -57,6 +54,28 @@ class ntp($servers=[ "0.debian.pool.ntp.org iburst",
       $svc_name   = "ntp"
       $config     = "/etc/ntp.conf"
       $config_tpl = "ntp.conf.debian.erb"
+      if ($servers == "UNSET") {
+        $servers_real = [ "0.debian.pool.ntp.org iburst",
+                          "1.debian.pool.ntp.org iburst",
+                          "2.debian.pool.ntp.org iburst",
+                          "3.debian.pool.ntp.org iburst", ]
+      } else {
+        $servers_real = $servers
+      }
+    }
+    centos, redhat, oel: {
+      $supported  = true
+      $pkg_name   = [ "ntp" ]
+      $svc_name   = "ntpd"
+      $config     = "/etc/ntp.conf"
+      $config_tpl = "ntp.conf.el.erb"
+      if ($servers == "UNSET") {
+        $servers_real = [ "0.centos.pool.ntp.org",
+                          "1.centos.pool.ntp.org",
+                          "2.centos.pool.ntp.org", ]
+      } else {
+        $servers_real = $servers
+      }
     }
     default: {
       $supported = false
diff --git a/templates/ntp.conf.debian.erb b/templates/ntp.conf.debian.erb
index e4275de..f51414f 100644
--- a/templates/ntp.conf.debian.erb
+++ b/templates/ntp.conf.debian.erb
@@ -20,7 +20,7 @@ filegen clockstats file clockstats type day enable
 # pool:
 # Managed by puppet class { "ntp": servers => [ ... ] }
-<% servers.each do |server| -%>
+<% servers_real.each do |server| -%>
 server <%= server %>
 <% end -%>
diff --git a/templates/ntp.conf.el.erb b/templates/ntp.conf.el.erb
index cfb4c8c..db2aa9c 100644
--- a/templates/ntp.conf.el.erb
+++ b/templates/ntp.conf.el.erb
@@ -14,9 +14,11 @@ restrict -6 ::1
# Use public servers from the pool.ntp.org project.
# Please consider joining the pool (http://www.pool.ntp.org/join.html).
-server 0.centos.pool.ntp.org
-server 1.centos.pool.ntp.org
-server 2.centos.pool.ntp.org
+
+# Managed by puppet class { "ntp": servers => [ ... ] }
+<% servers_real.each do |server| -%>
+server <%= server %>
+<% end -%>
 #broadcast 192.168.1.255 key 42                # broadcast server
 #broadcastclient                       # broadcast client

The operator has made a number of small edits to the NTP module, as shown in the difference between the Debian-only module and the newly-added Enterprise Linux support. These changes justify a new build of the module with a new version number. Working through the difference, let's review the changes made:

  • Increment the version in the
    Modulefile
    . This allows overwriting the already-installed module.
  • Add a description and summary for the
    Modulefile
    . These will show up on the Forge when published.
  • Add a case selection for the CentOS, RedHat, and OEL operating systems.
  • Add a conditional to set different default upstream servers from the Debian or CentOS pool if the user does not specify their own list of servers.

These changes modify the variables used by the Package, File and Service resources declared in the bottom section of the NTP class. Once these changes are made, the operator builds a new version of the package. He then installs the package on both the Enterprise Linux and Debian systems, using the commands shown in
Listing 8-18
.

Listing 8-18.
Building and installing version 0.0.2 of the NTP module

$ cd ~/src/modules/operator-ntp
$ puppet-module build
=======================================================
Building ~/src/modules/operator-ntp for release
-------------------------------------------------------
Done. Built: pkg/operator-ntp-0.0.2.tar.gz
$ cd /etc/puppet/modules
$ puppet-module install ~/src/modules/operator-ntp/pkg/operator-ntp-0.0.2.tar.gz
======================================
Existing module 'operator-ntp' found
--------------------------------------
Overwrite module installed at ./operator-ntp? [y/N]: y
Installed "operator-ntp-0.0.2" into directory: operator-ntp
Releasing the NTP Module to the Forge

After installing version 0.0.2 of the NTP module on both the Enterprise Linux and Debian systems, a final test shown in
Listing 8-19
verifies that the module is ready for publication.

Listing 8-19.
Final test of NTP module on Debian and Enterprise Linux

debian # puppet apply --verbose -e 'class { ntp: ensure => running, autoupdate => true }'
info: Applying configuration version '1298498306'
notice: /Stage[main]/Ntp/Package[ntp]/ensure: ensure changed 'purged' to 'latest'
info: /Stage[main]/Ntp/Package[ntp]: Scheduling refresh of Service[ntp]
info: FileBucket got a duplicate file /etc/ntp.conf ({md5}3e250ecaf470e1d3a2b68edd5de46bfd)
info: /Stage[main]/Ntp/File[/etc/ntp.conf]: Filebucketed /etc/ntp.conf to puppet with sum
 3e250ecaf470e1d3a2b68edd5de46bfd
notice: /Stage[main]/Ntp/File[/etc/ntp.conf]/content: content changed
 '{md5}3e250ecaf470e1d3a2b68edd5de46bfd' to '{md5}6e3461437c627101cf53e634abc62400'
info: /Stage[main]/Ntp/File[/etc/ntp.conf]: Scheduling refresh of Service[ntp]
notice: /Stage[main]/Ntp/Service[ntp]: Triggered 'refresh' from 2 events
debian # puppet apply --verbose -e 'class { ntp: ensure => running, autoupdate => true }'
info: Applying configuration version '1298498352'
centos # puppet apply --verbose -e 'class { ntp: ensure => running, autoupdate => true }'
info: Applying configuration version '1298499949'
notice: /Stage[main]/Ntp/Package[ntp]/ensure: created
info: /Stage[main]/Ntp/Package[ntp]: Scheduling refresh of Service[ntp]
info: FileBucket got a duplicate file /etc/ntp.conf ({md5}5baec8bdbf90f877a05f88ba99e63685)
info: /Stage[main]/Ntp/File[/etc/ntp.conf]: Filebucketed /etc/ntp.conf to puppet with sum
 5baec8bdbf90f877a05f88ba99e63685
notice: /Stage[main]/Ntp/File[/etc/ntp.conf]/content: content changed
 '{md5}5baec8bdbf90f877a05f88ba99e63685' to '{md5}35ea00fd40740faf3fd6d1708db6ad65'
info: /Stage[main]/Ntp/File[/etc/ntp.conf]: Scheduling refresh of Service[ntp]
notice: /Stage[main]/Ntp/Service[ntp]/ensure: ensure changed 'stopped' to 'running'
notice: /Stage[main]/Ntp/Service[ntp]: Triggered 'refresh' from 2 events

Other books

The Inquest by Stephen Dando-Collins
Discovering You by Brenda Novak
In Jeopardy by McClenaghan, Lynette
The Serial Killer Files by Harold Schechter
A Sword Upon The Rose by Brenda Joyce
The Guardian Herd: Stormbound by Jennifer Lynn Alvarez
Demon Thief by Darren Shan
Riddle Gully Runaway by Banyard, Jen