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

Last change on this file since 3108 was 3108, checked in by Bruno Cornec, 11 years ago

r5244@localhost: bruno | 2013-04-29 00:16:57 +0200

  • Minor improvements for devel version of mondorescue
File size: 3.4 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
63if (defined $msg) {
64 pb_log($pbdebug,$msg);
65}
66
67
68# Get the various location determined at installation time
69my ($etcdir,$pbproj) = mr_dynconf_init();
70
71# Temp dir
72pb_temp_init();
73
74# First use the main configuration file
75pb_conf_init($pbproj);
76#
77# Conf files Management
78# the $etcdir/mondorescue.conf.dist is delivered as part of the project and
79# its checksum is verified as we need good default values that we can trust
80#
81open(MD5,"$etcdir/$pbproj.conf.dist.md5") || die "Unable to read mandatory $etcdir/$pbproj.conf.dist.md5: $!";
82my $omd5 = <MD5>;
83chomp($omd5);
84close(MD5);
85open(CONF,"$etcdir/$pbproj.conf.dist") || die "Unable to read mandatory $etcdir/$pbproj.conf.dist: $!";
86my $md5 = Digest::MD5->new;
87binmode(CONF);
88$md5->addfile(CONF);
89my $digest = $md5->hexdigest;
90die "Invalid MD5 sum found for $etcdir/$pbproj.conf.dist: $digest" if ($omd5 ne $digest);
91close(CONF);
92
93pb_conf_add("$etcdir/$pbproj.conf.dist");
94}
95
96=item B<mr_exit>
97
98This function closes opened files, clean up the environment and exits MondoRescue
99It takes 2 parameters, the exit code, and the message to print if needed
100
101=cut
102
103sub mr_exit {
104
105my $code = shift;
106my $msg = shift;
107
108if (defined $msg) {
109 pb_log($pbdebug,$msg);
110}
111# CLoses log
112if (defined $mr->{'logdesc'}) {
113 close($mr->{'logdesc'});
114}
115die "ERROR returned\n" if ($code < 0);
116exit($code);
117}
118
119=item B<mr_conf_get>
120
121This 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.
122It takes a list of parameters to find and returns the values corresponding
123
124=cut
125
126
127sub mr_conf_get {
128 my @params = @_;
129 my @ptr = ();
130 my $ptr;
131
132 pb_log(2,"Entering mr_conf_get\n");
133 my @args1 = pb_conf_get_if(@params);
134 my $proj = $ENV{'PBPROJ'};
135 $ENV{'PBPROJ'} = $ENV{'PBPKG'};
136 my @args2 = pb_conf_get_if(@params);
137 foreach my $i (0..$#args1) {
138 $ptr = undef;
139 # Process from least important to more important
140 $ptr = $args1[$i]->{'default'};
141 $ptr[$i] = $ptr if (defined $ptr);
142 $ptr = $args1[$i]->{$ENV{'PBPROJ'}};
143 $ptr[$i] = $ptr if (defined $ptr);
144 $ptr = $args2[$i]->{$ENV{'PBPKG'}};
145 $ptr[$i] = $ptr if (defined $ptr);
146 $ptr[$i] = "Undefined" if (not defined $ptr[$i]);
147 pb_log(2,"Found parameter $params[$i] with value $ptr[$i]\n");
148 }
149 $ENV{'PBPROJ'} = $proj;
150 return(@ptr);
151}
Note: See TracBrowser for help on using the repository browser.