On Jul 11, 11:31 am, Arnaud Gomes-do-Vale <arnaud.go...@ircam.fr>
wrote:
> Hi folks,
>
> This is a "best practices" question as much as a technical one.
>
> I am trying to redefine variables when I include a specific class. The
> use case is installing php 5.3 on a few select centos 5 boxes while
> keeping the default php 5.1 install on others.


I think it would be more precise to say "I am trying to redefine
variables when a specific class *has been* included."  The distinction
is significant, because it narrows the possible approaches to only a
few bad ones.  Here's a good rule of thumb: never use Puppet's
"defined()" function in your manifests.  Ever.  It is brittle, and it
will cause you grief, increasing exponentially with the number of
uses.


> Here is the relevant part
> of my apache module:
>
> ---------------------------------------------------------------------
> class apache::php inherits apache::base {
>   include apache::params
>
>   package { 'php':
>     name => $apache::params::php_package,
>     ensure => installed,
>     require => Package['httpd'],
>     notify => Service['httpd'];
>   'php-gd':
>     name => $apache::params::php_gd_package,
>     ensure => installed,
>     require => Package['php'],
>     notify => Service['httpd'];
>   'php-imap':
>     name => $apache::params::php_imap_package,
>     ensure => installed,
>     require => Package['php'],
>     notify => Service['httpd'];
>   'php-ldap':
>     name => $apache::params::php_ldap_package,
>     ensure => installed,
>     require => Package['php'],
>     notify => Service['httpd'];
>   }
>
> }
>
> class apache::php53 inherits apache::php {
>   # $os is defined in site.pp.
>   if $::os == 'rhel5' {
>     # Dirty hack. Yuck.
>     exec { '/usr/bin/yum -y replace php --replace-with php53u':
>       onlyif => '/bin/rpm -q php',
>       # Class common::redhat::el::el5 installs yum-plugin-replace.
>       require => Class['common::redhat::el::el5'],
>       notify => Service['httpd'],
>     }
>   }
>   else {
>     warning 'Class apache::php53 should only ever be defined for RHEL5 and 
> its clones.'
>   }
>
> }
>
> class apache::params {
>   case $::os {
>     # .../...
>     'rhel5': {
>       $httpd_package = 'httpd'
>       $httpd_service = 'httpd'
>       $rootdir = '/var/www/html'
>       if defined(Class['apache::php53']) {
>         $php_package = 'php53u'
>         $php_gd_package = 'php53u-gd'
>         $php_imap_package = 'php53u-imap'
>         $php_ldap_package = 'php53u-ldap'
>       }
>       else {
>         $php_package = 'php'
>         $php_gd_package = 'php-gd'
>         $php_imap_package = 'php-imap'
>         $php_ldap_package = 'php-ldap'
>       }
>     }
>   }}
>
> ---------------------------------------------------------------------
>
> When I add the apache::php53 to my host (I use LDAP as a node classifier
> if that matters), php and its extensions get upgraded to 5.3. However,
> when I add a class that depends on apache::php53, the defined function
> evaluates as false.
>
> class wordpress {
>   # apache::lamp includes apache::php
>   include apache::lamp
>
>   if $::os == 'rhel5' {
>     include apache::php53
>   }
>   # .../...
>
> }
>
> I understand defined is quite unreliable. So I am wondering if there is
> a better option (read a working one)?
>
> Hardcoding the packages names in class apache::php53 is not enough as I
> have a bunch of other classes installing additional PHP modules. I
> suppose I could subclass them as well but things would probably get
> quite messy quite fast.


There is no need for subclassing or defined() here.  I would approach
the problem something like this:

---------------------------------------------------------------------
class apache::php {
  # Nothing from 'apache::base' is overridden, so it
  # should be included (if even that is needed) instead of
  # inherited from.
  include 'apache::base'
  include 'apache::params'

  # Resource defaults for Packages in this class
  Package {
    ensure => installed,
    require => Package['php'],
    notify => Service['httpd']
  }

  package { 'php':
    name => $apache::params::php_package,
    require => Package['httpd'];
  'php-gd':
    name => $apache::params::php_gd_package;
  'php-imap':
    name => $apache::params::php_imap_package;
  'php-ldap':
    name => $apache::params::php_ldap_package;
  }

  if $apache::params::php_package != 'php' {
    # Make sure the vanilla 'php' package is absent
    # before the alternative (e.g. 'php53') is installed
    package { 'php-alt':
      name => 'php',
      ensure => absent,
      require => undef,
      before => Package['php']
    }
  }
}


#
# ** No class apache::php53 **
#


#
# class apache::params unchanged, not shown
#

---------------------------------------------------------------------

Everybody that needs PHP just includes apache::php.  If Puppet gets
confused about having one package titled 'php' and a different one
named 'php', then just change the titles so there is no clash.

As my manifest shows, I'm not sure "yum replace" is really needed.  If
it is needed after all, then it shouldn't be too hard to adjust the
above to use it.


John

-- 
You received this message because you are subscribed to the Google Groups 
"Puppet Users" group.
To post to this group, send email to puppet-users@googlegroups.com.
To unsubscribe from this group, send email to 
puppet-users+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/puppet-users?hl=en.

Reply via email to