pf-tools/pf-tools: 6 new changesets

parmelan-guest at users.alioth.debian.org parmelan-guest at users.alioth.debian.org
Fri Dec 3 09:42:42 UTC 2010


details:   http://hg.debian.org/hg/pf-tools/pf-tools/rev/c101b98a08f8
changeset: 1113:c101b98a08f8
user:      Thomas Parmelan <tom+pf-tools at ankh.fr.EU.org>
date:      Fri Dec 03 08:49:20 2010 +0100
description:
New check_mandatory_args_type() and check_optional_args_type() functions, based on improved check_args_type()

details:   http://hg.debian.org/hg/pf-tools/pf-tools/rev/04f39caf1be8
changeset: 1114:04f39caf1be8
user:      Thomas Parmelan <tom+pf-tools at ankh.fr.EU.org>
date:      Fri Dec 03 08:52:58 2010 +0100
description:
Documentation for check_mandatory_args_type() and check_optional_args_type()

details:   http://hg.debian.org/hg/pf-tools/pf-tools/rev/ad97c55804a3
changeset: 1115:ad97c55804a3
user:      Thomas Parmelan <tom+pf-tools at ankh.fr.EU.org>
date:      Fri Dec 03 08:59:39 2010 +0100
description:
Mk_dhcp() -> __build_dhcpd_conf() + tests

details:   http://hg.debian.org/hg/pf-tools/pf-tools/rev/237de57c8244
changeset: 1116:237de57c8244
user:      Thomas Parmelan <tom+pf-tools at ankh.fr.EU.org>
date:      Fri Dec 03 09:02:16 2010 +0100
description:
Move __fix_etc_hosts() towards the end of the file, with other "private" functions

details:   http://hg.debian.org/hg/pf-tools/pf-tools/rev/af38076a6299
changeset: 1117:af38076a6299
user:      Thomas Parmelan <tom+pf-tools at ankh.fr.EU.org>
date:      Fri Dec 03 09:03:05 2010 +0100
description:
Move __build_dhcpd_conf() towards the end of the file, with other "private" functions

details:   http://hg.debian.org/hg/pf-tools/pf-tools/rev/d262ba34f2c5
changeset: 1118:d262ba34f2c5
user:      Thomas Parmelan <tom+pf-tools at ankh.fr.EU.org>
date:      Fri Dec 03 10:41:57 2010 +0100
description:
Add missing t/20.files.etc.hosts.input

diffstat:

3 files changed, 27 insertions(+), 2 deletions(-)
lib/PFTools.pm               |    1 +
t/20.files.dhcpd.conf.header |    1 +
t/20.files.t                 |   27 +++++++++++++++++++++++++--

diffs (497 lines):

