pf-tools commit: r798 [ccaillet-guest] - in /branches/next-gen: debian/changelog filters/filter_distrib lib/PFTools/Conf.pm lib/PFTools/Host.pm lib/PFTools/Net.pm lib/PFTools/Utils.pm sbin/fix_hosts

parmelan-guest at users.alioth.debian.org parmelan-guest at users.alioth.debian.org
Wed Aug 4 10:40:02 UTC 2010


Author: ccaillet-guest
Date: Wed Aug  4 10:40:00 2010
New Revision: 798

URL: http://svn.debian.org/wsvn/pf-tools/?sc=1&rev=798
Log:
* sbin/fix_hosts : rewrite according to new global structure, using
Getopt::Long for handling command line option(s)
* filter/filter_distrib : rewrite according to new global structure, using
Getopt::Long for handling command line option(s)

Modified:
    branches/next-gen/debian/changelog
    branches/next-gen/filters/filter_distrib
    branches/next-gen/lib/PFTools/Conf.pm
    branches/next-gen/lib/PFTools/Host.pm
    branches/next-gen/lib/PFTools/Net.pm
    branches/next-gen/lib/PFTools/Utils.pm
    branches/next-gen/sbin/fix_hosts

Modified: branches/next-gen/debian/changelog
URL: http://svn.debian.org/wsvn/pf-tools/branches/next-gen/debian/changelog?rev=798&op=diff
==============================================================================
--- branches/next-gen/debian/changelog (original)
+++ branches/next-gen/debian/changelog Wed Aug  4 10:40:00 2010
@@ -59,6 +59,8 @@
   * sbin/mk_pxelinuxcfg : rewrite according to new global structure, using
   Getopt::Long for handling command line option(s) and Template::Tiny for
   handling templates
+  * sbin/fix_hosts : rewrite according to new global structure, using
+  Getopt::Long for handling command line option(s)
   * sbin/mk_sourceslist : rewrite according to new global structure, using
   Getopt::Long for handling command line option(s) and Template::Tiny for
   handling templates
@@ -75,8 +77,10 @@
   Getopt::Long for handling command line option(s) 
   * filter/filter_vlan2if : rewrite according to new global structure, using
   Getopt::Long for handling command line option(s) 
-
- -- Christophe Caillet <tof at sitadelle.com>  Wed, 04 Aug 2010 11:43:05 +0200
+  * filter/filter_distrib : rewrite according to new global structure, using
+  Getopt::Long for handling command line option(s)
+
+ -- Christophe Caillet <tof at sitadelle.com>  Wed, 04 Aug 2010 12:38:45 +0200
 
 pf-tools (0.34.0-0WIP) unstable; urgency=low
 

Modified: branches/next-gen/filters/filter_distrib
URL: http://svn.debian.org/wsvn/pf-tools/branches/next-gen/filters/filter_distrib?rev=798&op=diff
==============================================================================
--- branches/next-gen/filters/filter_distrib (original)
+++ branches/next-gen/filters/filter_distrib Wed Aug  4 10:40:00 2010
@@ -2,7 +2,7 @@
 ##
 ##  $Id$
 ##
-##  Copyright (C) 2007-2008 Christophe Caillet <quadchris at free.fr>
+##  Copyright (C) 2007-2010 Christophe Caillet <quadchris at free.fr>
 ##
 ##  This program is free software; you can redistribute it and/or
 ##  modify it under the terms of the GNU General Public License
@@ -22,83 +22,102 @@
 use strict;
 use warnings;
 
-use PFTools::Conf ;
-use PFTools::Net;
-use PFTools::Update;
+use PFTools::Utils;
+use PFTools::Logger;
+use Getopt::Long qw( :config ignore_case_always bundling );
+use Sys::Hostname;
+
+#################################
+# VARS
+my $HELP				= 0;
+my $HOSTNAME			= hostname;
+my $SITE				= '';
+my $GLOBAL_STORE_FILE	= '';
+my $PF_CONFIG_FILE		= '';
+my $PF_CONFIG			= {};
+my $INPUT_FILE			= '';
+my $OUTPUT_FILE			= '';
+my $GLOBAL_STRUCT		= {};
 
 my $program = $0;
 $program =~ s%.*/%%; # cheap basename
 
-my $version = sprintf("svn-r%s", q$Revision$ =~ /([\d.]+)/);
+my $version = sprintf( "svn-r%s", q$Revision$ =~ /([\d.]+)/ );
 
-sub Usage () {
+###################################
+# Funtions
 
-	print <<EOF
+sub Do_help {
+    print STDERR << "# ENDHELP";
+    $program - version $version
 
-$program - version $version
-
-Synopsis : $program <src> <hostname> <dst>
-
-	This filter permits to replace the variables %DISTRIB% and %DISTSRC% according to
-	the values extract from <hostname> definition on private-network
-
-	<src>		Define here the source file on which you want to search and replace
-
-	<hostname>	Specify here the hostname
-
-	<dst>		Define here the destination file which receive the filtered datas
-
-EOF
-
+Usage:	$0 [options]
+	--help		: print help and exit
+	-h --host	: hostname for which we want to filter input
+	-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)
+	-i --input	: input file
+	-o --output	: output file
+    
+# ENDHELP
 }
 
