pf-tools commit: r670 [ccaillet-guest] - in /trunk: debian/changelog lib/PFTools/Net.pm lib/PFTools/Update.pm sbin/mk_grubopt

parmelan-guest at users.alioth.debian.org parmelan-guest at users.alioth.debian.org
Mon Nov 17 16:40:03 UTC 2008


Author: ccaillet-guest
Date: Mon Nov 17 16:40:03 2008
New Revision: 670

URL: http://svn.debian.org/wsvn/pf-tools/?sc=1&rev=670
Log:
* Adding function for comparing routing table against CVS configuration
* Adding mk_grubopt for inserting into GRUB configuration file the kernel
  options defined in cmdline into private-network definitions 

Added:
    trunk/sbin/mk_grubopt   (with props)
Modified:
    trunk/debian/changelog
    trunk/lib/PFTools/Net.pm
    trunk/lib/PFTools/Update.pm

Modified: trunk/debian/changelog
URL: http://svn.debian.org/wsvn/pf-tools/trunk/debian/changelog?rev=670&op=diff
==============================================================================
--- trunk/debian/changelog (original)
+++ trunk/debian/changelog Mon Nov 17 16:40:03 2008
@@ -16,8 +16,11 @@
     Get_Host_Props
   * Remove useless Getopt::Long on Update.pm
   * Remove useless ethtool command for bond interfaces
-
- -- Christophe Caillet <quadchris at free.fr>  Tue, 04 Nov 2008 12:21:43 +0100
+  * Adding function for comparing routing table against CVS configuration
+  * Adding mk_grubopt for inserting into GRUB configuration file the kernel
+    options defined in cmdline into private-network definitions 
+
+ -- Christophe Caillet <quadchris at free.fr>  Mon, 17 Nov 2008 17:37:58 +0100
 
 pf-tools (0.33.1-1) unstable; urgency=low
 

Modified: trunk/lib/PFTools/Net.pm
URL: http://svn.debian.org/wsvn/pf-tools/trunk/lib/PFTools/Net.pm?rev=670&op=diff
==============================================================================
--- trunk/lib/PFTools/Net.pm (original)
+++ trunk/lib/PFTools/Net.pm Mon Nov 17 16:40:03 2008
@@ -2,7 +2,7 @@
 ##
 ##  $Id$
 ##
-##  Copyright (C) 2007 Christophe Caillet <tof at sitadelle.com>
+##  Copyright (C) 2007 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>
@@ -49,6 +49,8 @@
 
     Mk_interfaces
     Mk_zone
+    Parse_routing_table
+    Cmp_routing_table
 );
 
 our @EXPORT_OK = qw();
@@ -2051,5 +2053,92 @@
     $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: trunk/lib/PFTools/Update.pm
URL: http://svn.debian.org/wsvn/pf-tools/trunk/lib/PFTools/Update.pm?rev=670&op=diff
==============================================================================
--- trunk/lib/PFTools/Update.pm (original)
+++ trunk/lib/PFTools/Update.pm Mon Nov 17 16:40:03 2008
@@ -2,7 +2,7 @@
 ##
 ##  $Id$
 ##
-##  Copyright (C) 2007 Christophe Caillet <tof at sitadelle.com>
+##  Copyright (C) 2007 Christophe Caillet <quadchris at free.fr>
 ##  Copyright (C) 2004-2007 Thomas Parmelan <tom+pf-tools at ankh.fr.EU.org>
 ##  Copyright (C) 2004 Gonéri Le Bouder <goneri at sitadelle.com>
 ##  Copyright (C) 2003-2005 Damien Clermonte <damien at sitadelle.com>

