Environment Initialisation
Since version 1.13.0 the Perl version of the ngeneric framework (
LCFG::Component
) provides an environment initialisation system for component methods with support for plugins which mean it is fully extensible. There is an
InitializeEnvironment
method which is called for most standard methods which are accessible via
om
(including
configure
,
start
,
restart
,
stop
,
run
, and
logrotate
). The method can also be called from any additional methods you have added to your own components, the method needs access to the resources so it must be called after a call to
LoadProfile
or
LoadStatus
.
Plugins
Currently all that the
InitializeEnvironment
method does is call the
CallPlugins
method which loads any plugins listed in the
ng_plugins
resource for the component, these plugins are implemented as Perl modules in the
LCFG::Component::Plugin
namespace.
Standard Plugin Modules
There are currently two standard environment initialisation plugin modules available, see the perl documentation for each for full details. They are:
- Env
- The
LCFG::Component::Plugin::Env
plugin can be used to set values for environment variables before methods are called.
- Kinit
- The
LCFG::Component::Plugin::Kinit
plugin can be used to acquire Kerberos tickets in the style of the kinit (1)
command. It can also acquire associated AFS tokens in the style of aklog (1)
.
Resources
- ng_plugins
- This is a list of ngeneric plugins which should be run as part of the environment initialisation process for a method call. The tags must be the short names of modules in the
LCFG::Component::Plugin
namespace (e.g. env
for LCFG::Component::Plugin::Env
).
- ng_plugin_module_<plugin>
- This resource is used to specify the short name of the environment initialization module which should be used (e.g.
env
). This will be in the LCFG::Component::Plugin
namespace, the value is case-insensitive. You only need to specify this when you want a module with a different name from the plugin tag name (e.g. the tag is foo
but you need a module named env
). This can be useful if you want to configure the plugin differently for different component methods. You could, for example, have plugin tags named env_run
and env_conf
which both use the env
module, the first which is only used for the run
method and the second which is only used with the configure
method.
- ng_plugin_methods_<plugin>
- This resource is used to control the methods for which the plugin will be loaded. By default this resource is empty and the plugin will be loaded for all methods. If this resource contains a set of method names then they will be considered as a whitelist and the plugin will only be loaded for those methods, the plugin will not be loaded for any unnamed method. Alternatively you can blacklist specific methods by having a list of method names each prefixed with a minus sign (i.e.
-
). For example, if the value of the resource is -configure
then the plugin will be loaded for ALL supported methods EXCEPT configure. Alternatively, for example, if the value of the resource is configure
(note lack of minus prefix) then the plugin will ONLY be loaded for the configure method.
- ng_plugin_params_<plugin>
- This is a list of parameters which will be passed in when the plugin object is created. Each parameter can have an associated value, if none is specified then the value
1
is used.
- ng_plugin_param_<plugin>_<param>
- A value for the parameter for the particular plugin.
Examples
The following loads the
LCFG::Component::Plugin::Env
plugin which sets two environment variables (
HOME
and
TMPDIR
) for each method call.
!foo.ng_plugins mSET(env)
!foo.ng_plugin_params_env mSET(HOME TMPDIR)
!foo.ng_plugin_param_env_HOME mSET(/disk/scratch/foo)
!foo.ng_plugin_param_env_TMPDIR mSET(/disk/scratch/foo/tmp)
The following loads the
LCFG::Component::Plugin::Kinit
plugin with 3 parameters: a keytab, a principal and the aklog option enabled. This will acquire a Kerberos ticket and AFS token whenever any supported component method is called (e.g.
configure
).
!rkhunter.ng_plugins mSET(kinit)
!rkhunter.ng_plugin_params_kinit mSET(keytab principal aklog)
!rkhunter.ng_plugin_param_kinit_keytab mSET(<%kerberos.file_krb5keytab%>)
!rkhunter.ng_plugin_param_kinit_principal mSET(host/<%profile.node%>.<%profile.domain%>)
!rkhunter.ng_plugin_param_kinit_aklog mSET(yes)
Writing your own plugin
If you want to write your own plugin the best starting point is to look at the existing modules (e.g.
Env
and
Kinit
) for ideas. You need to sub-class the
LCFG::Component::Plugin
module and provide a
run
method which does the necessary work. For example:
package LCFG::Component::Plugin::Foo;
use parent qw(LCFG::Component::Plugin);
sub run {
my ($self) = @_;
my $comp = $self->component;
for my $key ($self->params_list) {
my $value = $self->param($key);
if ( $comp->{_DEBUG} ) {
$comp->Debug("Setting env var '$key' to '$value'");
}
$ENV{$key} = $value;
}
return;
}
Sub-Classing
If you need to do something much more complicated with the environment before calling methods in your component, it is possible to override and extend the
InitializeEnvironment
method in your own component code. It works something like this:
sub InitializeEnvironment {
my ($self) = @_;
# do your super complicated stuff
return $self->SUPER::InitializeEnvironment();
}
Note that you need to call the parent method at some point for any standard behaviour and plugins you require.
-- Main.squinney - 2014-12-15