-my ( $src, $host, $dst ) = @ARGV;
-unless ( $src and $host and $dst ) {
-	warn "You MUST define <src>, <host> and <destination>\n" ;
-	Usage () ;
-	exit 1 ;
+##################################
+### MAIN
+
+GetOptions (
+	'help'			=> \$HELP,
+	'host|h=s'		=> \$HOSTNAME,
+	'site|s=s'		=> \$SITE,
+	'config|c=s'	=> \$PF_CONFIG_FILE,
+	'store=s'		=> \$GLOBAL_STORE_FILE,
+	'input|i=s'		=> \$INPUT_FILE,
+	'output|o=s'	=> \$OUTPUT_FILE
+) or die "Didn't grok options (see --help).\n";
+
+if ( $HELP ) {
+	Do_help ();
+	exit 0;
 }
 
-my $PF_NET = Init_lib_net( Get_source("GLOBAL:private-network") );
+( $PF_CONFIG, $GLOBAL_STRUCT ) = Init_TOOLS ( $PF_CONFIG_FILE, $GLOBAL_STORE_FILE );
 
-Init_SUBST( \%SUBST, $host, 'private' ); # does an Abort if $host is not parsable
-
-my $host_type = $SUBST{'HOSTTYPE'};
-
-unless ($PF_NET->{'SERVERS'}->{'BY_NAME'}->{$host_type}) {
-    die "FATAL: unable to find config for host type $host_type";
+if ( $SITE eq '' ) {
+	if ( ! 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};
+		}
+	}
+	else {
+		$SITE = $PF_CONFIG->{'location'}->{'site'}
+	}
 }
 
-unless ($PF_NET->{'SERVERS'}->{'BY_NAME'}->{$host_type}->{'SRVLIST'}->{$host}) {
-    die "FATAL: unable to find config for host $host";
+if ( $INPUT_FILE eq '' || $OUTPUT_FILE eq '' ) {
+	Abort ( $CODE->{'UNDEF_KEY'},
+		"Source and/or destination file is(are) not defined on CLI" );
 }
 
-my ($host_distrib, $host_distsrc) =
-    @{ $PF_NET->{'SERVERS'}->{'BY_NAME'}->{$host_type}->{'SRVLIST'}->{$host} }{ qw(distrib deploymode) };
+my $filtered_src = Search_and_replace ( $HOSTNAME, $SITE, $INPUT_FILE, 'distrib', $PF_CONFIG, "", $GLOBAL_STRUCT );
 
-unless ($host_distrib) {
-    die "FATAL: unable do find distrib for host $host";
+unless ( open ( OUTPUT, ">".$OUTPUT_FILE ) ) {
+	Abort ( $CODE->{'OPEN'},
+		"Unable to open destination file ".$OUTPUT_FILE." : $!" );
 }
 
-unless ($host_distsrc) {
-    die "FATAL: unable do find deploymode for host $host";
-}
+print OUTPUT join ( "", @{$filtered_src} );
+close ( OUTPUT );
 
-$host_distsrc =~ s/^(debian|ubuntu)-installer$/$1/;
-
-open SRC, "<$src" or die "open: $src: $!\n";
-open DST, ">$dst" or die "open: $dst: $!\n";
-
-while (<SRC>) {
-	my $line = $_;
-	my $pos  = length $line;
-
-	$line	=~ s/%DISTSRC%/$host_distsrc/gs ;
-	$line	=~ s/%DISTRIB%/$host_distrib/gs ;
-	
-	print DST $line;
-}
-
-close DST;
-close SRC;
-
+exit 0;

Modified: branches/next-gen/lib/PFTools/Conf.pm
URL: http://svn.debian.org/wsvn/pf-tools/branches/next-gen/lib/PFTools/Conf.pm?rev=798&op=diff
==============================================================================
--- branches/next-gen/lib/PFTools/Conf.pm (original)
+++ branches/next-gen/lib/PFTools/Conf.pm Wed Aug  4 10:40:00 2010
@@ -2,7 +2,7 @@
 ##
 ##  $Id$
 ##
-##  Copyright (C) 2007-2009 Christophe Caillet <quadchris at free.fr>
+##  Copyright (C) 2007-2010 Christophe Caillet <quadchris at free.fr>
 ##  Copyright (C) 2005-2007 Thomas Parmelan <tom+pf-tools at ankh.fr.EU.org>
 ##  Copyright (C) 2003-2005 Damien Clermonte <damien at sitadelle.com>
 ##  Copyright (C) 2001-2003 Olivier Molteni <olivier at molteni.net>
@@ -70,7 +70,6 @@
 ### Prototypes
 
 sub __Merge_conf_includes ($$$);
-sub __Load_conf_new ($$$);
 sub Load_conf ($$$$);
 
 #################################

Modified: branches/next-gen/lib/PFTools/Host.pm
URL: http://svn.debian.org/wsvn/pf-tools/branches/next-gen/lib/PFTools/Host.pm?rev=798&op=diff
==============================================================================
--- branches/next-gen/lib/PFTools/Host.pm (original)
+++ branches/next-gen/lib/PFTools/Host.pm Wed Aug  4 10:40:00 2010
@@ -690,8 +690,6 @@
 			$realip = __Check_host_ip ( $ip_type, $netblock, $ref_host->{$iface_section}->{$ip_type}, $hostnum, $hostnode, $nodes, $site, $ref_site );
 		}
 		$add_if->{$ip_type}				= $realip->cidr();
-# 		$add_if->{'netmask'.$suffix}	= $realip->mask();
-# 		$add_if->{'broadcast'.$suffix}	= $realip->broadcast(); $add_if->{'broadcast'.$suffix} =~ s/\/.+$//;
 		my $route_key	= ( $ip_type eq 'ipv6' ) ? '@route6' : '@route';
 		$route_key		.= $hostnum if ( $ref_host->{$iface_section}->{$route_key.'.'.$host_number} );
 		my $gw_key		= ( $ip_type eq 'ipv6' ) ? 'gateway6' : 'gateway';
@@ -911,11 +909,4 @@
 	}
 }
 
-#### BACKWARD Compatibility
-sub Get_Host_Props ($$;$) {
-    my ( $hostname, $global_config, $site ) = @_ ;
-
-	return Get_host_config_from_CONFIG ( $hostname, $global_config, $site );
-}
-
 1;

