pf-tools commit: r794 [ccaillet-guest] - in /branches/next-gen: lib/PFTools/Host.pm lib/PFTools/Logger.pm lib/PFTools/Net.pm sbin/mk_grubopt sbin/mk_interfaces sbin/mk_privatezone sbin/mk_resolvconf sbin/mk_sourceslist

parmelan-guest at users.alioth.debian.org parmelan-guest at users.alioth.debian.org
Fri Jul 30 14:45:48 UTC 2010


Author: ccaillet-guest
Date: Fri Jul 30 14:45:44 2010
New Revision: 794

URL: http://svn.debian.org/wsvn/pf-tools/?sc=1&rev=794
Log:
mk_resolconf is done adding function Resolv_hostname_from_GLOBAL and Resolv which use Net::DNS

Removed:
    branches/next-gen/sbin/mk_privatezone
Modified:
    branches/next-gen/lib/PFTools/Host.pm
    branches/next-gen/lib/PFTools/Logger.pm
    branches/next-gen/lib/PFTools/Net.pm
    branches/next-gen/sbin/mk_grubopt
    branches/next-gen/sbin/mk_interfaces
    branches/next-gen/sbin/mk_resolvconf
    branches/next-gen/sbin/mk_sourceslist

Modified: branches/next-gen/lib/PFTools/Host.pm
URL: http://svn.debian.org/wsvn/pf-tools/branches/next-gen/lib/PFTools/Host.pm?rev=794&op=diff
==============================================================================
--- branches/next-gen/lib/PFTools/Host.pm (original)
+++ branches/next-gen/lib/PFTools/Host.pm Fri Jul 30 14:45:44 2010
@@ -31,8 +31,10 @@
 	Get_hosttype_from_hostname
 	Get_iface_vlan_from_hostname
 	Get_cmdline_from_hostprops
+	Get_zone_from_hostname
 	Add_server
 	Add_host
+	Resolv_hostname_from_GLOBAL
 );
 
 our @EXPORT_OK = qw();
@@ -44,6 +46,10 @@
 use PFTools::Net;
 use Data::Dumper;
 
+#########################################################################
+# Prototypes : needed by recursive calls
+
+sub Resolv_hostname_from_GLOBAL ($$;$$);
 
 #########################################################################
 #
@@ -120,6 +126,27 @@
 	return ( $num_last, $node_last );
 }
 
+sub __Get_hostnumber_from_model ($$$) {
+	my ( $model, $num, $node ) = @_;
+	my ( $digits, $nodes, $index );
+
+	if ( $model !~ /%+/ && $model !~ /_+/ ) {
+		return "";
+	}
+	$model	=~ /(%*)(_*)$/;
+	$digits	= length ($1) || 0;
+	$nodes	= length ($2) || 0;
+	$index	= "";
+	while ( $digits > length ( $num ) ) {
+		$index .= "0";
+		$digits--;
+	}
+	$index = ( $node )
+		? $index.$num.$node
+		: $index.$num;
+	return $index;
+}
+
 #########################################################################
 #
 # STR __Get_hostname_from_model ( STR, STR, STR, STR, HASHREF )
@@ -137,25 +164,12 @@
 #
 sub __Get_hostname_from_model ($$$$) {
 	my ( $hostname_model, $hostnum, $hostnode, $site_prefix, $ref_host ) = @_;
-	my ( $hostname, $digits, $nodes, $index );
-
-	$hostname = $hostname_model;
-	if ( $hostname !~ /%+/ && $hostname !~ /_+/ ) {
-		return $hostname;
-	}
-	$hostname_model	=~ /(%*)(_*)$/;
-	$digits			= length ($1) || 0;
-	$nodes			= length ($2) || 0;
-	$index			= "";
-	while ( $digits > length ( $hostnum ) ) {
-		$index .= "0";
-		$digits--;
-	}
-	$index = ( $hostnode )
-		? $index.$hostnum.$hostnode
-		: $index.$hostnum;
-	$hostname =~ s/(%*)(_*)$/$index/;
-	$hostname = $site_prefix.$hostname if ( $ref_host->{'prefix'} && $ref_host->{'prefix'} eq 'true' );
+	my ( $hostname, $index );
+
+	$hostname	= $hostname_model;
+	$index		= __Get_hostnumber_from_model ( $hostname_model, $hostnum, $hostnode );
+	$hostname	=~ s/(%*)(_*)$/$index/;
+	$hostname	= $site_prefix.$hostname if ( $ref_host->{'prefix'} && $ref_host->{'prefix'} eq 'true' );
 	return $hostname;
 }
 
@@ -192,6 +206,41 @@
 	}
 	return $site_list;
 }
+
+#########################################################################
+#
+# STR Get_zone_from_hostname ( STR, HASHREF, STR )
+#
+# This function adds build the site list for a given section
+# Inputs :
+#  - $site			: site name
+#  - $global_config	: hashref where is stored global configuration
+#
+# Output :
+#  Return a string containing the zone name for the specified site.
+#
+sub Get_zone_from_hostname ($$;$) {
+	my ( $hostname, $global_config, $site ) = @_;
+
+	if ( ! defined $site ) {
+		my $ref_list = Get_site_from_hostname ( $hostname, $global_config );
+		if ( ! defined $ref_list ) {
+			Warn ( $CODE->{'UNDEF_KEY'},
+				"Hostname ".$hostname." is not defined into global configuration" );
+			return undef;
+		}
+		elsif ( scalar @{$ref_list} > 1 ) {
+			Warn ( $CODE->{'DUPLICATE_VALUE'},
+				"Hostname ".$hostname." is defined on multiple sites into global configuration" );
+			return undef;
+		}
+		else {
+			( $site ) = @{$ref_list};
+		}
+	}
+	return $global_config->{'SITE'}->{'BY_NAME'}->{$site}->{'zone'};
+}
+
 
 #########################################################################
 #
