The Perl Toolchain Summit needs more sponsors. If your company depends on Perl, please support this very important event.

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' ,
                       allow_empty => 1,     # optional
                     }
                   ],
   # 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') ;

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. See Config::Model::Backend::Augeas for details.

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.

Configuration class with auto read or auto write

read and write specification

A configuration class will be declared with optional read_config or write_config 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 the cds 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. By default, an exception is thrown if no read was successfull. This behavior can be overridden by specifying allow_empty => 1 in one of the backend specification. For instance:

    read_config  => [ { backend => 'cds_file', config_dir => '/etc/my_cfg/' } , 
                    { backend => 'custom', class => 'Bar' ,
                      allow_empty => 1
                    },
                  ],

This feature is necessary if you want to be able to create a configuration from scratch.

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' } ],

If write_config is missing, the data provided by read_config will be used. For instance:

  read_config  => { backend => 'custom', class => 'Bar', config_dir => '/etc/foo'},

In this case, configuration data will be read by Bar::read in directory /etc/foo and will be written back there by Bar::write.

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.

AUTHOR

Dominique Dumont, (ddumont at cpan dot org)

SEE ALSO

Config::Model, Config::Model::Instance, Config::Model::Node, Config::Model::Dumper, Config::Augeas