Modified: branches/next-gen/lib/PFTools/Net.pm
URL: http://svn.debian.org/wsvn/pf-tools/branches/next-gen/lib/PFTools/Net.pm?rev=798&op=diff
==============================================================================
--- branches/next-gen/lib/PFTools/Net.pm (original)
+++ branches/next-gen/lib/PFTools/Net.pm Wed Aug  4 10:40:00 2010
@@ -50,591 +50,6 @@
 use Data::Dumper;
 #$Data::Dumper::Sortkeys = 1;
 #$Data::Dumper::Useperl = 1;
-
-my @DEFAULTDHCPVLAN = ('vlan-7');
-
-my $UMLTRUNKINGWORKS  = 0;
-my $UMLTRUNKFACTORIZE = 1;
-
-my $NOETH3 = 0;
-
-# Error code
-my $ERR_OPEN   = 1;
-my $ERR_SYNTAX = 2;
-
-# Allowed architecture
-my $ALLOWED_ARCH = 'i386|amd64' ;
-# Allowed deploymode
-my $ALLOWED_DEPLOYMODE = 'pf-tools|(debian|ubuntu)-installer' ;
-
-
-sub node2index {
-    my ( $node, $pad ) = @_;
-    my $index = '';
-
-    while ( $node > 0 ) {
-	$index = chr( ord('a') + $node % 26 ) . $index;
-	$node  = floor( $node / 26 );
-    }
-
-    if ( defined $pad && $pad > length($index) ) {
-	$index .= 'a' x ( $pad - length($index) );
-    }
-
-    return ($index);
-}
-
-sub rotate {
-    my ( $array, $num ) = @_;
-
-    while ( $num > 0 ) {
-	push @{$array}, shift @{$array};
-	$num--;
-    }
-
-    return @{$array};
-}
-
-sub cmpif {
-    my ( $a, $b ) = @_;
-    my $ai;
-    my $an;
-    my $av;
-    my $aa;
-    my $bi;
-    my $bn;
-    my $bv;
-    my $ba;
-
-    $an = $av = $aa = -1;
-    if ( $a =~ m/([^:.\d]+)(\d+)?(\.(\d+))?(:(\d+))?/ ) {
-	$ai = $1;
-	if ( defined $2 ) { $an = $2 }
-	if ( defined $4 ) { $av = $4 }
-	if ( defined $6 ) { $aa = $6 }
-    }
-    $bn = $bv = $ba = -1;
-    if ( $b =~ m/([^:.\d]+)(\d+)?(\.(\d+))?(:(\d+))?/ ) {
-	$bi = $1;
-	if ( defined $2 ) { $bn = $2 }
-	if ( defined $4 ) { $bv = $4 }
-	if ( defined $6 ) { $ba = $6 }
-    }
-    ( $ai cmp $bi ) || ( $an <=> $bn ) || ( $av <=> $bv ) || ( $aa <=> $ba );
-}
-
-# sub Get_Host_Props ($$) {
-#     my ( $Z, $host ) = @_ ;
-# 
-#     $host =~ /^([^.]+)(\.([^.]+))?(\.$Z->{SOA}->{name})?$/ ;
-#     my ( $hostshort, $hostvlan ) = ( $1, $3 ) ;
-#     return undef if ( ! defined $hostshort ) ;
-# 
-#     my $hostclass = Host_class( $hostshort, $Z ) ;
-# 
-#     return $Z->{'SERVERS'}->{'BY_NAME'}->{$hostclass}->{'SRVLIST'}->{$hostshort} ;
-# }
-
-# $interface Get_If ( $Zone, $host, $vlan )
-sub Get_If {
-    my ( $Z, $host, $vlan ) = @_;
-
-    my $M = Get_Host_Props ( $Z, $host ) ;
-
-    if ( !defined($M) ) {
-	Abort( $ERR_SYNTAX, "Get_If: " . $host . " not found" );
-    }
-
-    return ( $M->{'ifup'}->{ $host . "." . $vlan } );
-}
-
-# ($dhcpif, $dhcpaddr) Get_Dhcp_Infos ( $Zone, $host )
-sub Get_Dhcp_Infos {
-    my ( $Z, $host ) = @_;
-
-    my $dhcpvlanregex
-	= '^(' . join( '|', @{ $Z->{'SOA'}->{'dhcpvlan'} } ) . ')$';
-
-    my $M = Get_Host_Props ( $Z, $host ) ;
-
-    if ( !defined($M) ) {
-	Abort( $ERR_SYNTAX, "Get_Dhcp_Infos: " . $host . " not found" );
-    }
-
-    foreach my $nam (
-	sort { cmpif( $M->{'ifup'}->{$a}, $M->{'ifup'}->{$b} ) }
-	keys %{ $M->{'ifup'} }
-	)
-    {
-	my $net = $nam;
-	$net =~ s/^[^\.]+\.//;
-	if ( $net =~ /$dhcpvlanregex/ ) {
-	    if ( defined $M->{'zone'}->{$nam}->{'ether'} ) {
-		return ( $M->{'ifup'}->{$nam},
-		    $M->{'zone'}->{$nam}->{'ether'} );
-	    }
-	    else {
-		return ( $M->{'ifup'}->{$nam},
-		    $M->{'zone'}->{$nam}->{'vmether'} );
-	    }
-	}
-
-    }
-    return undef;
-}
-
-sub Get_Ordered_Filtered_Hosts {
-    my ( $Z, @hostsfilter ) = @_;
-
-    my @list;
-
-    my $class;
-    foreach $class (
-	sort {
-	    my $i = $Z->{'SERVERS'}->{'BY_NAME'}->{$a}->{'order'};
-	    my $j = $Z->{'SERVERS'}->{'BY_NAME'}->{$b}->{'order'};
-	    if ( !defined $i ) { $i = 9999; }
-	    if ( !defined $j ) { $j = 9999; }
-	    if ( $i == $j ) { return ( $a cmp $b ); }
-	    $i <=> $j;
-	} keys %{ $Z->{'SERVERS'}->{'BY_NAME'} }
-	)
-    {
-	my $hostfilter;
-	foreach $hostfilter (@hostsfilter) {
-	    my $host;
-	    foreach $host (
-		sort
-		keys %{ $Z->{'SERVERS'}->{'BY_NAME'}->{$class}->{'SRVLIST'} }
-		)
-	    {
-		if ( $class =~ /$hostfilter/ || $host =~ /$hostfilter/ ) {
-		    push @list, $host;
-		}
-	    }
-	}
-    }
-
-    return @list;
-}
-
-# HASREF Get_UM_If ( $Zone, $host )
-sub Get_UM_If {
-    my ( $Z, $host ) = @_;
-
-    my $umif = {};
-
-    my $M = Get_Host_Props ( $Z, $host ) ;
-
-    if ( !defined($M) ) {
-	Abort( $ERR_SYNTAX, "Get_Dhcp_Infos: " . $host . " not found" );
-    }
-
-    foreach my $nam (
-	sort { cmpif( $M->{'ifup'}->{$a}, $M->{'ifup'}->{$b} ) }
-	keys %{ $M->{'ifup'} }
-	)
-    {
-	my $net = $nam;
-	$net =~ s/^[^\.]+\.//;
-	my $NET = $Z->{'NETWORK'}->{'BY_NAME'}->{$net};
-
-	if ( $M->{'ifup'}->{$nam} =~ m/([^:.\d]+)(\d+)?(\.(\d+))?(:(\d+))?/ )
-	{
-	    my $ai;
-	    my $an;
-	    my $av;
-	    my $aa;
-
-	    $ai = $1;
-	    if ( defined $2 ) { $an = $2 }
-	    if ( defined $4 ) { $av = $4 }
-	    if ( defined $6 ) { $aa = $6 }
-
-	    my $vlan_raw_device = $ai . ( ( defined $an ) ? $an : '' );
-
-	    if (   defined( $umif->{$vlan_raw_device} )
-		&& ( $umif->{$vlan_raw_device} ne 'TRUNK' || !defined $av )
-		&& $umif->{$vlan_raw_device} ne $NET->{'tag'} )
-	    {
-		Abort( $ERR_SYNTAX,
-		          "Get_UM_If[" 
-			. $host . "]: "
-			. $M->{'ifup'}->{$nam}
-			. " hasn't been remapped properly" );
-	    }
-
-	    if ( defined $av ) {
-		$umif->{$vlan_raw_device} = 'TRUNK';
-	    }
-	    else {
-		$umif->{$vlan_raw_device} = $NET->{'tag'};
-	    }
-	}
-	else {
-	    $umif->{ $M->{'ifup'}->{$nam} } = $NET->{'tag'};
-	}
-    }
-    return $umif;
-}
-
-sub UMRemap_If {
-    my ( $Z, $host ) = @_;
-
-    #Debug "UMRemap_If called for $host";
-
-    my %umif;
-    my %umvlan;
-    my %iforphan;
-    my %ifmap;
-    my $lastused = -1;
-    my %umtap;
-
-    my $M = Get_Host_Props ( $Z, $host ) ;
-
-    if ( !defined($M) ) {
-	Abort( $ERR_SYNTAX, "UMRemap_If: " . $host . " not found" );
-    }
-
-    foreach my $nam (
-	sort { cmpif( $M->{'ifup'}->{$a}, $M->{'ifup'}->{$b} ) }
-	keys %{ $M->{'ifup'} }
-	)
-    {
-	my $net = $nam;
-	$net =~ s/^[^\.]+\.//;
-	my $NET = $Z->{'NETWORK'}->{'BY_NAME'}->{$net};
-
-	if ( !defined( $NET->{'tag'} ) ) {
-	    Abort( $ERR_SYNTAX,
-		      "UMRemap_If[" 
-		    . $host . "]: " 
-		    . $net
-		    . ": cannot wire, tag unknown" );
-	    exit 1;
-	}
-
-#Debug "IF '$M->{ifup}->{$nam}' -> NET '$net' -> TAG '" . (defined $NET->{tag} ? $NET->{tag} : '?') . "'\n";
-	if ( $M->{'ifup'}->{$nam} =~ m/([^:.\d]+)(\d+)?(\.(\d+))?(:(\d+))?/ )
-	{
-	    my $ai;
-	    my $an;
-	    my $av;
-	    my $aa;
-
-	    $ai = $1;
-	    if ( defined $2 ) { $an = $2 }
-	    if ( defined $4 ) { $av = $4 }
-	    if ( defined $6 ) { $aa = $6 }
-
-	    if ( $ai eq 'eth' && $lastused < $an ) {
-		$lastused = $an;
-	    }
-
-	    if (   defined $av
-		&& defined $NET->{'tag'}
-		&& $av != $NET->{'tag'} )
-	    {
-		Abort( $ERR_SYNTAX,
-		          "UMRemap_If[" 
-			. $host . "]: "
-			. "IF TAG "
-			. $av
-			. " != NET TAG "
-			. $NET->{'tag'} );
-		exit 1;
-	    }
-
-	    my $vlan_raw_device = $ai . ( ( defined $an ) ? $an : '' );
-
-	    # Interface deja connectee?
-	    if ( defined( $umif{$vlan_raw_device} ) ) {
-		if (   defined($av)
-		    && $umif{$vlan_raw_device} ne 'TRUNK'
-		    && $UMLTRUNKINGWORKS )
-		{
-
-		    if ( !defined $umvlan{'TRUNK'} || !$UMLTRUNKFACTORIZE ) {
-
-#Debug "$vlan_raw_device promoted to TRUNK, needs new UNTAGGED $umif{$vlan_raw_device}\n";
-			delete $umvlan{ $umif{$vlan_raw_device} };
-			push @{ $iforphan{ $umif{$vlan_raw_device} } },
-			    $vlan_raw_device;
-			$umif{$vlan_raw_device} = 'TRUNK';
-			if ( !defined $umvlan{'TRUNK'} ) {
-			    $umvlan{'TRUNK'} = $vlan_raw_device;
-			}
-		    }
-		    else {
-
-#Debug "$umvlan{'TRUNK'} is already connected to TRUNK, using it for $M->{ifup}->{$nam}\n";
-			$ifmap{ $M->{'ifup'}->{$nam} } = $umvlan{'TRUNK'};
-		    }
-		}
-		elsif ($umif{$vlan_raw_device} ne 'TRUNK'
-		    && $umif{$vlan_raw_device} != $NET->{'tag'} )
-		{
-
-#Debug "$vlan_raw_device already connected to UNTAGGED $umif{$vlan_raw_device}, new one needed for UNTAGGED $NET->{tag}\n";
-		    delete $umvlan{ $NET->{'tag'} };
-		    push @{ $iforphan{ $NET->{'tag'} } },
-			$M->{'ifup'}->{$nam};
-		}
-	    }
-	    else {
-		if ( !defined $av ) {
-		    $umif{$vlan_raw_device} = $NET->{'tag'};
-		    if ( !defined $umvlan{ $NET->{'tag'} } ) {
-			$umvlan{ $NET->{'tag'} } = $vlan_raw_device;
-		    }
-		}
-		else {
-		    $umif{$vlan_raw_device} = 'TRUNK';
-		    if ( !defined $umvlan{'TRUNK'} ) {
-			$umvlan{'TRUNK'} = $vlan_raw_device;
-		    }
-		}
-	    }
-
-	    if (   defined $umif{$vlan_raw_device}
-		&& defined $umvlan{ $umif{$vlan_raw_device} }
-		&& $umvlan{ $umif{$vlan_raw_device} } ne $vlan_raw_device )
-	    {
-
-#Debug "TAG $umif{$vlan_raw_device} already connected to IF $umvlan{ $umif{$vlan_raw_device} }\n";
-		$ifmap{$vlan_raw_device} = $umvlan{ $umif{$vlan_raw_device} };
-		delete $umif{$vlan_raw_device};
-	    }
-	}
-	else {
-	    Warn( $ERR_SYNTAX,
-		      "UMRemap_If[" 
-		    . $host
-		    . "]: Can't parse interface name "
-		    . $M->{'ifup'}->{$nam}
-		    . ", skipped" );
-	}
-    }
-
-    #if ( scalar( keys %ifmap ) != 0 || scalar( keys %iforphan ) != 0 ) {
-    #    Warn( $ERR_SYNTAX, "UMRemap_If[" . $host
-    #        . "]: Automatic remapping, config may not work!" );
-    #}
-
-#
-# Exemple de %iforphan nouvelle formule :
-#    (
-#	'6' => [ 'eth2:5', 'eth2:6' ],
-#	'7' => [ 'eth2:1' ],
-#	'8' => [ 'eth2:2' ],
-#	'4' => [ 'eth2:7' ],
-#	'5' => [ 'eth2:3', 'eth2:4' ]
-#    )
-#
-# FIXME le double foreach et double sort c'est douteux, mais c'était le moins invasif.
-#
-
-#Debug "iforphan : "; warn Dumper(\%iforphan); Debug "ifmap : "; warn Dumper(\%ifmap);
-
-    my %tmpindexes;
-    foreach my $nam (
-	sort { cmpif( $iforphan{$a}[0], $iforphan{$b}[0] ) || $a <=> $b }
-	keys %iforphan
-	)
-    {
-	foreach my $namidx (
-	    sort {
-		cmpif( $iforphan{$nam}[$a], $iforphan{$nam}[$b] )
-		    || $a <=> $b;
-	    } 0 .. @{ $iforphan{$nam} } - 1
-	    )
-	{
-	    if ( !defined $umvlan{$nam} ) {
-		$lastused++;
-
-# Debug "orphan UNTAGGED $nam (was $iforphan{$nam}[$namidx]) allocated to eth$lastused\n";
-		$ifmap{ $iforphan{$nam}[$namidx] } = "eth" . $lastused;
-		$umvlan{$nam}                      = "eth" . $lastused;
-		$umif{ "eth" . $lastused }         = $nam;
-		$tmpindexes{ $umvlan{$nam} }       = 0;
-	    }
-	    else {
-
-#Debug "false orphan UNTAGGED $nam (was $iforphan{$nam}[$namidx]) merged with $umvlan{$nam}\n";
-		$ifmap{ $iforphan{$nam}[$namidx] } = $umvlan{$nam};
-		if ( defined $tmpindexes{ $umvlan{$nam} } ) {
-		    $ifmap{ $iforphan{$nam}[$namidx] }
-			.= ':' . $tmpindexes{ $umvlan{$nam} }++;
-		}
-	    }
-
-# Debug "iforphan : "; warn Dumper(\%iforphan); Debug "ifmap : "; warn Dumper(\%ifmap);
-	}
-    }
-
-    foreach my $nam (
-	sort { cmpif( $M->{'ifup'}->{$a}, $M->{'ifup'}->{$b} ) }
-	keys %{ $M->{'ifup'} }
-	)
-    {
-	if ( !defined $ifmap{ $M->{ifup}->{$nam} } ) {
-	    $M->{'ifupremapped'}->{$nam} = $M->{'ifup'}->{$nam};
-	    if ( defined $M->{'route'}->{ $M->{'ifup'}->{$nam} } ) {
-		$M->{'routeremapped'}->{ $M->{'ifup'}->{$nam} }
-		    = $M->{route}->{ $M->{'ifup'}->{$nam} };
-	    }
-	    if ( defined $M->{'delroute'}->{ $M->{'ifup'}->{$nam} } ) {
-		$M->{'delrouteremapped'}->{ $M->{'ifup'}->{$nam} }
-		    = $M->{'delroute'}->{ $M->{'ifup'}->{$nam} };
-	    }
-	}
-	else {
-	    $M->{'ifupremapped'}->{$nam} = $ifmap{ $M->{'ifup'}->{$nam} };
-	    if ( defined $M->{'route'}->{ $M->{ifup}->{$nam} } ) {
-		$M->{'routeremapped'}->{ $ifmap{ $M->{'ifup'}->{$nam} } }
-		    = $M->{'route'}->{ $M->{'ifup'}->{$nam} };
-	    }
-	    if ( defined $M->{'delroute'}->{ $M->{'ifup'}->{$nam} } ) {
-		$M->{'delrouteremapped'}->{ $ifmap{ $M->{'ifup'}->{$nam} } }
-		    = $M->{'delroute'}->{ $M->{'ifup'}->{$nam} };
-	    }
-	}
-    }
-
-    delete $M->{'ifup'};
-    delete $M->{'route'};
-    delete $M->{'delroute'};
-    $M->{'ifup'}     = $M->{'ifupremapped'};
-    $M->{'route'}    = $M->{'routeremapped'};
-    $M->{'delroute'} = $M->{'delrouteremapped'};
-    delete $M->{'ifupremapped'};
-    delete $M->{'routeremapped'};
-    delete $M->{'delrouteremapped'};
-}
-
-sub Get_UM_Filename {
-    my ( $Z, $host ) = @_;
-
-    my $M = Get_Host_Props ( $Z, $host ) ;
-
-    if ( !defined($M) ) {
-	return undef;
-    }
-
-    return ( $M->{'umlfilename'} );
-
-}
-
-sub Get_Initrd_Filename ($$) {
-    my ( $Z, $host ) = @_;
-
-    my $M = Get_Host_Props ( $Z, $host ) ;
-
-    if ( !defined($M) ) {
-	return undef;
-    }
-
-    return ( $M->{'initrd'} );
-}
-
-sub Get_Ramdisk_size_from_Initrd ($) {
-    my ($initrd) = @_;
-    $initrd = "/distrib/tftpboot/$initrd";
-
-    # On ne peut pas utiliser File::stat sans perturber lib-update etc.
-    my @st_initrd = stat $initrd;
-    unless ( @st_initrd and $st_initrd[2] & S_IFREG ) {
-	Abort( $ERR_OPEN, "Impossible de stat($initrd): $!" );
-    }
-
-    return $st_initrd[7] / 1024;
-}
-
-sub Get_Cmdline ($$) {
-    my ( $Z, $host ) = @_;
-
-    my $M = Get_Host_Props ( $Z, $host ) ;
-
-    if ( !defined($M) ) {
-	return undef;
-    }
-
-    return ( $M->{'cmdline'} );
-}
-
-sub Get_PXE_Filename {
-    my ( $Z, $host ) = @_;
-
-    my $M = Get_Host_Props ( $Z, $host ) ;
-
-    return undef if ( !defined($M) ) ;
-
-    if ( $M->{'arch'} eq 'amd64' ) {
-	return ( $M->{'arch'}."/".$M->{'pxefilename'} );
-    }
-    else {
-	return ( $M->{'pxefilename'} );
-    }
-}
-
-
-sub Get_Active_Filename {
-    my ( $Z, $host ) = @_;
-
-    my $M = Get_Host_Props ( $Z, $host ) ;
-
-    return ( $M->{'umlfilename'} ) if ($PFTOOLS_VARS->{'UML'} && defined $M->{'umlfilename'});
-    return ( $M->{'vmwfilename'} ) if ($PFTOOLS_VARS->{'VMWARE'} && defined $M->{'vmwfilename'});
-    return Get_PXE_Filename ( $Z, $host ) ;
-}
-
-sub Get_Active_Systemmap {
-    my ( $Z, $host ) = @_;
-
-    my $system_map = Get_PXE_Filename ( $Z, $host ) ;
-    $system_map =~ s/vmlinuz/System.map/ ;
-    return $system_map ;
-}
-
-sub Get_dns_from_hostname {
-    my ( $Z, $host ) = @_;
-
-    $host =~ m/^([^.]+)(\.([^.]+))?(\.$Z->{SOA}->{name})?$/ ;
-    my $hostnum = $1 ;
-    $hostnum =~ s/.*([\d]+)$/$1/ ;
-    my $M = Get_Host_Props ( $Z, $host ) ;
-
-    return Get_dns_from_zone( $Z, $M, $hostnum );
-}
-
-sub Get_dns_from_zone {
-    my ( $Z, $M, $hostnum ) = @_;
-
-    if ( !defined($M) ) {
-	return undef;
-    }
-
-    my $rawdns;
-    if ( defined $M->{'dns'} && $M->{'dns'} ne "" ) {
-	$rawdns = $M->{'dns'};
-    }
-    else {
-	$rawdns = join( ", ", @{ $Z->{'NS'} } );
-    }
-
-    my @dns;
-    foreach my $dns ( split( /[,\s]+/, $rawdns ) ) {
-	my @resolved = Resolv( $dns, $Z );
-	if ( $#resolved >= 0 && defined $resolved[0] ) {
-	    @resolved = sort @resolved;
-	    push @dns, rotate( \@resolved, $hostnum );
-	}
-	else {
-	    push @dns, $dns;
-	}
-    }
-    return @dns;
-}
 
 #########################################################################
 ### Rewrite with new syntax