Added: trunk/sbin/mk_grubopt
URL: http://svn.debian.org/wsvn/pf-tools/trunk/sbin/mk_grubopt?rev=670&op=file
==============================================================================
--- trunk/sbin/mk_grubopt (added)
+++ trunk/sbin/mk_grubopt Mon Nov 17 16:40:03 2008
@@ -1,0 +1,133 @@
+#!/usr/bin/perl
+##
+##  $Id: mk_pxelinuxcfg 646 2008-09-10 12:51:10Z ccaillet-guest $
+##
+##  Copyright (C) 2007-2008 Christophe Caillet <quadchris at free.fr>
+##  Copyright (C) 2003-2005 Damien Clermonte <damien at sitadelle.com>
+##  Copyright (C) 2001-2003 Olivier Molteni <olivier at molteni.net>
+##
+##  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 Sys::Hostname ;
+use Digest::MD5;
+use PFTools::Net;
+use PFTools::Update;
+use File::Compare ;
+use File::Copy ;
+use Getopt::Long qw( :config ignore_case_always bundling );
+
+my $program = $0;
+$program =~ s%.*/%%; # cheap basename
+
+my $version = sprintf( "svn-r%s", q$Revision: 539 $ =~ /([\d.]+)/ );
+
+my $MENU_GRUB	= "/boot/grub/menu.lst" ;
+my $HOSTNAME	= hostname ;
+my $DEST	= "-" ;
+my $HELP	= 0 ;
+my $DEBUG	= 0 ;
+my $VERBOSE	= 0 ;
+
+sub Display_usage () {
+print STDERR << "# ENDHELP";
+    $program - version $version
+
+Usage:	$0 [options]
+	-h --help:	print help and exit
+	-v --verbose:	be more verbose
+	-s --src	source for GRUB configuration (default: /boot/grub/menu.lst)
+	-d --dst	destination for modified GRUB configuration (default: /boot/grub/menu.lst)
+	--host		hostname on which GRUB configuration applied
+# ENDHELP
+}
+
+
+sub Change_kopt ($$$) {
+	my ( $cmd_line, $menulst, $dst ) = @_ ;
+
+	unless ( open ( MENU, $menulst ) ) {
+		warn "Unable top open ".$menulst."\n" ;
+		return 0 ;
+	}
+	my @tmp_grub = <MENU> ;
+	foreach ( @tmp_grub ) {
+		chomp ;
+		next if ( ! /^\# kopt=.*$/ ) ;
+		s/$/ $cmd_line/ ;
+	}
+	
+	if ( $dst eq '-' ) {
+		foreach ( @tmp_grub ) {
+			print $_."\n" ;
+		}
+	}
+	else {
+		unless ( open ( TMPDST, ">/tmp/menulst" ) ) {
+			warn "Unable to open temporary destination file /tmp/menulst\n" ;
+			return 0 ;
+		}
+		foreach ( @tmp_grub ) {
+			print TMPDST $_."\n" ;
+		}
+		close ( TMPDST ) ;
+		if ( compare ( '/tmp/menulst', $dst ) ) {
+			return move ( '/tmp/menulst', $dst) ;
+		} else {
+			if ( $DEBUG ) {
+				warn "No need to move /tmp/menulst to ".$dst." they are equals\n" ;
+				warn "Unlinking source file /tmp/menulst\n" ;
+			}
+			if ( ! unlink ( '/tmp/menulst' ) ) {
+				warn "Unable to unlink source file /tmp/menulst\n" ;
+				warn "Please clean it manually\n" ;
+			}
+		}
+		return 1 ;
+	}
+}
+
+##### MAIN
+GetOptions(
+    'src|s=s'	=> \$MENU_GRUB,
+    'dst|d=s'	=> \$DEST,
+    'host=s'	=> \$HOSTNAME,
+    'debug'	=> \$DEBUG,
+    'help|h'	=> \$HELP,
+    'verbose|v'	=> \$VERBOSE
+) or die "GetOptions error, try --help: $!\n";
+
+if ( $HELP ) {
+	Display_usage () ;
+	exit 0 ;
+}
+elsif ( ! -e $MENU_GRUB ) {
+	die "Unexistant GRUB configuration ".$MENU_GRUB."\n" ;
+}
+elsif ( -z $MENU_GRUB ) {
+	die "Empty configuration file for GRUB ".$MENU_GRUB."\n" ;
+}
+
+$DEST = "-" if ( $DEBUG ) ;
+$VERBOSE = 1 if ( $DEBUG ) ;
+
+# my $NETWORK = Init_lib_net ( Get_source ( "GLOBAL:private-network" ) ) ;
+my $NETWORK = Init_lib_net ( Get_source ( "/home/totof/arch/pf-tools/test/private-network" ) ) ;
+if ( ! Change_kopt ( Get_Cmdline ( $NETWORK, $HOSTNAME ), $MENU_GRUB, $DEST ) ) {
+	die "Unable to change kernel options(s) into file ".$MENU_GRUB."\n" ;
+}

Propchange: trunk/sbin/mk_grubopt
------------------------------------------------------------------------------
    svn:executable = *




More information about the pf-tools-commits mailing list