@@ -492,6 +541,7 @@
 						.$srvfile." and for site ".$site." : skipping this hostname" );
 					next;
 				}
+				my $index	= __Get_hostnumber_from_model ( $srvname_model, $hostnum, $hostnode);
 				my $srv_number	= ( $hostnode ) ? $hostnum.$hostnode : $hostnum;
 				my $short_vlan	= $srv2add->{'shortname.'.$srv_number} || $srv2add->{'shortname'} || "";
 				my $iface_idx	= 0;
@@ -541,11 +591,13 @@
 						}
 						my $alias_list = __Get_alias_list_from_server ( $srv2add, $vlan, $srv_number );
 						foreach my $alias ( @{$alias_list} ) {
+							$site_part->{'HOST'}->{'BY_NAME'}->{$hostclass}->{$alias} = $shortname;
 							if ( ! defined $zone_part->{$hostclass}->{$alias.'.'.$vlan} ) {
 								$zone_part->{$hostclass}->{$alias} = "CNAME\t".$shortname.'.'.$vlan;
 							}
 							if ( $shortname ne $srvname ) {
-								$zone_part->{$hostclass}->{$alias.$srv_number.'.'.$vlan}	= "CNAME\t".$srvname.'.'.$vlan;
+								$zone_part->{$hostclass}->{$alias.$index}	= "CNAME\t".$srvname.'.'.$vlan;
+								$site_part->{'HOST'}->{'BY_NAME'}->{$hostclass}->{$alias.$index} = $srvname;
 							}
 						}
 					}
@@ -571,15 +623,15 @@
 #  - $ref_site		: hashref where are stored the site definition into global configuration
 #  - $pf_config		: hashref where are stored pf-tools configuration datas
 #
-sub __Add_host_interface ($$$$$$$$$) {
-	my ( $iface, $hostname, $hostnum, $hostnode, $ref_host, $ref_if_list, $site, $ref_site, $pf_config ) = @_;
+sub __Add_host_interface ($$$$$$$$$$) {
+	my ( $iface, $hostname, $hostnum, $hostnode, $index, $ref_host, $ref_if_list, $site, $ref_site, $pf_config ) = @_;
 	my ( @if_list, $vlan, $ifraw, $iftag, $add_if, $iface_opt );
 
 	my $network_site	= $ref_site->{'NETWORK'};
 	my $host_site		= $ref_site->{'HOST'};	
 	my $iface_section	= 'interface::'.$iface;
 	my $nodes			= $ref_host->{'hostgroup'}->{'nodes'} || 0;
-	my $host_number		= ( $hostnode ) ? $hostnum.$hostnode : $hostnum;
+ 	my $host_number		= ( $hostnode ) ? $hostnum.$hostnode : $hostnum;
 	$iface =~ /^((eth|bond)[\d]+)(\.(TAG[\d]+))?$/;
 	( $ifraw, $iftag )	= ( $1, $4 );
 	$vlan		= $ref_host->{$iface_section}->{'vlan.'.$host_number} || $ref_host->{$iface_section}->{'vlan'};
@@ -754,7 +806,8 @@
 						.$hostfile." and for site ".$site." : skipping this hostname" );
 					next;
 				}