@@ -897,202 +312,4 @@
 	}
 }
 
-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)
-#
-#	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;
-# }
-
-
-if ( `grep -e '^host[ 	]*:' /proc/cpuinfo 2>/dev/null` ne "" ) {
-    $PFTOOLS_VARS->{'UML'} = 1;
-}
-
-if ( !$PFTOOLS_VARS->{'UML'}
-    && `LANG=C LC_ALL=C /sbin/ifconfig eth0 2>>/dev/null | grep HWaddr | awk '{print \$5}'`
-    =~ "^00:50:56:" )
-{
-    $PFTOOLS_VARS->{'VMWARE'} = 1;
-}
-
-if ( !$PFTOOLS_VARS->{'UML'} && `/sbin/ifconfig eth3 2>>/dev/null` eq "" ) {
-    $NOETH3 = 1;
-}
-
-#
-# Proto		: HASHREF Parse_routing_table ( HASHREF $ref_network )
-# Input		: $ref_network is an hashref obtained by an Init_lib_net on private-network file
-# 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 ;
-# }
-
 1;
-

Modified: branches/next-gen/lib/PFTools/Utils.pm
URL: http://svn.debian.org/wsvn/pf-tools/branches/next-gen/lib/PFTools/Utils.pm?rev=798&op=diff
==============================================================================
--- branches/next-gen/lib/PFTools/Utils.pm (original)
+++ branches/next-gen/lib/PFTools/Utils.pm Wed Aug  4 10:40:00 2010
@@ -34,6 +34,7 @@
 	Get_zone_from_site_GLOBAL
 	Get_cmdline_from_hostprops
 
