Class name as a variable

939 views
Skip to first unread message

Mark Drayton

unread,
Jun 12, 2008, 8:22:07 AM6/12/08
to puppet...@googlegroups.com
Hi!

We run a number of different types of Apache webserver (1.3 with
mod_perl, 2.0/worker, 2.0/prefork/PHP, stock RedHat, etc.) which I'd
like to manage with Puppet. I've been developing an Apache module with
a top-level class which represents the bare bones of all Apache
installs:

class apache {
package { "httpd": ensure => installed }

service { "httpd":
ensure => running,
enable => true,
require => Package["httpd"]
}

file {
"/etc/httpd/conf/httpd.conf":
ensure => present,
source => "puppet://$servername/apache/httpd.conf",
notify => Service["httpd"],
require => Package["httpd"];
"/etc/httpd/conf.d":
ensure => present,
recurse => true,
source => "puppet://$servername/apache/conf.d",
notify => Service["httpd"],
require => Package["httpd"];
}

define module {
$package = "mod_$name"
$config = "$name.conf"

package { $package:
ensure => installed,
require => Package["httpd"],
before => $before
}
}

This is class is inherited by other, more specific classes:

class apache::static_farm inherits apache {
Package["httpd"] {
ensure => "2.0.63-1.nol"
}

apache::module { [ "ipinfo", "zeus"]:
before => Service["httpd"]
}

line { "set_httpd_worker": # 'line' from DavidS
line => "HTTPD=/usr/sbin/httpd.worker",
file => "/etc/sysconfig/httpd";
}

file {
"/etc/httpd/conf/maps":
ensure => present,
recurse => true,
source => "puppet://$servername/apache/maps/",
notify => Exec["httpd_rebuild_maps"],
require => Package["httpd"];
}
}

I'm aiming to get to the point where I can "include apache" for a
bog-standard, install-the-rpm-and-run-the-service Apache box or
"include apache::specific" to deploy a particular type of Apache
server. One of the issues with this setup is that I'd like to be able
to deploy different resources (say, httpd.conf) depending on the
Apache class without needing to painstakingly specify a different
resource in each class. I don't want:

class apache {
file { "/etc/httpd/conf/httpd.conf":
source => "puppet://$servername/apache/httpd.conf-stock"
}
}

class apache::static_farm inherits apache {
file { "/etc/httpd/conf/httpd.conf":
source => "puppet://$servername/apache/httpd.conf-static_farm"
}
}

Is there a variable that contains the most-specific class name? That
is, could I do something like:

class apache {
file { "/etc/httpd/conf/httpd.conf":
source => [
"puppet://$servername/apache/httpd.conf-$classname",
"puppet://$servername/apache/httpd.conf-stock"
]
}
}

to get a class-specific file if one exists, or a standard one if not?
I don't want to define a $role variable or similar in the node
definitions; to have to specify $apache_role = "static_farm" as well
as "include apache::static_farm" seems redundant and a potential
source of problems later on.

Lastly, any other suggestions on my approach? Comments or criticisms
are welcome.

Thanks!

Mark Drayton

Luke Kanies

unread,
Jun 12, 2008, 3:35:02 PM6/12/08
to puppet...@googlegroups.com
On Jun 12, 2008, at 7:22 AM, Mark Drayton wrote:

> Is there a variable that contains the most-specific class name? That
> is, could I do something like:
>
> class apache {
> file { "/etc/httpd/conf/httpd.conf":
> source => [
> "puppet://$servername/apache/httpd.conf-$classname",
> "puppet://$servername/apache/httpd.conf-stock"
> ]
> }
> }
>
> to get a class-specific file if one exists, or a standard one if not?
> I don't want to define a $role variable or similar in the node
> definitions; to have to specify $apache_role = "static_farm" as well
> as "include apache::static_farm" seems redundant and a potential
> source of problems later on.

There's no such variable at the moment, but I've filed it as an
enhancement request:

http://reductivelabs.com/redmine/issues/show/1104

It shouldn't really be that difficult to implement, I just haven't
added it to my ever-growing ticket list.

However, I don't think that would do what you want anyway, because
$name would get dereferenced when the base class was evaluated, when
you would really want it to get dereferenced when the subclass was
defined.

>
> Lastly, any other suggestions on my approach? Comments or criticisms
> are welcome.


I'll leave it to others on that; what you've got seems about as good
as I can think of at the moment, but I bet there are other clever ways
to do it.

--
Chase after truth like hell and you'll free yourself, even though
you never touch its coat-tails. -- Clarence Darrow
---------------------------------------------------------------------
Luke Kanies | http://reductivelabs.com | http://madstop.com

Reply all
Reply to author
Forward
0 new messages