Changeset 51 for devel/dploy-common/lib/Dploy/Base.pm
- Timestamp:
- 05/02/08 16:00:33 (4 years ago)
- File:
-
- 1 edited
-
devel/dploy-common/lib/Dploy/Base.pm (modified) (2 diffs)
Legend:
- Unmodified
- Added
- Removed
-
devel/dploy-common/lib/Dploy/Base.pm
r48 r51 12 12 use Pod::Usage; 13 13 use English; 14 use ProjectBuilder::Base; 14 15 15 16 #use File::Basename; … … 30 31 # any code which uses this module. 31 32 32 our $debug = 0;33 our $LOG = \*STDOUT;33 our @ISA = qw(Exporter); 34 our @EXPORT = qw(dploy_check_mac dploy_check_ip); 34 35 35 our @ISA = qw(Exporter); 36 our @EXPORT = qw(dploy_syntax $debug $LOG); 36 sub dploy_check_mac { 37 37 38 sub dploy_syntax { 38 my $mac = shift; 39 39 40 # Internal mkdir -p function 41 sub dploy_mkdir_p { 42 my @dir = @_; 43 my $ret = mkpath(@dir, 0, 0755); 44 return($ret); 40 pb_syntax(-1,0) if (not defined $mac); 41 if ($mac !~ /^(?:[[:xdigit:]]{1,2}[-:]){5}[[:xdigit:]]{1,2}$/) { 42 print "Wrong MAC address\n"; 43 pb_syntax(-1,0); 44 } 45 # only lowercase for homogeneity in mac address 46 $mac =~ tr/A-Z/a-z/; 47 48 # newmac replaces : separator with - and only lowercase for dhcpd 49 my $newmac = $mac; 50 $newmac =~ s/:/-/g; 51 52 return($newmac); 45 53 } 46 54 47 # Internal rm -rf function 48 sub dploy_rm_rf { 49 my @dir = @_; 50 my $ret = rmtree(@dir, 0, 0); 51 return($ret); 55 sub dploy_check_ip { 56 57 my $ip = shift; 58 my $cmt = shift; 59 60 pb_syntax(-1,0) if (not defined $ip); 61 if ($ip !~ /^([1-9]?\d|1\d\d|2[0-4]\d|25[0-5])\.([1-9]?\d|1\d\d|2[0-4]\d|25[0-5])\.([1-9]?\d|1\d\d|2[0-4]\d|25[0-5])\.([1-9]?\d|1\d\d|2[0-4]\d|25[0-5])$/) { 62 print "$cmt\n"; 63 pb_syntax(-1,0); 64 } 52 65 } 53 66 54 # Internal system function55 sub dploy_system {56 57 my $cmd=shift;58 my $cmt=shift || $cmd;59 60 dploy_log(0,"$cmt... ");61 #system("$cmd 2>&1 > $ENV{'PBTMP'}/system.log");62 system($cmd);63 dploy_log(1,"Executing $cmd\n");64 my $res = $?;65 if ($res == -1) {66 dploy_log(0,"failed to execute ($cmd) : $!\n");67 dploy_display_file("$ENV{'PBTMP'}/system.log");68 } elsif ($res & 127) {69 dploy_log(0, "child ($cmd) died with signal ".($? & 127).", ".($? & 128) ? 'with' : 'without'." coredump\n");70 dploy_display_file("$ENV{'PBTMP'}/system.log");71 } elsif ($res == 0) {72 dploy_log(0,"OK\n");73 } else {74 dploy_log(0, "child ($cmd) exited with value ".($? >> 8)."\n");75 dploy_display_file("$ENV{'PBTMP'}/system.log");76 }77 return($res);78 }79 80 sub dploy_display_file {81 82 my $file=shift;83 84 return if (not -f $file);85 open(FILE,"$file");86 while (<FILE>) {87 print $_;88 }89 close(FILE);90 }91 92 # Function which returns a pointer on a table93 # corresponding to a set of values queried in the conf file94 # and test the returned vaue as they need to exist in that case95 sub dploy_conf_get {96 97 my @param = @_;98 my @return = dploy_conf_get_if(@param);99 100 die "No params found for $ENV{'PBPROJ'}" if (not @return);101 102 foreach my $i (0..$#param) {103 die "No $param[$i] defined for $ENV{'PBPROJ'}" if (not defined $return[$i]);104 }105 return(@return);106 }107 108 # Function which returns a pointer on a table109 # corresponding to a set of values queried in the conf file110 # Those value may be undef if they do not exist111 sub dploy_conf_get_if {112 113 my @param = @_;114 115 # Everything is returned via ptr1116 my @ptr1 = ();117 my @ptr2 = ();118 @ptr1 = dploy_conf_read_if("$ENV{'PBETC'}", @param) if (defined $ENV{'PBETC'});119 @ptr2 = dploy_conf_read_if("$ENV{'PBROOTDIR'}/$ENV{'PBPROJ'}.pb", @param) if ((defined $ENV{'PBROOTDIR'}) and (defined $ENV{'PBPROJ'}));120 121 my $p1;122 my $p2;123 124 dploy_log(2,"DEBUG: dploy_conf_get param1: ".Dumper(@ptr1)."\n");125 dploy_log(2,"DEBUG: dploy_conf_get param2: ".Dumper(@ptr2)."\n");126 127 foreach my $i (0..$#param) {128 $p1 = $ptr1[$i];129 $p2 = $ptr2[$i];130 # Always try to take the param from the home dir conf file in priority131 # in order to mask what could be defined under the CMS to allow for overloading132 if (not defined $p2) {133 # No ref in CMS project conf file so use the home dir one.134 $p1->{$ENV{'PBPROJ'}} = $p1->{'default'} if ((not defined $p1->{$ENV{'PBPROJ'}}) && (defined $p1->{'default'}));135 } else {136 # Ref found in CMS project conf file137 if (not defined $p1) {138 # No ref in home dir project conf file so use the CMS one.139 $p2->{$ENV{'PBPROJ'}} = $p2->{'default'} if ((not defined $p2->{$ENV{'PBPROJ'}}) && (defined $p2->{'default'}));140 $p1 = $p2;141 } else {142 # Both are defined - handling the overloading143 if (not defined $p1->{'default'}) {144 if (defined $p2->{'default'}) {145 $p1->{'default'} = $p2->{'default'};146 }147 }148 149 if (not defined $p1->{$ENV{'PBPROJ'}}) {150 if (defined $p2->{$ENV{'PBPROJ'}}) {151 $p1->{$ENV{'PBPROJ'}} = $p2->{$ENV{'PBPROJ'}} if (defined $p2->{$ENV{'PBPROJ'}});152 } else {153 $p1->{$ENV{'PBPROJ'}} = $p1->{'default'} if (defined $p1->{'default'});154 }155 }156 # Now copy back into p1 all p2 content which doesn't exist in p1157 # p1 content (local) always has priority over p2 (project)158 foreach my $k (keys %$p2) {159 $p1->{$k} = $p2->{$k} if (not defined $p1->{$k});160 }161 }162 }163 $ptr1[$i] = $p1;164 }165 dploy_log(2,"DEBUG: dploy_conf_get param ptr1: ".Dumper(@ptr1)."\n");166 return(@ptr1);167 }168 169 # Function which returns a pointer on a hash170 # corresponding to a declaration (arg2) in a conf file (arg1)171 # if that conf file doesn't exist returns undef172 sub dploy_conf_read_if {173 174 my $conffile = shift;175 my @param = @_;176 177 open(CONF,$conffile) || return((undef));178 close(CONF);179 return(dploy_conf_read($conffile,@param));180 }181 182 # Function which returns a pointer on a hash183 # corresponding to a declaration (arg2) in a conf file (arg1)184 sub dploy_conf_read {185 186 my $conffile = shift;187 my @param = @_;188 my $trace;189 my @ptr;190 my %h;191 192 open(CONF,$conffile) || die "Unable to open $conffile";193 while(<CONF>) {194 if (/^\s*([A-z0-9-_]+)\s+([[A-z0-9-_]+)\s*=\s*(.+)$/) {195 dploy_log(3,"DEBUG: 1:$1 2:$2 3:$3\n");196 $h{$1}{$2}=$3;197 }198 }199 close(CONF);200 201 for my $param (@param) {202 push @ptr,$h{$param};203 }204 return(@ptr);205 }206 207 # Analyze a url passed and return protocol, account, password, server, port, path208 sub dploy_get_uri {209 210 my $uri = shift || undef;211 212 dploy_log(2,"DEBUG: uri:$uri\n");213 # A URL has the format protocol://[ac@]host[:port][path[?query][#fragment]].214 # Cf man URI215 my ($scheme, $authority, $path, $query, $fragment) =216 $uri =~ m|(?:([^:/?#]+):)?(?://([^/?#]*))?([^?#]*)(?:\?([^#]*))?(?:#(.*))?| if (defined $uri);217 my ($account,$host,$port) = $authority =~ m|(?:([^\@]+)\@)?([^:]+)(:(?:[0-9]+))?| if (defined $authority);218 219 $scheme = "" if (not defined $scheme);220 $authority = "" if (not defined $authority);221 $path = "" if (not defined $path);222 $account = "" if (not defined $account);223 $host = "" if (not defined $host);224 $port = "" if (not defined $port);225 226 dploy_log(2,"DEBUG: scheme:$scheme ac:$account host:$host port:$port path:$path\n");227 return($scheme, $account, $host, $port, $path);228 }229 230 231 sub dploy_get_date {232 233 return(localtime->sec(), localtime->min(), localtime->hour(), localtime->mday(), localtime->mon(), localtime->year(), localtime->wday(), localtime->yday(), localtime->isdst());234 }235 236 sub dploy_log {237 238 my $dlevel = shift;239 my $msg = shift;240 241 print $LOG "$msg" if ($dlevel <= $debug);242 }243 244 sub dploy_syntax {245 246 my $exit_status = shift || -1;247 my $verbose_level = shift || 0;248 249 my $filehandle = \*STDERR;250 251 $filehandle = \*STDOUT if ($exit_status == 0);252 253 pod2usage( { -message => "Dploy.org Version PBVER-PBREV\n",254 -exitval => $exit_status ,255 -verbose => $verbose_level,256 -output => $filehandle } );257 }258 259 260 67 1;
Note: See TracChangeset
for help on using the changeset viewer.
