source: MondoRescue/devel/mr/lib/MondoRescue/Base.pm

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