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