pf-tools commit: r784 [ccaillet-guest] - in /branches/next-gen: debian/changelog lib/PFTools/Conf.pm lib/PFTools/Net.pm sbin/mk_interfaces sbin/mk_sitezone

parmelan-guest at users.alioth.debian.org parmelan-guest at users.alioth.debian.org
Mon Jul 26 12:31:41 UTC 2010


Author: ccaillet-guest
Date: Mon Jul 26 12:31:39 2010
New Revision: 784

URL: http://svn.debian.org/wsvn/pf-tools/?sc=1&rev=784
Log:
* sbin/mk_interfaces : rewrite according with new global structure, using
Getopt::Long for handling command line option(s)
* sbin/mk_sitezone : rewrite from mk_privatezone according to global structure
and site definition

Added:
    branches/next-gen/sbin/mk_sitezone   (with props)
Modified:
    branches/next-gen/debian/changelog
    branches/next-gen/lib/PFTools/Conf.pm
    branches/next-gen/lib/PFTools/Net.pm
    branches/next-gen/sbin/mk_interfaces

Modified: branches/next-gen/debian/changelog
URL: http://svn.debian.org/wsvn/pf-tools/branches/next-gen/debian/changelog?rev=784&op=diff
==============================================================================
--- branches/next-gen/debian/changelog (original)
+++ branches/next-gen/debian/changelog Mon Jul 26 12:31:39 2010
@@ -44,8 +44,12 @@
     - using new packages Parser.pm et Logger.pm
   * lib/PFTools/Update.pm
     - using new packages Parser.pm et Logger.pm
-
- -- Christophe Caillet <quadchris at free.fr>  Fri, 23 Jul 2010 14:15:49 +0200
+  * sbin/mk_interfaces : rewrite according with new global structure, using
+  Getopt::Long for handling command line option(s)
+  * sbin/mk_sitezone : rewrite from mk_privatezone according to global structure
+  and site definition
+
+ -- Christophe Caillet <quadchris at free.fr>  Mon, 26 Jul 2010 14:21:12 +0200
 
 pf-tools (0.34.0-0WIP) unstable; urgency=low
 

Modified: branches/next-gen/lib/PFTools/Conf.pm
URL: http://svn.debian.org/wsvn/pf-tools/branches/next-gen/lib/PFTools/Conf.pm?rev=784&op=diff
==============================================================================
--- branches/next-gen/lib/PFTools/Conf.pm (original)
+++ branches/next-gen/lib/PFTools/Conf.pm Mon Jul 26 12:31:39 2010
@@ -30,6 +30,7 @@
 use PFTools::Parser;
 use PFTools::Logger;
 use Data::Dumper;
+use Storable;
 
 our @ISA = ('Exporter');
 
@@ -57,6 +58,8 @@
 	Init_GLOBAL_NETCONFIG
 	__Translate_old2new_host
 	Load_conf
+	Flush2disk_GLOBAL
+	Retrieve_GLOBAL
 );
 
 ##########################
@@ -270,7 +273,8 @@
 $PF_CONFIG->{'path'} = {
 	'status_dir'	=> '/var/lib/pftools',
 	'distrib_dir'	=> '/distrib',
-	'tftp_dir'		=> '/distrib/tftpboot'
+	'tftp_dir'		=> '/distrib/tftpboot',
+	'global_struct'	=> '/var/lib/pf-tools/global_struct.stor'
 };
 $PF_CONFIG->{'features'} = {
 	'ipv4'			=> 1,
@@ -840,6 +844,23 @@
 	return $GLOBAL;
 }
 
+sub Flush2disk_GLOBAL ($$;$) {
+	my ( $global_config, $pf_config, $path_global_file ) = @_;
+
+	my $flush_file = $path_global_file || $pf_config->{'path'}->{'global_struct'};
+	if ( ! store ( $global_config, $flush_file ) ) {
+		Abort ( $CODE->{'STORABLE'},
+			"An error occured when trying to flush global structure to file ".$flush_file );
+	}
+}
+
+sub Retrieve_GLOBAL ($;$) {
+	my ( $pf_config, $path_global_file ) = @_;
+
+	my $flush_file = $path_global_file || $pf_config->{'path'}->{'global_struct'};
+	return retrieve ( $flush_file );
+}
+
 # Print_conf
 sub Print_conf {
     my ($C) = @_;

Modified: branches/next-gen/lib/PFTools/Net.pm
URL: http://svn.debian.org/wsvn/pf-tools/branches/next-gen/lib/PFTools/Net.pm?rev=784&op=diff
==============================================================================
--- branches/next-gen/lib/PFTools/Net.pm (original)
+++ branches/next-gen/lib/PFTools/Net.pm Mon Jul 26 12:31:39 2010
@@ -59,6 +59,7 @@
     Add_server
     Add_host
     Get_site_list
+    Get_site_zone_from_GLOBAL
 
     Mk_zone_for_site
 );
