Imported Upstream version 3.2.0
[debian/amanda] / perl / Amanda / Config.pod
1 /*
2  * Copyright (c) 2009, 2010 Zmanda, Inc.  All Rights Reserved.
3  *
4  * This program is free software; you can redistribute it and/or modify it
5  * under the terms of the GNU General Public License version 2 as published
6  * by the Free Software Foundation.
7  *
8  * This program is distributed in the hope that it will be useful, but
9  * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
10  * or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
11  * for more details.
12  *
13  * You should have received a copy of the GNU General Public License along
14  * with this program; if not, write to the Free Software Foundation, Inc.,
15  * 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
16  *
17  * Contact information: Zmanda Inc., 465 S. Mathilda Ave., Suite 300
18  * Sunnyvale, CA 94085, USA, or: http://www.zmanda.com
19  */
20
21 %perlcode %{
22
23 =head1 NAME
24
25 Amanda::Config - access to Amanda configuration parameters
26
27 =head1 SYNOPSIS
28
29     use Amanda::Config qw( :init :getconf );
30
31     my $config_name = shift @ARGV;
32     config_init($CONFIG_INIT_EXPLICIT_NAME, $config_name);
33     apply_config_overrides($config_overrides);
34     my ($cfgerr_level, @cfgerr_errors) = config_errors();
35     if ($cfgerr_level >= $CFGERR_WARNINGS) {
36         config_print_errors();
37         if ($cfgerr_level >= $CFGERR_ERRORS) {
38             die("errors processing config file");
39         }
40     }
41
42     print "tape device is ", getconf($CNF_TAPEDEV), "\n";
43
44 This API closely parallels the C API.
45
46 =head1 INITIALIZATION
47
48 The Amanda configuration is treated as a global state for the
49 application.  It is not possible to load two configurations
50 simultaneously.
51
52 All initialization-related symbols can be imported with the tag
53 C<:init>.
54
55 =head2 LOADING CONFIGURATION
56
57 The Amanda configuration is loaded with the aptly named
58 C<config_init($flags, $name)>.  Because of the great variety in
59 invocation method among Amanda applications, this function has a number
60 of flags that affect its behavior.  These flags can be OR'd together.
61
62 =over
63
64 =item If C<CONFIG_INIT_EXPLICIT_NAME> is given, then the C<$name> parameter can
65 contain the name of a configuration to load.  Note that if the parameter is
66 C<".">, this is equivalent to C<CONFIG_INIT_USE_CWD>.
67
68 =item If C<CONFIG_INIT_USE_CWD> is given, and if the current directory
69 contains C<amanda.conf>, then that file is loaded.
70
71 =item If C<CONFIG_INIT_CLIENT> is given, then a client configuration
72 is loaded.
73
74 =item If C<CONFIG_INIT_OVERLAY> is given, then any existing
75 configuration is not reset.
76
77 =back
78
79 See C<conffile.h> for more detailed information on these flags and
80 their interactions.
81
82 C<config_uninit()> reverses the effects of C<config_init>.  It is
83 not often used.
84
85 Once the configuration is loaded, the configuration name
86 (e.g., "DailySet1"), directory (C</etc/amanda/DailySet1>),
87 and filename (C</etc/amanda/DailySet1/amanda.conf>) are
88 available from C<get_config_name()>, C<get_config_dir()>, and
89 C<get_config_filename()>, respectively.
90
91 =head3 CONFIG ERRORS
92
93 This module collects configuration errors and warnings in a list, and also
94 tracks the overall error level with an enumeration: C<$CFGERR_OK>,
95 C<$CFGERR_WARNINGS>, and C<$CFGERR_ERRORS>.  C<config_init> and
96 C<apply_config_overrides> both return the current level.  The level and the
97 list of error messages are available from C<config_errors>:
98
99   my ($cfgerr_level, @errors) = Amanda::Config::config_errors();
100
101 As a convenience, C<config_print_errors> will print all error messages to
102 stderr.  The error state can be cleared with C<config_clear_errors>.
103
104 =head2 CONFIG OVERWRITES
105
106 Most Amanda applications accept the command-line option C<-o>
107 to "overwrite" configuration values in C<amanda.conf>.  In Perl
108 applications, these options should be parsed with L<Getopt::Long|Getopt::Long>, with
109 the action being a call to C<add_config_override_opt>.  For example:
110
111   my $config_overrides = new_config_overrides($#ARGV+1);
112     GetOptions(
113         # ...
114         'o=s' => sub { add_config_override_opt($config_overrides, $_[1]); },
115     ) or usage();
116   my $cfg_ok = config_init($CONFIG_INIT_EXPLICIT_NAME | $CONFIG_INIT_USE_CWD, $config_name);
117   apply_config_overrides($config_overrides);
118
119 C<new_config_overrides($size_estimate)> creates a new
120 overwrites object, using the given size as an estimate of
121 the number of items it will contain (C<$#ARGC/2> is a good
122 estimate).  Individual configuration options are then added via
123 C<add_config_override($co, $key, $value)> (which takes a key/value
124 pair) or C<add_config_override_opt($co, $optarg)>, which parses a
125 string following C<-o> on the command line.
126
127 Once the overwrites are gathered, they are applied with
128 C<apply_config_overrides($co)>, which applies the overwrites to the
129 active configuration.  No further operations can be performed on the
130 overwrites object after C<apply_config_overrides> has been called.
131
132 The utility function C<get_config_options()> returns a list of
133 command-line arguments to represent any overwrites that were used
134 to generate the current configuration.  (TODO: this function isn't
135 available yet)
136
137 =head1 PARAMETER ACCESS
138
139 Amanda configurations consist of "global" parameters and several
140 sets of "subsections" -- one set for dumptypes, one for tapetypes,
141 and so on.
142
143 All of the global parameters are represented by a constant beginning with
144 C<$CNF_>, e.g., C<$CNF_LABELSTR>.  The function C<getconf($cnf)> returns the
145 value of parameter C<$cnf>, in whatever format is appropriate for the parameter
146 (see DATA FORMATS, below).  C<getconf_seen($cnf)> returns a true value if
147 C<$cnf> was seen in the configuration file.  If it was not seen, then it will
148 have its default value.  C<getconf_linenum($cnf)> returns the line number in
149 the configuration file where it is set, 0 if it is not in a configuration file,
150 or -2 if it is set in a command line argument.
151
152 Some parameters have enumerated types.  The values for those
153 enumerations are available from this module with the same name as
154 in C<conffile.h>.  For example, C<$CNF_TAPERALGO> will yield a value
155 from the enumeration C<taperalgo_t>, the constants for which all
156 begin with C<$ALGO_>.  See C<conffile.h> for the details.
157
158 Each subsection type C<TYP> has the following functions:
159
160 =over
161
162 =item C<lookup_TYP($subsec_name)>
163
164 which returns an opaque object
165 (C<$ss>) representing the subsection, or C<undef> if no subsection
166 with that name exists;
167
168 =item C<TYP_name($ss)>
169
170 returning the name of the subsection;
171
172 =item C<TYP_getconf($ss, $cnf)>
173
174 which fetches a parameter value from C<$ss>; and
175
176 =item C<TYP_seen($ss, $cnf)>
177
178 which returns a true value if <$cnf> was seen in the subsection.
179
180 =back
181
182 The subsections are:
183
184 =over
185
186 =item C<tapetype>
187
188 with constants beginning with C<$TAPETYPE_>
189
190 =item C<dumptype>
191
192 with constants beginning with C<$DUMPTYPE_>
193
194 =item C<interface>
195
196 with constants beginning with C<$INTER_>
197
198 =item C<holdingdisk>
199
200 with constants beginning with C<$HOLDING_>
201
202 =item C<application>
203
204 with constants beginning with C<$APPLICATION_>
205
206 =item C<script>
207
208 with constants beginning with C<$PP_SCRIPT_>
209
210 =item C<device>
211
212 with constants beginning with C<$DEVICE_CONFIG_>.
213
214 =item C<changer>
215
216 with constants beginning with C<$CHANGER_CONFIG_>.
217
218 =back
219
220 See C<conffile.h> for the names of the constants themselves.
221
222 =head2 DATA FORMATS
223
224 Each configuration parameter has a "conftype", as assigned in
225 C<common-src/conffile.c>.  The translation of most of these types into Perl
226 values is straightforward:
227
228   CONFTYPE_INT                        Math::BigInt
229   CONFTYPE_INT64                      Math::BigInt
230   CONFTYPE_REAL                       floating-point value
231   CONFTYPE_STR                        string
232   CONFTYPE_IDENT                      string
233   CONFTYPE_TIME                       Math::BigInt (epoch value)
234   CONFTYPE_SIZE                       Math::BigInt
235   CONFTYPE_BOOLEAN                    Math::BigInt
236   CONFTYPE_COMPRESS                   Math::BigInt
237   CONFTYPE_ENCRYPT                    Math::BigInt
238   CONFTYPE_HOLDING                    Math::BigInt
239   CONFTYPE_ESTIMATELIST               [ Math::BigInt, .. ]
240   CONFTYPE_STRATEGY                   Math::BigInt
241   CONFTYPE_TAPERALGO                  Math::BigInt
242   CONFTYPE_PRIORITY                   Math::BigInt
243   CONFTYPE_RATE                       float, float
244   CONFTYPE_INTRANGE                   Math::BigInt, Math::BigInt
245   CONFTYPE_APPLICATION                string
246   CONFTYPE_EXECUTE_ON                 string
247   CONFTYPE_EXECUTE_WHERE              Math::BigInt
248   CONFTYPE_SEND_AMREPORT_ON           Math::BigInt
249   CONFTYPE_IDENTLIST                  [ string, .. ]
250   CONFTYPE_PART_CACHE_TYPE            Math::BigInt
251   CONFTYPE_RECOVERY_LIMIT             [ string, .. ] (undef if not specified;
252                                             undef in the list means same-host)
253
254 Note that C<CONFTYPE_INTRANGE> and C<CONFTYPE_RATE> each return two values, not
255 an array reference.
256
257 Include and exclude lists with type C<CONFTYPE_EXINCLUDE> return a hash giving
258 all listed filenames (in the C<list> key), include/exclude files (C<files>),
259 and a boolean indicating that the list is optional (C<optional>):
260
261   { list => [ str, .. ], file => [ str, .. ], optional => Math::BigInt }
262
263 Properties are represented as a hash of hashes.  The keys are the property
264 names, converted to ASCII lowercase.  Each property has a C<values> array
265 giving all values specified for this property, as well as booleans C<priority>
266 and C<append> that are true if the corresponding keyword was supplied.
267
268   { prop1 => { values => [ str, .. ] priority => int, append => int },
269     prop2 => { .. } .. }
270
271 Note that integer types of all sizes become C<Math::BigInt> objects rather than
272 Perl integers, as is the habit throughout Amanda.
273
274 The C<CNF_AUTOLABEL> value is a hash with the following keys
275
276   template      label template, or undef
277   other_config  boolean
278   non_amanda    boolean
279   volume_error  boolean
280   empty         boolean
281
282 =head2 OTHER ACCESS
283
284 Parameter values are available by name from C<getconf_byname($name)> and
285 C<getconf_byname_strs($name, $str_needs_quotes)>.  These functions implement
286 the C<TYP:NAME:PARAM> syntax advertised by C<amgetconf> to access values in
287 subsections.  The first function returns a Perl value (see DATA FORMATS,
288 above), while the second returns a list of strings suitable for use in
289 C<amanda.conf>, including quotes around strings if C<$str_needs_quotes> is
290 true.
291
292 C<getconf_list($typ)> returns a list of the names of all subsections of the
293 given type.  C<%subsection_names> is a hash whose keys are allowed subsection
294 names.
295
296 =head2 DERIVED VALUES
297
298 The C<$CNF_DISPLAYUNIT> implies a certain divisor to convert from
299 kilobytes to the desired unit.  This divisor is available from
300 C<getconf_unit_divisor()>.  Note carefully that it is a I<divisor>
301 for a value in I<kilobytes>!
302
303 Finally, various subsections of Amanda enable verbose debugging via
304 configuration parameters.  The status of each parameter is available
305 a similarly-named variable, e.g., C<$debug_auth>.
306
307 All parameter access functions and constants can be imported with
308 the tag C<:getconf>.
309
310 =head1 MISCELLANEOUS
311
312 These functions defy categorization.
313
314 The function C<config_dir_relative> will interpret a path relative to
315 the current configuration directory.  Absolute paths are passed through
316 unchanged, while relative paths are converted to absolute paths.
317
318 C<dump_configuration()> dumps the current configuration, in a format
319 suitable for re-evaluation for this module, to standard output.
320 This function may be revised to return a string.
321
322 Several parts of Amanda need to convert unit modifier value like
323 "gbytes" to a multiplier.  The function C<find_multiplier($str)>
324 returns the unit multiplier for such a string.  For example, "mbytes"
325 is converted to 1048576 (1024*1024).
326
327 C<string_to_boolean()> takes a string and returns 0 if it matches any of
328 Amanda's names for false, or 1 if matches a name for true. If it can't be
329 interpreted, C<undef> is returned.
330
331 C<amandaify_property_name()> converts a string into Amanda's property style:
332 all lower-case and with "-" replacing "_".
333
334 =head1 CONSTANTS
335
336 This section lists all of the configuration parameter constants defined in this
337 module.  All of these constants are available with the C<:getconf> export tag.
338
339 =cut
340
341 %}