NAME
Config::Model::AutoRead - Load configuration node on demand
SYNOPSIS
# top level config file name matches instance name
$model->create_config_class
(
config_class_name => 'OneAutoReadConfigClass',
read_config => [ { backend => 'cds_file' , config_dir => '/etc/cfg_dir'},
{ backend => 'custom' , # dir hardcoded in custom class
class => 'ProcessRead'
}
],
# if omitted, write_config will be written using read_config specifications
# write_config can be array of hash ref to write several syntaxes
write_config => { backend => 'cds_file', config_dir => '/etc/cfg_dir' } ,
element => ...
) ;
# config data will be written in /etc/my_config_dir/foo.cds
# according to the instance name
my $instance = $model->instance(instance_name => 'foo') ;
# use with Augeas
$model->create_config_class
(
config_class_name => 'OpenSsh::Sshd',
# try Augeas and fall-back with custom method
read_config => [ { backend => 'augeas' ,
config_file => '/etc/ssh/sshd_config',
# declare "seq" Augeas elements
lens_with_seq => [/AcceptEnv AllowGroups [etc]/],
},
{ backend => 'custom' , # dir hardcoded in custom class
class => 'Config::Model::Sshd'
}
],
# write_config will be written using read_config specifications
element => ...
) ;
DESCRIPTION
This class provides a way to specify how to load or store configuration data within the model (instead of writing dedicated perl code).
With these specifications, all the configuration information are read during creation of a node.
This load/store can be done with:
Config dump string (cds) in a file. I.e. a string that describes the content of a configuration tree is loaded from or saved in a text file. See Config::Model::Dumper.
Ini files (written with Config::Tiny. See limitations in "Limitations depending on storage".
Perl data structure (perl) in a file. See Config::Model::DumpAsData for details on the data structure.
XML. Not yet implemented (ask the author if you're interested)
Any format when the user provides a dedicated class and function to read and load the configuration tree.
Data can be loaded or stored using RedHat's Augeas library.
After loading the data, the object registers itself to the instance. Then the user can call the write_back
method on the instance (See Config::Model::Instance) to store all configuration informations back.
Built-in read write format
Currently, this class supports the following built-in formats:
- cds_file
-
Config dump string. See Config::Model::Dumper.
- ini_file
-
Ini files written by Config::Tiny.
- augeas
-
Use Augeas library
Custom backend
Custom backend must be specified with a class name that will features the methods used to write and read the configuration files:
read_config => [ { backend => 'custom' , class => 'MyRead' } ]
The MyRead
class that you will provide must have the methods read
and write
. Then, MyRead::read
will be called with there parameters:
(object => config_tree_root, root => 'filesystem root' ,
config_dir => 'config dir', )
You can choose to specify yourself the read and write methods:
read_config => { backend => 'custom',
class => 'MyRead',
function => 'my_read'
}
and
write_config => { backend => 'custom',
class => 'MyRead',
function => 'my_write'
}
Limitations depending on storage
Some storage system will limit the structure of the model you can map to the file.
Ini files limitation
Structure of the Config::Model must be very simple. Either:
A single class with hash of leaves elements.
2 levels of classes. The top level has nodes elements. All other classes have only leaf elements.
Augeas backend limitation
The structure and element names of the Config::Model tree must match the structure defined in Augeas lenses.
Sometimes, the structure of a file loaded by Augeas starts directly with a list of items. For instance /etc/hosts
structure starts with a list of lines that specify hosts and IP adresses. The set_in
parameter specifies an element name in Config::Model root class that will hold the configuration data retrieved by Augeas.
Configuration class with auto read or auto write
read and write specification
A configuration class will be declared with optional read
or write
parameters:
read_config => [ { backend => 'cds_file', config_dir => '/etc/my_cfg/' } ,
{ backend => 'custom', class => 'Bar' },
],
write_config => { backend => 'cds_file', config_dir => '/etc/my_cfg/' },
The read backends will be tried in the specified order:
First the cds file whose name depend on the parameters used in model creation and instance creation:
<model_config_dir>/<instance_name>.cds
The syntax of thecds
file is described in Config::Model::Dumper.A call to
Bar::read
with these parameters:(object => config_tree_root, root => 'filesystem root', config_dir => '...')
When a read operation is successful, the remaining read methods will be skipped.
When necessary (or required by the user), all configuration informations are written back using all the write method passed.
In the example above, only a cds
file is written. But, both custom format and cds
file are tried for read. This is also an example of a graceful migration from a customized format to a cds
format.
You can choose also to read and write only customized files:
read_config => { backend => 'custom', class => 'Bar'},
Or to read and write only cds files :
read_config => { backend => 'cds_file'} ,
You can also specify more paratmeters that must be passed to your custom class:
read_config => { backend => 'custom', class => 'Bar', config_dir => '/etc/foo'},
To migrate from an old format to a new format:
read_config => [ { backend => 'custom', class => 'OldFormat', function => 'old_read'} ,
{ backend => 'custom', class => 'NewFormat', function => 'new_read'}
],
write_config => [ { backend => 'custom', class => 'NewFormat' } ],
read write directory
By default, configurations files are read from the directory specified by config_dir
parameter specified in the model. You may override the root
directory for test.
Read and write with Augeas library
You can use Config::Augeas to read and write data back. This way, the structure and commments of the original configuration file will preserved.
To use Augeas as a backend, you must specify the following read_config
parameters:
- backend
-
Use
augeas
in this case. - save
-
Either
backup
ornewfile
. See "Constructor" in Config::Augeas for details. - config_file
-
Name of the config_file.
- lens_with_seq
-
This one is tricky. When an Augeas lens use the
seq
keywords in a lens, a special type of list element is created (See http://augeas.net/docs/lenses.html for details on lenses). This special list element must be declared so that Config::Model can use the correct Augeas call to write this list values.lens_with_seq
must be passed a list ref of all lens names that contains aseq
statement.
For instance:
read_config => [ { backend => 'augeas' ,
save => 'backup',
config_file => '/etc/ssh/sshd_config',
# declare "seq" Augeas elements
lens_with_seq => [/AcceptEnv AllowGroups/],
},
],
AUTHOR
Dominique Dumont, (ddumont at cpan dot org)
SEE ALSO
Config::Model, Config::Model::Instance, Config::Model::Node, Config::Model::Dumper, Config::Augeas