FaiReplace
From FAIWiki
Contents |
Abstract
This script is an addition to FAI. It makes templating easier and allows central configuration management.
To get a hold of configuration values which are not centralized in the FAI installation this script helps. The script allows changing configuration values for the installation clients and server like the used LDAP server, Mail server, syslog server and whatever you want centrally in just a few class based configuration files. This makes it easier to migrate one FAI configuration space from one environment to another and avoids errors.
Overview
What this script actually does is to replace strings in files in the FAI configuration space. If it is correctly hooked into the various installation stages of FAI is possible to use this string replacement in FAI scripts, hooks and files. Additionally it is also possible to use the templating script on the FAI server it self.
Quickstart:
1. Put fai-replace in the $FAI/bin directory 2. Create the hooks specified using (fai-replace -a -m [files|scripts|hooks]) 3. Create *.rpl files in the $FAI/config directory which have classnames ($FAI/config/FAIBASE.rpl) 4. Use special strings in like [$ FAIBASE.version %] in your scripts, hooks, files
Example
Exemplary usage of the fai-replace script.
Put this script in $FAI/bin so you get $FAI/bin/fai-replace Create CLASSNAME.rpl in $FAI/config/ Add the installation client to this class.
Setup perl hashes in the .rpl file (the hase-name must be the same as CLASSNAME):
$FAI/config/FAIBASE.rpl:
$FAIBASE{"systemname"} = "utopiaplanitita";
$FAIBASE{"comment"} = "A comment";
$FAI/config/CLASSNAME.rpl:
# special hash for importing variables from other classes
$CLASSNAME{"requires"} = ("FAIBASE");
# special hash array of filenames for replacment
# usually is not necessary because they can be found automatically using the automode
$CLASSNAME{"files"} = ("/etc/motd", "/etc/smb/samba.conf");
$CLASSNAME{"version"} = "1.1";
$CLASSNAME{"sysname_ver"} = $FAIBASE{"systemname"}.$CLASSNAME{"version"};
Use the expression [% CLASSNAME.version %] in any file in the following subdirectories of the FAI config space: /files /hooks /scripts , e.g.:
$FAI/files/etc/motd/CLASSNAME:
Welcome to [% systemname %] version [% CLASSNAME.version %] A little comment from FAIBAS [% FAIBASE.comment %]
If the CLASSNAME of template file can be derived from its path you can omit CLASSNAME an just write [% version %]
Add a hook appropriate for each stage of string replacement:
hooks/action.FAIBASE:
$FAI/bin/fai-replace -m scripts -a $FAI/bin/fai-replace -m hooks -a
hooks/savelog.FAIBASE:
$FAI/bin/fai-replace -m files -a
The fai hooks must be in the right stage of the installation, scripts and hook templates need to be replaced before they are executed. File templates need to be replaced after they were transfered to the install client
Automode
The automode (option -a) in fai-replace means that the script will find all the files that belong to a class in the specified processing mode. Automode is inactive by default. fai-replace -m scripts -a will go through all defined Fai classes for the installation client and find all scripts belonging to every class and replace all found strings in the scripts which have the form [% CLASSNAME.attributename %]. If -a is not given as option it is necessary to define the files which should be considered for replacement by hand using the special hash "files". So you need something like this in your $FAI/config/CLASSNAME.rpl:
$CLASSNAME{"files"} = ("/etc/motd", "/etc/samba/smb.conf"); # the RHS is an perl array.
fai-replace on the FAI server
fai-replace supports a server mode where the same script can be run to template files on the FAI server. I use this to update my pammount.conf.xml file to make the users mount network shares according to the groups they belong to in the LDAP. The clients the rsync this pammount.conf.xml file regulary. To use this feature it is necassary to specify the server- command line options:
Example:
/srv/fai/bin/fai-replace -m server --server-config-directory=/srv/fai/config/config \ --server-target-path=/home/getconfig/etc --server-post-scripts-path=/srv/fai/bin
Server config directory is the directory with the *rpl files you use in FAI. If you use the nfs root you have to point it the, if you use SVN make a checkout before each run of fai-replace
Server target-path is the path where the templates are. Templates have the same name as the real filename put are hidden and have the tmpl extension, example:
/home/getconfig/etc/security/.pammount.conf.xml.tmpl
Optionally you can run so-called post scripts on the template files. These are scripts that are run after the template replacing of fai-replace was done, they should accept a filename as input and leave a modified file with the same filename as given, example:
/srv/fai/sbin/pammount.conf.xml.post
For my example this script will get the LDAP data from the LDAP server and insert mount directives for pammount according to the users group memberships.
The idea of this server mode is the possibitiy to have central configuration accross server and clients.
Usage
Usage: fai-replace [OPTION] ... -m [server|hooks|scripts|files] ... -m [server|hooks|scripts|files] Processing mode --processing-mode=[server|...] Same as above -- Optional Paramaeters -c class[.class] Define classes. -t target_dir Copy files relativ to target_dir. -s path,--fai-config-sapce path FAI config space -a,--automode Automode -l logfile, --logfile=logfile Logfile --max-template-size Maximum file size to be considerated for replacing --exclude-extensions=gif,jpg[,etc] Exclude files of this type from replacing -v,--verbose Create verbose output. -D,--debug Create debug output. -h,--help Print this help -- SERVER MODE OPTIONS -- --server-config-directory Directory where the *.rpl files are --server-target-path Directory where the templates are --server-post-scripts-path Directory where the post processing scripts are
Debugging and logging
Debugging is turn on by default. The script will log into the same directory as the normal FAI installation procedure. The log files will be called according to the mode the script is run in, e.g.: $LOGDIR/replace-scripts.rpl. If you want to turn debugging off, you need to edit the $debug variable in the script accordingly.
Perldoc
Get a man page with the command:
perldoc fai-replace
