[3219] | 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 |
|
---|
| 10 | package MondoRescue::Base;
|
---|
| 11 |
|
---|
| 12 | use strict 'vars';
|
---|
| 13 | use Data::Dumper;
|
---|
| 14 | use English;
|
---|
| 15 | use File::Basename;
|
---|
| 16 | use File::Copy;
|
---|
| 17 | use POSIX qw(strftime);
|
---|
| 18 | use lib qw (lib);
|
---|
| 19 | use Digest::MD5;
|
---|
| 20 | use ProjectBuilder::Base;
|
---|
| 21 | use ProjectBuilder::Conf;
|
---|
| 22 | use MondoRescue::DynConf;
|
---|
| 23 |
|
---|
| 24 | # Inherit from the "Exporter" module which handles exporting functions.
|
---|
| 25 |
|
---|
| 26 | use Exporter;
|
---|
| 27 |
|
---|
| 28 | # Global hash for configuration params of mr
|
---|
| 29 | my %mr;
|
---|
| 30 | our $mr = \%mr;
|
---|
| 31 |
|
---|
| 32 | # Export, by default, all the functions into the namespace of
|
---|
| 33 | # any code which uses this module.
|
---|
| 34 |
|
---|
| 35 | our @ISA = qw(Exporter);
|
---|
| 36 | our @EXPORT = qw(mr_init mr_exit mr_conf_get $mr);
|
---|
| 37 |
|
---|
| 38 | =pod
|
---|
| 39 |
|
---|
| 40 | =head1 NAME
|
---|
| 41 |
|
---|
| 42 | MondoRescue::Base, part of the mondorescue.org
|
---|
| 43 |
|
---|
| 44 | =head1 DESCRIPTION
|
---|
| 45 |
|
---|
| 46 | This 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 |
|
---|
| 54 | This function initialize MondoRescue, point to the right conf files, setup stuff
|
---|
| 55 | It takes 1 parameter, the message to print if needed
|
---|
| 56 |
|
---|
| 57 | =cut
|
---|
| 58 |
|
---|
| 59 | sub mr_init {
|
---|
| 60 |
|
---|
| 61 | my $msg = shift || "";
|
---|
| 62 |
|
---|
| 63 | pb_log_init($pbdebug);
|
---|
| 64 |
|
---|
| 65 | if (defined $msg) {
|
---|
| 66 | pb_log($pbdebug,$msg);
|
---|
| 67 | }
|
---|
| 68 |
|
---|
| 69 |
|
---|
| 70 | my $pbproj;
|
---|
| 71 | # Get the various location determined at installation time
|
---|
| 72 | ($mr->{'confdir'},$pbproj) = mr_dynconf_init();
|
---|
| 73 |
|
---|
| 74 | # Temp dir
|
---|
| 75 | pb_temp_init();
|
---|
| 76 |
|
---|
| 77 | # First use the main configuration file
|
---|
| 78 | pb_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 | #
|
---|
| 85 | open(MD5,"$mr->{'confdir'}/$pbproj.conf.dist.md5") || die "Unable to read mandatory $mr->{'confdir'}/$pbproj.conf.dist.md5: $!";
|
---|
| 86 | my $omd5 = <MD5>;
|
---|
| 87 | close(MD5);
|
---|
| 88 | chomp($omd5);
|
---|
| 89 | # remove file name
|
---|
| 90 | $omd5 =~ s/ .*//;
|
---|
| 91 | open(CONF,"$mr->{'confdir'}/$pbproj.conf.dist") || die "Unable to read mandatory $mr->{'confdir'}/$pbproj.conf.dist: $!";
|
---|
| 92 | binmode(CONF);
|
---|
| 93 | my $md5 = Digest::MD5->new->addfile(*CONF)->hexdigest;
|
---|
| 94 | chomp($md5);
|
---|
| 95 | die "Invalid MD5 found sum for $mr->{'confdir'}/$pbproj.conf.dist: *$md5* instead of *$omd5*" if ($omd5 ne $md5);
|
---|
| 96 | close(CONF);
|
---|
| 97 |
|
---|
| 98 | pb_conf_add("$mr->{'confdir'}/$pbproj.conf.dist");
|
---|
| 99 | pb_conf_add("$mr->{'confdir'}/$pbproj.conf") if (-f "$mr->{'confdir'}/$pbproj.conf");
|
---|
| 100 |
|
---|
| 101 | my @date = pb_get_date();
|
---|
| 102 | $mr->{'start_date'} = strftime("%Y-%m-%d %H:%M:%S", @date);
|
---|
| 103 | }
|
---|
| 104 |
|
---|
| 105 | =item B<mr_exit>
|
---|
| 106 |
|
---|
| 107 | This function closes opened files, clean up the environment and exits MondoRescue
|
---|
| 108 | It takes 2 parameters, the exit code, and the message to print if needed
|
---|
| 109 |
|
---|
| 110 | =cut
|
---|
| 111 |
|
---|
| 112 | sub mr_exit {
|
---|
| 113 |
|
---|
| 114 | my $code = shift;
|
---|
| 115 | my $msg = shift;
|
---|
| 116 |
|
---|
| 117 | if (defined $msg) {
|
---|
| 118 | pb_log($pbdebug,$msg);
|
---|
| 119 | }
|
---|
| 120 | # CLoses log
|
---|
| 121 | if (defined $mr->{'logdesc'}) {
|
---|
| 122 | close($mr->{'logdesc'});
|
---|
| 123 | }
|
---|
| 124 | die "ERROR returned\n" if ($code lt 0);
|
---|
| 125 | exit($code);
|
---|
| 126 | }
|
---|
| 127 |
|
---|
| 128 | =item B<mr_conf_get>
|
---|
| 129 |
|
---|
| 130 | This 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.
|
---|
| 131 | It takes a list of parameters to find and returns the values corresponding
|
---|
| 132 |
|
---|
| 133 | =cut
|
---|
| 134 |
|
---|
| 135 |
|
---|
| 136 | sub 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 | }
|
---|