1 | #!/usr/bin/perl -w
|
---|
2 | #
|
---|
3 | # Base subroutines brought by the MondoRescue project
|
---|
4 | #
|
---|
5 | # $Id$
|
---|
6 | #
|
---|
7 | # Copyright B. Cornec 2008
|
---|
8 | # Provided under the GPL v2
|
---|
9 |
|
---|
10 | package MondoRescue::Base;
|
---|
11 |
|
---|
12 | use strict 'vars';
|
---|
13 | use Data::Dumper;
|
---|
14 | use English;
|
---|
15 | use File::Basename;
|
---|
16 | use File::Copy;
|
---|
17 | use POSIX qw(strftime);
|
---|
18 | use lib qw (lib);
|
---|
19 | use ProjectBuilder::Base;
|
---|
20 | use ProjectBuilder::Conf;
|
---|
21 | use MondoRescue::DynConf;
|
---|
22 |
|
---|
23 | # Inherit from the "Exporter" module which handles exporting functions.
|
---|
24 |
|
---|
25 | use Exporter;
|
---|
26 |
|
---|
27 | # Global hash for configuration params of mr
|
---|
28 | my %mr;
|
---|
29 | our $mr = \%mr;
|
---|
30 |
|
---|
31 | # Export, by default, all the functions into the namespace of
|
---|
32 | # any code which uses this module.
|
---|
33 |
|
---|
34 | our @ISA = qw(Exporter);
|
---|
35 | our @EXPORT = qw(mr_init mr_exit mr_conf_get $mr);
|
---|
36 |
|
---|
37 | =pod
|
---|
38 |
|
---|
39 | =head1 NAME
|
---|
40 |
|
---|
41 | MondoRescue::Base, part of the mondorescue.org
|
---|
42 |
|
---|
43 | =head1 DESCRIPTION
|
---|
44 |
|
---|
45 | This modules provides low level and generic functions for the Mondorescue project
|
---|
46 |
|
---|
47 | =head1 USAGE
|
---|
48 |
|
---|
49 | =over 4
|
---|
50 |
|
---|
51 | =item B<mr_init>
|
---|
52 |
|
---|
53 | This function initialize MondoRescue, point to the right conf files, setup stuff
|
---|
54 | It takes 1 parameter, the message to print if needed
|
---|
55 |
|
---|
56 | =cut
|
---|
57 |
|
---|
58 | sub mr_init {
|
---|
59 |
|
---|
60 | my $msg = shift || "";
|
---|
61 |
|
---|
62 | if (defined $msg) {
|
---|
63 | pb_log($pbdebug,$msg);
|
---|
64 | }
|
---|
65 |
|
---|
66 |
|
---|
67 | # Get the various location determined at installation time
|
---|
68 | my ($etcdir,$pbproj) = mr_dynconf_init();
|
---|
69 |
|
---|
70 | # Temp dir
|
---|
71 | pb_temp_init();
|
---|
72 |
|
---|
73 | # First use the main configuration file
|
---|
74 | pb_conf_init($pbproj);
|
---|
75 | #
|
---|
76 | # Conf files Management
|
---|
77 | # the $etcdir/mondorescue.conf.dist is delivered as part of the project and
|
---|
78 | # its checksum is verified as we need good default values that we can trust
|
---|
79 | #
|
---|
80 | open(MD5,"$etcdir/$pbproj.conf.dist.md5") || die "Unable to read mandatory $etcdir/$pbproj.conf.dist.md5: $!";
|
---|
81 | my $omd5 = <MD5>;
|
---|
82 | chop($omd5);
|
---|
83 | close(MD5);
|
---|
84 | open(CONF,"$etcdir/$pbproj.conf.dist") || die "Unable to read mandatory $etcdir/$pbproj.conf.dist: $!";
|
---|
85 | my $md5 = Digest::MD5->new;
|
---|
86 | binmode(CONF);
|
---|
87 | $md5->addfile(CONF);
|
---|
88 | die "Invalid MD5 found sum for $etcdir/$pbproj.conf.dist: $md5->hexdigest" if ($omd5 ne $md5->hexdigest);
|
---|
89 | close(CONF);
|
---|
90 |
|
---|
91 | pb_conf_add("$etcdir/$pbproj.conf.dist");
|
---|
92 | }
|
---|
93 |
|
---|
94 | =item B<mr_exit>
|
---|
95 |
|
---|
96 | This function closes opened files, clean up the environment and exits MondoRescue
|
---|
97 | It takes 2 parameters, the exit code, and the message to print if needed
|
---|
98 |
|
---|
99 | =cut
|
---|
100 |
|
---|
101 | sub mr_exit {
|
---|
102 |
|
---|
103 | my $code = shift;
|
---|
104 | my $msg = shift;
|
---|
105 |
|
---|
106 | if (defined $msg) {
|
---|
107 | pb_log($pbdebug,$msg);
|
---|
108 | }
|
---|
109 | # CLoses log
|
---|
110 | if (defined $mr->{'logdesc'}) {
|
---|
111 | close($mr->{'logdesc'});
|
---|
112 | }
|
---|
113 | die "ERROR returned\n" if ($code < 0);
|
---|
114 | exit($code);
|
---|
115 | }
|
---|
116 |
|
---|
117 | =item B<mr_conf_get>
|
---|
118 |
|
---|
119 | This function get parameters in configuration files and returns from the least significant level (default) to the most significant level (application name), passing by the project name.
|
---|
120 | It takes a list of parameters to find and returns the values corresponding
|
---|
121 |
|
---|
122 | =cut
|
---|
123 |
|
---|
124 |
|
---|
125 | sub mr_conf_get {
|
---|
126 | my @params = @_;
|
---|
127 | my @ptr = ();
|
---|
128 | my $ptr;
|
---|
129 |
|
---|
130 | pb_log(2,"Entering mr_conf_get\n");
|
---|
131 | my @args1 = pb_conf_get_if(@params);
|
---|
132 | my $proj = $ENV{'PBPROJ'};
|
---|
133 | $ENV{'PBPROJ'} = $ENV{'PBPKG'};
|
---|
134 | my @args2 = pb_conf_get_if(@params);
|
---|
135 | foreach my $i (0..$#args1) {
|
---|
136 | $ptr = undef;
|
---|
137 | # Process from least important to more important
|
---|
138 | $ptr = $args1[$i]->{'default'};
|
---|
139 | $ptr[$i] = $ptr if (defined $ptr);
|
---|
140 | $ptr = $args1[$i]->{$ENV{'PBPROJ'}};
|
---|
141 | $ptr[$i] = $ptr if (defined $ptr);
|
---|
142 | $ptr = $args2[$i]->{$ENV{'PBPKG'}};
|
---|
143 | $ptr[$i] = $ptr if (defined $ptr);
|
---|
144 | $ptr[$i] = "Undefined" if (not defined $ptr[$i]);
|
---|
145 | pb_log(2,"Found parameter $params[$i] with value $ptr[$i]\n");
|
---|
146 | }
|
---|
147 | $ENV{'PBPROJ'} = $proj;
|
---|
148 | return(@ptr);
|
---|
149 | }
|
---|