2 # Copyright (c) 2006 Zmanda Inc. All Rights Reserved.
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.
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
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
17 # Contact information: Zmanda Inc, 505 N Mathlida Ave, Suite 120
18 # Sunnyvale, CA 94085, USA, or: http://www.zmanda.com
26 Amconfig - set up amanda configurations for installcheck testing
32 my $testconf = Amconfig->new();
33 $testconf->add_param("runtapes", "5");
34 $testconf->add_subsec("tapetype", "DUCKTAPE", { length => "10G", filemark => "4096k" });
38 The resulting configuration is always named "TESTCONF". The basic
39 configuration contains only a few parameters that are necessary just
40 to run Amanda applications in the test environment. It also contains
41 a tapetype, C<TEST-TAPE>.
43 Note that it's quite possible to produce an invalid configuration with this
44 package (and, in fact, some of the tests do just that).
48 This module can set up a vtape configuration, replete with the proper
49 vtape directories, using C<setup_vtape>. The vtapes are created under
50 the "TESTCONF" configuration directory, for ease of later deletion. Do
51 not store anything large in these vtapes!
55 Using this module I<will> destroy any existing configuration named
56 TESTDIR. I<Please> do not use this on a production machine!
64 Create a new configuration object
71 # An instance is a blessed hash containing parameters. Start with
72 # some defaults to make sure things run.
73 my $infofile = '@CONFIG_DIR@/TESTCONF/curinfo';
74 my $logdir = '@CONFIG_DIR@/TESTCONF/log';
75 my $indexdir = '@CONFIG_DIR@/TESTCONF/index';
78 'infofile' => $infofile,
80 'indexdir' => $indexdir,
84 # Global params are stored as an arrayref, so that the same declaration
85 # can appear multiple times
87 'mailto' => '"nobody@invalidomain"',
88 'dumpuser' => '"' . (getpwuid($<))[0] . '"', # current username
90 # These dirs are under CONFIG_DIR just for ease of destruction.
91 # This is not a recommended layout!
92 'infofile' => "\"$infofile\"",
93 'logdir' => "\"$logdir\"",
94 'indexdir' => "\"$indexdir\"",
96 'tapetype' => '"TEST-TAPE"',
99 # Subsections are stored as a hashref of arrayrefs, keyed by
104 'length' => '50 mbytes',
105 'filemark' => '4 kbytes'
113 'holdingdisks' => { },
117 bless($self, $class);
121 =item C<add_param($param, $value)>
123 Add the given parameter to the configuration file, overriding any
124 previous value. Note that strings which should be quoted in the configuration
125 file itself must be double-quoted here, e.g.,
127 $testconf->add_param('org' => '"MyOrganization"');
133 my ($param, $value) = @_;
135 push @{$self->{'params'}}, $param, $value;
138 =item C<add_tapetype($name, $values_hashref)>
139 =item C<add_dumptype($name, $values_hashref)>
140 =item C<addholdingdisk($name, $values_hashref)>
141 =item C<add_interface($name, $values_arrayref)>
143 Add the given subsection to the configuration file, including all
144 values in the arrayref. The values should be specified as alternating
151 my ($name, $values_arrayref) = @_;
152 $self->{'tapetypes'}{$name} = $values_arrayref;
157 my ($name, $values_arrayref) = @_;
158 $self->{'dumptypes'}{$name} = $values_arrayref;
161 sub add_holdingdisk {
163 my ($name, $values_arrayref) = @_;
164 $self->{'holdingdisks'}{$name} = $values_arrayref;
169 my ($name, $values_arrayref) = @_;
170 $self->{'interfaces'}{$name} = $values_arrayref;
173 =item C<add_dle($line)>
175 Add a disklist entry; C<$line> is inserted verbatim into the disklist.
182 push @{$self->{'dles'}}, $line;
185 =item C<setup_vtape()>
187 Set up to use a single vtape (no changer). This creates the proper
188 directory hierarchy and sets C<tapedev> to the relevant path.
194 my $tapepath = "@CONFIG_DIR@/TESTCONF/vtapes/tape1";
195 push @{$self->{'vtapes'}}, $tapepath;
197 $self->add_param("tapedev", "\"file:$tapepath\"");
202 Write out the accumulated configuration file, along with any other
203 files necessary to run Amanda.
210 my $testconf_dir = '@CONFIG_DIR@/TESTCONF';
211 if (-e $testconf_dir) {
212 rmtree($testconf_dir) or die("Could not remove '$testconf_dir'");
214 mkpath($testconf_dir);
216 # set up curinfo dir, etc.
217 mkpath($self->{'infofile'}) or die("Could not create infofile directory");
218 mkpath($self->{'logdir'}) or die("Could not create logdir directory");
219 mkpath($self->{'indexdir'}) or die("Could not create indexdir directory");
222 for my $vtape (@{$self->{'vtapes'}}) {
223 mkpath("$vtape/data") or die("Could not create vtape directory '$vtape/data'");
226 $self->_write_tapelist("$testconf_dir/tapelist");
227 $self->_write_disklist("$testconf_dir/disklist");
228 $self->_write_amanda_conf("$testconf_dir/amanda.conf");
231 sub _write_tapelist {
235 # create an empty tapelist
236 open(my $tapelist, ">", $filename);
240 sub _write_disklist {
244 # don't bother writing a disklist if there are no dle's
245 return unless $self->{'dles'};
247 open(my $disklist, ">", $filename);
249 for my $dle_line (@{$self->{'dles'}}) {
250 print $disklist "$dle_line\n";
256 sub _write_amanda_conf {
260 open my $amanda_conf, ">", $filename
261 or croak("Could not open '$filename'");
263 # write key/value pairs
264 my @params = @{$self->{'params'}};
266 $param = shift @params;
267 $value = shift @params;
268 print $amanda_conf "$param $value\n";
271 # write out subsections
272 $self->_write_amanda_conf_subsection($amanda_conf, "tapetype", $self->{"tapetypes"});
273 $self->_write_amanda_conf_subsection($amanda_conf, "dumptype", $self->{"dumptypes"});
274 $self->_write_amanda_conf_subsection($amanda_conf, "interface", $self->{"interfaces"});
275 $self->_write_amanda_conf_subsection($amanda_conf, "holdingdisk", $self->{"holdingdisks"});
280 sub _write_amanda_conf_subsection {
282 my ($amanda_conf, $subsec_type, $subsec_ref) = @_;
284 for my $subsec_name (keys %$subsec_ref) {
285 my @values = @{$subsec_ref->{$subsec_name}};
287 if ($subsec_type eq "holdingdisk") {
288 print $amanda_conf "\nholdingdisk $subsec_name {\n";
290 print $amanda_conf "\ndefine $subsec_type $subsec_name {\n";
294 $param = shift @values;
295 $value = shift @values;
296 print $amanda_conf "$param $value\n";
298 print $amanda_conf "}\n";