diff -r de2d381543f5 -r d262ba34f2c5 lib/PFTools.pm
--- a/lib/PFTools.pm	Wed Dec 01 08:25:18 2010 +0100
+++ b/lib/PFTools.pm	Fri Dec 03 10:41:57 2010 +0100
@@ -26,6 +26,8 @@
 use English qw( -no_match_vars );    # Avoids regex performance penalty
 
 our @EXPORT = qw(
+    check_mandatory_args_type
+    check_optional_args_type
     check_args_type
     check_args
     check_true_args
@@ -36,47 +38,81 @@
 our $VERSION = '1.0.1-WIP';
 
 
-my %type_string = (
-    q{}     => 'scalar',
-    'ARRAY' => 'arrayref',
-    'HASH'  => 'hashref',
-);
+=head2 FIXME
+=head2 check_args_type( $args_ref, $mandatory, $type, $names_ref )
 
-=head2 FIXME
-=head2 check_hashref_args( $args_ref, $names_ref )
+Checks that the %{$args_ref} keys listed in @{$names_ref} all have a I<type>
+reference value. If I<$mandatory> has a true value, the arguments are
+considered mandatory (ie: the keys must exist). The available types are the
+known return values of the ref() builtin function, for instance:
 
-Checks that the %{$args_ref} keys listed in @{$names_ref} all have a hashref
-value.
+=over
+
+=item I<(an empty string)>: the value must be a scalar
+
+=item I<ARRAY>: the value must be an array reference
+
+=item I<HASH>: the value must be a hash reference
+
+=over
 
 =cut
 
 sub check_args_type {
-    my ($args_ref, $type, @keys) = @_;
+    my ($args_ref, $mandatory, $type, @keys) = @_;
 
-    if ( ref $args_ref ne 'HASH' or ref $type or not @keys ) {
+    if ( ref $args_ref ne 'HASH' or ref $mandatory or ref $type or not defined $type or not @keys ) {
         confess q{ERROR: BUG: invalid args};
     }
 
-    if ( $type eq 'SCALAR' ) {
-        $type = q{};
-    }
-
-    if ( not $type_string{$type} ) {
-        confess qq{ERROR: Unknown argument type '$type'};
-    }
+    my $type_string
+        = $type ? ( qq{non-} . lc($type) . q{ reference} ) : q{non-scalar};
 
     foreach my $key (@keys) {
-        if ( not exists $args_ref->{$key} ) {
+        if (not defined $key) {
+            carp qq{WARNING: PROBABLE BUG: undefined key in check_args_type()};
+            next;
+        }
+
+        if ( $mandatory and not exists $args_ref->{$key} ) {
             croak qq{ERROR: Mandatory argument $key not found};
         }
+
         if ( ref $args_ref->{$key} ne $type ) {
-            croak qq{ERROR: Invalid non-$type_string{$type} $key};
+            croak qq{ERROR: Invalid $type_string $key};
         }
     }
 
     return 1;
 
 }
+
+=head2 check_mandatory_args_type( $args_ref, $type, @keys )
+
+Checks the arguments, treating them as mandatory (see I<check_args_type() for
+details).
+
+=cut
+
+sub check_mandatory_args_type {
+    my ($args_ref, $type, @keys) = @_;
+
+    return check_args_type( $args_ref, 1, $type, @keys );
+}
+
+=head2 check_optional_args_type( $args_ref, $type, @keys )
+
+Checks the arguments, treating them as optional (see I<check_args_type() for
+details).
+
+=cut
+
+sub check_optional_args_type {
+    my ($args_ref, $type, @keys) = @_;
+
+    return check_args_type( $args_ref, 0, $type, @keys );
+}
+
 
 =head2 check_args( $function_ref, $args_ref, @keys)
 
@@ -89,7 +125,7 @@
 
 =cut
 
-sub check_args (&@$@) {
+sub check_args (&$@) {
     my ($function_ref, $args_ref, @keys) = @_;
 
     foreach my $key (@keys) {
@@ -102,7 +138,7 @@
 =head2 check_true_args( $args_ref, $names_ref )
 
 Checks that all the %{$args_ref} keys listed in @{$names_ref} all have a true
-value. NOTE: this is therefor NOT suitable for 0, q{0}, etc.
+value. NOTE: this is therefor NOT suitable for 0, q{}, etc.
 
 =cut
 
diff -r de2d381543f5 -r d262ba34f2c5 lib/PFTools/Utils.pm
--- a/lib/PFTools/Utils.pm	Wed Dec 01 08:25:18 2010 +0100
+++ b/lib/PFTools/Utils.pm	Fri Dec 03 10:41:57 2010 +0100
@@ -475,7 +475,7 @@
         croak q{ERROR: Invalid $arguments_ref};
     }
 
-    check_args_type( $arguments_ref, q{},
+    check_mandatory_args_type( $arguments_ref, q{},
         qw( input_filename output_filename ) );
 
     check_true_args( $arguments_ref,
@@ -490,114 +490,6 @@
     );
 
     return 1;
-}
-
-=head2 __fix_etc_hosts($arguments_ref)
-
-Reads an /etc/hosts-type file, fixes the IP address for a given hostname,
-returns a reference to the list of lines. THe named arguments are:
-
-=over
-
-=item I<hostname> the host name
-
-=item I<site_name> (optional) the site name
-
-=item I<input_file> the file to read from (usually I</etc/hosts>)
-
-=item I<ip_type> I<ipv4> or I<ipv6> (but only I<ipv4> is currently supported)
-
-=item I<global_config> a reference to the global configuration hash
-
-=item I<pf_config> a reference to the pf-tools configuration hash
-
-=back
-
-=cut
-
-# FIXME: move to end of file
-# FIXME: what is the problem exactly, is this really necessary?
-
-sub __fix_etc_hosts {
-    my ($arguments_ref) = @_;
-
-    my ($hostname, $site_name,     $input_filename,
-        $ip_type,  $global_config, $pf_config
-        )
-        = @{$arguments_ref}
-        {qw( hostname site_name input_filename
-            ip_type global_config pf_config )};
-
-    # $hostname, $site_name and $global_config will be checked by
-    # get_host_config()
-
-    # input_filename will be checked by __read_file_in_array()
-
-    # FIXME pf_config is not used for the moment
-
-    if ( not $ip_type ) {
-        croak q{ERROR: Invalid empty ip_type};
-    }
-    if ( ref $ip_type ) {
-        croak q{ERROR: Invalid non-scalar ip_type};
-    }
-    if ( $ip_type ne q{ipv4} ) {
-        croak qq{ERROR: __fix_etc_hosts: ip_type '$ip_type' not implemented};
-    }
-
-    my $host_ref = get_host_config( $hostname, $global_config, $site_name );
-    my $dhcp_iface = get_iface_vlan_from_hostname(
-        $host_ref->{'deployment'}->{'dhcpvlan'}, $host_ref
-    );
-
-    # This should work for IPv6 too...
-    my $ip_and_prefix = $host_ref->{'interfaces'}->{$dhcp_iface}->{$ip_type};
-    my $ip_deploy     = NetAddr::IP->new($ip_and_prefix)->addr();
-
-    my $lines_ref = __read_file_in_array( $input_filename, 1 );
-
-    foreach my $line ( @{$lines_ref} ) {
-        next unless $line =~ m{ $hostname }xms;
-
-        $line =~ s{ \A 127 [.] 0 [.] [\d]{1,3} [.] [\d]{1,3} }{$ip_deploy}xms;
-    }
-
-    return $lines_ref;
-}
-
-# FIXME convert to __make_file()
-sub Mk_dhcp {
-    my ( $header_file, $site_part ) = @_;
-    my @dhcp_hosts = ();
-
-    my $dhcp_headers
-        = $header_file ? __read_file_in_array( $header_file, 1 ) : [];
-
-    my @dhcp_subnets = ();
-    foreach my $vlan ( keys %{$site_part} ) {
-        push @dhcp_subnets,
-            "subnet $site_part->{$vlan}->{'subnet'} netmask $site_part->{$vlan}->{'netmask'} {\n}\n";
-
-        foreach my $hostclass ( keys %{ $site_part->{$vlan} } ) {
-            next
-                if $hostclass eq q{subnet}
-                    or $hostclass eq q{netmask}
-                    or $hostclass eq q{routers};
-
-            my $host_part = $site_part->{$vlan}->{$hostclass};
-            foreach my $host ( keys %{$host_part} ) {
-                push @dhcp_hosts, "host $host {";
-
-                foreach my $def ( @{ $host_part->{$host} } ) {
-                    push @dhcp_hosts, "\t$def";
-                }
-
-                push @dhcp_hosts, "}", q{};
-            }
-        }
-    }
-
-    return [ @{$dhcp_headers}, @dhcp_subnets, @dhcp_hosts ];
 }
 
 =head2 make_sources_list_file($arguments_ref)
@@ -1753,6 +1645,139 @@
     return [$content];
 }
 
+=head2 __fix_etc_hosts($arguments_ref)
+
+Reads an /etc/hosts-type file, fixes the IP address for a given hostname,
+returns a reference to the list of lines. THe named arguments are:
+
+=over
+
+=item I<hostname> the host name
+
+=item I<site_name> (optional) the site name
+
+=item I<input_file> the file to read from (usually I</etc/hosts>)
+
+=item I<ip_type> I<ipv4> or I<ipv6> (but only I<ipv4> is currently supported)
+
+=item I<global_config> a reference to the global configuration hash
+
+=item I<pf_config> a reference to the pf-tools configuration hash
+
+=back
+
+=cut
+
+# FIXME: what is the problem exactly, is this really necessary?
+
+sub __fix_etc_hosts {
+    my ($arguments_ref) = @_;
+
+    my ($hostname, $site_name,     $input_filename,
+        $ip_type,  $global_config, $pf_config
+        )
+        = @{$arguments_ref}
+        {qw( hostname site_name input_filename
+            ip_type global_config pf_config )};
+
+    # $hostname, $site_name and $global_config will be checked by
+    # get_host_config()
+
+    # input_filename will be checked by __read_file_in_array()
+
+    # FIXME pf_config is not used for the moment
+
+    if ( not $ip_type ) {
+        croak q{ERROR: Invalid empty ip_type};
+    }
+    if ( ref $ip_type ) {
+        croak q{ERROR: Invalid non-scalar ip_type};
+    }
+    if ( $ip_type ne q{ipv4} ) {
+        croak qq{ERROR: __fix_etc_hosts: ip_type '$ip_type' not implemented};
+    }
+
+    my $host_ref = get_host_config( $hostname, $global_config, $site_name );
+    my $dhcp_iface = get_iface_vlan_from_hostname(
+        $host_ref->{'deployment'}->{'dhcpvlan'}, $host_ref
+    );
+
+    # This should work for IPv6 too...
+    my $ip_and_prefix = $host_ref->{'interfaces'}->{$dhcp_iface}->{$ip_type};
+    my $ip_deploy     = NetAddr::IP->new($ip_and_prefix)->addr();
+
+    my $lines_ref = __read_file_in_array( $input_filename, 1 );
+
+    foreach my $line ( @{$lines_ref} ) {
+        next unless $line =~ m{ $hostname }xms;
+
+        $line =~ s{ \A 127 [.] 0 [.] [\d]{1,3} [.] [\d]{1,3} }{$ip_deploy}xms;
+    }
+
+    return $lines_ref;
+}
+
+=head2 __build_dhcpd_conf($args_ref)
+
+Builds the I<dhcpd.conf> content for a given site. Returns a reference to the
+list of generated lines. The named arguments are:
+
+=over
+
+=item I<header_filename> the name of a file whose content will be used as a
+header for the generated content (FIXME use a template instead)
+
+=item I<site_ref> a reference to the site configuration hash (that would be 
+C<$global_config->{'DHCP'}->{'BY_SITE'}->{$site}> for the current
+implementation) (FIXME use I<site_name> instead)
+
+=back
+
+=cut
+
+sub __build_dhcpd_conf {
+    my ($args_ref) = @_;
+
+    check_mandatory_args_type( $args_ref, q{HASH}, qw( site_ref ) );
+    my $site_ref = $args_ref->{'site_ref'};
+
+    check_optional_args_type( $args_ref, q{}, qw( header_filename ) );
+
+    my $headers_ref
+        = $args_ref->{'header_filename'}
+        ? __read_file_in_array( $args_ref->{'header_filename'}, 1 )
+        : [];
+
+    my @hosts = ();
+    my @subnets = ();
+    foreach my $vlan ( keys %{$site_ref} ) {
+        push @subnets,
+            qq/subnet $site_ref->{$vlan}->{'subnet'} netmask $site_ref->{$vlan}->{'netmask'} {/,
+            q/}/,
+            q{};
+
+        foreach my $hostclass ( keys %{ $site_ref->{$vlan} } ) {
+            next
+                if $hostclass eq q{subnet}
+                    or $hostclass eq q{netmask}
+                    or $hostclass eq q{routers};
+
+            my $host_ref = $site_ref->{$vlan}->{$hostclass};
+            foreach my $hostname ( keys %{$host_ref} ) {
+                push @hosts, qq/host $hostname {/;
+
+                push @hosts,
+                    map {qq{\t$_}}    # 1 tab indentation
+                    @{ $host_ref->{$hostname} };
+
+                push @hosts, q/}/, q{};
+            }
+        }
+    }
+
+    return [ @{$headers_ref}, @subnets, @hosts, q{} ];
+}
+
 =head2 __get_kpkg_from_kernel( $pxefilename, $deploymode )
 
 This functions computes the name of the kernel package to install, base on the
diff -r de2d381543f5 -r d262ba34f2c5 sbin/mk_dhcp
--- a/sbin/mk_dhcp	Wed Dec 01 08:25:18 2010 +0100
+++ b/sbin/mk_dhcp	Fri Dec 03 10:41:57 2010 +0100
@@ -87,7 +87,7 @@
 }
 
 ( $PF_CONFIG, $GLOBAL_STRUCT ) = Init_TOOLS(
-    "",
+    q{},
     $options->{'config'},
     $options->{'store'}
 );
diff -r de2d381543f5 -r d262ba34f2c5 t/20.files.dhcpd.conf.header
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/t/20.files.dhcpd.conf.header	Fri Dec 03 10:41:57 2010 +0100
@@ -0,0 +1,3 @@
+This is a multi-line
+header test
+
diff -r de2d381543f5 -r d262ba34f2c5 t/20.files.etc.hosts.input
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/t/20.files.etc.hosts.input	Fri Dec 03 10:41:57 2010 +0100
@@ -0,0 +1,1 @@
+127.0.0.1	cbv4-rdeploy01
diff -r de2d381543f5 -r d262ba34f2c5 t/20.files.t
--- a/t/20.files.t	Wed Dec 01 08:25:18 2010 +0100
+++ b/t/20.files.t	Fri Dec 03 10:41:57 2010 +0100
@@ -1189,10 +1189,68 @@
 
 $result = PFTools::Utils::__read_file_in_array($output_filename, 1);
 is_deeply $result, $expected_result
-    => q{Correctly fixes /etc/hosts file for host cbv4-rdeploy01 site cbv4,
-    even if input and output are the same file'}
+    => q{Correctly fixes /etc/hosts file for host cbv4-rdeploy01 site cbv4, even if input and output are the same file'}
     or note explain $result;
 
 ok unlink $output_filename
     => q{Removed test output file};
 
+########################################################################
+note('Testing PFTools::Utils::__build_dhcpd_conf');
+can_ok( 'PFTools::Utils', qw( PFTools::Utils::__build_dhcpd_conf ) );
+
+my $header_filename = q{}; # q{FOOBAR};
+$output_filename = q{/tmp/test.dhcpd.conf};
+$site_name = q{cbv4-pfds};
+my $site_ref = $global_config->{'DHCP'}->{'BY_SITE'}->{$site_name};
+
+$expected_result = [ split qr{ \n }xms, <<"EOT", -1 ];
+subnet 10.1.0.0 netmask 255.255.0.0 {
+}
+
+host cbv4-spawn01 {
+\thardware ethernet 00:1e:c9:ff:42:0a;
+\tfixed-address 10.1.167.1;
+\tfilename pxelinux.0;
+\toption domain-name-servers nsprivate.private,spawn.private;
+}
+
+host cbv4-spawn00 {
+\thardware ethernet 00:1e:c9:ff:08:e3;
+\tfixed-address 10.1.167.0;
+\tfilename pxelinux.0;
+\toption domain-name-servers nsprivate.private,spawn.private;
+}
+
+EOT
+
+$args_ref = {
+    header_filename => $header_filename,
+    site_ref => $site_ref,
+};
+$result = PFTools::Utils::__build_dhcpd_conf($args_ref);
+
+is_deeply $result, $expected_result
+    => q{Mk_dhcp returns the correct content for site cbv4}
+    or note explain $result;
+
+$header_filename = q{t/20.files.dhcpd.conf.header};
+my $headers_ref = PFTools::Utils::__read_file_in_array($header_filename, 1);
+$expected_result = [ @{$headers_ref}, @{$expected_result} ];
+$args_ref->{'header_filename'} = $header_filename;
+
+$result = PFTools::Utils::__build_dhcpd_conf($args_ref);
+
+is_deeply $result, $expected_result
+    => q{Mk_dhcp returns the correct content for site cbv4 with header file}
+    or note explain $result;
+
+#$args_ref = {
+#    site_name => $site_name,
+#    header_filename => $header_filename,
+#    global_config => $global_config,
+#};
+#
+#$result = PFTools::Utils::__build_dhcpd_conf($args_ref);
+
+



More information about the pf-tools-commits mailing list