.\" Automatically generated by Pod::Man 4.07 (Pod::Simple 3.32) .\" .\" Standard preamble: .\" ======================================================================== .de Sp \" Vertical space (when we can't use .PP) .if t .sp .5v .if n .sp .. .de Vb \" Begin verbatim text .ft CW .nf .ne \\$1 .. .de Ve \" End verbatim text .ft R .fi .. .\" Set up some character translations and predefined strings. \*(-- will .\" give an unbreakable dash, \*(PI will give pi, \*(L" will give a left .\" double quote, and \*(R" will give a right double quote. \*(C+ will .\" give a nicer C++. Capital omega is used to do unbreakable dashes and .\" therefore won't be available. \*(C` and \*(C' expand to `' in nroff, .\" nothing in troff, for use with C<>. .tr \(*W- .ds C+ C\v'-.1v'\h'-1p'\s-2+\h'-1p'+\s0\v'.1v'\h'-1p' .ie n \{\ . ds -- \(*W- . ds PI pi . if (\n(.H=4u)&(1m=24u) .ds -- \(*W\h'-12u'\(*W\h'-12u'-\" diablo 10 pitch . if (\n(.H=4u)&(1m=20u) .ds -- \(*W\h'-12u'\(*W\h'-8u'-\" diablo 12 pitch . ds L" "" . ds R" "" . ds C` "" . ds C' "" 'br\} .el\{\ . ds -- \|\(em\| . ds PI \(*p . ds L" `` . ds R" '' . ds C` . ds C' 'br\} .\" .\" Escape single quotes in literal strings from groff's Unicode transform. .ie \n(.g .ds Aq \(aq .el .ds Aq ' .\" .\" If the F register is >0, we'll generate index entries on stderr for .\" titles (.TH), headers (.SH), subsections (.SS), items (.Ip), and index .\" entries marked with X<> in POD. Of course, you'll have to process the .\" output yourself in some meaningful fashion. .\" .\" Avoid warning from groff about undefined register 'F'. .de IX .. .if !\nF .nr F 0 .if \nF>0 \{\ . de IX . tm Index:\\$1\t\\n%\t"\\$2" .. . if !\nF==2 \{\ . nr % 0 . nr F 2 . \} .\} .\" .\" Accent mark definitions (@(#)ms.acc 1.5 88/02/08 SMI; from UCB 4.2). .\" Fear. Run. Save yourself. No user-serviceable parts. . \" fudge factors for nroff and troff .if n \{\ . ds #H 0 . ds #V .8m . ds #F .3m . ds #[ \f1 . ds #] \fP .\} .if t \{\ . ds #H ((1u-(\\\\n(.fu%2u))*.13m) . ds #V .6m . ds #F 0 . ds #[ \& . ds #] \& .\} . \" simple accents for nroff and troff .if n \{\ . ds ' \& . ds ` \& . ds ^ \& . ds , \& . ds ~ ~ . ds / .\} .if t \{\ . ds ' \\k:\h'-(\\n(.wu*8/10-\*(#H)'\'\h"|\\n:u" . ds ` \\k:\h'-(\\n(.wu*8/10-\*(#H)'\`\h'|\\n:u' . ds ^ \\k:\h'-(\\n(.wu*10/11-\*(#H)'^\h'|\\n:u' . ds , \\k:\h'-(\\n(.wu*8/10)',\h'|\\n:u' . ds ~ \\k:\h'-(\\n(.wu-\*(#H-.1m)'~\h'|\\n:u' . ds / \\k:\h'-(\\n(.wu*8/10-\*(#H)'\z\(sl\h'|\\n:u' .\} . \" troff and (daisy-wheel) nroff accents .ds : \\k:\h'-(\\n(.wu*8/10-\*(#H+.1m+\*(#F)'\v'-\*(#V'\z.\h'.2m+\*(#F'.\h'|\\n:u'\v'\*(#V' .ds 8 \h'\*(#H'\(*b\h'-\*(#H' .ds o \\k:\h'-(\\n(.wu+\w'\(de'u-\*(#H)/2u'\v'-.3n'\*(#[\z\(de\v'.3n'\h'|\\n:u'\*(#] .ds d- \h'\*(#H'\(pd\h'-\w'~'u'\v'-.25m'\f2\(hy\fP\v'.25m'\h'-\*(#H' .ds D- D\\k:\h'-\w'D'u'\v'-.11m'\z\(hy\v'.11m'\h'|\\n:u' .ds th \*(#[\v'.3m'\s+1I\s-1\v'-.3m'\h'-(\w'I'u*2/3)'\s-1o\s+1\*(#] .ds Th \*(#[\s+2I\s-2\h'-\w'I'u*3/5'\v'-.3m'o\v'.3m'\*(#] .ds ae a\h'-(\w'a'u*4/10)'e .ds Ae A\h'-(\w'A'u*4/10)'E . \" corrections for vroff .if v .ds ~ \\k:\h'-(\\n(.wu*9/10-\*(#H)'\s-2\u~\d\s+2\h'|\\n:u' .if v .ds ^ \\k:\h'-(\\n(.wu*10/11-\*(#H)'\v'-.4m'^\v'.4m'\h'|\\n:u' . \" for low resolution devices (crt and lpr) .if \n(.H>23 .if \n(.V>19 \ \{\ . ds : e . ds 8 ss . ds o a . ds d- d\h'-1'\(ga . ds D- D\h'-1'\(hy . ds th \o'bp' . ds Th \o'LP' . ds ae ae . ds Ae AE .\} .rm #[ #] #H #V #F C .\" ======================================================================== .\" .IX Title "Config::Model::BackendMgr 3pm" .TH Config::Model::BackendMgr 3pm "2017-06-09" "perl v5.24.1" "User Contributed Perl Documentation" .\" For nroff, turn off justification. Always turn off hyphenation; it makes .\" way too many mistakes in technical documents. .if n .ad l .nh .SH "NAME" Config::Model::BackendMgr \- Load configuration node on demand .SH "VERSION" .IX Header "VERSION" version 2.105 .SH "SYNOPSIS" .IX Header "SYNOPSIS" .Vb 2 \& # Use BackendMgr to write data in Yaml file \& use Config::Model; \& \& # define configuration tree object \& my $model = Config::Model\->new; \& $model\->create_config_class( \& name => "Foo", \& element => [ \& [qw/foo bar/] => { \& type => \*(Aqleaf\*(Aq, \& value_type => \*(Aqstring\*(Aq \& }, \& ] \& ); \& \& $model\->create_config_class( \& name => "MyClass", \& \& # read_config spec is used by Config::Model::BackendMgr \& read_config => [ \& { \& backend => \*(Aqyaml\*(Aq, \& config_dir => \*(Aq/tmp/\*(Aq, \& file => \*(Aqmy_class.yml\*(Aq, \& auto_create => 1, \& }, \& ], \& \& element => [ \& [qw/foo bar/] => { \& type => \*(Aqleaf\*(Aq, \& value_type => \*(Aqstring\*(Aq \& }, \& hash_of_nodes => { \& type => \*(Aqhash\*(Aq, # hash id \& index_type => \*(Aqstring\*(Aq, \& cargo => { \& type => \*(Aqnode\*(Aq, \& config_class_name => \*(AqFoo\*(Aq \& }, \& }, \& ], \& ); \& \& my $inst = $model\->instance( root_class_name => \*(AqMyClass\*(Aq ); \& \& my $root = $inst\->config_root; \& \& # put data \& my $steps = \*(Aqfoo=FOO hash_of_nodes:fr foo=bonjour \- \& hash_of_nodes:en foo=hello \*(Aq; \& $root\->load( steps => $steps ); \& \& $inst\->write_back; \& \& # now look at file /tmp/my_class.yml .Ve .SH "DESCRIPTION" .IX Header "DESCRIPTION" This class provides a way to specify how to load or store configuration data within the model. .PP With these specifications, all configuration information is read during creation of a node (which triggers the creation of a backend manager object) and written back when write_back method is called (either on the node or on this backend manager). .PP This load/store can be done with different backends: .IP "\(bu" 4 Any of the \f(CW\*(C`Config::Model::Backend::*\*(C'\fR classes available on your system. For instance \f(CW\*(C`Config::Model::Backend::Yaml\*(C'\fR. .IP "\(bu" 4 \&\f(CW\*(C`cds_file\*(C'\fR: 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. This format is defined by this project. See \&\*(L"load string syntax\*(R" in Config::Model::Loader. .IP "\(bu" 4 \&\f(CW\*(C`perl_file\*(C'\fR: Perl data structure (perl) in a file. See Config::Model::DumpAsData for details on the data structure. .IP "\(bu" 4 \&\f(CW\*(C`custom\*(C'\fR: specifies a dedicated class and function to read and load the configuration tree. This is provided for backward compatibility and should not be used for new projects. .PP When needed, \f(CW\*(C`write_back\*(C'\fR method can be called on the instance (See Config::Model::Instance) to store back all configuration information. .SH "Backend specification" .IX Header "Backend specification" The backend specification is provided as an attribute of a Config::Model::Node specification. These attributes are optional: A node without \f(CW\*(C`read_config\*(C'\fR attribute must rely on another node for its data to be read and saved. .PP When needed (usually for the root node), the configuration class is declared with a \f(CW\*(C`read_config\*(C'\fR parameter. This parameter is a list of possible backend. Usually, only one read backend is needed. .SS "Parameters available for all backends" .IX Subsection "Parameters available for all backends" The following parameters are accepted by all backends: .IP "config_dir" 4 .IX Item "config_dir" Specify configuration directory. This parameter is optional as the directory can be hardcoded in the custom class. \f(CW\*(C`config_dir\*(C'\fR beginning with '\f(CW\*(C`~\*(C'\fR' is munged so \f(CW\*(C`~\*(C'\fR is replaced by \f(CW\*(C`File::HomeDir\->my_data\*(C'\fR. See File::HomeDir for details. .IP "file" 4 .IX Item "file" Specify configuration file name (without the path). This parameter is optional as the file name can be hardcoded in the custom class. .Sp The configuration file name can be specified with \f(CW&index\fR keyword when a backend is associated to a node contained in a hash. For instance, with \f(CW\*(C`file\*(C'\fR set to \f(CW\*(C`&index.conf\*(C'\fR: .Sp .Vb 5 \& service # hash element \& foo # hash index \& nodeA # values of nodeA are stored in foo.conf \& bar # hash index \& nodeB # values of nodeB are stored in bar.conf .Ve .Sp Likewise, the keyword \f(CW&element\fR can be used to specify the file name. For instance, with \f(CW\*(C`file\*(C'\fR set to \f(CW\*(C`&element\-&index.conf\*(C'\fR: .Sp .Vb 5 \& service # hash element \& foo # hash index \& nodeA # values of nodeA are stored in service.foo.conf \& bar # hash index \& nodeB # values of nodeB are stored in service.bar.conf .Ve .Sp Alternatively, \f(CW\*(C`file\*(C'\fR can be set to \f(CW\*(C`\-\*(C'\fR, in which case, the configuration is read from \s-1STDIN.\s0 .IP "file_mode" 4 .IX Item "file_mode" \&\f(CW\*(C`file_mode\*(C'\fR parameter can be used to set the mode of the written file(s). \f(CW\*(C`file_mode\*(C'\fR value can be in any form supported by \&\*(L"chmod\*(R" in Path::Tiny. Example: .Sp .Vb 3 \& file_mode => 0664, \& file_mode => \*(Aq0664\*(Aq, \& file_mode => \*(Aqg+w\*(Aq .Ve .IP "os_config_dir" 4 .IX Item "os_config_dir" Specify alternate location of a configuration directory depending on the \s-1OS \&\s0(as returned by \f(CW$^O\fR, see \*(L"\s-1PLATFORMS\*(R"\s0 in perlport). For instance: .Sp .Vb 2 \& config_dir => \*(Aq/etc/ssh\*(Aq, \& os_config_dir => { darwin => \*(Aq/etc\*(Aq } .Ve .IP "default_layer" 4 .IX Item "default_layer" Optional. Specifies where to find a global configuration file that specifies default values. For instance, this is used by OpenSSH to specify a global configuration file (\f(CW\*(C`/etc/ssh/ssh_config\*(C'\fR) that is overridden by user's file: .Sp .Vb 5 \& \*(Aqdefault_layer\*(Aq => { \& os_config_dir => { \*(Aqdarwin\*(Aq => \*(Aq/etc\*(Aq }, \& config_dir => \*(Aq/etc/ssh\*(Aq, \& file => \*(Aqssh_config\*(Aq \& } .Ve .Sp Only the 3 above parameters can be specified in \f(CW\*(C`default_layer\*(C'\fR. .IP "auto_create" 4 .IX Item "auto_create" By default, an exception is thrown if no read was successful. This behavior can be overridden by specifying \&\f(CW\*(C`auto_create => 1\*(C'\fR in one of the backend specification. For instance: .Sp .Vb 6 \& read_config => [ { \& backend => \*(AqIniFile\*(Aq, \& config_dir => \*(Aq/tmp\*(Aq, \& file => \*(Aqfoo.conf\*(Aq, \& auto_create => 1 \& } ], .Ve .Sp Setting \f(CW\*(C`auto_create\*(C'\fR to 1 is necessary to create a configuration from scratch .Sp When \f(CW\*(C`auto_create\*(C'\fR is set in write backend, missing directory and files are created with current umask. Default is false. .IP "auto_delete" 4 .IX Item "auto_delete" Delete configuration files that contains no data. (default is to leave an empty file) .SS "Config::Model::Backend::* backends" .IX Subsection "Config::Model::Backend::* backends" Specify the backend name and the parameters of the backend defined in their documentation. .PP For instance: .PP .Vb 5 \& read_config => [{ \& backend => \*(Aqyaml\*(Aq, \& config_dir => \*(Aq/tmp/\*(Aq, \& file => \*(Aqmy_class.yml\*(Aq, \& }], .Ve .PP See Config::Model::Backend::Yaml for more details for this backend. .SS "Your own backend" .IX Subsection "Your own backend" You can also write a dedicated backend. See How to write your own backend for details. .SS "Built-in backend" .IX Subsection "Built-in backend" \&\f(CW\*(C`cds_file\*(C'\fR and \f(CW\*(C`perl_file\*(C'\fR backend must be specified with mandatory \f(CW\*(C`config_dir\*(C'\fR parameter. For instance: .PP .Vb 5 \& read_config => { \& backend => \*(Aqcds_file\*(Aq , \& config_dir => \*(Aq/etc/cfg_dir\*(Aq, \& file => \*(Aqcfg_file.cds\*(Aq, #optional \& }, .Ve .PP When \f(CW\*(C`file\*(C'\fR is not specified, a file name is constructed with \&\f(CW\*(C`.\*(C'\fR where suffix is \f(CW\*(C`pl\*(C'\fR or \f(CW\*(C`cds\*(C'\fR. .SS "Custom backend" .IX Subsection "Custom backend" Custom backend is provided to be backward compatible but should not be used for new project. Writing your own backend is preferred. .PP Custom backend must be specified with a class name that features the methods used to write and read the configuration files: .PP .Vb 7 \& read_config => [ { \& backend => \*(Aqcustom\*(Aq , \& class => \*(AqMyRead\*(Aq, \& function => \*(Aqread_it", # optional, defaults to \*(Aqread\*(Aq \& config_dir => \*(Aq/etc/foo\*(Aq, # optional \& file => \*(Aqfoo.conf\*(Aq, # optional \& } ] .Ve .PP \&\f(CW\*(C`custom\*(C'\fR backend parameters are: .IP "class" 4 .IX Item "class" Specify the class that contains the read methods .IP "function" 4 .IX Item "function" Function name that is called back to read the file. See \*(L"read callback\*(R" for details. (default is \f(CW\*(C`read\*(C'\fR) .IP "file" 4 .IX Item "file" optional. Configuration file. This parameter may not apply if the configuration is stored in several files. By default, the instance name is used as configuration file name. .PP Most of the times, there's no need to create a write specification: the read specification is enough for this module to write back the configuration file. .PP The write method must be specified if the writer class is not the same as the reader class or if the writer method is not \f(CW\*(C`write\*(C'\fR: .PP .Vb 7 \& write_config => [ { \& backend => \*(Aqcustom\*(Aq , \& class => \*(AqMyWrite\*(Aq, \& function => \*(Aqwrite_it", # optional, defaults to \*(Aqread\*(Aq \& config_dir => \*(Aq/etc/foo\*(Aq, # optional \& file => \*(Aqfoo.conf\*(Aq, # optional \& } ] .Ve .PP Read callback function is called with these parameters: .PP .Vb 7 \& object => $obj, # Config::Model::Node object \& root => \*(Aq./my_test\*(Aq, # fake root directory, used for tests \& config_dir => /etc/foo\*(Aq, # absolute path \& file => \*(Aqfoo.conf\*(Aq, # file name \& file_path => \*(Aq./my_test/etc/foo/foo.conf\*(Aq \& io_handle => $io # IO::File object with binmode :utf8 \& check => [yes|no|skip] .Ve .PP The IO::File object is undef if the file cannot be read. .PP The callback must return 0 on failure and 1 on successful read. .PP Write callback function is called with these parameters: .PP .Vb 9 \& object => $obj, # Config::Model::Node object \& root => \*(Aq./my_test\*(Aq, # fake root directory, used for tests \& config_dir => /etc/foo\*(Aq, # absolute path \& file => \*(Aqfoo.conf\*(Aq, # file name \& file_path => \*(Aq./my_test/etc/foo/foo.conf\*(Aq \& io_handle => $io # IO::File object opened in write mode \& # with binmode :utf8 \& auto_create => 1 # create dir as needed \& check => [yes|no|skip] .Ve .PP The IO::File object is undef if the file cannot be written to. .PP The callback must return 0 on failure and 1 on successful write. .SH "Using backend to change configuration file syntax" .IX Header "Using backend to change configuration file syntax" \&\f(CW\*(C`read_config\*(C'\fR tries all the specified backends. This feature can be used to migrate from one syntax to another. .PP In this example, backend manager first tries to read an \s-1INI\s0 file and then to read a \s-1YAML\s0 file: .PP .Vb 4 \& read_config => [ \& { backend => \*(AqIniFile\*(Aq, ... }, \& { backend => \*(Aqyaml\*(Aq, ... }, \& ], .Ve .PP When a read operation is successful, the remaining read methods are skipped. .PP Likewise, the \f(CW\*(C`write_config\*(C'\fR specification accepts several backends. By default, the specifications are tried in order, until the first succeeds. .PP In the example above, the migration from \s-1INI\s0 to \s-1YAML\s0 can be achieved by specifying only the \s-1YAML\s0 backend: .PP .Vb 3 \& write_config => [ \& { backend => \*(Aqyaml\*(Aq, ... }, \& ], .Ve .SH "Test setup" .IX Header "Test setup" By default, configurations files are read from the directory specified by \f(CW\*(C`config_dir\*(C'\fR parameter specified in the model. You may override the \&\f(CW\*(C`root\*(C'\fR directory for test. .SH "CAVEATS" .IX Header "CAVEATS" When both \f(CW\*(C`config_dir\*(C'\fR and \f(CW\*(C`file\*(C'\fR are specified, this class write-opens the configuration file (and thus clobber it) before calling the \f(CW\*(C`write\*(C'\fR call-back and pass the file handle with \f(CW\*(C`io_handle\*(C'\fR parameter. \f(CW\*(C`write\*(C'\fR should use this handle to write data in the target configuration file. .PP If this behavior causes problem (e.g. with augeas backend), the solution is either to set \f(CW\*(C`file\*(C'\fR to undef or an empty string in the \&\f(CW\*(C`write_config\*(C'\fR specification. .SH "Methods" .IX Header "Methods" .SS "support_annotation" .IX Subsection "support_annotation" Returns 1 if at least one of the backends support to read and write annotations (aka comments) in the configuration file. .SH "AUTHOR" .IX Header "AUTHOR" Dominique Dumont, (ddumont at cpan dot org) .SH "SEE ALSO" .IX Header "SEE ALSO" Config::Model, Config::Model::Instance, Config::Model::Node, Config::Model::Dumper .SH "AUTHOR" .IX Header "AUTHOR" Dominique Dumont .SH "COPYRIGHT AND LICENSE" .IX Header "COPYRIGHT AND LICENSE" This software is Copyright (c) 2005\-2017 by Dominique Dumont. .PP This is free software, licensed under: .PP .Vb 1 \& The GNU Lesser General Public License, Version 2.1, February 1999 .Ve