-				my $host_number = ( $hostnode ) ? $hostnum.$hostnode : $hostnum;
+				my $index		= __Get_hostnumber_from_model ( $hostname_model, $hostnum, $hostnode);
+				my $host_number	= ( $hostnode ) ? $hostnum.$hostnode : $hostnum;
 				# Checking path for PXE elements kernel, initrd ...
 				foreach my $key ( 'pxefilename', 'kernel', 'initrd', 'kerneluml', 'initrduml', 'console', 'cmdline' ) {
 					my $value;
@@ -790,7 +843,7 @@
 				# Check interfaces
 				my @if_list = __Get_host_interfaces ($host2add);
 				foreach my $iface ( @if_list ) {
-					my $if2add = __Add_host_interface ( $iface, $hostname, $hostnum, $hostnode, $host2add, \@if_list, $site, $site_part, $pf_config );
+					my $if2add = __Add_host_interface ( $iface, $hostname, $hostnum, $hostnode, $host_number, $host2add, \@if_list, $site, $site_part, $pf_config );
 					my $iface_name = $iface;
 					if ( $iface =~ /^((eth|bond)[\d]+)(\.(TAG[\d]+))$/ ) {
 						$iface_name = $1.'.'.__Get_vlan_tag_from_site ( $if2add->{'vlan'}, $site, $global_config );
@@ -809,6 +862,7 @@
 						my $zone_key	= ( $ip_type eq 'ipv6' ) ? 'ZONE6' : 'ZONE';
 						my $dhcp_key	= ( $ip_type eq 'ipv6' ) ? 'DHCP6' : 'DHCP';
 						my $suffix		= ( $ip_type eq 'ipv6' ) ? '6' : '';
+						my $zone_ip		= $if2add->{$ip_type}; $zone_ip =~ s/\/.+$//;
 						my $zone_part	= $global_config->{$zone_key}->{'BY_NAME'}->{$zone}->{'BY_SITE'}->{$site};
 						my $dhcp_part	= $global_config->{$dhcp_key}->{'BY_SITE'}->{$site};
 						$site_part->{'HOST'}->{$addr_key}->{$if2add->{$ip_type}} = $hostname.'.'.$if2add->{'vlan'};
@@ -816,18 +870,21 @@
 							$zone_part->{$hostclass} = {};
 							push ( @{$global_config->{$zone_key}->{'BY_NAME'}->{$zone}->{'__hostclass_order'}->{$site}}, $hostclass );
 						}
-						$zone_part->{$hostclass}->{$hostname.'.'.$if2add->{'vlan'}}		= "A\t".$if2add->{$ip_type};
+						$zone_part->{$hostclass}->{$hostname.'.'.$if2add->{'vlan'}}		= "A\t".$zone_ip;
 						my $shortname_vlan	= $host2add->{'dns'}->{'shortname.'.$host_number} || $host2add->{'dns'}->{'shortname'} || "";
 						if ( $shortname_vlan eq $if2add->{'vlan'} ) {
-							push ( @{$zone_part->{$hostclass}->{$shortname.'.'.$if2add->{'vlan'}}}, "A\t".$if2add->{$ip_type} );
+							push ( @{$zone_part->{$hostclass}->{$shortname.'.'.$if2add->{'vlan'}}}, "A\t".$zone_ip );
 							$zone_part->{$hostclass}->{$shortname} = "CNAME\t".$shortname.'.'.$if2add->{'vlan'};
 						}
 						foreach my $key ( keys %{$host2add->{'dns'}} ) {
 							next if ( $key !~ /^alias/ );
-							my ( $key_type, $alias, $host_num ) = split ( /\./, $key );
+							my ( $key_type, $alias, $host_num )	= split ( /\./, $key );
+							$host_part->{$alias}				= $shortname if ( ! defined $host_part->{$alias} );
+							$host_part->{$alias.$index}			= $hostname;
 							if ( $host2add->{'dns'}->{$key} eq $if2add->{'vlan'} ) {
-								$zone_part->{$hostclass}->{$alias.'.'.$if2add->{'vlan'}}				= "CNAME\t".$shortname.'.'.$if2add->{'vlan'};
-								$zone_part->{$hostclass}->{$alias.$host_number.'.'.$if2add->{'vlan'}}	= "CNAME\t".$hostname.'.'.$if2add->{'vlan'};
+								$zone_part->{$hostclass}->{$alias}			= "CNAME\t".$shortname.'.'.$if2add->{'vlan'};
+								$zone_part->{$hostclass}->{$alias.$index}	= "CNAME\t".$hostname.'.'.$if2add->{'vlan'};
+								
 							}
 						}
 						my $resolver = $host2add->{'dns'}->{'resolver.'.$host_number} || $host2add->{'dns'}->{'resolver'};
@@ -873,6 +930,56 @@
 	return ( $cmdline, $bond_cmdline );
 }
 
+sub Resolv_hostname_from_GLOBAL ($$;$$) {
+	my ( $hostname, $global_config, $site, $hosttype ) = @_;
+	my $resolved = [];
+
+	if ( ! defined $site ) {
+		my $ref_site_list = Get_site_from_hostname ( $hostname, $global_config );
+		if ( ! defined $ref_site_list ) {
+			Warn ( $CODE->{'UNDEF_KEY'},
+				"Hostname ".$hostname." is not defined into the global configuration" );
+			return undef;
+		}
+		elsif ( scalar @{$ref_site_list} > 1 ) {
+			Warn ( $CODE->{'DUPLICATE_VALUE'},
+				"Hostname ".$hostname." is defined into multiple sites : unable to choose the right one" );
+		}
+		else {
+			( $site ) = @{$ref_site_list};
+		}
+	}
+
+	my $zone	= Get_zone_from_hostname ( $hostname, $global_config, $site );
+	$hostname	=~ s/\.$zone$//;
+	if ( ! defined $hosttype ) {
+		$hosttype = Get_hosttype_from_hostname ( $hostname, $global_config, $site );
+		return undef if ( ! defined $hosttype );
+	}
+	my $zone_part = $global_config->{'ZONE'}->{'BY_NAME'}->{$zone}->{'BY_SITE'}->{$site};
+	foreach my $entry ( keys %{$zone_part->{$hosttype}} ) {
+		next if ( $entry !~ /^$hostname$/ );
+		my @fields;
+		if ( ref ( $zone_part->{$hosttype}->{$entry} ) eq 'ARRAY' ) {
+			@fields = @{$zone_part->{$hosttype}->{$entry}}
+		}
+		else {
+			@fields = ( $zone_part->{$hosttype}->{$entry} );
+		}
+		foreach my $line ( @fields ) {
+			my ( $type, $field ) = split ( /\s+/, $line );
+			if ( $type eq 'A' ) {
+				push ( @{$resolved}, $field );
+			}
+			elsif ( $type eq 'CNAME' ) {
+				my $cname_resolved = Resolv_hostname_from_GLOBAL ( $field, $global_config, $site, $hosttype );
+				push ( @{$resolved}, @{$cname_resolved} );
+			}
+		}
+	}
+	return $resolved;
+}
+
 #### BACKWARD Compatibility
 sub Get_Host_Props ($$;$) {
     my ( $hostname, $global_config, $site ) = @_ ;

Modified: branches/next-gen/lib/PFTools/Logger.pm
URL: http://svn.debian.org/wsvn/pf-tools/branches/next-gen/lib/PFTools/Logger.pm?rev=794&op=diff
==============================================================================
--- branches/next-gen/lib/PFTools/Logger.pm (original)
+++ branches/next-gen/lib/PFTools/Logger.pm Fri Jul 30 14:45:44 2010
@@ -57,6 +57,7 @@
 $CODE->{'RIGHTS'}			= 4;
 $CODE->{'UNLINK'}			= 5;
 $CODE->{'EXEC'}				= 6;
+$CODE->{'BIND_QUERY'}		= 7;
 
 $CODE->{'INVALID_CONTEXT'}	= 10;
 $CODE->{'INVALID_SECTNAME'}	= 11;

Modified: branches/next-gen/lib/PFTools/Net.pm
URL: http://svn.debian.org/wsvn/pf-tools/branches/next-gen/lib/PFTools/Net.pm?rev=794&op=diff
==============================================================================
--- branches/next-gen/lib/PFTools/Net.pm (original)
+++ branches/next-gen/lib/PFTools/Net.pm Fri Jul 30 14:45:44 2010
@@ -34,7 +34,6 @@
 	Add_site
 	Add_zone
 	Get_site_list
-	Get_site_zone_from_GLOBAL
 	Get_netblock_from_vlan
 );
 
@@ -46,6 +45,7 @@
 use PFTools::Logger;
 use PFTools::Parser;
 use NetAddr::IP;
+use Net::DNS;
 
 use Data::Dumper;
 #$Data::Dumper::Sortkeys = 1;
@@ -897,6 +897,25 @@
 	}
 }
 
