pf-tools commit: r795 [ccaillet-guest] - in /branches/next-gen: debian/ lib/PFTools/ sbin/
parmelan-guest at users.alioth.debian.org
parmelan-guest at users.alioth.debian.org
Mon Aug 2 12:08:43 UTC 2010
Author: ccaillet-guest
Date: Mon Aug 2 12:08:40 2010
New Revision: 795
URL: http://svn.debian.org/wsvn/pf-tools/?sc=1&rev=795
Log:
* lib/PFTools/Utils.pm
- new library for handling functions used by scripts, filters, and tools
Added:
branches/next-gen/lib/PFTools/Utils.pm
Modified:
branches/next-gen/debian/changelog
branches/next-gen/lib/PFTools/Conf.pm
branches/next-gen/lib/PFTools/Host.pm
branches/next-gen/sbin/mk_dhcp
branches/next-gen/sbin/mk_grubopt
branches/next-gen/sbin/mk_interfaces
branches/next-gen/sbin/mk_pxelinuxcfg
branches/next-gen/sbin/mk_resolvconf
branches/next-gen/sbin/mk_sitezone
branches/next-gen/sbin/mk_sourceslist
Modified: branches/next-gen/debian/changelog
URL: http://svn.debian.org/wsvn/pf-tools/branches/next-gen/debian/changelog?rev=795&op=diff
==============================================================================
--- branches/next-gen/debian/changelog (original)
+++ branches/next-gen/debian/changelog Mon Aug 2 12:08:40 2010
@@ -45,6 +45,8 @@
- using new packages Parser.pm et Logger.pm
* lib/PFTools/Update.pm
- using new packages Parser.pm et Logger.pm
+ * lib/PFTools/Utils.pm
+ - new library for handling functions used by scripts, filters, and tools
* 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
@@ -67,7 +69,7 @@
* debian/compat
- update level for avoiding warning during package build
- -- Christophe Caillet <quadchris at free.fr> Fri, 30 Jul 2010 11:44:23 +0200
+ -- Christophe Caillet <quadchris at free.fr> Mon, 02 Aug 2010 14:07:05 +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=795&op=diff
==============================================================================
--- branches/next-gen/lib/PFTools/Conf.pm (original)
+++ branches/next-gen/lib/PFTools/Conf.pm Mon Aug 2 12:08:40 2010
@@ -62,7 +62,6 @@
Load_conf
Flush2disk_GLOBAL
Retrieve_GLOBAL
- Get_host_config_from_CONFIG
);
##########################
@@ -901,31 +900,31 @@
# - $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 ) = @_ ;
-
- if ( ! defined $site ) {
- my $site_list = Get_site_from_hostname ( $hostname, $global_config );
- if ( ! defined $site_list ) {
- Warn ( $CODE->{'UNDEF_KEY'},
- "Unable to retrieve site for hostname ".$hostname." : hostname not defined" );
- }
- elsif ( scalar @{$site_list} > 1 ) {
- Warn ( $CODE->{'UNDEF_KEY'},
- "Unable to retrieve site for hostname ".$hostname." : hostname appeared in multiple sites : "
- .join ( ",", @{$site_list} ) );
- }
- else {
- ( $site ) = @{$site_list};
- }
- }
- my $site_part = $global_config->{'SITE'}->{'BY_NAME'}->{$site};
- my $zone = $site_part->{'zone'};
- $hostname =~ /^([^.]+)(\.([^.]+))?(\.$zone)?$/ ;
- my ( $hostshort, $hostvlan ) = ( $1, $3 ) ;
- my $hosttype = Get_hosttype_from_hostname ( $hostname, $global_config );
- return $site_part->{'HOST'}->{'BY_NAME'}->{$hosttype}->{$hostshort} ;
-}
+# sub Get_host_config_from_CONFIG ($$;$) {
+# my ( $hostname, $global_config, $site ) = @_ ;
+#
+# if ( ! defined $site ) {
+# my $site_list = Get_site_from_hostname ( $hostname, $global_config );
+# if ( ! defined $site_list ) {
+# Warn ( $CODE->{'UNDEF_KEY'},
+# "Unable to retrieve site for hostname ".$hostname." : hostname not defined" );
+# }
+# elsif ( scalar @{$site_list} > 1 ) {
+# Warn ( $CODE->{'UNDEF_KEY'},
+# "Unable to retrieve site for hostname ".$hostname." : hostname appeared in multiple sites : "
+# .join ( ",", @{$site_list} ) );
+# }
+# else {
+# ( $site ) = @{$site_list};
+# }
+# }
+# my $site_part = $global_config->{'SITE'}->{'BY_NAME'}->{$site};
+# my $zone = $site_part->{'zone'};
+# $hostname =~ /^([^.]+)(\.([^.]+))?(\.$zone)?$/ ;
+# my ( $hostshort, $hostvlan ) = ( $1, $3 ) ;
+# my $hosttype = Get_hosttype_from_hostname ( $hostname, $global_config );
+# return $site_part->{'HOST'}->{'BY_NAME'}->{$hosttype}->{$hostshort} ;
+# }
# Print_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=795&op=diff
==============================================================================
--- branches/next-gen/lib/PFTools/Host.pm (original)
+++ branches/next-gen/lib/PFTools/Host.pm Mon Aug 2 12:08:40 2010
@@ -27,14 +27,11 @@
our @ISA = ('Exporter');
our @EXPORT = qw(
- Get_site_from_hostname
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();
@@ -185,27 +182,27 @@
# Output :
# Returns an array ref containing the sites list or undef if hostname doesn't exist
#
-sub Get_site_from_hostname ($$) {
- my ( $hostname, $global_config ) = @_;
- my $site_list;
-
- foreach my $site ( @{$global_config->{'SITE'}->{'__site_list'}} ) {
- my $host_part = $global_config->{'SITE'}->{'BY_NAME'}->{$site}->{'HOST'}->{'BY_NAME'};
- foreach my $hostclass ( keys %{$host_part} ) {
- if ( $hostclass eq $hostname ) {
- push ( @{$site_list}, $site ) if ( ! grep ( /^$site$/, @{$site_list} ) );
- next;
- }
- foreach my $host ( keys %{$host_part->{$hostclass}} ) {
- if ( $host eq $hostname ) {
- push ( @{$site_list}, $site ) if ( ! grep ( /^$site$/, @{$site_list} ) );
- last;
- }
- }
- }
- }
- return $site_list;
-}
+# sub Get_site_from_hostname ($$) {
+# my ( $hostname, $global_config ) = @_;
+# my $site_list;
+#
+# foreach my $site ( @{$global_config->{'SITE'}->{'__site_list'}} ) {
+# my $host_part = $global_config->{'SITE'}->{'BY_NAME'}->{$site}->{'HOST'}->{'BY_NAME'};
+# foreach my $hostclass ( keys %{$host_part} ) {
+# if ( $hostclass eq $hostname ) {
+# push ( @{$site_list}, $site ) if ( ! grep ( /^$site$/, @{$site_list} ) );
+# next;
+# }
+# foreach my $host ( keys %{$host_part->{$hostclass}} ) {
+# if ( $host eq $hostname ) {
+# push ( @{$site_list}, $site ) if ( ! grep ( /^$site$/, @{$site_list} ) );
+# last;
+# }
+# }
+# }
+# }
+# return $site_list;
+# }
#########################################################################
#
@@ -913,72 +910,72 @@
}
}
-sub Get_cmdline_from_hostprops ($) {
- my ( $host_props ) = @_;
- my ( $bond_cmdline, $cmdline );
-
- $cmdline = $host_props->{'boot'}->{'cmdline'};
- foreach my $iface ( sort keys %{$host_props->{'interfaces'}} ) {
- next if ( $iface !~ /^bond/ );
- $bond_cmdline = "bonding.mode=".$host_props->{'interfaces'}->{$iface}->{'mode'}." ";
- foreach my $opt ( split ( /\s*,\s*/, $host_props->{'interfaces'}->{$iface}->{'options'} ) ) {
- $bond_cmdline .= "bonding.".$opt." ";
- }
- $bond_cmdline =~ s/\s*$//;
- last;
- }
- 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;
-}
+# sub Get_cmdline_from_hostprops ($) {
+# my ( $host_props ) = @_;
+# my ( $bond_cmdline, $cmdline );
+#
+# $cmdline = $host_props->{'boot'}->{'cmdline'};
+# foreach my $iface ( sort keys %{$host_props->{'interfaces'}} ) {
+# next if ( $iface !~ /^bond/ );
+# $bond_cmdline = "bonding.mode=".$host_props->{'interfaces'}->{$iface}->{'mode'}." ";
+# foreach my $opt ( split ( /\s*,\s*/, $host_props->{'interfaces'}->{$iface}->{'options'} ) ) {
+# $bond_cmdline .= "bonding.".$opt." ";
+# }
+# $bond_cmdline =~ s/\s*$//;
+# last;
+# }
+# 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 ($$;$) {
Added: branches/next-gen/lib/PFTools/Utils.pm
URL: http://svn.debian.org/wsvn/pf-tools/branches/next-gen/lib/PFTools/Utils.pm?rev=795&op=file
==============================================================================
--- branches/next-gen/lib/PFTools/Utils.pm (added)
+++ branches/next-gen/lib/PFTools/Utils.pm Mon Aug 2 12:08:40 2010
@@ -1,0 +1,754 @@
+package PFTools::Utils;
+##
+## $Id: Net.pm 786 2010-07-27 15:16:09Z ccaillet-guest $
+##
+## 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 Exporter;
+
+our @ISA = ('Exporter');
+
+our @EXPORT = qw(
+ Init_TOOLS
+
+ Get_host_config_from_CONFIG
+ Get_site_from_hostname
+ Get_cmdline_from_hostprops
+
+ Mk_dhcp
+ Mk_interfaces
+ Mk_PXE_bootfile
+ Mk_sourceslist
+ Mk_resolvconf
+ Mk_zone_for_site
+ Change_kopt_for_hostname
+);
+
+our @EXPORT_OK = qw();
+
+use Fcntl ':mode';
+use POSIX qw(ceil floor);
+
+use PFTools::Logger;
+use PFTools::Net;
+use PFTools::Conf;
+use PFTools::Host;
+use Data::Dumper;
+use Template::Tiny;
+use File::Compare;
+use File::Copy;
+use Digest::MD5;
+use Net::DNS;
+
+#########################################################################
+# Prototypes : needed by recursive calls
+
+sub Resolv_hostname_from_GLOBAL ($$;$$);
+
+#########################################################################
+# Functions
+
+sub Init_TOOLS ($$) {
+ my ( $pf_config_file, $global_store_file ) = @_;
+ my ( $pf_config, $global_struct );
+
+ 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 );
+ }
+ return ( $pf_config, $global_struct );
+}
+
+#########################################################################
+#
+# 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 ) = @_ ;
+
+ if ( ! defined $site ) {
+ my $site_list = Get_site_from_hostname ( $hostname, $global_config );
+ if ( ! defined $site_list ) {
+ Warn ( $CODE->{'UNDEF_KEY'},
+ "Unable to retrieve site for hostname ".$hostname." : hostname not defined" );
+ }
+ elsif ( scalar @{$site_list} > 1 ) {
+ Warn ( $CODE->{'UNDEF_KEY'},
+ "Unable to retrieve site for hostname ".$hostname." : hostname appeared in multiple sites : "
+ .join ( ",", @{$site_list} ) );
+ }
+ else {
+ ( $site ) = @{$site_list};
+ }
+ }
+ my $site_part = $global_config->{'SITE'}->{'BY_NAME'}->{$site};
+ my $zone = $site_part->{'zone'};
+ $hostname =~ /^([^.]+)(\.([^.]+))?(\.$zone)?$/ ;
+ my ( $hostshort, $hostvlan ) = ( $1, $3 ) ;
+ my $hosttype = Get_hosttype_from_hostname ( $hostname, $global_config );
+ return $site_part->{'HOST'}->{'BY_NAME'}->{$hosttype}->{$hostshort} ;
+}
+
+#########################################################################
+#
+# STR Get_site_from_hostname ( STR, STR, STR, STR, HASHREF )
+#
+# This function returns the sites list for a given hostname
+# Inputs :
+# - $hostname : string containing the model definition for building hostname
+# - $global_config : hashref containing the parsed global configuration
+#
+# Output :
+# Returns an array ref containing the sites list or undef if hostname doesn't exist
+#
+sub Get_site_from_hostname ($$) {
+ my ( $hostname, $global_config ) = @_;
+ my $site_list;
+
+ foreach my $site ( @{$global_config->{'SITE'}->{'__site_list'}} ) {
+ my $host_part = $global_config->{'SITE'}->{'BY_NAME'}->{$site}->{'HOST'}->{'BY_NAME'};
+ foreach my $hostclass ( keys %{$host_part} ) {
+ if ( $hostclass eq $hostname ) {
+ push ( @{$site_list}, $site ) if ( ! grep ( /^$site$/, @{$site_list} ) );
+ next;
+ }
+ foreach my $host ( keys %{$host_part->{$hostclass}} ) {
+ if ( $host eq $hostname ) {
+ push ( @{$site_list}, $site ) if ( ! grep ( /^$site$/, @{$site_list} ) );
+ last;
+ }
+ }
+ }
+ }
+ return $site_list;
+}
+
+sub Get_cmdline_from_hostprops ($) {
+ my ( $host_props ) = @_;
+ my ( $bond_cmdline, $cmdline );
+
+ $cmdline = $host_props->{'boot'}->{'cmdline'};
+ foreach my $iface ( sort keys %{$host_props->{'interfaces'}} ) {
+ next if ( $iface !~ /^bond/ );
+ $bond_cmdline = "bonding.mode=".$host_props->{'interfaces'}->{$iface}->{'mode'}." ";
+ foreach my $opt ( split ( /\s*,\s*/, $host_props->{'interfaces'}->{$iface}->{'options'} ) ) {
+ $bond_cmdline .= "bonding.".$opt." ";
+ }
+ $bond_cmdline =~ s/\s*$//;
+ last;
+ }
+ return ( $cmdline, $bond_cmdline );
+}
+
+sub Get_kpkg_from_kernel ($$) {
+ my ( $pxefilename, $deploymode ) = @_ ;
+
+ if ( $deploymode =~ /^ubuntu/ ) {
+ $pxefilename =~ /vmlinuz-(.+)$/;
+ return "linux-image-".$1;
+ }
+ else {
+ if ( $pxefilename =~ /pxe/ ) {
+ my ( $vm, $type, $pxe, $version, @append ) = split ( /-/, $pxefilename ) ;
+ return "linux-image-".$version."-".$type."-".join ( "-", @append )."-grsec" ;
+ }
+ else {
+ my ( $vm, $version, $type, $append_version ) = split ( /-/, $pxefilename ) ;
+ return "linux-image-".$version."-".$type."-".$append_version."-grsec" ;
+ }
+ }
+}
+
+sub Build_preseed_filename ($$$$$) {
+ my ( $srv_name, $preseed_tpl, $host_props, $default_preseed, $pf_script ) = @_ ;
+
+ if ( ! open ( PRESEED_TPL, $preseed_tpl ) ) {
+ Warn ( $CODE->{'OPEN'},
+ "Unable to get preseed template from file ".$preseed_tpl );
+ return $default_preseed ;
+ }
+ my $preseed_content = join '', <PRESEED_TPL> ;
+ close ( PRESEED_TPL ) ;
+ my $kernel_pkg = Get_kpkg_from_kernel ( $host_props->{'boot'}->{'kernel'}, $host_props->{'deployment'}->{'mode'} );
+ my $tpl = Template::Tiny->new ( TRIM => 1 );
+ my $preseed_subst = {
+ 'kernelpkg' => $kernel_pkg,
+ 'mode' => $host_props->{'deployment'}->{'mode'},
+ 'distrib' => $host_props->{'deployment'}->{'distrib'},
+ 'config_script' => $pf_script
+ };
+ $preseed_content = $tpl->process ( \$preseed_content, $preseed_subst );
+
+ if ( ! open ( DST_PRESEED, ">/tmp/tmp_preseed" ) ) {
+ Warn ( $CODE->{'OPEN'},
+ "Unable to create preseed file /tmp_preseed" );
+ return $default_preseed ;
+ }
+ print DST_PRESEED $preseed_content ;
+ close ( DST_PRESEED ) ;
+ my $src = "/tmp/tmp_preseed" ;
+ my $dst = $PF_CONFIG->{'path'}->{'preseed_dir'}."/preseed_".$srv_name ;
+ if ( compare ( $src, $dst ) ) {
+ move ($src, $dst) ;
+ } else {
+ if ( ! unlink ( $src ) ) {
+ Warn ( $CODE->{'UNLINK'},
+ "Unable to unlink source file ".$src );
+ }
+ }
+ return "preseed_".$srv_name ;
+}
+
+sub Get_MD5SUM_for_preseedfile ($) {
+ my ( $filename ) = @_;
+ my ( $md5, $hdl );
+
+ $md5 = Digest::MD5->new;
+ open $hdl, $PF_CONFIG->{'path'}->{'preseed_dir'}."/".$filename || return undef;
+ $md5->addfile ( $hdl );
+ my $md5sum = $md5->hexdigest;
+ close ( $hdl );
+ return $md5sum;
+}
+
+sub Mk_PXE_bootfile ($$$$$$) {
+ my ( $hostname, $host_props, $pxe_tpl, $preseed_tpl, $default_preseed, $pf_script ) = @_;
+
+ my $iface = Get_iface_vlan_from_hostname ( $host_props->{'deployment'}->{'dhcpvlan'}, $host_props );
+ my $mac = $host_props->{'interfaces'}->{$iface}->{'mac'};
+ my $pxe_boot_file = $mac;
+ $pxe_boot_file =~ s/\:/\-/g;
+
+ if ( ! -e $pxe_tpl ) {
+ Abort ( $CODE->{'UNDEF_KEY'},
+ "Unable to open PXE template file ".$pxe_tpl." : no such file or directory" );
+ }
+ elsif ( ! open ( PXETPL, $pxe_tpl ) ) {
+ Abort ( $CODE->{'OPEN'},
+ "Unable to open PXE template file ".$pxe_tpl );
+ }
+ my $content_pxe = join ( "", <PXETPL> );
+ close ( PXETPL );
+ my $preseed = Build_preseed_filename ( $hostname, $preseed_tpl, $host_props, $default_preseed, $pf_script );
+ my $preseed_md5 = Get_MD5SUM_for_preseedfile ( $preseed );
+ my $tpl = Template::Tiny->new ( TRIM => 1 );
+ my $pxe_subst = {
+ 'iface' => $iface,
+ 'mode' => $host_props->{'deployment'}->{'mode'}.'-installer',
+ 'arch' => $host_props->{'deployment'}->{'arch'},
+ 'distrib' => $host_props->{'deployment'}->{'distrib'},
+ 'serial_speed' => '115200',
+ 'preseed_url' => $preseed,
+ 'preseed_md5' => $preseed_md5,
+ 'console' => $host_props->{'boot'}->{'console'},
+ 'install_cmdline' => $host_props->{'boot'}->{'cmdline'},
+ 'cmdline' => join ( " ", Get_cmdline_from_hostprops ( $host_props ) ),
+ 'kernel' => $host_props->{'boot'}->{'kernel'}
+ };
+ if ( $host_props->{'boot'}->{'initrd'} ) {
+ $pxe_subst->{'initrd'} = $host_props->{'boot'}->{'initrd'};
+ }
+ else {
+ $content_pxe =~ s/initrd=(([^\/]+\/)+)?\[% initrd %\]//gs ;
+ }
+ $content_pxe = $tpl->process ( \$content_pxe, $pxe_subst );
+ if ( ! open ( PXETMP, ">/tmp/tmp_pxe" ) ) {
+ Warn ( $CODE->{'OPEN'},
+ "Unable to open temporary PXE file /tmp/tmp_pxe" );
+ return undef;
+ }
+ print PXETMP $content_pxe;
+ close ( PXETMP );
+ my $src = "/tmp/tmp_pxe" ;
+ my $dst = $PF_CONFIG->{'path'}->{'pxefiles_dir'}."/".$pxe_boot_file ;
+ if ( compare ( $src, $dst ) ) {
+ move ($src, $dst) ;
+ } else {
+ if ( ! unlink ( $src ) ) {
+ Warn ( $CODE->{'UNLINK'},
+ "Unable to unlink source file ".$src );
+ }
+ }
+ return $pxe_boot_file;
+}
+
+###############################################################
+### Building zone file for IPv4 entries
+
+sub __Mk_zoneheader ($$$) {
+ my ( $zone_name, $zone_site, $zone_part ) = @_;
+ my $zone_result = [];
+
+ # SOA
+ push ( @{$zone_result}, ";;" );
+ push ( @{$zone_result}, ";; BIND configuration file for zone : ".$zone_name );
+ push ( @{$zone_result}, ";; Site : ".$zone_site );
+ push ( @{$zone_result}, ";;" );
+ push ( @{$zone_result}, ";; ".$zone_part->{'SOA'}->{'comment'} );
+ push ( @{$zone_result}, "\n;;============================================================================\n" );
+ push ( @{$zone_result}, "\$TTL ".$zone_part->{'SOA'}->{'ttl'} );
+ push ( @{$zone_result}, "\@\t\tIN SOA ".$zone_part->{'SOA'}->{'soa'}." ".$zone_part->{'SOA'}->{'mail'}." (" );
+ my $serial = ( $zone_part->{'SOA'}->{'serial'} eq 'AUTO' ) ? time."\t; Serial" : $zone_part->{'SOA'}->{'serial'};
+ push ( @{$zone_result}, "\t\t\t".$serial );
+ foreach my $spec ( 'refresh', 'retry', 'expire', 'negttl' ) {
+ push ( @{$zone_result}, "\t\t\t".$zone_part->{'SOA'}->{$spec} );
+ }
+ push ( @{$zone_result}, "\t\t)" );
+ push ( @{$zone_result}, "\n" );
+ # NS fields
+ foreach my $nameserver ( @{ $zone_part->{'SOA'}->{'@ns'} } ) {
+ push ( @{$zone_result}, "\t\tIN NS\t".$nameserver );
+ }
+ push ( @{$zone_result}, "\n" );
+ # MX fields
+ foreach my $mx ( @{ $zone_part->{'SOA'}->{'@mx'} } ) {
+ push ( @{$zone_result}, "\t\tIN MX\t".$mx );
+ }
+ push ( @{$zone_result}, "\n" );
+ return $zone_result;
+}
+
+#
+# VOID Mk_zone (STRING $fic_zone, HASHREF $Z)
+#
+# Construit un le fichier $fic_zone pour la zone DNS decrite par la structure $Z
+#
+#==============================================================================================
+sub Mk_zone_for_site ($$$) {
+ my ( $zone_name, $zone_site, $global_config ) = @_;
+ my $zone_result = [];
+
+ # Building Header (SOA, NS an MX fileds)
+ $zone_result = __Mk_zoneheader( $zone_name, $zone_site, $global_config->{'ZONE'}->{'BY_NAME'}->{$zone_name} );
+ my $zone_part = $global_config->{'ZONE'}->{'BY_NAME'}->{$zone_name}->{'BY_SITE'}->{$zone_site};
+
+ ### Building Networks part
+ push ( @{$zone_result}, "\n\n;;" );
+ push ( @{$zone_result}, ";; Networks" );
+ push ( @{$zone_result}, ";;============================================================================\n" );
+
+ foreach my $network ( @{$global_config->{'ZONE'}->{'BY_NAME'}->{$zone_name}->{'__network_order'}->{$zone_site}} ) {
+ my $head = $network;
+ $head .= " : ".$zone_part->{$network}->{'comment'} if ( $zone_part->{$network}->{'comment'} );
+ push ( @{$zone_result}, "; ".$head );
+ push ( @{$zone_result}, ";----------------------------------------------------------------------------" );
+ foreach my $spec ( 'network', 'netmask', 'broadcast', 'gateway' ) {
+ next if ( ! defined $zone_part->{$network}->{$spec} );
+ push ( @{$zone_result}, $spec.".".$network."\t\t".$zone_part->{$network}->{$spec} );
+ }
+ push ( @{$zone_result}, "" );
+ }
+
+ ### Servers
+ push ( @{$zone_result}, "\n\n;;" );
+ push ( @{$zone_result}, ";; Servers" );
+ push ( @{$zone_result}, ";;============================================================================\n" );
+
+ foreach my $server ( @{$global_config->{'ZONE'}->{'BY_NAME'}->{$zone_name}->{'__hostclass_order'}->{$zone_site}} ) {
+ my $head = $server;
+ $head .= " : ".$zone_part->{$server}->{'comment'} if ( $zone_part->{$server}->{'comment'} );
+ push ( @{$zone_result}, "; ".$head );
+ push ( @{$zone_result}, ";----------------------------------------------------------------------------" );
+ foreach my $field ( sort keys %{$zone_part->{$server}} ) {
+ next if ( $field eq 'comment' );
+ if ( ref $zone_part->{$server}->{$field} eq 'ARRAY' ) {
+ foreach my $elt ( @{$zone_part->{$server}->{$field}} ) {
+ push ( @{$zone_result}, $field."\t\t".$elt );
+ }
+ }
+ else {
+ push ( @{$zone_result}, $field."\t".$zone_part->{$server}->{$field} );
+ }
+ }
+ push ( @{$zone_result}, "" );
+ }
+ return $zone_result;
+}
+
+sub Mk_resolvconf ($$$$) {
+ my ( $hostname, $global_config, $site, $output ) = @_;
+
+ my $host_props = Get_host_config_from_CONFIG ( $hostname, $global_config, $site );
+ if ( ! defined $host_props ) {
+ Warn ( $CODE->{'UNDEF_KEY'},
+ "Unable to find hostname ".$hostname." on site ".$site." : no such host definition" );
+ return 0;
+ }
+ my $domain = Get_zone_from_hostname ( $hostname, $global_config, $site );
+ if ( ! defined $domain ) {
+ Warn ( $CODE->{'UNDEF_KEY'},
+ "Unable to get domain from hostname ".$domain );
+ return 0;
+ }
+
+ my @dns = split ( /\s*,\s*/, $host_props->{'dns'}->{'resolver'} );
+
+ unless ( open ( OUTPUT, ">".$output ) ) {
+ Warn ( $CODE->{'OPEN'},
+ "Unable to open output file ".$output );
+ return 0;
+ }
+ 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_config, $site );
+ foreach my $ip ( @{$resolved} ) {
+ print OUTPUT "nameserver ".$ip."\n";
+ }
+ }
+ close ( OUTPUT );
+ return 1;
+}
+
+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;
+}
+
+sub Resolv_hostname_from_DNS ($) {
+ 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;
+}
+
+sub Mk_dhcp ($$) {
+ my ( $header_file, $site_part ) = @_;
+ my $dhcp = [];
+
+ if ( $header_file ne '' ) {
+ if ( ! -e $header_file ) {
+ Abort ( $CODE->{'OPEN'},
+ "Unable to open DHCP header file ".$header_file." : no such file or directory" );
+ }
+ elsif ( ! open ( HEAD, $header_file ) ) {
+ Abort ( $CODE->{'OPEN'},
+ "Unable to open DHCP header file ".$header_file );
+ }
+ foreach ( <HEAD> ) {
+ chomp;
+ push ( @{$dhcp}, $_ );
+ }
+ close ( HEAD );
+ }
+
+ foreach my $vlan ( keys %{$site_part} ) {
+ push ( @{$dhcp}, "subnet ".$site_part->{$vlan}->{'subnet'}
+ ." netmask ".$site_part->{$vlan}->{'netmask'}." {" );
+ if ( $site_part->{$vlan}->{'routers'} ) {
+ push ( @{$dhcp}, "\toption routers ".$site_part->{$vlan}->{'routers'}.";", '' );
+ }
+ foreach my $hostclass ( keys %{$site_part->{$vlan}} ) {
+ next if ( $hostclass =~ /^(subnet|netmask|routers)$/);
+ my $host_part = $site_part->{$vlan}->{$hostclass};
+ foreach my $host ( keys %{$host_part} ) {
+ push ( @{$dhcp}, "\thost ".$host." {");
+ foreach my $def ( @{$host_part->{$host}} ) {
+ push ( @{$dhcp}, "\t\t".$def );
+ }
+ push ( @{$dhcp}, "\t}" );
+ }
+ }
+ push ( @{$dhcp}, "}", '' );
+ }
+ return $dhcp;
+}
+
+sub Mk_sourceslist ($$$$$$$$) {
+ my ( $hostname, $site, $dst, $sections, $template, $backports, $global_config, $pf_config ) = @_ ;
+
+ my $host_props = Get_host_config_from_CONFIG ( $hostname, $global_config, $site );
+ if ( ! defined $host_props ) {
+ Abort ( $CODE->{'UNDEF_KEY'},
+ "Unable to find hostname ".$hostname." on site ".$site." : no such host definition" );
+ }
+ my $tpl = Template::Tiny->new ( TRIM => 1 );
+ my $mode = $host_props->{'deployment'}->{'mode'};
+ if ( $template eq "" ) {
+ $template = $pf_config->{'path'}->{'templates_dir'}.'/'.$pf_config->{$mode}->{'sources_list'};
+ }
+
+ if ( ! open ( SOURCESTPL, $template ) ) {
+ Abort ( $CODE->{'OPEN'},
+ "Unable to get sources.list template from file ".$template );
+ return 1 ;
+ }
+ my $sources_content = join '', <SOURCESTPL>;
+ close ( SOURCESTPL );
+
+ my $sources_subst = {
+ 'mode' => $host_props->{'deployment'}->{'mode'},
+ 'distrib' => $host_props->{'deployment'}->{'distrib'},
+ 'default_sections' => $pf_config->{$mode}->{'default_sections'},
+ 'custom_sections' => $sections
+ };
+ $sources_content = $tpl->process ( \$sources_content, $sources_subst );
+
+ if ( $backports ) {
+ my $back_src = ( $mode eq 'debian' )
+ ? $mode."-backports"
+ : $mode;
+ $sources_content .=
+ "\ndeb http://mirrors.private/".$back_src." ".$host_props->{'deployment'}->{'distrib'}."-backports ".$pf_config->{$mode}->{'default_sections'}."\n" ;
+ }
+
+ if ( ! open ( DST, ">".$dst ) ) {
+ warn "Unable to open destination's sources.list ".$dst."\n" ;
+ return 1 ;
+ }
+ print DST $sources_content ;
+ close ( DST ) ;
+ return 0 ;
+}
+
+sub Change_kopt_for_hostname ($$$$$$) {
+ my ( $hostname, $site, $grub_src, $dst, $grub_version, $global_config ) = @_;
+ my $tmp_grub = [];
+ my ( $cmd_line );
+
+ my $host_props = Get_host_config_from_CONFIG ( $hostname, $global_config, $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 );
+
+ $grub_src = $PF_CONFIG->{$mode}->{'grub'.$grub_version} if ( $grub_src eq '' );
+ if ( ! -e $grub_src ) {
+ Abort ( $CODE->{'UNDEF_KEY'},
+ "Unable to modify GRUB option(s) on file ".$grub_src." : no such file or directory" );
+ }
+
+
+ unless ( open ( MENU, $grub_src ) ) {
+ Warn ( $CODE->{'OPEN'},
+ "Unable to open current file ".$grub_src." for modifying GRUB option(s)" );
+ return 0 ;
+ }
+ @{$tmp_grub} = <MENU> ;
+ close ( MENU );
+
+ foreach ( @{$tmp_grub} ) {
+ chomp ;
+ next if ( $grub_version == 2 && ! /^GRUB_CMDLINE_LINUX_DEFAULT=".*"$/ ) ;
+ next if ( $grub_version == 1 && ! /^\# kopt=.*$/ ) ;
+ s/\"$/ $cmd_line\"/ if ( defined $cmd_line && ! /\Q$cmd_line\E\"$/ ) ;
+ }
+ my $tmp_dst = ( $dst eq "-" ) ? $dst : "/tmp/menulst";
+ unless ( open ( TMPDST, ">".$tmp_dst ) ) {
+ Warn ( $CODE->{'OPEN'},
+ "Unable to open temporary destination file /tmp/menulst" );
+ return 0 ;
+ }
+ print TMPDST join ( "\n", @{$tmp_grub} );
+ close ( TMPDST ) ;
+ if ( $tmp_dst ne "-" ) {
+ if ( compare ( $tmp_dst, $dst ) ) {
+ return move ( $tmp_dst, $dst) ;
+ } else {
+ if ( ! unlink ( $tmp_dst ) ) {
+ Warn ( $CODE->{'UNLINK'},
+ "Unable to unlink source file ".$tmp_dst );
+ }
+ return 1 ;
+ }
+ }
+}
+
+#
+# VOID Mk_interfaces (STRING $host, STRING $fic_iface, HASHREF $Z)
+#
+# Construit le fichier de declaration d'interfaces $fic_iface pour la
+# machine $host a partir des informations contenues dans la structure $Z
+#
+#======================================================================================
+sub Mk_interfaces ($$$;$){
+ my ( $hostname, $global_config, $pf_config, $site ) = @_;
+
+ 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 = {};
+
+ foreach my $iface ( 'lo', sort keys %{$properties->{'interfaces'}} ) {
+ push ( @{$interfaces->{'__order'}}, $iface );
+ my $if_part = $properties->{'interfaces'}->{$iface} if ( defined $properties->{'interfaces'}->{$iface});
+ push ( @{$interfaces->{$iface}}, "auto ".$iface );
+ if ( $if_part->{'method'} ) {
+ push ( @{$interfaces->{$iface}}, "iface ".$iface." inet ".$if_part->{'method'} );
+ }
+ elsif ( $iface eq 'lo' ) {
+ push ( @{$interfaces->{$iface}}, "iface ".$iface." inet loopback" );
+ }
+ else {
+ push ( @{$interfaces->{$iface}}, "iface ".$iface." inet static" );
+ }
+ next if ( ( $if_part->{'method'} && $if_part->{'method'} eq 'dhcp' ) || $iface eq 'lo' );
+ foreach my $ip_type ( 'ipv4', 'ipv6' ) {
+ next if ( ! $pf_config->{'features'}->{$ip_type} );
+ my $suffix = ( $ip_type eq 'ipv6' ) ? '6' : '';
+ my $ip = new NetAddr::IP ( $if_part->{$ip_type} );
+ push ( @{$interfaces->{$iface}}, "\tslaves\t\t".$if_part->{'slaves'} ) if ( $if_part->{'slaves'} );
+ push ( @{$interfaces->{$iface}}, "\taddress\t\t".$ip->addr() );
+ push ( @{$interfaces->{$iface}}, "\tnetmask\t\t".$ip->mask() );
+ my $net = $ip->network(); push ( @{$interfaces->{$iface}}, "\tnetwork\t\t".$net->addr() );
+ my $broad = $ip->broadcast(); push ( @{$interfaces->{$iface}}, "\tbroadcast\t".$broad->addr() );
+ foreach my $route ( @{$if_part->{'@route'.$suffix}} ) {
+ $route =~ /^([^\s]+)\s*(via ([^\s]+))?$/;
+ push ( @{$routes->{$1}}, $iface." ".$route );
+ }
+ if ( $iface =~ /^([^\.]+)\.\d+$/ ) {
+ push ( @{$interfaces->{$iface}}, "\tvlan_raw_device\t".$1 );
+ if ( $if_part->{'iface_opt'} && $if_part->{'iface_opt'} !~ /mtu/ ) {
+ $if_part->{'iface_opt'} .= ",mtu 1496";
+ }
+ else {
+ $if_part->{'iface_opt'} = "mtu 1496";
+ }
+ }
+ if ( defined $if_part->{'iface_opt'} ) {
+ foreach my $option ( split ( /\s*,\s*/, $if_part->{'iface_opt'} ) ) {
+ push ( @{$interfaces->{$iface}}, "\tup\t\t/sbin/ip link set ".$iface." ".$option );
+ }
+ }
+
+ }
+ }
+ foreach my $dest ( keys %{$routes} ) {
+ if ( scalar @{$routes->{$dest}} > 1 ) {
+ foreach my $entry ( @{$routes->{$dest}} ) {
+ my ( $if, $dst, $via, $gw ) = split ( /\s+/, $entry );
+ if ( ! defined $gw ) {
+ Warn ( $CODE->{'UNDEF_KEY'},
+ "Unable to add a route for destination ".$dst." with multiple gateway without gateway definition"
+ ." on hostname ".$hostname );
+ last;
+ }
+ push ( @{$interfaces->{$if}}, "\tup\t\t/sbin/ip route add ".$dst." scope global via ".$gw." dev ".$if );
+ }
+ }
+ else {
+ my ( $entry ) = @{$routes->{$dest}};
+ my ( $if, $dst, $via, $gw ) = split ( /\s+/, $entry );
+ if ( $dst eq 'default' ) {
+ if ( ! defined $gw ) {
+ Abort ( $CODE->{'UNDEF_KEY'},
+ "Unable to define default route without gateway defined for interface ".$if." on hostname ".$hostname );
+ }
+ push ( @{$interfaces->{$if}}, "\tgateway\t\t$gw" );
+ }
+ else {
+ push ( @{$interfaces->{$if}}, "\tup\t\t/sbin/ip route add ".$entry." dev ".$if );
+ }
+ }
+ }
+ return $interfaces;
+}
+
+1;
Modified: branches/next-gen/sbin/mk_dhcp
URL: http://svn.debian.org/wsvn/pf-tools/branches/next-gen/sbin/mk_dhcp?rev=795&op=diff
==============================================================================
--- branches/next-gen/sbin/mk_dhcp (original)
+++ branches/next-gen/sbin/mk_dhcp Mon Aug 2 12:08:40 2010
@@ -25,8 +25,8 @@
use warnings;
use Getopt::Long qw( :config ignore_case_always bundling );
-use PFTools::Conf;
use PFTools::Logger;
+use PFTools::Utils;
#####################################
# Vars
@@ -62,48 +62,6 @@
# ENDHELP
}
-sub Mk_dhcp ($$) {
- my ( $header_file, $site_part ) = @_;
- my $dhcp = [];
-
- if ( $header_file ne '' ) {
- if ( ! -e $header_file ) {
- Abort ( $CODE->{'OPEN'},
- "Unable to open DHCP header file ".$header_file." : no such file or directory" );
- }
- elsif ( ! open ( HEAD, $header_file ) ) {
- Abort ( $CODE->{'OPEN'},
- "Unable to open DHCP header file ".$header_file );
- }
- foreach ( <HEAD> ) {
- chomp;
- push ( @{$dhcp}, $_ );
- }
- close ( HEAD );
- }
-
- foreach my $vlan ( keys %{$site_part} ) {
- push ( @{$dhcp}, "subnet ".$site_part->{$vlan}->{'subnet'}
- ." netmask ".$site_part->{$vlan}->{'netmask'}." {" );
- if ( $site_part->{$vlan}->{'routers'} ) {
- push ( @{$dhcp}, "\toption routers ".$site_part->{$vlan}->{'routers'}.";", '' );
- }
- foreach my $hostclass ( keys %{$site_part->{$vlan}} ) {
- next if ( $hostclass =~ /^(subnet|netmask|routers)$/);
- my $host_part = $site_part->{$vlan}->{$hostclass};
- foreach my $host ( keys %{$host_part} ) {
- push ( @{$dhcp}, "\thost ".$host." {");
- foreach my $def ( @{$host_part->{$host}} ) {
- push ( @{$dhcp}, "\t\t".$def );
- }
- push ( @{$dhcp}, "\t}" );
- }
- }
- push ( @{$dhcp}, "}", '' );
- }
- return $dhcp;
-}
-
############################################
### MAIN
@@ -121,24 +79,7 @@
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 );
-}
+( $PF_CONFIG, $GLOBAL_STRUCT ) = Init_TOOLS ( $PF_CONFIG_FILE, $GLOBAL_STORE_FILE );
if ( $SITE eq '' && ! defined $PF_CONFIG->{'location'}->{'site'} ) {
Abort ( $CODE->{'UNDEF_KEY'},
Modified: branches/next-gen/sbin/mk_grubopt
URL: http://svn.debian.org/wsvn/pf-tools/branches/next-gen/sbin/mk_grubopt?rev=795&op=diff
==============================================================================
--- branches/next-gen/sbin/mk_grubopt (original)
+++ branches/next-gen/sbin/mk_grubopt Mon Aug 2 12:08:40 2010
@@ -25,12 +25,8 @@
use warnings;
use Sys::Hostname ;
-use Digest::MD5;
-use PFTools::Conf;
use PFTools::Logger;
-use PFTools::Host;
-use File::Compare ;
-use File::Copy ;
+use PFTools::Utils;
use Getopt::Long qw( :config ignore_case_always bundling );
####################################################
@@ -71,46 +67,6 @@
# ENDHELP
}
-
-sub Change_kopt ($$$) {
- my ( $cmd_line, $menulst, $dst ) = @_;
- my $tmp_grub = [];
-
- unless ( open ( MENU, $menulst ) ) {
- Warn ( $CODE->{'OPEN'},
- "Unable to open current file ".$menulst." for modifying GRUB option(s)" );
- return 0 ;
- }
- @{$tmp_grub} = <MENU> ;
- close ( MENU );
-
- foreach ( @{$tmp_grub} ) {
- chomp ;
- next if ( $GRUB_VERSION == 2 && ! /^GRUB_CMDLINE_LINUX_DEFAULT=".*"$/ ) ;
- next if ( $GRUB_VERSION == 1 && ! /^\# kopt=.*$/ ) ;
- s/\"$/ $cmd_line\"/ if ( defined $cmd_line && ! /\Q$cmd_line\E\"$/ ) ;
- }
- my $tmp_dst = ( $dst eq "-" ) ? $dst : "/tmp/menulst";
- unless ( open ( TMPDST, ">".$tmp_dst ) ) {
- Warn ( $CODE->{'OPEN'},
- "Unable to open temporary destination file /tmp/menulst" );
- return 0 ;
- }
- print TMPDST join ( "\n", @{$tmp_grub} );
- close ( TMPDST ) ;
- if ( $tmp_dst ne "-" ) {
- if ( compare ( $tmp_dst, $dst ) ) {
- return move ( $tmp_dst, $dst) ;
- } else {
- if ( ! unlink ( $tmp_dst ) ) {
- Warn ( $CODE->{'UNLINK'},
- "Unable to unlink source file ".$tmp_dst );
- }
- return 1 ;
- }
- }
-}
-
#######################################################""
### MAIN
@@ -130,16 +86,7 @@
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 ();
-}
+( $PF_CONFIG, $GLOBAL_STRUCT ) = Init_TOOLS ( $PF_CONFIG_FILE, $GLOBAL_STORE_FILE );
if ( $OUTPUT eq "" ) {
Abort ( $CODE->{'UNDEF_KEY'},
@@ -151,13 +98,6 @@
"Unable to modify grub options for an undefined hostname" );
}
-$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'} ) {
Abort ( $CODE->{'UNDEF_KEY'},
"A site MUST BE defined for building DNS zone forward" );
@@ -167,22 +107,7 @@
"Site ".$SITE." is not defined into global configuration" );
}
-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 );
-
-$MENU_GRUB = $PF_CONFIG->{$mode}->{'grub'.$GRUB_VERSION} if ( $MENU_GRUB eq '' );
-if ( ! -e $MENU_GRUB ) {
- Abort ( $CODE->{'UNDEF_KEY'},
- "Unable to modify GRUB option(s) on file ".$MENU_GRUB." : no such file or directory" );
-}
-
-if ( ! Change_kopt ( $cmdline.$bond_cmdline, $MENU_GRUB, $OUTPUT ) ) {
+if ( ! Change_kopt_for_hostname ( $HOSTNAME, $SITE, $MENU_GRUB, $OUTPUT, $GRUB_VERSION, $GLOBAL_STRUCT ) ) {
Abort ( $CODE->{'EXEC'},
"Unable to change kernel options(s) into file ".$MENU_GRUB );
}
Modified: branches/next-gen/sbin/mk_interfaces
URL: http://svn.debian.org/wsvn/pf-tools/branches/next-gen/sbin/mk_interfaces?rev=795&op=diff
==============================================================================
--- branches/next-gen/sbin/mk_interfaces (original)
+++ branches/next-gen/sbin/mk_interfaces Mon Aug 2 12:08:40 2010
@@ -24,8 +24,7 @@
use warnings;
use Getopt::Long qw( :config ignore_case_always bundling );
-use PFTools::Conf;
-use PFTools::Host;
+use PFTools::Utils;
use PFTools::Logger;
use Sys::Hostname;
@@ -63,102 +62,6 @@
# ENDHELP
}
-#
-# VOID Mk_interfaces (STRING $host, STRING $fic_iface, HASHREF $Z)
-#
-# Construit le fichier de declaration d'interfaces $fic_iface pour la
-# machine $host a partir des informations contenues dans la structure $Z
-#
-#======================================================================================
-sub Mk_interfaces ($$$;$){
- my ( $hostname, $global_config, $pf_config, $site ) = @_;
-
- 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 = {};
-
- foreach my $iface ( 'lo', sort keys %{$properties->{'interfaces'}} ) {
- push ( @{$interfaces->{'__order'}}, $iface );
- my $if_part = $properties->{'interfaces'}->{$iface} if ( defined $properties->{'interfaces'}->{$iface});
- push ( @{$interfaces->{$iface}}, "auto ".$iface );
- if ( $if_part->{'method'} ) {
- push ( @{$interfaces->{$iface}}, "iface ".$iface." inet ".$if_part->{'method'} );
- }
- elsif ( $iface eq 'lo' ) {
- push ( @{$interfaces->{$iface}}, "iface ".$iface." inet loopback" );
- }
- else {
- push ( @{$interfaces->{$iface}}, "iface ".$iface." inet static" );
- }
- next if ( ( $if_part->{'method'} && $if_part->{'method'} eq 'dhcp' ) || $iface eq 'lo' );
- foreach my $ip_type ( 'ipv4', 'ipv6' ) {
- next if ( ! $pf_config->{'features'}->{$ip_type} );
- my $suffix = ( $ip_type eq 'ipv6' ) ? '6' : '';
- my $ip = new NetAddr::IP ( $if_part->{$ip_type} );
- push ( @{$interfaces->{$iface}}, "\tslaves\t\t".$if_part->{'slaves'} ) if ( $if_part->{'slaves'} );
- push ( @{$interfaces->{$iface}}, "\taddress\t\t".$ip->addr() );
- push ( @{$interfaces->{$iface}}, "\tnetmask\t\t".$ip->mask() );
- my $net = $ip->network(); push ( @{$interfaces->{$iface}}, "\tnetwork\t\t".$net->addr() );
- my $broad = $ip->broadcast(); push ( @{$interfaces->{$iface}}, "\tbroadcast\t".$broad->addr() );
- foreach my $route ( @{$if_part->{'@route'.$suffix}} ) {
- $route =~ /^([^\s]+)\s*(via ([^\s]+))?$/;
- push ( @{$routes->{$1}}, $iface." ".$route );
- }
- if ( $iface =~ /^([^\.]+)\.\d+$/ ) {
- push ( @{$interfaces->{$iface}}, "\tvlan_raw_device\t".$1 );
- if ( $if_part->{'iface_opt'} && $if_part->{'iface_opt'} !~ /mtu/ ) {
- $if_part->{'iface_opt'} .= ",mtu 1496";
- }
- else {
- $if_part->{'iface_opt'} = "mtu 1496";
- }
- }
- if ( defined $if_part->{'iface_opt'} ) {
- foreach my $option ( split ( /\s*,\s*/, $if_part->{'iface_opt'} ) ) {
- push ( @{$interfaces->{$iface}}, "\tup\t\t/sbin/ip link set ".$iface." ".$option );
- }
- }
-
- }
- }
- foreach my $dest ( keys %{$routes} ) {
- if ( scalar @{$routes->{$dest}} > 1 ) {
- foreach my $entry ( @{$routes->{$dest}} ) {
- my ( $if, $dst, $via, $gw ) = split ( /\s+/, $entry );
- if ( ! defined $gw ) {
- Warn ( $CODE->{'UNDEF_KEY'},
- "Unable to add a route for destination ".$dst." with multiple gateway without gateway definition"
- ." on hostname ".$hostname );
- last;
- }
- push ( @{$interfaces->{$if}}, "\tup\t\t/sbin/ip route add ".$dst." scope global via ".$gw." dev ".$if );
- }
- }
- else {
- my ( $entry ) = @{$routes->{$dest}};
- my ( $if, $dst, $via, $gw ) = split ( /\s+/, $entry );
- if ( $dst eq 'default' ) {
- if ( ! defined $gw ) {
- Abort ( $CODE->{'UNDEF_KEY'},
- "Unable to define default route without gateway defined for interface ".$if." on hostname ".$hostname );
- }
- push ( @{$interfaces->{$if}}, "\tgateway\t\t$gw" );
- }
- else {
- push ( @{$interfaces->{$if}}, "\tup\t\t/sbin/ip route add ".$entry." dev ".$if );
- }
- }
- }
- return $interfaces;
-}
-
##################################
### MAIN
@@ -176,24 +79,7 @@
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 );
-}
+( $PF_CONFIG, $GLOBAL_STRUCT ) = Init_TOOLS ( $PF_CONFIG_FILE, $GLOBAL_STORE_FILE );
if ( $SITE eq '' && ! defined $PF_CONFIG->{'location'}->{'site'} ) {
my $site_list = Get_site_from_hostname ( $HOSTNAME, $GLOBAL_STRUCT );
Modified: branches/next-gen/sbin/mk_pxelinuxcfg
URL: http://svn.debian.org/wsvn/pf-tools/branches/next-gen/sbin/mk_pxelinuxcfg?rev=795&op=diff
==============================================================================
--- branches/next-gen/sbin/mk_pxelinuxcfg (original)
+++ branches/next-gen/sbin/mk_pxelinuxcfg Mon Aug 2 12:08:40 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>
## Copyright (C) 2003-2005 Damien Clermonte <damien at sitadelle.com>
## Copyright (C) 2001-2003 Olivier Molteni <olivier at molteni.net>
##
@@ -25,13 +25,8 @@
use warnings;
use Getopt::Long qw( :config ignore_case_always bundling );
-use Digest::MD5;
-use PFTools::Conf;
-use PFTools::Host;
+use PFTools::Utils;
use PFTools::Logger;
-use File::Compare ;
-use File::Copy ;
-use Template::Tiny;
use Data::Dumper;
@@ -72,138 +67,6 @@
# ENDHELP
}
-sub Get_kpkg_from_kernel ($$) {
- my ( $pxefilename, $deploymode ) = @_ ;
-
- if ( $deploymode =~ /^ubuntu/ ) {
- $pxefilename =~ /vmlinuz-(.+)$/;
- return "linux-image-".$1;
- }
- else {
- if ( $pxefilename =~ /pxe/ ) {
- my ( $vm, $type, $pxe, $version, @append ) = split ( /-/, $pxefilename ) ;
- return "linux-image-".$version."-".$type."-".join ( "-", @append )."-grsec" ;
- }
- else {
- my ( $vm, $version, $type, $append_version ) = split ( /-/, $pxefilename ) ;
- return "linux-image-".$version."-".$type."-".$append_version."-grsec" ;
- }
- }
-}
-
-sub Build_preseed_filename ($$$) {
- my ( $srv_name, $preseed_tpl, $host_props ) = @_ ;
-
- if ( ! open ( PRESEED_TPL, $preseed_tpl ) ) {
- Warn ( $CODE->{'OPEN'},
- "Unable to get preseed template from file ".$preseed_tpl );
- return $DEFAULT_PRESEED ;
- }
- my $preseed_content = join '', <PRESEED_TPL> ;
- close ( PRESEED_TPL ) ;
- my $kernel_pkg = Get_kpkg_from_kernel ( $host_props->{'boot'}->{'kernel'}, $host_props->{'deployment'}->{'mode'} );
- my $tpl = Template::Tiny->new ( TRIM => 1 );
- my $preseed_subst = {
- 'kernelpkg' => $kernel_pkg,
- 'mode' => $host_props->{'deployment'}->{'mode'},
- 'distrib' => $host_props->{'deployment'}->{'distrib'},
- 'config_script' => $PF_SCRIPT
- };
- $preseed_content = $tpl->process ( \$preseed_content, $preseed_subst );
-
- if ( ! open ( DST_PRESEED, ">/tmp/tmp_preseed" ) ) {
- Warn ( $CODE->{'OPEN'},
- "Unable to create preseed file /tmp_preseed" );
- return $DEFAULT_PRESEED ;
- }
- print DST_PRESEED $preseed_content ;
- close ( DST_PRESEED ) ;
- my $src = "/tmp/tmp_preseed" ;
- my $dst = $PF_CONFIG->{'path'}->{'preseed_dir'}."/preseed_".$srv_name ;
- if ( compare ( $src, $dst ) ) {
- move ($src, $dst) ;
- } else {
- if ( ! unlink ( $src ) ) {
- Warn ( $CODE->{'UNLINK'},
- "Unable to unlink source file ".$src );
- }
- }
- return "preseed_".$srv_name ;
-}
-
-sub Get_MD5SUM_for_preseedfile ($) {
- my ( $filename ) = @_;
- my ( $md5, $hdl );
-
- $md5 = Digest::MD5->new;
- open $hdl, $PF_CONFIG->{'path'}->{'preseed_dir'}."/".$filename || return undef;
- $md5->addfile ( $hdl );
- my $md5sum = $md5->hexdigest;
- close ( $hdl );
- return $md5sum;
-}
-
-sub Mk_PXE_bootfile ($$$$) {
- my ( $hostname, $host_props, $pxe_tpl, $preseed_tpl ) = @_;
-
- my $iface = Get_iface_vlan_from_hostname ( $host_props->{'deployment'}->{'dhcpvlan'}, $host_props );
- my $mac = $host_props->{'interfaces'}->{$iface}->{'mac'};
- my $pxe_boot_file = $mac;
- $pxe_boot_file =~ s/\:/\-/g;
-
- if ( ! -e $pxe_tpl ) {
- Abort ( $CODE->{'UNDEF_KEY'},
- "Unable to open PXE template file ".$pxe_tpl." : no such file or directory" );
- }
- elsif ( ! open ( PXETPL, $pxe_tpl ) ) {
- Abort ( $CODE->{'OPEN'},
- "Unable to open PXE template file ".$pxe_tpl );
- }
- my $content_pxe = join ( "", <PXETPL> );
- close ( PXETPL );
- my $preseed = Build_preseed_filename ( $hostname, $preseed_tpl, $host_props );
- my $preseed_md5 = Get_MD5SUM_for_preseedfile ( $preseed );
- my $tpl = Template::Tiny->new ( TRIM => 1 );
- my $pxe_subst = {
- 'iface' => $iface,
- 'mode' => $host_props->{'deployment'}->{'mode'}.'-installer',
- 'arch' => $host_props->{'deployment'}->{'arch'},
- 'distrib' => $host_props->{'deployment'}->{'distrib'},
- 'serial_speed' => '115200',
- 'preseed_url' => $preseed,
- 'preseed_md5' => $preseed_md5,
- 'console' => $host_props->{'boot'}->{'console'},
- 'install_cmdline' => $host_props->{'boot'}->{'cmdline'},
- 'cmdline' => join ( " ", Get_cmdline_from_hostprops ( $host_props ) ),
- 'kernel' => $host_props->{'boot'}->{'kernel'}
- };
- if ( $host_props->{'boot'}->{'initrd'} ) {
- $pxe_subst->{'initrd'} = $host_props->{'boot'}->{'initrd'};
- }
- else {
- $content_pxe =~ s/initrd=(([^\/]+\/)+)?\[% initrd %\]//gs ;
- }
- $content_pxe = $tpl->process ( \$content_pxe, $pxe_subst );
- if ( ! open ( PXETMP, ">/tmp/tmp_pxe" ) ) {
- Warn ( $CODE->{'OPEN'},
- "Unable to open temporary PXE file /tmp/tmp_pxe" );
- return undef;
- }
- print PXETMP $content_pxe;
- close ( PXETMP );
- my $src = "/tmp/tmp_pxe" ;
- my $dst = $PF_CONFIG->{'path'}->{'pxefiles_dir'}."/".$pxe_boot_file ;
- if ( compare ( $src, $dst ) ) {
- move ($src, $dst) ;
- } else {
- if ( ! unlink ( $src ) ) {
- Warn ( $CODE->{'UNLINK'},
- "Unable to unlink source file ".$src );
- }
- }
- return $pxe_boot_file;
-}
-
############################################
### MAIN
@@ -220,16 +83,7 @@
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 ();
-}
+( $PF_CONFIG, $GLOBAL_STRUCT ) = Init_TOOLS ( $PF_CONFIG_FILE, $GLOBAL_STORE_FILE );
if ( ! -e $PF_CONFIG->{'path'}->{'preseed_dir'} ) {
mkdir $PF_CONFIG->{'path'}->{'preseed_dir'} ;
@@ -238,16 +92,7 @@
my $mode = '2750' ;
chmod ( oct ( $mode ), $PF_CONFIG->{'path'}->{'preseed_dir'} ) ;
}
-
$DEFAULT_PRESEED = $PF_CONFIG->{'path'}->{'preseed_dir'}."/default_preseed.txt" ;
-$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'} ) {
Abort ( $CODE->{'UNDEF_KEY'},
"A site MUST BE defined for building DNS zone forward" );
@@ -261,10 +106,11 @@
my $host_part = $site_part->{'HOST'}->{'BY_NAME'};
foreach my $hostclass ( @{$site_part->{'HOST'}->{'__hostclass_pxe'}} ) {
foreach my $host ( keys %{$host_part->{$hostclass}} ) {
+ next if ( ref $host_part->{$hostclass}->{$host} ne 'HASH' );
my $mode = $host_part->{$hostclass}->{$host}->{'deployment'}->{'mode'};
my $pxe_template = $PF_CONFIG->{'path'}->{'templates_dir'}.'/'.$PF_CONFIG->{$mode}->{'pxe'};
my $preseed_tpl = $PF_CONFIG->{'path'}->{'templates_dir'}.'/'.$PF_CONFIG->{$mode}->{'preseed'};
- my $pxe_file = Mk_PXE_bootfile ( $host, $host_part->{$hostclass}->{$host}, $pxe_template, $preseed_tpl );
+ my $pxe_file = Mk_PXE_bootfile ( $host, $host_part->{$hostclass}->{$host}, $pxe_template, $preseed_tpl, $DEFAULT_PRESEED, $PF_SCRIPT );
}
}
Modified: branches/next-gen/sbin/mk_resolvconf
URL: http://svn.debian.org/wsvn/pf-tools/branches/next-gen/sbin/mk_resolvconf?rev=795&op=diff
==============================================================================
--- branches/next-gen/sbin/mk_resolvconf (original)
+++ branches/next-gen/sbin/mk_resolvconf Mon Aug 2 12:08:40 2010
@@ -24,10 +24,8 @@
use warnings;
use Getopt::Long qw( :config ignore_case_always bundling );
-use PFTools::Conf;
-use PFTools::Host;
+use PFTools::Utils;
use PFTools::Logger;
-use Net::DNS;
use Sys::Hostname;
#################################
@@ -64,10 +62,6 @@
# ENDHELP
}
-sub Mk_resolvconf ($$) {
- my ( $host_props, $zone )
-}
-
##################################
### MAIN
@@ -85,24 +79,7 @@
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 );
-}
+( $PF_CONFIG, $GLOBAL_STRUCT ) = Init_TOOLS ( $PF_CONFIG_FILE, $GLOBAL_STORE_FILE );
if ( $SITE eq '' && ! defined $PF_CONFIG->{'location'}->{'site'} ) {
my $site_list = Get_site_from_hostname ( $HOSTNAME, $GLOBAL_STRUCT );
@@ -121,33 +98,9 @@
}
}
-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 );
+if ( ! Mk_resolvconf ( $HOSTNAME, $GLOBAL_STRUCT, $SITE, $OUTPUT_FILE ) ) {
+ Abort ( $CODE->{'EXEC'},
+ "An error occured during build of file ".$OUTPUT_FILE );
}
-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_sitezone
URL: http://svn.debian.org/wsvn/pf-tools/branches/next-gen/sbin/mk_sitezone?rev=795&op=diff
==============================================================================
--- branches/next-gen/sbin/mk_sitezone (original)
+++ branches/next-gen/sbin/mk_sitezone Mon Aug 2 12:08:40 2010
@@ -26,7 +26,7 @@
use warnings;
use Getopt::Long qw( :config ignore_case_always bundling );
-use PFTools::Conf;
+use PFTools::Utils;
use PFTools::Logger;
#################################
@@ -61,99 +61,6 @@
# ENDHELP
}
-###############################################################
-### Building zone file for IPv4 entries
-
-sub __Mk_zoneheader ($$$) {
- my ( $zone_name, $zone_site, $zone_part ) = @_;
- my $zone_result = [];
-
- # SOA
- push ( @{$zone_result}, ";;" );
- push ( @{$zone_result}, ";; BIND configuration file for zone : ".$zone_name );
- push ( @{$zone_result}, ";; Site : ".$zone_site );
- push ( @{$zone_result}, ";;" );
- push ( @{$zone_result}, ";; ", $zone_part->{'SOA'}->{'comment'} );
- push ( @{$zone_result}, "\n;;============================================================================\n" );
- push ( @{$zone_result}, "\$TTL ".$zone_part->{'SOA'}->{'ttl'} );
- push ( @{$zone_result}, "\@\t\tIN SOA ".$zone_part->{'SOA'}->{'soa'}." ".$zone_part->{'SOA'}->{'mail'}." (" );
- my $serial = ( $zone_part->{'SOA'}->{'serial'} eq 'AUTO' ) ? time."\t; Serial" : $zone_part->{'SOA'}->{'serial'};
- push ( @{$zone_result}, "\t\t\t".$serial );
- foreach my $spec ( 'refresh', 'retry', 'expire', 'negttl' ) {
- push ( @{$zone_result}, "\t\t\t".$zone_part->{'SOA'}->{$spec} );
- }
- push ( @{$zone_result}, "\t\t)" );
- push ( @{$zone_result}, "\n" );
- # NS fields
- foreach my $nameserver ( @{ $zone_part->{'SOA'}->{'@ns'} } ) {
- push ( @{$zone_result}, "\t\tIN NS\t".$nameserver );
- }
- push ( @{$zone_result}, "\n" );
- # MX fields
- foreach my $mx ( @{ $zone_part->{'SOA'}->{'@mx'} } ) {
- push ( @{$zone_result}, "\t\tIN MX\t".$mx );
- }
- push ( @{$zone_result}, "\n" );
- return $zone_result;
-}
-
-#
-# VOID Mk_zone (STRING $fic_zone, HASHREF $Z)
-#
-# Construit un le fichier $fic_zone pour la zone DNS decrite par la structure $Z
-#
-#==============================================================================================
-sub Mk_zone_for_site ($$$) {
- my ( $zone_name, $zone_site, $global_config ) = @_;
- my $zone_result = [];
-
- # Building Header (SOA, NS an MX fileds)
- $zone_result = __Mk_zoneheader( $zone_name, $zone_site, $global_config->{'ZONE'}->{'BY_NAME'}->{$zone_name} );
- my $zone_part = $global_config->{'ZONE'}->{'BY_NAME'}->{$zone_name}->{'BY_SITE'}->{$zone_site};
-
- ### Building Networks part
- push ( @{$zone_result}, "\n\n;;" );
- push ( @{$zone_result}, ";; Networks" );
- push ( @{$zone_result}, ";;============================================================================\n" );
-
- foreach my $network ( @{$global_config->{'ZONE'}->{'BY_NAME'}->{$zone_name}->{'__network_order'}->{$zone_site}} ) {
- my $head = $network;
- $head .= " : ".$zone_part->{$network}->{'comment'} if ( $zone_part->{$network}->{'comment'} );
- push ( @{$zone_result}, "; ".$head );
- push ( @{$zone_result}, ";----------------------------------------------------------------------------" );
- foreach my $spec ( 'network', 'netmask', 'broadcast', 'gateway' ) {
- next if ( ! defined $zone_part->{$network}->{$spec} );
- push ( @{$zone_result}, $spec.".".$network."\t\t".$zone_part->{$network}->{$spec} );
- }
- push ( @{$zone_result}, "" );
- }
-
- ### Servers
- push ( @{$zone_result}, "\n\n;;" );
- push ( @{$zone_result}, ";; Servers" );
- push ( @{$zone_result}, ";;============================================================================\n" );
-
- foreach my $server ( @{$global_config->{'ZONE'}->{'BY_NAME'}->{$zone_name}->{'__hostclass_order'}->{$zone_site}} ) {
- my $head = $server;
- $head .= " : ".$zone_part->{$server}->{'comment'} if ( $zone_part->{$server}->{'comment'} );
- push ( @{$zone_result}, "; ".$head );
- push ( @{$zone_result}, ";----------------------------------------------------------------------------" );
- foreach my $field ( sort keys %{$zone_part->{$server}} ) {
- next if ( $field eq 'comment' );
- if ( ref $zone_part->{$server}->{$field} eq 'ARRAY' ) {
- foreach my $elt ( @{$zone_part->{$server}->{$field}} ) {
- push ( @{$zone_result}, $field."\t\t".$elt );
- }
- }
- else {
- push ( @{$zone_result}, $field."\t".$zone_part->{$server}->{$field} );
- }
- }
- push ( @{$zone_result}, "" );
- }
- return $zone_result;
-}
-
##########################################################""
### MAIN
@@ -170,24 +77,7 @@
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 );
-}
+( $PF_CONFIG, $GLOBAL_STRUCT ) = Init_TOOLS ( $PF_CONFIG_FILE, $GLOBAL_STORE_FILE );
if ( $SITE eq '' && ! defined $PF_CONFIG->{'location'}->{'site'} ) {
Abort ( $CODE->{'UNDEF_KEY'},
Modified: branches/next-gen/sbin/mk_sourceslist
URL: http://svn.debian.org/wsvn/pf-tools/branches/next-gen/sbin/mk_sourceslist?rev=795&op=diff
==============================================================================
--- branches/next-gen/sbin/mk_sourceslist (original)
+++ branches/next-gen/sbin/mk_sourceslist Mon Aug 2 12:08:40 2010
@@ -24,12 +24,8 @@
use strict;
use warnings;
-use PFTools::Conf;
-# use PFTools::Host;
+use PFTools::Utils;
use PFTools::Logger;
-use File::Compare;
-use File::Copy;
-use Template::Tiny;
use Getopt::Long qw( :config ignore_case_always bundling );
############################################
@@ -102,48 +98,6 @@
EOF
}
-sub Mk_sourceslist ($$$$) {
- my ( $host_props, $dst, $sections, $template ) = @_ ;
-
- my $tpl = Template::Tiny->new ( TRIM => 1 );
- my $mode = $host_props->{'deployment'}->{'mode'};
- if ( $template eq "" ) {
- $template = $PF_CONFIG->{'path'}->{'templates_dir'}.'/'.$PF_CONFIG->{$mode}->{'sources_list'};
- }
-
- if ( ! open ( SOURCESTPL, $template ) ) {
- Abort ( $CODE->{'OPEN'},
- "Unable to get sources.list template from file ".$template );
- return 1 ;
- }
- my $sources_content = join '', <SOURCESTPL>;
- close ( SOURCESTPL );
-
- my $sources_subst = {
- 'mode' => $host_props->{'deployment'}->{'mode'},
- 'distrib' => $host_props->{'deployment'}->{'distrib'},
- 'default_sections' => $PF_CONFIG->{$mode}->{'default_sections'},
- 'custom_sections' => $sections
- };
- $sources_content = $tpl->process ( \$sources_content, $sources_subst );
-
- if ( $BACKPORTS ) {
- my $back_src = ( $mode eq 'debian' )
- ? $mode."-backports"
- : $mode;
- $sources_content .=
- "\ndeb http://mirrors.private/".$back_src." ".$host_props->{'deployment'}->{'distrib'}."-backports ".$PF_CONFIG->{$mode}->{'default_sections'}."\n" ;
- }
-
- if ( ! open ( DST, ">".$dst ) ) {
- warn "Unable to open destination's sources.list ".$dst."\n" ;
- return 1 ;
- }
- print DST $sources_content ;
- close ( DST ) ;
- return 0 ;
-}
-
#############################################################
### MAIN
@@ -169,42 +123,15 @@
"Unable to build sources.list for an undefined hostname" );
}
-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 );
-}
+( $PF_CONFIG, $GLOBAL_STRUCT ) = Init_TOOLS ( $PF_CONFIG_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 $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'},
- "Unbale to create sources.list file from template ".$TEMPLATE." : no such file or directory" );
-}
$TO_ADD =~ s/,/ /g;
$SECTIONS .= " ".$TO_ADD;
-Mk_sourceslist ( $host_props, $OUTPUT_FILE, $SECTIONS, $TEMPLATE ) ;
+Mk_sourceslist ( $HOSTNAME, $SITE, $OUTPUT_FILE, $SECTIONS, $TEMPLATE, $BACKPORTS, $GLOBAL_STRUCT, $PF_CONFIG ) ;
exit 0;
More information about the pf-tools-commits
mailing list