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

Last change on this file since 2667 was 2667, checked in by Bruno Cornec, 14 years ago

r3936@dhcp184-49-175-19: bruno | 2010-06-26 09:48:04 +0200

  • Inventory now in better shape. Needs useful and exhaustive content now.
File size: 3.3 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 ProjectBuilder::Base;
20use ProjectBuilder::Conf;
21use MondoRescue::DynConf;
22
23# Inherit from the "Exporter" module which handles exporting functions.
24
25use Exporter;
26
27# Global hash for configuration params of mr
28my %mr;
29our $mr = \%mr;
30
31# Export, by default, all the functions into the namespace of
32# any code which uses this module.
33
34our @ISA = qw(Exporter);
35our @EXPORT = qw(mr_init mr_exit mr_conf_get $mr);
36
37=pod
38
39=head1 NAME
40
41MondoRescue::Base, part of the mondorescue.org
42
43=head1 DESCRIPTION
44
45This 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
53This function initialize MondoRescue, point to the right conf files, setup stuff
54It takes 1 parameter, the message to print if needed
55
56=cut
57
58sub mr_init {
59
60my $msg = shift || "";
61
62if (defined $msg) {
63 pb_log($pbdebug,$msg);
64}
65
66
67# Get the various location determined at installation time
68my ($etcdir,$pbproj) = mr_dynconf_init();
69
70# Temp dir
71pb_temp_init();
72
73# First use the main configuration file
74pb_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#
80open(MD5,"$etcdir/$pbproj.conf.dist.md5") || die "Unable to read mandatory $etcdir/$pbproj.conf.dist.md5: $!";
81my $omd5 = <MD5>;
82chop($omd5);
83close(MD5);
84open(CONF,"$etcdir/$pbproj.conf.dist") || die "Unable to read mandatory $etcdir/$pbproj.conf.dist: $!";
85my $md5 = Digest::MD5->new;
86binmode(CONF);
87$md5->addfile(CONF);
88die "Invalid MD5 found sum for $etcdir/$pbproj.conf.dist: $md5->hexdigest" if ($omd5 ne $md5->hexdigest);
89close(CONF);
90
91pb_conf_add("$etcdir/$pbproj.conf.dist");
92}
93
94=item B<mr_exit>
95
96This function closes opened files, clean up the environment and exits MondoRescue
97It takes 2 parameters, the exit code, and the message to print if needed
98
99=cut
100
101sub mr_exit {
102
103my $code = shift;
104my $msg = shift;
105
106if (defined $msg) {
107 pb_log($pbdebug,$msg);
108}
109# CLoses log
110if (defined $mr->{'logdesc'}) {
111 close($mr->{'logdesc'});
112}
113die "ERROR returned\n" if ($code < 0);
114exit($code);
115}
116
117=item B<mr_conf_get>
118
119This 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.
120It takes a list of parameters to find and returns the values corresponding
121
122=cut
123
124
125sub 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}
Note: See TracBrowser for help on using the repository browser.