Executable Configuration Files

From Cfwiki

Revision as of 01:46, 28 August 2009 by SiliconSlick (Talk | contribs)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search

Like other interpreters, cfengine can be used on the first line of a text file as an interpreter, just like Perl or ksh in an executable script.

This allows you to run your script using just the filename, instead of running cfagent and manually passing parameters.

Here is an example:

#!/var/cfengine/bin/cfagent -qvKf

######################################################################
# SampleExecutableConfig.cf

control:

   any::

      timezone = ( CDT )

      actionsequence = ( checktimezone )

With the executable bit set for the file's permissions, it can be executed straight from the command line.

root@localhost:/var/cfengine/inputs>  ./SampleExecutableConfig.cf

The line that makes this possible is the "shebang line", the very first line in the script:

#!/var/cfengine/bin/cfagent -qvKf

Another useful option is to define classes on the shebang line.

I've used this for scripts that would normally require another class to trigger its functionality (ie: a one time script, or depends on other scripts), and instead added the class to the shebang line. The logic says that if I want to run the script manually by itself, the obviously I want it to perform its function then and there.

Here is an example shebang line with an installed class:

#!/var/cfengine/bin/cfagent -D MyDefinedClass -qvKf

I also ensure that every configuration file I write is self contained. Basically, if it relies on information that would be imported from another file when cfagent runs, that it ought to import that file itself.

When using import with class locking to prevent circular imports like in Importing Examples, this works very well.

--RussellAdams 2004-12-01