+	Fix_hosts
 	Mk_dhcp
 	Mk_interfaces
 	Mk_PXE_bootfile
@@ -645,9 +646,41 @@
 		elsif ( $type_replace eq 'iface' ) {
 			$line = __Search_and_resolve_IFACE ( $line, $host_props, $subst );
 		}
+		elsif ( $type_replace eq 'distrib' ) {
+			$line	=~ s/%DISTSRC%/$host_props->{'deployment'}->{'mode'}/gs ;
+			$line	=~ s/%DISTRIB%/$host_props->{'deployment'}->{'distrib'}/gs ;
+		}
 		push ( @{$result}, $line );
 	}
 	return $result;
+}
+
+sub Fix_hosts ($$$$$$) {
+	my ( $hostname, $input_file, $site, $ip_type, $global_config, $pf_config ) = @_ ;
+	my $tmp_hosts = [];
+
+	if ( $ip_type !~ /^ipv4$/ ) {
+		Warn ( $CODE->{'INVALID_VALUE'},
+			$ip_type." is not implemented for fixing ".$input_file );
+		return undef;
+	}
+	unless ( open ( HOSTS, $input_file ) ) {
+		Warn ( $CODE->{'OPEN'},
+			"Unable top open ".$input_file."\n" );
+		return 0 ;
+	}
+	@{$tmp_hosts} = <HOSTS> ;
+	close ( HOSTS );
+	
+	my $host_props		= Get_host_config_from_CONFIG ( $hostname, $global_config, $site );
+	my $iface_dhcpvlan	= Get_iface_vlan_from_hostname ( $host_props->{'deployment'}->{'dhcpvlan'}, $host_props ); 
+	my $ip_deploy		= $host_props->{'interfaces'}->{$iface_dhcpvlan}->{'ipv4'};
+	$ip_deploy			=~ s/\/[\d]+$//;
+	foreach ( @{$tmp_hosts} ) {
+		next if ( ! /$hostname/ ) ;
+		s/^127.0.([\d]{1,3}\.[\d]{1,3})/$ip_deploy/ ;
+	}
+	return $tmp_hosts;
 }
 
 sub Mk_dhcp ($$) {

Modified: branches/next-gen/sbin/fix_hosts
URL: http://svn.debian.org/wsvn/pf-tools/branches/next-gen/sbin/fix_hosts?rev=798&op=diff
==============================================================================
--- branches/next-gen/sbin/fix_hosts (original)
+++ branches/next-gen/sbin/fix_hosts Wed Aug  4 10:40:00 2010
@@ -24,124 +24,109 @@
 use strict;
 use warnings;
 
-use Sys::Hostname ;
-use Digest::MD5;
-use PFTools::Conf ;
-use PFTools::Net;
-use PFTools::Update;
-use File::Compare ;
-use File::Copy ;
 use Getopt::Long qw( :config ignore_case_always bundling );
+use PFTools::Utils;
+use PFTools::Logger;
+use Sys::Hostname;
+use Data::Dumper;
+
+#################################
+# VARS
+my $HELP				= 0;
+my $HOSTNAME			= hostname;
+my $SITE				= '';
+my $GLOBAL_STORE_FILE	= '';
+my $PF_CONFIG_FILE		= '';
+my $PF_CONFIG			= {};
+my $IP_TYPE				= 'ipv4';
+my $INPUT_FILE			= '/etc/hosts';
+my $OUTPUT_FILE			= '/etc/hosts';
+my $GLOBAL_STRUCT		= {};
 
 my $program = $0;
 $program =~ s%.*/%%; # cheap basename
 
 my $version = sprintf( "svn-r%s", q$Revision$ =~ /([\d.]+)/ );
 
-my $HOSTS_CFG	= "/etc/hosts" ;
-my $HOSTNAME	= hostname ;
-my $DEST	= "-" ;
-my $NET		= "GLOBAL:private-network" ;
-my $HELP	= 0 ;
-my $DEBUG	= 0 ;
-my $VERBOSE	= 0 ;
+###################################
+# Funtions
 
-sub Display_usage () {
-print STDERR << "# ENDHELP";
+sub Do_help {
+    print STDERR << "# ENDHELP";
     $program - version $version
 
 Usage:	$0 [options]
-	-h --help:	print help and exit
-	-v --verbose:	be more verbose
-	-s --src	source for hosts configuration (default: /etc/hosts)
-	-d --dest	destination for fixed information (default: /etc/hosts)
-	--host		hostname on which hosts configuration applied
-	-n --net	Possiblitiy for defining an alternate PATH for private-network file (default GLOBAL:private-network)
+	--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)
+	-t --type	: IP type to fix, allowed values are ipv4 and ipv6. Default value is ipv4
+	-i --input	: input file to fix default value is /etc/hosts
+	-o --output	: output file default value is /etc/hosts
+    
 # ENDHELP
 }
 
+##################################
+### MAIN
 
-sub Fix_hosts ($$$) {
-	my ( $hostname, $ip_systeme, $dst ) = @_ ;
+GetOptions (
+	'help'			=> \$HELP,
+	'host|h=s'		=> \$HOSTNAME,
+	'site|s=s'		=> \$SITE,
+	'config|c=s'	=> \$PF_CONFIG_FILE,
+	'store=s'		=> \$GLOBAL_STORE_FILE,
+	'type|t=s'		=> \$IP_TYPE,
+	'input|i=s'		=> \$INPUT_FILE,
+	'output|o=s'	=> \$OUTPUT_FILE
+) or die "Didn't grok options (see --help).\n";
 
-	unless ( open ( HOSTS, $HOSTS_CFG ) ) {
-		warn "Unable top open ".$HOSTS_CFG."\n" ;
-		return 0 ;
-	}
-	my @tmp_hosts = <HOSTS> ;
-	foreach ( @tmp_hosts ) {
-		chomp ;
-		next if ( ! /$hostname/ ) ;
-		s/^127.0.([\d]{1,3}\.[\d]{1,3})/$ip_systeme/ ;
-	}
-	
-	if ( $dst eq '-' ) {
-		foreach ( @tmp_hosts ) {
-			print $_."\n" ;
+if ( $HELP ) {
+	Do_help ();
+	exit 0;
+}
+
+( $PF_CONFIG, $GLOBAL_STRUCT ) = Init_TOOLS ( $PF_CONFIG_FILE, $GLOBAL_STORE_FILE );
+
+if ( ! $PF_CONFIG->{'features'}->{$IP_TYPE} ) {
+	print Dumper $PF_CONFIG;
+	Abort ( $CODE->{'UNDEF_KEY'},
+		"Aborting because ".$IP_TYPE." is not activated inot PF-Tools" );
+}
+
+if ( $SITE eq '' ) {
+	if ( ! 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" );
 		}
-		return 1 ;
+		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};
+		}
 	}
 	else {
-		unless ( open ( TMPDST, ">/tmp/etc_hosts" ) ) {
-			warn "Unable to open temporary destination file /tmp/etc_hosts\n" ;
-			return 0 ;
-		}
-		foreach ( @tmp_hosts ) {
-			print TMPDST $_."\n" ;
-		}
-		close ( TMPDST ) ;
-		if ( compare ( '/tmp/etc_hosts', $dst ) ) {
-			return move ( '/tmp/etc_hosts', $dst) ;
-		} else {
-			if ( $DEBUG ) {
-				warn "No need to move /tmp/etc_hosts to ".$dst." they are equals\n" ;
-				warn "Unlinking source file /tmp/etc_hosts\n" ;
-			}
-			if ( ! unlink ( '/tmp/etc_hosts' ) ) {
-				warn "Unable to unlink source file /tmp/etc_hosts\n" ;
-				warn "Please clean it manually\n" ;
-			}
-		}
-		return 1 ;
+		$SITE = $PF_CONFIG->{'location'}->{'site'}
 	}
 }
 
-##### MAIN
-GetOptions(
-    'src|s=s'	=> \$HOSTS_CFG,
-    'dst|d=s'	=> \$DEST,
-    'host=s'	=> \$HOSTNAME,
-    'net|n=s'	=> \$NET,
-    'debug'	=> \$DEBUG,
-    'help|h'	=> \$HELP,
-    'verbose|v'	=> \$VERBOSE
-) or die "GetOptions error, try --help: $!\n";
+my $fixed_input = Fix_hosts ( $HOSTNAME, $INPUT_FILE, $SITE, $IP_TYPE, $GLOBAL_STRUCT, $PF_CONFIG );
+if ( ! defined $fixed_input ) {
+	Abort ( $CODE->{'EXEC'},
+		"An error occured during fixing file ".$OUTPUT_FILE );
+}
+unless ( open ( FIXED, ">".$OUTPUT_FILE ) ) {
+	Abort ( $CODE->{'OPEN'},
+		"Unable to open output file ".$OUTPUT_FILE );
+}
+print FIXED join ( "", @{$fixed_input} );
+close ( FIXED );
 
-if ( $HELP ) {
-	Display_usage () ;
-	exit 0 ;
-}
-elsif ( ! -e $HOSTS_CFG ) {
-	die "Unexistant hosts configuration ".$HOSTS_CFG."\n" ;
-}
-elsif ( -z $HOSTS_CFG ) {
-	die "Empty configuration file for hosts ".$HOSTS_CFG."\n" ;
-}
-
-$DEST = "-" if ( $DEBUG ) ;
-$VERBOSE = 1 if ( $DEBUG ) ;
-
-my $NETWORK		= Init_lib_net ( Get_source ( $NET ) ) ;
-my ( $IF_SYS, $MAC )	= Get_Dhcp_Infos ( $NETWORK, $HOSTNAME ) ;
-my $subst = {} ;
-Init_SUBST ( $subst, $HOSTNAME, 'private' ) ;
-my $IF_LIST		= $NETWORK->{'SERVERS'}->{'BY_NAME'}->{$subst->{'HOSTTYPE'}}->{'SRVLIST'}->{$HOSTNAME}->{'ifup'} ;
-my $VLAN		= '' ;
-foreach my $if ( keys %{$IF_LIST} ) {
-	$VLAN = $if if ( $IF_LIST->{$if} eq $IF_SYS ) ;
-}
-my $IP_SYS	= $NETWORK->{'SERVERS'}->{'BY_NAME'}->{$subst->{'HOSTTYPE'}}->{'SRVLIST'}->{$HOSTNAME}->{'zone'}->{$VLAN}->{'FIELD'} ;
-if ( ! Fix_hosts ( $HOSTNAME, $IP_SYS, $DEST ) ) {
-	die "Unable to fix file ".$HOSTS_CFG."\n" ;
-}
-
+exit 0;




More information about the pf-tools-commits mailing list