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

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

r3911@wsip-70-165-196-181: bruno | 2010-06-22 03:34:20 +0200

  • First packaged version of a mrmin version begining to work (but doing nothing interesting yet ;-)
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}
109die "ERROR returned\n" if ($code < 0);
110exit($code);
111}
112
113=item B<mr_conf_get>
114
115This function get parameters in configuration files and returns from the least significant level (default) to the emost significant level (application name), passing by the project name.
116It takes a list of parameters to find and returns the values corresponding
117
118=cut
119
120
121sub mr_conf_get {
122 my @params = @_;
123 my @ptr = ();
124 my $ptr;
125
126 pb_log(2,"Entering mr_conf_get\n");
127 my @args1 = pb_conf_get_if(@params);
128 my $proj = $ENV{'PBPROJ'};
129 $ENV{'PBPROJ'} = $ENV{'PBPKG'};
130 my @args2 = pb_conf_get_if(@params);
131 foreach my $i (0..$#args1) {
132 $ptr = undef;
133 # Process from least important to more important
134 $ptr = $args1[$i]->{'default'};
135 $ptr[$i] = $ptr if (defined $ptr);
136 $ptr = $args1[$i]->{$ENV{'PBPROJ'}};
137 $ptr[$i] = $ptr if (defined $ptr);
138 $ptr = $args2[$i]->{$ENV{'PBPKG'}};
139 $ptr[$i] = $ptr if (defined $ptr);
140 $ptr[$i] = "Undefined" if (not defined $ptr[$i]);
141 pb_log(2,"Found parameter $params[$i] with value $ptr[$i]\n");
142 }
143 $ENV{'PBPROJ'} = $proj;
144 return(@ptr);
145}
Note: See TracBrowser for help on using the repository browser.