source: branches/3.3/MondoRescue/lib/MondoRescue/Base.pm @ 3644

Last change on this file since 3644 was 3644, checked in by Bruno Cornec, 4 years ago
  • Adds a new perl script (mr-getparam) to get parameters from mondorescue conf file (WIP)
  • Adds a new perl script (mr-distro-getparam) to get parameters from mondorescue conf file based on the current distribution
File size: 3.8 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-2014
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, $pbLOG);
64
65if (defined $msg) {
66    pb_log($pbdebug,$msg);
67}
68
69# Temp dir
70pb_temp_init($pbdebug);
71
72my $pbproj;
73# Get the various location determined at installation time
74($mr->{'confdir'},$pbproj) = mr_dynconf_init();
75
76# First setup the PBPRJ env var
77pb_conf_init($pbproj);
78
79#
80# Conf files Management
81# the $mr->{'confdir'}/mondorescue.conf.dist is delivered as part of the project and
82# its checksum is verified as we need good default values that we can trust
83#
84open(MD5,"$mr->{'confdir'}/$pbproj.conf.dist.md5") || die "Unable to read mandatory $mr->{'confdir'}/$pbproj.conf.dist.md5: $!";
85my $omd5 = <MD5>;
86close(MD5);
87chomp($omd5);
88# remove file name
89$omd5 =~ s/ .*//;
90open(CONF,"$mr->{'confdir'}/$pbproj.conf.dist") || die "Unable to read mandatory $mr->{'confdir'}/$pbproj.conf.dist: $!";
91binmode(CONF);
92my $md5 = Digest::MD5->new->addfile(*CONF)->hexdigest;
93chomp($md5);
94die "Invalid MD5 found sum for $mr->{'confdir'}/$pbproj.conf.dist: *$md5* instead of *$omd5*" if ($omd5 ne $md5);
95close(CONF);
96
97pb_conf_add("$mr->{'confdir'}/$pbproj.conf.dist");
98pb_conf_add("$mr->{'confdir'}/$pbproj.conf") if (-f "$mr->{'confdir'}/$pbproj.conf");
99
100my @date = pb_get_date();
101$mr->{'start_date'} = strftime("%Y-%m-%d %H:%M:%S", @date);
102}
103
104=item B<mr_exit>
105
106This function closes opened files, clean up the environment and exits MondoRescue
107It takes 2 parameters, the exit code, and the message to print if needed
108
109=cut
110
111sub mr_exit {
112
113my $code = shift;
114my $msg = shift;
115
116if (defined $msg) {
117    pb_log($pbdebug,$msg);
118}
119# CLoses log
120if (defined $mr->{'logdesc'}) {
121    close($mr->{'logdesc'});
122}
123die "ERROR returned\n" if ($code lt 0);
124exit($code);
125}
126
127=item B<mr_conf_get>
128
129This 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.
130It takes a list of parameters to find and returns the values corresponding
131
132=cut
133
134
135sub mr_conf_get {
136    my @params = @_;
137    my @ptr = ();
138    my $ptr;
139   
140    pb_log(2,"Entering mr_conf_get\n");
141    my @args1 = pb_conf_get_if(@params);
142    my @args2 = ();
143    my $proj = $ENV{'PBPROJ'};
144    if (defined $ENV{'PBPKG'}) {
145        $ENV{'PBPROJ'} = $ENV{'PBPKG'};
146        @args2 = pb_conf_get_if(@params);
147        $ENV{'PBPROJ'} = $proj;
148    }
149    foreach my $i (0..$#args1) {
150        $ptr = undef;
151        # Process from least important to more important
152        if (defined $args1[$i]->{'default'}) {
153            $ptr[$i] = $args1[$i]->{'default'};
154        }
155        if (defined $args1[$i]->{$ENV{'PBPROJ'}}) {
156            $ptr[$i] = $args1[$i]->{$ENV{'PBPROJ'}};
157        }
158        if ((defined $ENV{'PBPKG'}) and (defined $args2[$i]) and (defined $args2[$i]->{$ENV{'PBPKG'}})) {
159            $ptr[$i] = $args2[$i]->{$ENV{'PBPKG'}};
160        }
161        $ptr[$i] = "Undefined" if (not defined $ptr[$i]);
162        pb_log(2,"Found parameter $params[$i] with value $ptr[$i]\n");
163    }
164    return(@ptr);
165}
Note: See TracBrowser for help on using the repository browser.