pf-tools commit: r897 [parmelan-guest] - /branches/next-gen/lib/PFTools/Conf.pm

parmelan-guest at users.alioth.debian.org parmelan-guest at users.alioth.debian.org
Wed Sep 8 07:20:38 UTC 2010


Author: parmelan-guest
Date: Wed Sep  8 07:20:33 2010
New Revision: 897

URL: http://svn.debian.org/wsvn/pf-tools/?sc=1&rev=897
Log:
Style

Modified:
    branches/next-gen/lib/PFTools/Conf.pm

Modified: branches/next-gen/lib/PFTools/Conf.pm
URL: http://svn.debian.org/wsvn/pf-tools/branches/next-gen/lib/PFTools/Conf.pm?rev=897&op=diff
==============================================================================
--- branches/next-gen/lib/PFTools/Conf.pm (original)
+++ branches/next-gen/lib/PFTools/Conf.pm Wed Sep  8 07:20:33 2010
@@ -54,12 +54,6 @@
 );
 
 our @EXPORT_OK = qw();
-
-##########################
-### Prototypes
-
-sub __Merge_conf_includes ($$$);
-sub Load_conf ($$$$);
 
 #################################
 ### Constants
@@ -132,17 +126,20 @@
 };
 
 # Subst_vars
-sub Subst_vars ($$) {
+sub Subst_vars {
     my ( $str, $hash_subst ) = @_;
+
+    return unless $str and $hash_subst;
 
     $str =~ s/%([^\%]+)%/$hash_subst->{$1}/gm;
     return $str;
 }
 
-sub Init_PF_CONFIG (;$) {
+sub Init_PF_CONFIG {
     my ($config_file) = @_;
 
-    return $PF_CONFIG if ( !defined $config_file );
+    return $PF_CONFIG unless $config_file;
+
     if ( !-e $config_file ) {
         Abort( $CODE->{'UNDEF_KEY'},
                   "Unable to proceed with configuration file "
@@ -176,20 +173,18 @@
     return $PF_CONFIG;
 }
 
-sub Init_SUBST (;$$$$) {
+sub Init_SUBST {
     my ( $host, $hosttype, $pf_config, $domain ) = @_;
-    my $host_regex;
+
+    unless ($host) {
+        Abort( $CODE->{'UNDEF_KEY'},
+            "Unable to init substitution hash (undefined hostname).\n" );
+    }
+    unless ($pf_config) {
+        $pf_config = Init_PF_CONFIG();
+    }
+
     my $ref_subst = {};
-
-    if ( !defined $pf_config ) {
-        $pf_config = Init_PF_CONFIG();
-    }
-
-    if ( !defined $host ) {
-        Abort( $CODE->{'UNDEF_KEY'},
-            "Unable to init substitution hash for hostname " . $host . "\n" );
-        return;
-    }
     $ref_subst->{'HOSTNAME'} = ( $host ne "" ) ? $host : hostname;
     $ref_subst->{'DOMAINNAME'} 
         = $domain
@@ -206,26 +201,24 @@
         }
     }
     chomp( $ref_subst->{'OS_RELEASE'} = `/bin/uname -r` );
-    if ( defined $pf_config->{'regex'}->{'hostname'} ) {
-        $host_regex = $pf_config->{'regex'}->{'hostname'};
-    }
-    else {
-        $host_regex = $HOST_CONFIG_REGEX;
-    }
+
+    my $host_regex = $pf_config->{'regex'}->{'hostname'}
+        || $HOST_CONFIG_REGEX;
     unless ( $ref_subst->{'HOSTNAME'} =~ m/$host_regex/ ) {
         Abort( $CODE->{'OPEN'},
             "Init_SUBST failed: invalid hostname $ref_subst->{'HOSTNAME'}" );
     }
+
     $ref_subst->{'HOSTTYPE'}    = $hosttype || $+{HOSTTYPE};
     $ref_subst->{'HOSTDIGITS'}  = $+{HOSTDIGITS};
     $ref_subst->{'HOSTCLUSTER'} = $+{HOSTDIGITS} . $+{HOSTNODEINDEX}
-        if ( defined $+{HOSTDIGITS} && defined $+{HOSTNODEINDEX} );
-    $ref_subst->{'HOSTNODEINDEX'} = $+{HOSTNODEINDEX} || "";
-    $ref_subst->{'POPNAME'}       = $+{POPNAME}       || "";
+        if defined $+{HOSTDIGITS} and defined $+{HOSTNODEINDEX};
+    $ref_subst->{'HOSTNODEINDEX'} = $+{HOSTNODEINDEX} || '';
+    $ref_subst->{'POPNAME'}       = $+{POPNAME}       || '';
     $ref_subst->{'HOSTNUM'}       = $ref_subst->{'HOSTDIGITS'};
     $ref_subst->{'HOSTNUM'} =~ s/^0*//;
 
-    if ( $ref_subst->{'HOSTNUM'} eq "" ) {
+    if ( $ref_subst->{'HOSTNUM'} eq '' ) {
         $ref_subst->{'HOSTNUM'} = 0;
     }
     $ref_subst->{'HOSTMINUTE'} = $ref_subst->{'HOSTNUM'} % 60;
@@ -233,15 +226,18 @@
     return $ref_subst;
 }
 
-sub Get_source ($$;$$) {
+sub Get_source {
     my ( $source, $hostname, $hash_subst, $pf_config ) = @_;
 
-    if ( !defined $hash_subst ) {
+    return unless $source and $hostname;
+
+    unless ($hash_subst) {
         $hash_subst = Init_SUBST( $hostname, $pf_config );
     }
-    if ( !defined $pf_config ) {
+    unless ($pf_config) {
         $pf_config = $PF_CONFIG;
     }
+
     my $vcs_work_dir = $pf_config->{'path'}->{'checkout_dir'};
     my $module       = $pf_config->{'vcs'}->{'module'};
     $source =~ s!^MODSITE_([^:]+):!$vcs_work_dir/$module/SITE/$1/MODEL!;
@@ -260,37 +256,44 @@
     return $source;
 }
 
-sub __Get_config_path ($$$) {
+sub __Get_config_path {
     my ( $hostvalue, $pf_config, $site ) = @_;
 
-    my $site_conf_file
-        = Get_source( 'CONFSITE_' . $site . ':/update-' . $hostvalue,
+    return unless $hostvalue and $pf_config and $site;
+
+    my $site_conf_file = Get_source( "CONFSITE_${site}:/update-${hostvalue}",
         $hostvalue, {}, $pf_config );
-    return $site_conf_file if ( -e $site_conf_file );
-    my $default_conf_file = Get_source( 'CONFIG:/update-' . $hostvalue,
-        $hostvalue, {}, $pf_config );
-    return $default_conf_file if ( -e $default_conf_file );
-    return undef;
-}
-
-sub __Merge_host_config ($$) {
+    return $site_conf_file
+        if -e $site_conf_file;
+
+    my $default_conf_file
+        = Get_source( "CONFIG:/update-${hostvalue}", $hostvalue, {},
+        $pf_config );
+    return $default_conf_file
+        if -e $default_conf_file;
+
+    return;
+}
+
+sub __Merge_host_config {
     my ( $hash_to_merge, $hash_subst ) = @_;
+
+    return unless $hash_to_merge and $hash_subst;
+
     my $merge = {};
-
     if ( $hash_to_merge->{'hostgroup'}->{'__model'} ) {
         $merge = $hash_to_merge->{'hostgroup'}->{'__model'};
     }
+
     foreach my $section ( @{ $hash_to_merge->{'__sections_order'} } ) {
         if ( defined $merge->{$section} ) {
             foreach my $key ( keys %{ $hash_to_merge->{$section} } ) {
-                next if ( $section eq 'hostgroup' && $key eq '__model' );
+                next if $section eq 'hostgroup' and $key eq '__model';
 
                 # Adding key if not defined into model else overriding it
                 if ( $key =~ /^\@/ ) {
-                    push(
-                        @{ $merge->{$section}->{$key} },
-                        @{ $hash_to_merge->{$section}->{$key} }
-                    );
+                    push @{ $merge->{$section}->{$key} },
+                        @{ $hash_to_merge->{$section}->{$key} };
                 }
                 else {
                     $merge->{$section}->{$key}
@@ -299,97 +302,86 @@
             }
         }
         else {
-            push( @{ $merge->{'__sections_order'} }, $section );
+            push @{ $merge->{'__sections_order'} }, $section;
             $merge->{$section} = $hash_to_merge->{$section};
         }
     }
+
     return $merge;
 }
 
 # sub __Merge_other_context
 
-sub __Merge_conf_includes ($$$) {
+sub __Merge_conf_includes {
     my ( $hash_to_merge, $hash_subst, $context ) = @_;
-    my $host_context  = 0;
-    my $global_order  = [];
-    my $global_parsed = {};
+
+    return unless $hash_to_merge and $hash_subst and $context;
 
     if ( $context =~ /^host|model$/ ) {
         return __Merge_host_config( $hash_to_merge, $hash_subst );
     }
-    else {
-        my $select = ( $context eq 'config' ) ? 'action' : 'type';
-        foreach my $section ( @{ $hash_to_merge->{'__sections_order'} } ) {
-            if ( $hash_to_merge->{$section}->{$select} ne 'include' ) {
-                push( @{ $global_parsed->{'__sections_order'} }, $section );
-                $global_parsed->{$section} = $hash_to_merge->{$section};
-            }
-            else {
-                my $tmp_merged
-                    = __Merge_conf_includes(
-                    $hash_to_merge->{$section}->{'__content'},
-                    $hash_subst, $context );
-                foreach my $tomerge_section (
-                    @{ $tmp_merged->{'__sections_order'} } )
+
+    my $global_parsed = {};
+
+    my $select = ( $context eq 'config' ) ? 'action' : 'type';
+    foreach my $section ( @{ $hash_to_merge->{'__sections_order'} } ) {
+        if ( $hash_to_merge->{$section}->{$select} ne 'include' ) {
+            push @{ $global_parsed->{'__sections_order'} }, $section;
+            $global_parsed->{$section} = $hash_to_merge->{$section};
+            next;
+        }
+
+        my $tmp_merged
+            = __Merge_conf_includes(
+            $hash_to_merge->{$section}->{'__content'},
+            $hash_subst, $context );
+        foreach my $tomerge_section ( @{ $tmp_merged->{'__sections_order'} } )
+        {
+            if ( defined $global_parsed->{$tomerge_section} ) {
+                if ( !defined $tmp_merged->{$tomerge_section}->{'override'}
+                    || $tmp_merged->{$tomerge_section}->{'override'} ne
+                    'replace' )
                 {
-                    if ( defined $global_parsed->{$tomerge_section} ) {
-                        if ( !defined $tmp_merged->{$tomerge_section}
-                            ->{'override'}
-                            || $tmp_merged->{$tomerge_section}->{'override'}
-                            ne 'replace' )
-                        {
-                            Warn( $CODE->{'WARNING'},
-                                      "Section "
-                                    . $tomerge_section
-                                    . " from file "
-                                    . $section
-                                    . " already defined ... skipping it\n" );
-                            next;
-                        }
-                        else {
-                            Warn( $CODE->{'WARNING'},
-                                      "Section "
-                                    . $tomerge_section
-                                    . " already defined but override is set to replace ... overriding it\n"
-                            );
+                    Warn( $CODE->{'WARNING'},
+                        "Section $tomerge_section from file $section already defined ... skipping it\n"
+                    );
+                    next;
+                }
+                else {
+                    Warn( $CODE->{'WARNING'},
+                        "Section $tomerge_section already defined but override is set to replace ... overriding it\n"
+                    );
 
          # Need to evalute if order must be changed
          # push ( @{$global_parsed->{'__sections_order'}}, $tomerge_section );
-                        }
-                    }
-                    else {
-                        push(
-                            @{ $global_parsed->{'__sections_order'} },
-                            $tomerge_section
-                        );
-                    }
-                    $global_parsed->{$tomerge_section}
-                        = $tmp_merged->{$tomerge_section};
                 }
             }
-        }
-    }
+            else {
+                push @{ $global_parsed->{'__sections_order'} },
+                    $tomerge_section;
+            }
+            $global_parsed->{$tomerge_section}
+                = $tmp_merged->{$tomerge_section};
+        }
+    }
+
     return $global_parsed;
 }
 
-sub Load_conf ($$$$) {
+sub Load_conf {
     my ( $file, $hash_subst, $context, $pf_config ) = @_;
-    my ( $sect_type, $iface_name );
-    my $parsed;
-
-    if ( defined $context && $context !~ /^$ALLOWED_PARSING_CONTEXT$/ ) {
+
+    return unless $file and $hash_subst and $context and $pf_config;
+
+    if ( $context !~ m/^$ALLOWED_PARSING_CONTEXT$/ ) {
         Abort( $CODE->{'INVALID_CONTEXT'},
-                  "Context " 
-                . $context
-                . " for file "
-                . $file
-                . " doesn't match "
-                . $ALLOWED_PARSING_CONTEXT );
-    }
-
-    $parsed = Parser_ini($file);
+            "Context $context for file $file doesn't match $ALLOWED_PARSING_CONTEXT"
+        );
+    }
+
+    my $parsed = Parser_ini($file);
     if ( !defined $parsed ) {
-        Abort( $CODE->{'PARSING'}, "Parsing error for file " . $file );
+        Abort( $CODE->{'PARSING'}, "Parsing error for file $file" );
     }
 
     if ( $context =~ /^(model|host)$/ ) {
@@ -411,15 +403,10 @@
             next if ( $section =~ /^__/ );
             if ( !defined $parsed->{$section}->{$select} ) {
                 Abort( $CODE->{'UNDEF_KEY'},
-                          "Key " 
-                        . $select
-                        . " on section "
-                        . $section
-                        . " from file "
-                        . $file
-                        . " MUST BE defined" );
-            }
-            $sect_type = $parsed->{$section}->{$select};
+                    "Key $select on section $section from file $file MUST BE defined"
+                );
+            }
+            my $sect_type = $parsed->{$section}->{$select};
             if ( $sect_type eq 'include' ) {
 
                 # We need to dive into deep ...
@@ -436,23 +423,20 @@
 
     # Basic checks
     foreach my $section ( keys %{$parsed} ) {
-        next if ( $section =~ /^__/ );
+        next if $section =~ /^__/;
+        my $sect_type;
         if ( $context =~ /^(host|model)$/ ) {
             $section =~ /^([^:]+)(::(.+))?$/;
-            $sect_type  = $1;
-            $iface_name = $3;
+            $sect_type = $1;
+
+            # $iface_name = $3;
         }
         else {
             my $select = ( $context eq 'config' ) ? 'action' : 'type';
             if ( !defined $parsed->{$section}->{$select} ) {
                 Abort( $CODE->{'UNDEF_KEY'},
-                          "Key " 
-                        . $select
-                        . " on section "
-                        . $section
-                        . " from file "
-                        . $file
-                        . " MUST BE defined" );
+                    "Key  $select on section $section from file $file MUST BE defined"
+                );
             }
             $sect_type = $parsed->{$section}->{$select};
         }
@@ -461,35 +445,41 @@
             $context );
         if ( $code > 1 ) {
             Warn( $code,
-                "Errors occur during parsing model from file " . $file );
+                "Errors occur during parsing model from file $file" );
             Abort( $code, $msg );
         }
     }
+
     return $parsed;
 }
 
 ### Like old Init_lib_net
-sub __Sort_net_prio ($$) {
-    my ( $type, $section ) = @_;
+sub __Sort_net_prio {
+
+    #my ( $type, $section ) = @_;
+    my ($type) = @_;
 
     my $prio = 0;
 
     foreach my $prio_type ( 'zone', 'site', 'network', 'server', 'service' ) {
-        return $prio if ( $type eq $prio_type );
+        return $prio if $type eq $prio_type;
         $prio++;
     }
+
     return $prio;
 }
 
-sub __Sort_net_section ($$$) {
+sub __Sort_net_section {
     my ( $net_parsed, $a, $b ) = @_;
 
     return __Sort_net_prio( $net_parsed->{$a}->{'type'}, $a )
         <=> __Sort_net_prio( $net_parsed->{$b}->{'type'}, $b );
 }
 
-sub Init_GLOBAL_NETCONFIG ($$;$) {
+sub Init_GLOBAL_NETCONFIG {
     my ( $start_file, $hash_subst, $pf_config ) = @_;
+
+    return unless $start_file and $hash_subst;
 
     if ( !defined $pf_config ) {
         $pf_config = $PF_CONFIG;
@@ -497,7 +487,7 @@
 
     my $GLOBAL = { 'SITE' => { 'BY_NAME' => {}, } };
     foreach my $ip_type ( 'ipv4', 'ipv6' ) {
-        next if ( !$pf_config->{'features'}->{$ip_type} );
+        next if !$pf_config->{'features'}->{$ip_type};
         my $zone_key = ( $ip_type eq 'ipv6' ) ? 'ZONE6' : 'ZONE';
         my $dhcp_key = ( $ip_type eq 'ipv6' ) ? 'DHCP6' : 'DHCP';
         $GLOBAL->{$zone_key} = {
@@ -541,42 +531,47 @@
                         = Load_conf( $hostfile, $hash_subst, 'host',
                         $pf_config );
                     Add_host( $hostfile, $host_parsed, $GLOBAL, $pf_config );
-                    push( @{ $service_part->{$section} }, $host );
+                    push @{ $service_part->{$section} }, $host;
                 }
-
-            }
-        }
-    }
+            }
+        }
+    }
+
     return $GLOBAL;
 }
 
-sub Flush2disk_GLOBAL ($$;$) {
+sub Flush2disk_GLOBAL {
     my ( $global_config, $pf_config, $path_global_file ) = @_;
+
+    return unless $global_config and $pf_config;
 
     my $flush_file = $path_global_file
         || $pf_config->{'path'}->{'global_struct'};
     if ( !store( $global_config, $flush_file ) ) {
         Warn( $CODE->{'STORABLE'},
-            "An error occured when trying to flush global structure to file "
-                . $flush_file );
-        return 1;
-    }
-    return 0;
-}
-
-sub Retrieve_GLOBAL ($) {
+            "An error occured when trying to flush global structure to file $flush_file"
+        );
+        return;
+    }
+
+    return 1;
+}
+
+sub Retrieve_GLOBAL {
     my ($path_global_file) = @_;
+
+    return unless $path_global_file;
 
     if ( !-e $path_global_file ) {
         Abort( $CODE->{'OPEN'},
-                  "Unable to open global configuration storable file "
-                . $path_global_file
-                . " : no such file or directory" );
-    }
+            "Unable to open global configuration storable file $path_global_file: no such file or directory"
+        );
+    }
+
     return retrieve($path_global_file);
 }
 
-sub Get_config_for_hostname_on_site ($$$$$) {
+sub Get_config_for_hostname_on_site {
     my ( $hostname, $site, $hash_subst, $global_config, $pf_config ) = @_;
 
     # Common configuration file e.g. update-common
@@ -593,9 +588,8 @@
         = Get_hosttype_from_hostname( $hostname, $global_config, $site );
     if ( !defined $hosttype ) {
         Abort( $CODE->{'UNDEF_KEY'},
-                  "Unable to get hosttype from hostname "
-                . $hostname
-                . " for getting hosttype configuration file" );
+            "Unable to get hosttype from hostname $hostname for getting hosttype configuration file"
+        );
     }
 
     # Hosttype configuration file e.g. update-<hosttype>
@@ -605,15 +599,17 @@
     # Hostname configuration file e.g. update-<hostname>
     my $hostname_conf_file
         = __Get_config_path( $hostname, $pf_config, $site );
+
     foreach my $file ( $hosttype_conf_file, $hostname_conf_file ) {
-        next if ( !defined $file );
+        next if !defined $file;
         my $config = Load_conf( $file, $hash_subst, 'config', $pf_config );
         foreach my $section ( @{ $config->{'__sections_order'} } ) {
-            push( @{ $global_host_conf->{'__sections_order'} }, $section )
-                if ( !defined $global_host_conf->{$section} );
+            push @{ $global_host_conf->{'__sections_order'} }, $section
+                if !defined $global_host_conf->{$section};
             $global_host_conf->{$section} = $config->{$section};
         }
     }
+
     return $global_host_conf;
 }
 




More information about the pf-tools-commits mailing list