+sub Resolv ($) {
+	my ( $hostname ) = @_;
+	my $resolved = [];
+
+	my $res   = Net::DNS::Resolver->new;
+	my $query = $res->search( $hostname );
+
+	if ($query) {
+		foreach my $rr ( $query->answer ) {
+			next unless $rr->type eq "A";
+			push ( @{$resolved}, $rr->address );
+		}
+	} else {
+		Warn ( $CODE->{'BIND_QUERY'},
+			"Query failed: ".$res->errorstring );
+		return undef;
+	}
+	return $resolved;
+}
 
 #
 #	STRING/ARRAY Resolv (STRING $host, HASHREF $Z)
@@ -904,73 +923,73 @@
 #	Resout un nom
 #
 #======================================================================================
-sub Resolv {
-    my ( $host, $Z ) = @_;
-
-    my $i;
-    my $j;
-
-    my $host2 = $host ;
-    $host2 =~ s/\.$Z->{SOA}->{name}// ;
-    my ( $hostshort, $hostvlan ) = split ( /\./, $host2 ) ;
-    if ( ! defined ) { $hostvlan = "" ; }
-    my $M = Get_Host_Props ( $Z, $host2 ) ;
-
-    if ( defined( $M->{'zone'}->{$host2}->{'FIELD'} ) ) {
-	if ( $M->{'zone'}->{$host2}->{'TYPE'} eq "CNAME" ) {
-	    return (
-		Resolv(
-		    $M->{'zone'}->{$host2}->{'FIELD'} . '.'
-			. $Z->{'SOA'}->{'name'},
-		    $Z
-		)
-	    );
-	}
-	else {
-	    return ( $M->{'zone'}->{$host2}->{'FIELD'} );
-	}
-    }
-
-    foreach $i ( keys %{ $Z->{'SERVERS'}->{'BY_NAME'} } ) {
-	my $N2 = $Z->{'SERVERS'}->{'BY_NAME'}->{$i};
-	foreach $j ( keys %{ $N2->{'SRVLIST'} } ) {
-	    my $M2 = $N2->{'SRVLIST'}->{$j};
-	    if ( defined( $M2->{'zone'}->{$host2}->{'FIELD'} ) ) {
-		if ( $M2->{'zone'}->{$host2}->{'TYPE'} eq "CNAME" ) {
-		    return (
-			Resolv(
-			    $M2->{'zone'}->{$host2}->{'FIELD'} . '.'
-				. $Z->{'SOA'}->{'name'},
-			    $Z
-			)
-		    );
-		}
-		else {
-		    return ( $M2->{'zone'}->{$host2}->{'FIELD'} );
-		}
-	    }
-	}
-    }
-
-    if ( defined( $Z->{'ALIAS'}->{$host2} ) ) {
-	if (wantarray) {
-	    return ( @{ $Z->{'ALIAS'}->{$host2} } );
-	}
-	else {
-	    return ( $Z->{'ALIAS'}->{$host2}
-		    [ int( rand( $#{ $Z->{'ALIAS'}->{$host2} } + 1 ) ) ] );
-	}
-    }
-
-    if (   defined($hostvlan)
-	&& $hostvlan ne ""
-	&& defined( $Z->{'NETWORK'}->{'BY_NAME'}->{$hostvlan} ) )
-    {
-	return ( $Z->{'NETWORK'}->{'BY_NAME'}->{$hostvlan}->{$hostshort} );
-    }
-
-    return undef;
-}
+# sub Resolv {
+#     my ( $host, $Z ) = @_;
+# 
+#     my $i;
+#     my $j;
+# 
+#     my $host2 = $host ;
+#     $host2 =~ s/\.$Z->{SOA}->{name}// ;
+#     my ( $hostshort, $hostvlan ) = split ( /\./, $host2 ) ;
+#     if ( ! defined ) { $hostvlan = "" ; }
+#     my $M = Get_Host_Props ( $Z, $host2 ) ;
+# 
+#     if ( defined( $M->{'zone'}->{$host2}->{'FIELD'} ) ) {
+# 	if ( $M->{'zone'}->{$host2}->{'TYPE'} eq "CNAME" ) {
+# 	    return (
+# 		Resolv(
+# 		    $M->{'zone'}->{$host2}->{'FIELD'} . '.'
+# 			. $Z->{'SOA'}->{'name'},
+# 		    $Z
+# 		)
+# 	    );
+# 	}
+# 	else {
+# 	    return ( $M->{'zone'}->{$host2}->{'FIELD'} );
+# 	}
+#     }
+# 
+#     foreach $i ( keys %{ $Z->{'SERVERS'}->{'BY_NAME'} } ) {
+# 	my $N2 = $Z->{'SERVERS'}->{'BY_NAME'}->{$i};
+# 	foreach $j ( keys %{ $N2->{'SRVLIST'} } ) {
+# 	    my $M2 = $N2->{'SRVLIST'}->{$j};
+# 	    if ( defined( $M2->{'zone'}->{$host2}->{'FIELD'} ) ) {
+# 		if ( $M2->{'zone'}->{$host2}->{'TYPE'} eq "CNAME" ) {
+# 		    return (
+# 			Resolv(
+# 			    $M2->{'zone'}->{$host2}->{'FIELD'} . '.'
+# 				. $Z->{'SOA'}->{'name'},
+# 			    $Z
+# 			)
+# 		    );
+# 		}
+# 		else {
+# 		    return ( $M2->{'zone'}->{$host2}->{'FIELD'} );
+# 		}
+# 	    }
+# 	}
+#     }
+# 
+#     if ( defined( $Z->{'ALIAS'}->{$host2} ) ) {
+# 	if (wantarray) {
+# 	    return ( @{ $Z->{'ALIAS'}->{$host2} } );
+# 	}
+# 	else {
+# 	    return ( $Z->{'ALIAS'}->{$host2}
+# 		    [ int( rand( $#{ $Z->{'ALIAS'}->{$host2} } + 1 ) ) ] );
+# 	}
+#     }
+# 
+#     if (   defined($hostvlan)
+# 	&& $hostvlan ne ""
+# 	&& defined( $Z->{'NETWORK'}->{'BY_NAME'}->{$hostvlan} ) )
+#     {
+# 	return ( $Z->{'NETWORK'}->{'BY_NAME'}->{$hostvlan}->{$hostshort} );
+#     }
+# 
+#     return undef;
+# }
 
 
 if ( `grep -e '^host[ 	]*:' /proc/cpuinfo 2>/dev/null` ne "" ) {
@@ -994,86 +1013,86 @@
 # Output	: return an hashref with the parsed routing table inside which is equivalent to 
 #	$ref_network->{'SERVERS'}->{'BY_NAME'}->{<HOSTTYPE>}->{'SRVLIST'}->{<HOSTNAME>}->{'route'}
 #	UNDEF if an error occured
-sub Parse_routing_table ($) {
-	my ( $ref_network ) = @_ ;
-
-	my $parsed_rt = {} ;
-	
-	unless ( open ( RT, "/sbin/ip route |" ) ) {
-		Warn ( $ERR_OPEN, "Unable to parse local routing table on host" ) ;
-		return undef ;
-	}
-	while ( <RT> ) {
-		chomp ;
-		next if ( /proto kernel/ ) ;
-		m/^([\S]+)\s*(via\s*([\S]+))?\s*(dev\s*([\S]+)).*$/ ;
-		my ( $dest, $gw, $dev ) = ( $1, $3, $5 ) ;
-		$dest =~ s/^([^\/]+)\/[\d]+$/$1/ ;
-		# Convert network into his configuration name
-		$dest = $ref_network->{'NETWORK'}->{'BY_ADDR'}->{$dest}->{'name'} if ( $dest ne 'default' ) ;
-		if ( ! defined $parsed_rt->{$dev} ) {
-			$parsed_rt->{$dev}->{'route1'} = ( defined $gw ) ? $dest." ".$gw : $dest ;
-		}
-		else {
-			my $card = scalar ( keys %{$parsed_rt->{$dev}} ) + 1 ;
-			$parsed_rt->{$dev}->{'route'.$card} = ( defined $gw ) ? $dest." ".$gw : $dest ;
-		}
-	}
-	close ( RT ) ;
-	return $parsed_rt
-}
-
-sub Cmp_routing_table ($$$) {
-	my ( $host, $local_rt, $ref_network ) = @_ ;
-
-	my $result = {} ;
-	my $srv_props	= Get_Host_Props ( $ref_network, $host ) ;
-	if ( ! defined $srv_props ) {
-		Warn ( $ERR_OPEN, "Unable to retrieve ".$host." properties in CVS configuration" ) ;
-		return undef ;
-	}
-	my $cvs_rt	= $srv_props->{'route'} ;
-	# Check for unknown or manual defined routes
-	foreach my $iface ( keys %{$local_rt} ) {
-		foreach my $rt ( keys %{$local_rt->{$iface}} ) {
-			my $exist = 0 ;
-			foreach my $r ( keys %{$cvs_rt->{$iface}} ) {
-				my $route = $cvs_rt->{$iface}->{$r} ;
-				my ( $dst, $via ) = split ( /\s+/, $route ) ;
-				if ( defined $via ) {
-					$via = Resolv ( $via, $ref_network ) ;
-					$route = $dst." ".$via ;
-				}
-				$exist = 1 if ( $route eq $local_rt->{$iface}->{$rt} ) ;
-			}
-			if ( ! $exist ) {
-				$result->{'err'}++ ;
-				push ( @{$result->{'unknown'}}, "Route ".$local_rt->{$iface}->{$rt}." not defined in CVS configuration\n" ) ;
-			}
-		}
-	}
-	# Check for deleted routes
-	foreach my $iface ( keys %{$cvs_rt} ) {
-		foreach my $rc ( keys %{$cvs_rt->{$iface}} ) {
-			my $exist = 0 ;
-			my $route = $cvs_rt->{$iface}->{$rc} ;
-			my ( $dst, $via ) = split ( /\s+/, $route ) ;
-			if ( defined $via ) {
-				$via = Resolv ( $via, $ref_network ) ;
-				$route = $dst." ".$via ;
-			}
-			foreach my $r ( keys %{$local_rt->{$iface}} ) {
-				$exist = 1 if ( $route eq $local_rt->{$iface}->{$r} ) ;
-				last if ( $exist ) ;
-			}
-			if ( ! $exist ) {
-				$result->{'err'}++ ;
-				push ( @{$result->{'undef'}}, "Route ".$cvs_rt->{$iface}->{$rc}." not defined in local routing table\n" ) ;
-			}
-		}
-	}
-	return $result ;
-}
+# sub Parse_routing_table ($) {
+# 	my ( $ref_network ) = @_ ;
+# 
+# 	my $parsed_rt = {} ;
+# 	
+# 	unless ( open ( RT, "/sbin/ip route |" ) ) {
+# 		Warn ( $ERR_OPEN, "Unable to parse local routing table on host" ) ;
+# 		return undef ;
+# 	}
+# 	while ( <RT> ) {
+# 		chomp ;
+# 		next if ( /proto kernel/ ) ;
+# 		m/^([\S]+)\s*(via\s*([\S]+))?\s*(dev\s*([\S]+)).*$/ ;
+# 		my ( $dest, $gw, $dev ) = ( $1, $3, $5 ) ;
+# 		$dest =~ s/^([^\/]+)\/[\d]+$/$1/ ;
+# 		# Convert network into his configuration name
+# 		$dest = $ref_network->{'NETWORK'}->{'BY_ADDR'}->{$dest}->{'name'} if ( $dest ne 'default' ) ;
+# 		if ( ! defined $parsed_rt->{$dev} ) {
+# 			$parsed_rt->{$dev}->{'route1'} = ( defined $gw ) ? $dest." ".$gw : $dest ;
+# 		}
+# 		else {
+# 			my $card = scalar ( keys %{$parsed_rt->{$dev}} ) + 1 ;
+# 			$parsed_rt->{$dev}->{'route'.$card} = ( defined $gw ) ? $dest." ".$gw : $dest ;
+# 		}
+# 	}
+# 	close ( RT ) ;
+# 	return $parsed_rt
+# }
+# 
+# sub Cmp_routing_table ($$$) {
+# 	my ( $host, $local_rt, $ref_network ) = @_ ;
+# 
+# 	my $result = {} ;
+# 	my $srv_props	= Get_Host_Props ( $ref_network, $host ) ;
+# 	if ( ! defined $srv_props ) {
+# 		Warn ( $ERR_OPEN, "Unable to retrieve ".$host." properties in CVS configuration" ) ;
+# 		return undef ;
+# 	}
+# 	my $cvs_rt	= $srv_props->{'route'} ;
+# 	# Check for unknown or manual defined routes
+# 	foreach my $iface ( keys %{$local_rt} ) {
+# 		foreach my $rt ( keys %{$local_rt->{$iface}} ) {
+# 			my $exist = 0 ;
+# 			foreach my $r ( keys %{$cvs_rt->{$iface}} ) {
+# 				my $route = $cvs_rt->{$iface}->{$r} ;
+# 				my ( $dst, $via ) = split ( /\s+/, $route ) ;
+# 				if ( defined $via ) {
+# 					$via = Resolv ( $via, $ref_network ) ;
+# 					$route = $dst." ".$via ;
+# 				}
+# 				$exist = 1 if ( $route eq $local_rt->{$iface}->{$rt} ) ;
+# 			}
+# 			if ( ! $exist ) {
+# 				$result->{'err'}++ ;
+# 				push ( @{$result->{'unknown'}}, "Route ".$local_rt->{$iface}->{$rt}." not defined in CVS configuration\n" ) ;
+# 			}
+# 		}
+# 	}
+# 	# Check for deleted routes
+# 	foreach my $iface ( keys %{$cvs_rt} ) {
+# 		foreach my $rc ( keys %{$cvs_rt->{$iface}} ) {
+# 			my $exist = 0 ;
+# 			my $route = $cvs_rt->{$iface}->{$rc} ;
+# 			my ( $dst, $via ) = split ( /\s+/, $route ) ;
+# 			if ( defined $via ) {
+# 				$via = Resolv ( $via, $ref_network ) ;
+# 				$route = $dst." ".$via ;
+# 			}
+# 			foreach my $r ( keys %{$local_rt->{$iface}} ) {
+# 				$exist = 1 if ( $route eq $local_rt->{$iface}->{$r} ) ;
+# 				last if ( $exist ) ;
+# 			}
+# 			if ( ! $exist ) {
+# 				$result->{'err'}++ ;
+# 				push ( @{$result->{'undef'}}, "Route ".$cvs_rt->{$iface}->{$rc}." not defined in local routing table\n" ) ;
+# 			}
+# 		}
+# 	}
+# 	return $result ;
+# }
 
 1;
 

Modified: branches/next-gen/sbin/mk_grubopt
URL: http://svn.debian.org/wsvn/pf-tools/branches/next-gen/sbin/mk_grubopt?rev=794&op=diff
==============================================================================
--- branches/next-gen/sbin/mk_grubopt (original)
+++ branches/next-gen/sbin/mk_grubopt Fri Jul 30 14:45:44 2010
@@ -168,6 +168,10 @@
 }
 
 my $host_props	= Get_host_config_from_CONFIG ( $HOSTNAME, $GLOBAL_STRUCT, $SITE );
+if ( ! defined $host_props ) {
+	Abort ( $CODE->{'UNDEF_KEY'},
+		"Unable to find hostname ".$HOSTNAME." on site ".$SITE." : no such host definition" );
+}
 my $mode		= $host_props->{'deployment'}->{'mode'};
 my ( $cmdline, $bond_cmdline ) = Get_cmdline_from_hostprops ( $host_props );
 $GRUB_VERSION = "" if ( $GRUB_VERSION == 1 );

Modified: branches/next-gen/sbin/mk_interfaces
URL: http://svn.debian.org/wsvn/pf-tools/branches/next-gen/sbin/mk_interfaces?rev=794&op=diff
==============================================================================
--- branches/next-gen/sbin/mk_interfaces (original)
+++ branches/next-gen/sbin/mk_interfaces Fri Jul 30 14:45:44 2010
@@ -75,6 +75,11 @@
 
 	my $resolve		= 0;
 	my $properties	= Get_host_config_from_CONFIG ( $hostname, $global_config, $site );
+	if ( ! defined $properties ) {
+		Warn ( $CODE->{'UNDEF_KEY'},
+			"Unable to find hostname ".$HOSTNAME." on site ".$SITE." : no such host definition" );
+		return undef;
+	}
 	my $hostclass	= $properties->{'deployment'}->{'hosttype'};
 	my $interfaces	= {};
 	my $routes		= {};
@@ -208,7 +213,10 @@
 }
 
 my $iface = Mk_interfaces ( $HOSTNAME, $GLOBAL_STRUCT, $PF_CONFIG, $SITE );
-
+if ( ! defined $iface ) {
+	Abort ( $CODE->{'EXEC'},
+		"An error occured during building interfaces file ".$OUTPUT_FILE );
+}
 unless ( open ( IFACE, ">".$OUTPUT_FILE ) ) {
 	die "Unable to open interface file ".$OUTPUT_FILE."\n";
 }

Modified: branches/next-gen/sbin/mk_resolvconf
URL: http://svn.debian.org/wsvn/pf-tools/branches/next-gen/sbin/mk_resolvconf?rev=794&op=diff
==============================================================================
--- branches/next-gen/sbin/mk_resolvconf (original)
+++ branches/next-gen/sbin/mk_resolvconf Fri Jul 30 14:45:44 2010
@@ -23,23 +23,131 @@
 use strict;
 use warnings;
 
-use PFTools::Net;
-use PFTools::Update;
+use Getopt::Long qw( :config ignore_case_always bundling );
+use PFTools::Conf;
+use PFTools::Host;
+use PFTools::Logger;
+use Net::DNS;
+use Sys::Hostname;
 
-my ($config, $host, $dst) = @ARGV;
-unless ($config and $host and $dst) {
-    die "Usage: $0 config host dest\n";
+#################################
+# VARS
+my $HELP				= 0;
+my $HOSTNAME			= hostname;
+my $SITE				= '';
+my $GLOBAL_STORE_FILE	= '';
+my $PF_CONFIG_FILE		= '';
+my $PF_CONFIG			= {};
+my $OUTPUT_FILE			= '/etc/resolv.conf';
+my $GLOBAL_STRUCT		= {};
+
+my $program = $0;
+$program =~ s%.*/%%; # cheap basename
+
+my $version = sprintf( "svn-r%s", q$Revision$ =~ /([\d.]+)/ );
+
+###################################
+# Funtions
+
+sub Do_help {
+    print STDERR << "# ENDHELP";
+    $program - version $version
+
+Usage:	$0 [options]
+	--help		: print help and exit
+	-h --host	: hostname for which we want to build interfaces file
+	-s --site	: site on which hostname is defined (optional)
+	-c --config	: file where pf-tools configuration is stored e.g. /etc/pf-tools.conf (optional)
+	--store		: file where global structure datas are in storable format (optional)
+	-o --output	: output file default value is /etc/network/interfaces
+    
+# ENDHELP
 }
 
-my $Z   = Init_lib_net( Get_source($config) );
-my @dns = Get_dns_from_hostname( $Z, $host );
-
-open OUT, ">$dst" or die "open: $dst: $!\n";;
-
-print OUT "search " . $Z->{SOA}->{name} . "\n";
-foreach my $dns (@dns) {
-    print OUT "nameserver $dns\n" if $dns;
+sub Mk_resolvconf ($$) {
+	my ( $host_props, $zone )
 }
 
-close OUT;
+##################################
+### MAIN
 
+GetOptions (
+	'help'			=> \$HELP,
+	'host|h=s'		=> \$HOSTNAME,
+	'site|s=s'		=> \$SITE,
+	'config|c=s'	=> \$PF_CONFIG_FILE,
+	'store=s'		=> \$GLOBAL_STORE_FILE,
+	'output|o=s'	=> \$OUTPUT_FILE
+) or die "Didn't grok options (see --help).\n";
+
+if ( $HELP ) {
+	Do_help ();
+	exit 0;
+}
+
+if ( $PF_CONFIG_FILE ne '' ) {
+	if ( ! -e $PF_CONFIG_FILE ) {
+		Abort ( $CODE->{'OPEN'},
+			"Unable to open configuration file ".$PF_CONFIG_FILE." : no such file or directory" );
+	}
+	$PF_CONFIG = Init_PF_CONFIG ( $PF_CONFIG_FILE );
+}
+else {
+	$PF_CONFIG = Init_PF_CONFIG ();
+}
+
+$GLOBAL_STORE_FILE = $PF_CONFIG->{'path'}->{'global_struct'} if ( $GLOBAL_STORE_FILE eq '' );
+$GLOBAL_STRUCT = Retrieve_GLOBAL ( $GLOBAL_STORE_FILE );
+
+if ( ! defined $GLOBAL_STRUCT ) {
+	Abort ( $CODE->{'UNDEF_KEY'},
+		"An error occured during retrieve from the storable file ".$GLOBAL_STORE_FILE );
+}
+
+if ( $SITE eq '' && ! defined $PF_CONFIG->{'location'}->{'site'} ) {
+	my $site_list = Get_site_from_hostname ( $HOSTNAME, $GLOBAL_STRUCT );
+	if ( ! defined $site_list ) {
+		Abort ( $CODE->{'UNDEF_KEY'},
+			"Unable to retrieve site for hostname ".$HOSTNAME." : hostname not defined" );
+	}
+	elsif ( scalar @{$site_list} > 1 ) {
+		Abort ( $CODE->{'DUPLICATE_VALUE'},
+			"Unable to retrieve site for hostname ".$HOSTNAME." : hostname appeared in multiple sites : "
+			.join ( ",", @{$site_list} ).".\n"
+			."Please relaunch this command with the right site" );
+	}
+	else {
+		( $SITE ) = @{$site_list};
+	}
+}
+
+my $host_props = Get_host_config_from_CONFIG ( $HOSTNAME, $GLOBAL_STRUCT, $SITE );
+if ( ! defined $host_props ) {
+	Abort ( $CODE->{'UNDEF_KEY'},
+		"Unable to find hostname ".$HOSTNAME." on site ".$SITE." : no such host definition" );
+}
+my $domain	= Get_zone_from_hostname ( $HOSTNAME, $GLOBAL_STRUCT, $SITE );
+if ( ! defined $domain ) {
+	Abort ( $CODE->{'UNDEF_KEY'},
+		"Unable to get domain from hostname ".$domain );
+}
+
+my @dns = split ( /\s*,\s*/, $host_props->{'dns'}->{'resolver'} );
+
+unless ( open ( OUTPUT, ">".$OUTPUT_FILE ) ) {
+	Abort ( $CODE->{'OPEN'},
+		"Unable to open output file ".$OUTPUT_FILE );
+}
+print OUTPUT "###############################################\n";
+print OUTPUT "# This file was auto-genrated by mk_resolvconf\n";
+print OUTPUT "\n";
+print OUTPUT "search ".$domain."\n";
+foreach my $dns (@dns) {
+	my $resolved = Resolv_hostname_from_GLOBAL ( $dns, $GLOBAL_STRUCT, $SITE );
+	foreach my $ip ( @{$resolved} ) {
+		print OUTPUT "nameserver ".$ip."\n";
+	}
+}
+close OUTPUT;
+
+exit 0;

Modified: branches/next-gen/sbin/mk_sourceslist
URL: http://svn.debian.org/wsvn/pf-tools/branches/next-gen/sbin/mk_sourceslist?rev=794&op=diff
==============================================================================
--- branches/next-gen/sbin/mk_sourceslist (original)
+++ branches/next-gen/sbin/mk_sourceslist Fri Jul 30 14:45:44 2010
@@ -194,6 +194,10 @@
 
 
 my $host_props	= Get_host_config_from_CONFIG ( $HOSTNAME, $GLOBAL_STRUCT, $SITE );
+if ( ! defined $host_props ) {
+	Abort ( $CODE->{'UNDEF_KEY'},
+		"Unable to find hostname ".$HOSTNAME." on site ".$SITE." : no such host definition" );
+}
 my $mode		= $host_props->{'deployment'}->{'mode'};
 if ( $TEMPLATE ne "" && ! -e $TEMPLATE ) {
 	Abort ( $CODE->{'UNDEF_KEY'},




More information about the pf-tools-commits mailing list