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

Last change on this file since 3109 was 3109, checked in by Bruno Cornec, 7 years ago
  • First working Base.pm module
File size: 3.7 KB
Line 
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);
19use Digest::MD5;
20use ProjectBuilder::Base;
21use ProjectBuilder::Conf;
22use MondoRescue::DynConf;
23
24# Inherit from the "Exporter" module which handles exporting functions.
25
26use Exporter;
27
28# Global hash for configuration params of mr
29my %mr;
30our $mr = \%mr;
31
32# Export, by default, all the functions into the namespace of
33# any code which uses this module.
34
35our @ISA = qw(Exporter);
36our @EXPORT = qw(mr_init mr_exit mr_conf_get $mr);
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
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
59sub mr_init {
60
61my $msg = shift || "";
62
63pb_log_init($pbdebug);
64
65if (defined $msg) {
66    pb_log($pbdebug,$msg);
67}
68
69
70my $pbproj;
71# Get the various location determined at installation time
72($mr->{'confdir'},$pbproj) = mr_dynconf_init();
73
74# Temp dir
75pb_temp_init();
76
77# First use the main configuration file
78pb_conf_init($pbproj);
79$ENV{'PBPROJ'} = $pbproj;
80#
81# Conf files Management
82# the $mr->{'confdir'}/mondorescue.conf.dist is delivered as part of the project and
83# its checksum is verified as we need good default values that we can trust
84#
85open(MD5,"$mr->{'confdir'}/$pbproj.conf.dist.md5") || die "Unable to read mandatory $mr->{'confdir'}/$pbproj.conf.dist.md5: $!";
86my $omd5 = <MD5>;
87close(MD5);
88chomp($omd5);
89# remove file name
90$omd5 =~ s/ .*//;
91open(CONF,"$mr->{'confdir'}/$pbproj.conf.dist") || die "Unable to read mandatory $mr->{'confdir'}/$pbproj.conf.dist: $!";
92binmode(CONF);
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);
96close(CONF);
97
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);
103}
104
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;
115my $msg = shift;
116
117if (defined $msg) {
118    pb_log($pbdebug,$msg);
119}
120# CLoses log
121if (defined $mr->{'logdesc'}) {
122    close($mr->{'logdesc'});
123}
124die "ERROR returned\n" if ($code lt 0);
125exit($code);
126}
127
128=item B<mr_conf_get>
129
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.
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.