@@ -831,20 +832,24 @@
 
 #########################################################################
 #
-# STR __Get_site_zone ( STR , HASHREF )
+# STR Get_site_zone_from_GLOBAL ( STR , HASHREF )
 #
 # This function returns the zone name which is defined for the specified site
 # Inputs :
-#  - $site		: site name
-#  - $ref_site	: hashref where are stored site definitions according to networkfile-syntax
+#  - $site			: site name
+#  - $global_config	: hashref where is stored global configuration
 #
 # Output :
 #  Return a string containing the zone defined for this site.
 #
-sub __Get_site_zone ($$) {
-	my ( $site, $ref_site ) = @_;
+sub Get_site_zone_from_GLOBAL ($$) {
+	my ( $site, $global_config ) = @_;
 	
-	return $ref_site->{'zone'};
+	if ( ! defined $global_config->{'SITE'}->{'BY_NAME'}->{$site} ) {
+		Abort ( $CODE->{'UNDEF_KEY'},
+			"" );
+	}
+	return $global_config->{'SITE'}->{'BY_NAME'}->{$site}->{'zone'};
 }
 
 #########################################################################
@@ -983,7 +988,7 @@
 			$net_part->{$netaddr_key}->{$net_block->cidr()}	= $net_name;
 			$net_part->{'BY_TAG'}->{$ref_net->{'tag'}}		= $net_name if ( $ref_net->{'tag'} );
 			# Adding entries for network, netmask, broadcast etc. into the DNS zone
-			my $zone		= __Get_site_zone ( $site, $global_config->{'SITE'}->{'BY_NAME'}->{$site} );
+			my $zone		= Get_site_zone_from_GLOBAL ( $site, $global_config );
 			my $zone_part = $global_config->{$zone_key}->{'BY_NAME'}->{$zone};
 			push ( @{$zone_part->{'__network_order'}->{$site}}, $net_name );
 			$zone_part->{'BY_SITE'}->{$site}->{$net_name} = {};
@@ -1130,14 +1135,23 @@
 # Inputs :
 #  - $hostname		: string containing the model definition for building hostname
 #  - $global_config	: hashref containing the parsed global configuration
+#  - $site			: define here the site where hostname is defined
 #
 # Output :
 #  Returns a string containing the hosttype or undef if hostname doesn't exist
 #
-sub Get_hosttype_from_hostname ($$) {
-	my ( $hostname, $global_config ) = @_;
-
-	foreach my $site ( @{$global_config->{'SITE'}->{'__site_list'}} ) {
+sub Get_hosttype_from_hostname ($$;$) {
+	my ( $hostname, $global_config, $site ) = @_;
+	my $site_list;
+
+	if ( ! defined $site ) {
+		$site_list = $global_config->{'SITE'}->{'__site_list'};
+	}
+	else {
+		$site_list = [ $site ];
+	}
+	
+	foreach my $site ( @{$site_list} ) {
 		my $host_part = $global_config->{'SITE'}->{'BY_NAME'}->{$site}->{'HOST'}->{'BY_NAME'};
 		foreach my $hostclass ( keys %{$host_part} ) {
 			return $hostclass if ( $hostclass eq $hostname );
@@ -1379,7 +1393,7 @@
 			$site_part->{'HOST'}->{'BY_NAME'}->{$hostclass} = {};
 		}
 		my $srv_part	= $site_part->{'HOST'}->{'BY_NAME'}->{$hostclass};
-		my $zone		= __Get_site_zone ( $site, $global_config->{'SITE'}->{'BY_NAME'}->{$site} );
+		my $zone		= Get_site_zone_from_GLOBAL ( $site, $global_config );
 		my $prefix		= __Get_site_prefix ( $site, $global_config->{'SITE'}->{'BY_NAME'}->{$site} );
 		my $vlan_list	= __Get_vlan_list_from_server ( $srv2add );
 		foreach my $hostnum ( 0 .. $host_last ) {
@@ -1606,6 +1620,18 @@
 	return $add_if;
 }
 
+#########################################################################
+#
+# VOID Add_host ( STR, HASHREF, HASHREF, HASHREF )
+#
+# This function adds host into global configuration, zone informations
+# and dhcp entries if needed
+# Inputs :
+#  - $hostfile		: filename where host is parsed
+#  - $host2add		: hashref where are stored host definitions according to hostfile-syntax
+#  - $global_config	: hashref where are stored global configuration datas
+#  - $pf_config		: hashref where are stored pf-tools configuration datas
+#
 sub Add_host ($$$$) {
 	my ( $hostfile, $host2add, $global_config, $pf_config ) = @_;
 
@@ -1624,7 +1650,7 @@
 			$site_part->{'HOST'}->{'BY_NAME'}->{$hostclass} = {};
 		}
 		my $host_part	= $site_part->{'HOST'}->{'BY_NAME'}->{$hostclass};
-		my $zone		= __Get_site_zone ( $site, $global_config->{'SITE'}->{'BY_NAME'}->{$site} );
+		my $zone		= Get_site_zone_from_GLOBAL ( $site, $global_config );
 		my $prefix		= __Get_site_prefix ( $site, $global_config->{'SITE'}->{'BY_NAME'}->{$site} );
 		foreach my $hostnum ( 0 .. $host_last ) {
 			foreach my $hostnode (  0 .. $node_last ) {
@@ -1740,6 +1766,17 @@
 	}
 }
 
+#########################################################################
+#
+# VOID Get_host_config_from_CONFIG ( STR, HASHREF[, STR] )
+#
+# This function try to determine site from hostname if site is not defined
+# and return host definition from global configuration structure
+# Inputs :
+#  - $hostname		: filename where server is parsed
+#  - $global_config	: hashref where are stored global configuration datas
+#  - $site			: define here the site where hostname is defined (optional)
+#
 sub Get_host_config_from_CONFIG ($$;$) {
 	my ( $hostname, $global_config, $site ) = @_ ;
 
@@ -1759,7 +1796,7 @@
 		}
 	}
 	my $site_part					= $global_config->{'SITE'}->{'BY_NAME'}->{$site};
-	my $zone						= __Get_site_zone ( $site, $global_config->{'SITE'}->{'BY_NAME'}->{$site} );
+	my $zone						= Get_site_zone_from_GLOBAL ( $site, $global_config );
 	$hostname						=~ /^([^.]+)(\.([^.]+))?(\.$zone)?$/ ;
 	my ( $hostshort, $hostvlan )	= ( $1, $3 ) ;
 	my $hosttype					= Get_hosttype_from_hostname ( $hostname, $global_config );
@@ -1942,7 +1979,6 @@
 		}
 		else {
 			my ( $entry ) = @{$routes->{$dest}};
-			print "Entry --> ".$entry."\n";
 			my ( $if, $dst, $via, $gw ) = split ( /\s+/, $entry );
 			if ( $dst eq 'default' ) {
 				if ( ! defined $gw ) {

Modified: branches/next-gen/sbin/mk_interfaces
URL: http://svn.debian.org/wsvn/pf-tools/branches/next-gen/sbin/mk_interfaces?rev=784&op=diff
==============================================================================
--- branches/next-gen/sbin/mk_interfaces (original)
+++ branches/next-gen/sbin/mk_interfaces Mon Jul 26 12:31:39 2010
@@ -23,13 +23,113 @@
 use strict;
 use warnings;
 
+use Getopt::Long qw( :config ignore_case_always bundling );
+use PFTools::Conf;
 use PFTools::Net;
-use PFTools::Update;
+use PFTools::Logger;
+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/network/interfaces';
+my $GLOBAL_STRUCT		= {};
+
+my $program = $0;
+$program =~ s%.*/%%; # cheap basename
+
+my $version = sprintf( "svn-r%s", q$Revision$ =~ /([\d.]+)/ );
+
+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
 }
 
-Mk_interfaces( $host, $dst, Init_lib_net( Get_source($config) ) );
+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 ();
+}
+
+if ( $GLOBAL_STORE_FILE eq '' ) {
+	$GLOBAL_STRUCT = Retrieve_GLOBAL ( $PF_CONFIG->{'path'}->{'global_struct'} );
+}
+elsif ( ! -e $GLOBAL_STORE_FILE ) {
+	Abort ( $CODE->{'OPEN'},
+		"Unable to open global configuration storable file ".$GLOBAL_STORE_FILE." : no such file or directory" );
+}
+else {
+	$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 $iface = Mk_interfaces ( $HOSTNAME, $GLOBAL_STRUCT, $PF_CONFIG, $SITE );
+
+unless ( open ( IFACE, ">".$OUTPUT_FILE ) ) {
+	die "Unable to open interface file ".$OUTPUT_FILE."\n";
+}
+print IFACE "#################################################\n";
+print IFACE "# File was auto-generated with mk_interfaces tool\n";
+print IFACE "#\n\n";
+foreach my $if ( @{$iface->{'__order'}} ) {
+	print IFACE join ( "\n", @{$iface->{$if}} );
+	print IFACE "\n\n";
+}
+close ( IFACE );
+
+exit 0;

Added: branches/next-gen/sbin/mk_sitezone
URL: http://svn.debian.org/wsvn/pf-tools/branches/next-gen/sbin/mk_sitezone?rev=784&op=file
==============================================================================
--- branches/next-gen/sbin/mk_sitezone (added)
+++ branches/next-gen/sbin/mk_sitezone Mon Jul 26 12:31:39 2010
@@ -1,0 +1,113 @@
+#!/usr/bin/perl
+##
+##  $Id$
+##
+##  Copyright (C) 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
+##  as published by the Free Software Foundation; either version 2
+##  of the License, or (at your option) any later version.
+##
+##  This program is distributed in the hope that it will be useful,
+##  but WITHOUT ANY WARRANTY; without even the implied warranty of
+##  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+##  GNU General Public License for more details.
+##
+##  You should have received a copy of the GNU General Public License
+##  along with this program; if not, write to the Free Software
+##  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
+##
+
+use strict;
+use warnings;
+
+use strict;
+use warnings;
+
+use Getopt::Long qw( :config ignore_case_always bundling );
+use PFTools::Conf;
+use PFTools::Net;
+use PFTools::Logger;
+use Sys::Hostname;
+
+#################################
+# VARS
+my $HELP				= 0;
+my $SITE				= '';
+my $GLOBAL_STORE_FILE	= '';
+my $PF_CONFIG_FILE		= '';
+my $PF_CONFIG			= {};
+my $OUTPUT_FILE			= '';
+my $GLOBAL_STRUCT		= {};
+
+my $program = $0;
+$program =~ s%.*/%%; # cheap basename
+
+my $version = sprintf( "svn-r%s", q$Revision$ =~ /([\d.]+)/ );
+
+sub Do_help {
+    print STDERR << "# ENDHELP";
+    $program - version $version
+
+Usage:	$0 [options]
+	--help		: print help and exit
+	-s --site	: site on which hostname is defined
+	--store		: file where global structure datas are in storable format (optional)
+	-c --config	: pf-tools config file (optional)
+	-o --output	: output file default value is /etc/network/interfaces
+    
+# ENDHELP
+}
+
+GetOptions (
+	'help'			=> \$HELP,
+	'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 ( $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 ();
+}
+
+if ( $GLOBAL_STORE_FILE eq '' ) {
+	$GLOBAL_STRUCT = Retrieve_GLOBAL ( $PF_CONFIG->{'path'}->{'global_struct'} );
+}
+elsif ( ! -e $GLOBAL_STORE_FILE ) {
+	Abort ( $CODE->{'OPEN'},
+		"Unable to open global configuration storable file ".$GLOBAL_STORE_FILE." : no such file or directory" );
+}
+else {
+	$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'} ) {
+	Abort ( $CODE->{'UNDEF_KEY'},
+		"A site MUST BE defined for building DNS zone forward" );
+}
+
+my $zone_name = Get_site_zone_from_GLOBAL ( $SITE, $GLOBAL_STRUCT );
+my $zone = Mk_zone_for_site ( $zone_name, $SITE, $GLOBAL_STRUCT );
+
+unless ( open ( ZONE, ">".$OUTPUT_FILE ) ) {
+	Abort ( $CODE->{'OPEN'},
+		"Unable to open zone file ".$OUTPUT_FILE );
+}
+print ZONE join ( "\n", @{$zone} );
+close ( ZONE );
+
+exit 0;

Propchange: branches/next-gen/sbin/mk_sitezone
------------------------------------------------------------------------------
--- svn:keywords (added)
+++ svn:keywords Mon Jul 26 12:31:39 2010
@@ -1,0 +1,2 @@
+Id
+Revision




More information about the pf-tools-commits mailing list