Cfengine 3: copying config files for services
28 Jan 2011At $work I'm migrating slowly to Cfengine 3. One of the attractions is the ability to do what this page shows: loop over lists in a Cf-ish kind of way.
Here's the first bundle. (It's pretty much stolen from that page, but customized for my environment.) It tells you some basic details about the config file, the process name and the restart command for different daemons:
bundle common services {
vars:
redhat|centos::
"cfg_file_prefix" string => "centos/5";
"cfg_file[ssh]" string => "/etc/ssh/sshd_config";
"daemon[ssh]" string => "sshd";
"start[ssh]" string => "/sbin/service sshd restart";
"enable[ssh]" string => "/sbin/chkconfig sshd on";
"cfg_file[iptables]" string => "/etc/sysconfig/iptables";
"start[iptables]" string => "/sbin/service iptables restart";
"enable[iptables]" string => "/sbin/chkconfig iptables on";
}
Here's the bundle that copies config files and restarts the daemon if necessary:
bundle agent fix_service(service) {
files:
"$(services.cfg_file[$(service)])"
copy_from => secure_cp("$(g.masterfiles)/$(services.cfg_file_prefix)/$(services.cfg_file[$(service)])", "$(g.masterserver)"),
perms => mog("0600","root","root"),
classes => if_repaired("$(service)_restart"),
comment => "Copy a stock configuration file template from repository";
processes:
"$(services.daemon[$(service)])"
comment => "Check that the server process is running, and start if necessary",
restart_class => canonify("$(service)_restart");
commands:
"$(services.start[$(service)])"
comment => "Method for starting this service",
ifvarclass => canonify("$(service)_restart");
"$(services.enable[$(service)])"
comment => "Method for enabling this service",
ifvarclass => canonify("$(service)_restart");
}
And here's the loop that puts it all together:
bundle agent redhat {
vars:
"service" slist => { "ssh", "iptables" };
methods:
"any" usebundle => fix_service("$(service)"),
comment => "Make sure the basic application services are running";
}
I ran into a problem with this, though: it would always, without
fail, restart iptables even though no config file had been copied.
The problem was with the process check: there's no process to check
for with iptables. And from what I can tell, when the processes
stanza was asked to check for a non-existent variable, it checked for
the literal string $(services.daemon[$(service)])
-- that is,
dollar-bracket-s-e-r-v-.... Since there was no such thing, it decided
it needed restarting.
The way around this was to add this variable to the services bundle (the one that has all the info about the daemons):
"daemon[iptables]" string => "cf_null";
I also had to modify the processes stanza:
processes:
$(services.daemon[$(service)])"
comment => "Check that the server process is running, and start if necessary",
restart_class => canonify("$(service)_restart"),
ifvarclass => canonify("$(services.daemon[$(service)])");
That ifvarclass
check on the last line says to run iff there is a
value for daemon. cf_null
is a NULL value special to cfengine.
Since the check fails for iptables, the process check isn't run and
we only restart if we copy over a new config file.
Add a comment:
Name and email required; email is not displayed.
Related Posts
QRP weekend 08 Oct 2018
Open Source Cubesat Workshop 2018 03 Oct 2018
mpd crash? try removing files in /var/lib/mpd/ 11 Aug 2018