pf-tools/pf-tools: 9 new changesets
parmelan-guest at users.alioth.debian.org
parmelan-guest at users.alioth.debian.org
Sun Jan 9 19:38:12 UTC 2011
details: http://hg.debian.org/hg/pf-tools/pf-tools/rev/584b86ad3452
changeset: 1162:584b86ad3452
user: Thomas Parmelan <tom+pf-tools at ankh.fr.EU.org>
date: Thu Jan 06 19:01:34 2011 +0100
description:
New function get_complete_cmdline_from_host_ref()
details: http://hg.debian.org/hg/pf-tools/pf-tools/rev/603bd92721ae
changeset: 1163:603bd92721ae
user: Thomas Parmelan <tom+pf-tools at ankh.fr.EU.org>
date: Fri Jan 07 08:24:42 2011 +0100
description:
New function trim_surrounding_whitespace()
details: http://hg.debian.org/hg/pf-tools/pf-tools/rev/c308b5ab0ad8
changeset: 1164:c308b5ab0ad8
user: Thomas Parmelan <tom+pf-tools at ankh.fr.EU.org>
date: Fri Jan 07 08:29:06 2011 +0100
description:
Use trim_surrounding_whitespace() in get_complete_cmdline_from_host_ref()
details: http://hg.debian.org/hg/pf-tools/pf-tools/rev/9b158ff22f58
changeset: 1165:9b158ff22f58
user: Thomas Parmelan <tom+pf-tools at ankh.fr.EU.org>
date: Fri Jan 07 08:32:21 2011 +0100
description:
Rewrite Change_kopt_for_hostname() as fix_grub_kopt() using check_* and __make_file()
details: http://hg.debian.org/hg/pf-tools/pf-tools/rev/55d04b2d7db0
changeset: 1166:55d04b2d7db0
user: Thomas Parmelan <tom+pf-tools at ankh.fr.EU.org>
date: Fri Jan 07 18:18:47 2011 +0100
description:
Tests for get_complete_cmdline_from_host_ref()
details: http://hg.debian.org/hg/pf-tools/pf-tools/rev/24290088610d
changeset: 1167:24290088610d
user: Thomas Parmelan <tom+pf-tools at ankh.fr.EU.org>
date: Fri Jan 07 18:27:45 2011 +0100
description:
Style: reorder
details: http://hg.debian.org/hg/pf-tools/pf-tools/rev/06c4e0edbfa0
changeset: 1168:06c4e0edbfa0
user: Thomas Parmelan <tom+pf-tools at ankh.fr.EU.org>
date: Fri Jan 07 18:36:02 2011 +0100
description:
Tests for get_cmdline_from_host_ref()
details: http://hg.debian.org/hg/pf-tools/pf-tools/rev/bbcb1d726cd6
changeset: 1169:bbcb1d726cd6
user: Thomas Parmelan <tom+pf-tools at ankh.fr.EU.org>
date: Fri Jan 07 18:39:28 2011 +0100
description:
Tests for get_distrib_from_host_ref()
details: http://hg.debian.org/hg/pf-tools/pf-tools/rev/c8d956f0d489
changeset: 1170:c8d956f0d489
user: Thomas Parmelan <tom+pf-tools at ankh.fr.EU.org>
date: Fri Jan 07 18:54:58 2011 +0100
description:
Tests for get_host_config()
diffstat:
7 files changed, 36 insertions(+), 6 deletions(-)
lib/PFTools/Structqueries.pm | 3 +++
lib/PFTools/Utils.pm | 2 --
sbin/mk_grubopt | 8 +++++---
t/10.whitespace.t | 17 +++++++++++++++++
t/13.conf.t | 1 -
t/20.files.fix_grub_kopt.input.1 | 5 +++++
t/20.files.fix_grub_kopt.input.2 | 6 ++++++
diffs (1558 lines):
diff -r 418635e2d6eb -r c8d956f0d489 lib/PFTools.pm
--- a/lib/PFTools.pm Mon Jan 03 12:05:47 2011 +0100
+++ b/lib/PFTools.pm Fri Jan 07 18:54:58 2011 +0100
@@ -32,6 +32,7 @@
check_args_type
check_args
check_true_args
+ trim_surrounding_whitespace
);
#our @EXPORT_OK = qw();
@@ -226,5 +227,32 @@
return 1;
}
+=head2 trim_surrounding_whitespace($string)
+
+Just like the name suggets!
+
+=cut
+
+sub trim_surrounding_whitespace {
+ my ($string) = @_;
+
+ if ( ref $string ) {
+ croak q{ERROR: Invalid non-scalar string};
+ }
+
+ return unless defined $string;
+
+ $string =~ s{
+ \A
+ \s* # leading whitespace
+ ( .*? ) # not greedy, to allow whitespace between
+ # non-whitespace parts!
+ \s* # trailing whitespace
+ \z
+ }{$1}xms;
+
+ return $string;
+}
+
1; # Magic true value required at end of module
diff -r 418635e2d6eb -r c8d956f0d489 lib/PFTools/Structqueries.pm
--- a/lib/PFTools/Structqueries.pm Mon Jan 03 12:05:47 2011 +0100
+++ b/lib/PFTools/Structqueries.pm Fri Jan 07 18:54:58 2011 +0100
@@ -29,8 +29,13 @@
use English qw( -no_match_vars ); # Avoids regex performance penalty
use List::MoreUtils qw( uniq );
+use PFTools;
+
+our @CARP_NOT = qw( PFTools );
+
our @EXPORT = qw(
get_cmdline_from_host_ref
+ get_complete_cmdline_from_host_ref
get_distrib_from_host_ref
get_host_config
get_hostshort_hostvlan_zone_from_fqdn
@@ -52,6 +57,110 @@
);
our @EXPORT_OK = qw ();
+
+=head2 get_cmdline_from_host_ref($host_ref)
+
+Returns a list of two elements. The second one contains the bonding-related
+part of the cmdline, the first one is the rest.
+
+=cut
+
+sub get_cmdline_from_host_ref {
+ my ($host_ref) = @_;
+
+ if ( ref $host_ref ne 'HASH' ) {
+ croak q{ERROR: Invalid non-hash reference host_ref};
+ }
+
+ my $cmdline = $host_ref->{'boot'}->{'cmdline'} || q{};
+ my $bond_cmdline = q{};
+ foreach my $iface ( sort keys %{ $host_ref->{'interfaces'} } ) {
+ next if $iface !~ m{\A bond }xms;
+
+ $bond_cmdline
+ = qq/bonding.mode=$host_ref->{'interfaces'}->{$iface}->{'mode'}/;
+ foreach my $opt (
+ split qr{ \s* [,] \s* }xms,
+ $host_ref->{'interfaces'}->{$iface}->{'options'}
+ )
+ {
+ $bond_cmdline .= qq{ bonding.$opt};
+ }
+
+ last;
+ }
+
+ return ( $cmdline, $bond_cmdline );
+}
+
+=head2 get_complete_cmdline_from_host_ref($host_ref)
+
+Builds the complete cmdline with get_cmdline_from_host_ref() and removes both
+leading and trailing whitespace. Returns the complete cmdline.
+
+=cut
+
+sub get_complete_cmdline_from_host_ref {
+ my ($host_ref) = @_;
+
+ my $cmdline = join q{ }, get_cmdline_from_host_ref($host_ref);
+
+ return trim_surrounding_whitespace($cmdline);
+}
+
+=head2 get_distrib_from_host_ref($host_ref)
+
+Returns the distribution name for the host described by I<$host_ref>.
+
+=cut
+
+sub get_distrib_from_host_ref {
+ my ($host_ref) = @_;
+
+ if ( ref $host_ref ne 'HASH' ) {
+ croak q{ERROR: Invalid non-hash reference host_ref};
+ }
+
+ return $host_ref->{'deployment'}->{'distrib'};
+}
+
+=head2 get_host_config( $hostname, $global_config, $site_name )
+
+This function returns a reference to the host config hash for I<$hostname>.
+I<$site_name> is optional.
+
+=cut
+
+sub get_host_config {
+ my ( $hostname, $global_config, $site_name ) = @_;
+
+ $site_name ||= get_uniq_site_from_hostname( $hostname, $global_config );
+
+ my $hosttype
+ = get_hosttype_from_hostname( $hostname, $global_config, $site_name );
+
+ my $site_ref = get_site_config( $site_name, $global_config );
+ my $zone = $site_ref->{'zone'};
+ my ($hostshort)
+ = get_hostshort_hostvlan_zone_from_fqdn( $hostname, $zone );
+ if ( not $hostshort ) {
+ croak
+ qq{ERROR: Unable to get hostshort from hostname $hostname};
+ }
+
+ my $host_config
+ = $site_ref->{'HOST'}->{'BY_NAME'}->{$hosttype}->{$hostshort};
+
+ unless ($host_config) {
+ croak qq{ERROR: hostname $hostname: no configuration found};
+ }
+
+ unless ( ref $host_config eq q{HASH} ) {
+ croak qq{ERROR: host_config for hostname $hostname is not an array reference (BUG?)};
+ }
+
+ return $host_config;
+}
=head2 get_hostshort_hostvlan_from_fqdn($fqdn, $zone)
@@ -111,6 +220,233 @@
return @return;
}
+=head2
+
+get_hosttype_from_hostname ( $hostname, $global_config, $site_name )
+
+Returns the hosttype.
+
+=cut
+
+sub get_hosttype_from_hostname {
+ my ( $hostname, $global_config, $site_name ) = @_;
+
+ if ( not $hostname ) {
+ croak q{ERROR: Invalid empty $hostname};
+ }
+ if ( ref $hostname ) {
+ croak q{ERROR: Invalid non-scalar $hostname};
+ }
+
+ if ( ref $global_config ne 'HASH' ) {
+ croak q{ERROR: Invalid non-hash reference global_config};
+ }
+
+ if ( $site_name and ref $site_name ) {
+ croak q{ERROR: Invalid non-scalar site_name};
+ }
+
+ my $site_list = ($site_name)
+ ? [$site_name]
+ : $global_config->{'SITE'}->{'__site_list'};
+
+ foreach my $site ( @{$site_list} ) {
+ my $site_ref = get_site_config( $site, $global_config );
+ my $host_part = $site_ref->{'HOST'}->{'BY_NAME'};
+ foreach my $hostclass ( keys %{$host_part} ) {
+ return $hostclass if $hostclass eq $hostname;
+
+ foreach my $host ( keys %{ $host_part->{$hostclass} } ) {
+ return $hostclass if $host eq $hostname;
+ }
+ }
+ }
+
+ croak qq{ERROR: Unknown hostname '$hostname'}
+ . ( $site_name ? qq{ on site '$site_name'} : q{} );
+}
+
+=head2 get_iface_vlan_from_hostname( $vlan, $host_ref )
+
+This function returns the interface name for a given vlan.
+
+=cut
+
+sub get_iface_vlan_from_hostname {
+ my ( $vlan, $host_ref ) = @_;
+
+ foreach my $iface ( keys %{ $host_ref->{'interfaces'} } ) {
+ if ( $host_ref->{'interfaces'}->{$iface}->{'vlan'} eq $vlan ) {
+ return $iface;
+ }
+ }
+
+ return;
+}
+
+=head2 get_mode_from_host_ref($host_ref)
+
+Returns the deployment mode for the host described by I<$host_ref>.
+
+=cut
+
+sub get_mode_from_host_ref {
+ my ($host_ref) = @_;
+
+ return $host_ref->{'deployment'}->{'mode'};
+}
+
+=head2 get_pkgtype_from_hostname( $hostname, $global_config, $site_name )
+
+This function returns the pkgtype for I<$hostname>. I<$site_name> is optional.
+The only pkgtype currently implemented is 'deb'.
+
+=cut
+
+sub get_pkgtype_from_hostname {
+ my ( $hostname, $global_config, $site_name ) = @_;
+
+ my $host_props = get_host_config( $hostname, $global_config, $site_name );
+
+ my %pkgtype_for = (
+ debian => q{deb},
+ ubuntu => q{deb},
+ );
+
+ my $mode = $host_props->{'deployment'}->{'mode'};
+ if ( not exists $pkgtype_for{$mode} ) {
+ croak qq{ERROR: Unknown/not implemented deployment mode $mode};
+ }
+
+ return $pkgtype_for{$mode};
+}
+
+=head2 get_site_config( $site_name, $global_config )
+
+This function returns a reference to the site configuration hash for
+I<$site_name>.
+
+=cut
+
+sub get_site_config {
+ my ( $site_name, $global_config ) = @_;
+
+ if ( not $site_name or ref $site_name ) {
+ croak q{ERROR: Invalid site_name};
+ }
+
+ if (ref $global_config ne 'HASH'
+ or not exists $global_config->{'SITE'}->{'BY_NAME'}
+ )
+ {
+ croak q{ERROR: Invalid global_config};
+ }
+
+ my $site_ref = $global_config->{'SITE'}->{'BY_NAME'}->{$site_name};
+
+ if ( not $site_ref ) {
+ croak qq{ERROR: Unknown site '$site_name'};
+ }
+
+ return $site_ref;
+}
+
+=head2 get_site_dhcp_config( $site_name, $global_config )
+
+This function returns a reference to the site DHCP configuration hash for
+I<$site_name>.
+
+=cut
+
+sub get_site_dhcp_config {
+ my ( $site_name, $global_config ) = @_;
+
+ if ( not $site_name or ref $site_name ) {
+ croak q{ERROR: Invalid site_name};
+ }
+
+ if (ref $global_config ne 'HASH'
+ or not exists $global_config->{'DHCP'}->{'BY_SITE'}
+ )
+ {
+ croak q{ERROR: Invalid global_config};
+ }
+
+ my $site_ref = $global_config->{'DHCP'}->{'BY_SITE'}->{$site_name};
+
+ if ( not $site_ref ) {
+ croak qq{ERROR: Unknown site '$site_name'};
+ }
+
+ return $site_ref;
+}
+
+=head2 get_site_list_from_hostname( $hostname, $global_config )
+
+This function returns the list of sites for a given hostname.
+
+=cut
+
+sub get_site_list_from_hostname {
+ my ( $hostname, $global_config ) = @_;
+
+ if ( not $hostname ) {
+ croak q{ERROR: Invalid empty hostname};
+ }
+ if ( ref $hostname ) {
+ croak q{ERROR: Invalid non-scalar hostname};
+ }
+
+ if ( ref $global_config ne 'HASH' ) {
+ croak q{ERROR: Invalid non-hash reference global_config};
+ }
+
+ my @site_list = ();
+ foreach my $site ( @{ $global_config->{'SITE'}->{'__site_list'} } ) {
+ my $site_ref = get_site_config( $site, $global_config );
+ my $host_part = $site_ref->{'HOST'}->{'BY_NAME'};
+ foreach my $hostclass ( keys %{$host_part} ) {
+ if ( $hostclass eq $hostname ) {
+ push @site_list, $site;
+ next;
+ }
+
+ foreach my $host ( keys %{ $host_part->{$hostclass} } ) {
+ if ( $host eq $hostname ) {
+ push @site_list, $site;
+ last;
+ }
+ }
+ }
+ }
+
+ @site_list = uniq @site_list;
+
+ return \@site_list;
+}
+
+=head2 get_site_list_from_section ( $section_ref, $global_config )
+
+This function builds up the list of sites specified in a given section (hash).
+Returns a reference to the list.
+
+=cut
+
+sub get_site_list_from_section {
+ my ( $section_ref, $global_config ) = @_;
+
+ if ( $section_ref->{'site'} eq 'ALL' ) {
+ return $global_config->{'SITE'}->{'__site_list'};
+ }
+
+ my @site_list = split qr{ \s* [,] \s* }xms, $section_ref->{'site'};
+
+# FIXME also permit '@site' notation ?
+# @site_list = ( @$section_ref->{'@site'}, split qr{ \s* [,] \s* }xms, $section_ref->{'site'} );
+
+ return \@site_list;
+}
+
=head2 get_suffix_from_ip_type ( $ip_type )
This function returns the suffix according to an IP type on the global
@@ -129,6 +465,25 @@
: q{};
return $suffix;
+}
+
+=head2 get_vlan_config( $vlan_name, $global_config, $site_name )
+
+This function returns a reference to the vlan config hash for I<$vlan_name>.
+I<$site_name> is mandatory.
+
+=cut
+
+sub get_vlan_config {
+ my ( $vlan_name, $global_config, $site_name ) = @_;
+
+ unless ($site_name) {
+ croak q{ERROR: $site MUST BE defined};
+ }
+
+ my $site_ref = get_site_config( $site_name, $global_config );
+
+ return $site_ref->{'NETWORK'}->{'BY_NAME'}->{$vlan_name};
}
=head2 get_uniq_site_from_hostname( $hostname, $global_config )
@@ -167,117 +522,6 @@
return $site_ref->{'zone'};
}
-=head2
-
-get_hosttype_from_hostname ( $hostname, $global_config, $site )
-
-Returns the hosttype.
-
-=cut
-
-sub get_hosttype_from_hostname {
- my ( $hostname, $global_config, $site ) = @_;
-
- if ( not $hostname ) {
- croak q{ERROR: Invalid empty $hostname};
- }
- if ( ref $hostname ) {
- croak q{ERROR: Invalid non-scalar $hostname};
- }
-
- if ( not $global_config ) {
- croak q{ERROR: Invalid empty $global_config};
- }
- if ( ref $global_config ne 'HASH' ) {
- croak q{ERROR: Invalid non-hashref $global_config};
- }
-
- if ( $site and ref $site ) {
- croak q{ERROR: Invalid non-scalar $site};
- }
-
- my $site_list = ($site)
- ? [$site]
- : $global_config->{'SITE'}->{'__site_list'};
-
- foreach my $site ( @{$site_list} ) {
- my $site_ref = get_site_config( $site, $global_config );
- my $host_part = $site_ref->{'HOST'}->{'BY_NAME'};
- foreach my $hostclass ( keys %{$host_part} ) {
- return $hostclass if $hostclass eq $hostname;
-
- foreach my $host ( keys %{ $host_part->{$hostclass} } ) {
- return $hostclass if $host eq $hostname;
- }
- }
- }
-
- croak qq{ERROR: Unknown hostname $hostname}
- . ( $site ? qq{ on site $site} : q{} );
-}
-
-=head2 get_iface_vlan_from_hostname( $vlan, $host_ref )
-
-This function returns the interface name for a given vlan.
-
-=cut
-
-sub get_iface_vlan_from_hostname {
- my ( $vlan, $host_ref ) = @_;
-
- foreach my $iface ( keys %{ $host_ref->{'interfaces'} } ) {
- if ( $host_ref->{'interfaces'}->{$iface}->{'vlan'} eq $vlan ) {
- return $iface;
- }
- }
-
- return;
-}
-
-=head2 get_site_list_from_hostname( $hostname, $global_config )
-
-This function returns the list of sites for a given hostname.
-
-=cut
-
-sub get_site_list_from_hostname {
- my ( $hostname, $global_config ) = @_;
-
- if ( not $hostname ) {
- croak q{ERROR: Invalid empty hostname};
- }
- if ( ref $hostname ) {
- croak q{ERROR: Invalid non-scalar hostname};
- }
-
- if ( ref $global_config ne 'HASH' ) {
- croak q{ERROR: Invalid non-hashref global_config};
- }
-
- my @site_list = ();
- foreach my $site ( @{ $global_config->{'SITE'}->{'__site_list'} } ) {
- my $site_ref = get_site_config( $site, $global_config );
- my $host_part = $site_ref->{'HOST'}->{'BY_NAME'};
- foreach my $hostclass ( keys %{$host_part} ) {
- if ( $hostclass eq $hostname ) {
- push @site_list, $site;
- next;
- }
-
- foreach my $host ( keys %{ $host_part->{$hostclass} } ) {
- if ( $host eq $hostname ) {
- push @site_list, $site;
- last;
- }
- }
- }
- }
-
- @site_list = uniq @site_list;
-
- return \@site_list;
-}
-
=head2 get_zone_from_site( $site_name, $global_config )
Returns a reference to the hash describing the zone for a given site.
@@ -289,141 +533,6 @@
my $site_ref = get_site_config( $site_name, $global_config );
return $site_ref->{'zone'};
-}
-
-=head2 get_site_list_from_section ( $section_ref, $global_config )
-
-This function builds up the list of sites specified in a given section (hash).
-Returns a reference to the list.
-
-=cut
-
-sub get_site_list_from_section {
- my ( $section_ref, $global_config ) = @_;
-
- if ( $section_ref->{'site'} eq 'ALL' ) {
- return $global_config->{'SITE'}->{'__site_list'};
- }
-
- my @site_list = split qr{ \s* [,] \s* }xms, $section_ref->{'site'};
-
-# FIXME also permit '@site' notation ?
-# @site_list = ( @$section_ref->{'@site'}, split qr{ \s* [,] \s* }xms, $section_ref->{'site'} );
-
- return \@site_list;
-}
-
-=head2 get_host_config( $hostname, $global_config, $site_name )
-
-This function returns a reference to the host config hash for I<$hostname>.
-I<$site_name> is optional.
-
-=cut
-
-sub get_host_config {
- my ( $hostname, $global_config, $site_name ) = @_;
-
- $site_name ||= get_uniq_site_from_hostname( $hostname, $global_config );
-
- my $hosttype
- = get_hosttype_from_hostname( $hostname, $global_config, $site_name );
-
- my $site_ref = get_site_config( $site_name, $global_config );
- my $zone = $site_ref->{'zone'};
- my ($hostshort)
- = get_hostshort_hostvlan_zone_from_fqdn( $hostname, $zone );
- if ( not $hostshort ) {
- croak
- qq{ERROR: Unable to get hostshort from hostname $hostname};
- }
-
- my $host_config
- = $site_ref->{'HOST'}->{'BY_NAME'}->{$hosttype}->{$hostshort};
-
- unless ($host_config) {
- croak qq{ERROR: hostname $hostname: no configuration found};
- }
-
- return $host_config;
-}
-
-=head2 get_site_dhcp_config( $site_name, $global_config )
-
-This function returns a reference to the site DHCP configuration hash for
-I<$site_name>.
-
-=cut
-
-sub get_site_dhcp_config {
- my ( $site_name, $global_config ) = @_;
-
- if ( not $site_name or ref $site_name ) {
- croak q{ERROR: Invalid site_name};
- }
-
- if (ref $global_config ne 'HASH'
- or not exists $global_config->{'DHCP'}->{'BY_SITE'}
- )
- {
- croak q{ERROR: Invalid global_config};
- }
-
- my $site_ref = $global_config->{'DHCP'}->{'BY_SITE'}->{$site_name};
-
- if ( not $site_ref ) {
- croak qq{ERROR: Unknown site '$site_name'};
- }
-
- return $site_ref;
-}
-
-=head2 get_site_config( $site_name, $global_config )
-
-This function returns a reference to the site configuration hash for
-I<$site_name>.
-
-=cut
-
-sub get_site_config {
- my ( $site_name, $global_config ) = @_;
-
- if ( not $site_name or ref $site_name ) {
- croak q{ERROR: Invalid site_name};
- }
-
- if (ref $global_config ne 'HASH'
- or not exists $global_config->{'SITE'}->{'BY_NAME'}
- )
- {
- croak q{ERROR: Invalid global_config};
- }
-
- my $site_ref = $global_config->{'SITE'}->{'BY_NAME'}->{$site_name};
-
- if ( not $site_ref ) {
- croak qq{ERROR: Unknown site '$site_name'};
- }
-
- return $site_ref;
-}
-
-=head2 get_vlan_config( $vlan_name, $global_config, $site_name )
-
-This function returns a reference to the vlan config hash for I<$vlan_name>.
-I<$site_name> is mandatory.
-
-=cut
-
-sub get_vlan_config {
- my ( $vlan_name, $global_config, $site_name ) = @_;
-
- unless ($site_name) {
- croak q{ERROR: $site MUST BE defined};
- }
-
- my $site_ref = get_site_config( $site_name, $global_config );
-
- return $site_ref->{'NETWORK'}->{'BY_NAME'}->{$vlan_name};
}
=head2 is_private_vlan( $vlan_name, $global_config, $site_name )
@@ -455,90 +564,6 @@
my $scope = $site_ref->{'NETWORK'}->{'BY_NAME'}->{$vlan_name}->{'scope'};
return $scope eq 'private';
-}
-
-=head2 get_pkgtype_from_hostname( $hostname, $global_config, $site_name )
-
-This function returns the pkgtype for I<$hostname>. I<$site_name> is optional.
-The only pkgtype currently implemented is 'deb'.
-
-=cut
-
-sub get_pkgtype_from_hostname {
- my ( $hostname, $global_config, $site_name ) = @_;
-
- my $host_props = get_host_config( $hostname, $global_config, $site_name );
-
- my %pkgtype_for = (
- debian => q{deb},
- ubuntu => q{deb},
- );
-
- my $mode = $host_props->{'deployment'}->{'mode'};
- if ( not exists $pkgtype_for{$mode} ) {
- croak qq{ERROR: Unknown/not implemented deployment mode $mode};
- }
-
- return $pkgtype_for{$mode};
-}
-
-=head2 get_cmdline_from_host_ref($host_ref)
-
-Returns a list of two elements. The second one contains the bonding-related
-part of the cmdline, the first one is the rest.
-
-=cut
-
-sub get_cmdline_from_host_ref {
- my ($host_ref) = @_;
-
- my $cmdline = $host_ref->{'boot'}->{'cmdline'} || q{};
- my $bond_cmdline = q{};
- foreach my $iface ( sort keys %{ $host_ref->{'interfaces'} } ) {
- next if $iface !~ m{\A bond }xms;
-
- $bond_cmdline
- = qq/bonding.mode=$host_ref->{'interfaces'}->{$iface}->{'mode'}/;
- foreach my $opt (
- split qr{ \s* [,] \s* }xms,
- $host_ref->{'interfaces'}->{$iface}->{'options'}
- )
- {
- $bond_cmdline .= qq{ bonding.$opt};
- }
-
- last;
- }
-
- return ( $cmdline, $bond_cmdline );
-}
-
-=head2 get_distrib_from_host_ref($host_ref)
-
-Returns the distribution name for the host described by I<$host_ref>.
-
-=cut
-
-sub get_distrib_from_host_ref {
- my ($host_ref) = @_;
-
- if ( ref $host_ref ne 'HASH' ) {
- croak q{ERROR: Invalid non-hashref $host_ref};
- }
-
- return $host_ref->{'deployment'}->{'distrib'};
-}
-
-=head2 get_mode_from_host_ref($host_ref)
-
-Returns the deployment mode for the host described by I<$host_ref>.
-
-=cut
-
-sub get_mode_from_host_ref {
- my ($host_ref) = @_;
-
- return $host_ref->{'deployment'}->{'mode'};
}
=head2 resolve_hostname_from_global_config($arguments_ref)
diff -r 418635e2d6eb -r c8d956f0d489 lib/PFTools/Utils.pm
--- a/lib/PFTools/Utils.pm Mon Jan 03 12:05:47 2011 +0100
+++ b/lib/PFTools/Utils.pm Fri Jan 07 18:54:58 2011 +0100
@@ -51,12 +51,11 @@
Init_TOOLS
Do_update_from_GLOBAL
- Change_kopt_for_hostname
-
);
our @EXPORT_OK = qw(
fix_etc_hosts
+ fix_grub_kopt
make_dhcpd_conf_file
make_interfaces_file
make_preseed_file
@@ -478,7 +477,7 @@
check_mandatory_true_args_type(
$args_ref, q{},
- qw( input_filename output_filename )
+ qw( output_filename )
);
__make_file(
@@ -530,61 +529,29 @@
return 1;
}
-# FIXME tests
-# FIXME doc
-# FIXME named arguments
-# FIXME convert to __make_file()
-sub Change_kopt_for_hostname {
- my ($hostname, $site_name, $grub_src, $dst, $grub_version, $global_config,
- $pf_config
- )
- = @_;
+=head2 fix_grub_kopt($args_ref)
- my $host_ref = get_host_config( $hostname, $global_config, $site_name );
- my $mode = $host_ref->{'deployment'}->{'mode'};
+Reads a GRUB configuration file, fixes the kopt configuration, and writes the
+resulting GRUB configuration file. See C<__fix_grub_kopt()> documentation for
+details.
- my $cmdline = join q{ }, get_cmdline_from_host_ref($host_ref);
- $cmdline =~ s{ \A \s* }{}xms; # Remove leading white space
+=cut
- my $grub_key = $grub_version == 1 ? q{} : $grub_version;
- $grub_src ||= $pf_config->{$mode}->{"grub$grub_key"};
+sub fix_grub_kopt {
+ my ($args_ref) = @_;
- my $lines_ref = __read_file_in_array( $grub_src, 1 );
+ check_mandatory_true_args_type(
+ $args_ref, q{},
+ qw( output_filename )
+ );
- foreach my $line ( @{$lines_ref} ) {
- if ($grub_version == 1
- and $line =~ m{ \A [#] kopt=.* \z }xms
- or $grub_version == 2
- and $line =~ m{ \A GRUB_CMDLINE_LINUX_DEFAULT=".*" \z }xms
- )
+ __make_file(
{
-
- if ( $cmdline and $line !~ m{ \Q $cmdline \E ["] \z }xms ) {
- $line =~ s{ ["] \z }{ $cmdline" }xms
- ; # FIXME really want to add a " if $grub_version == 1 ??
- }
+ %{$args_ref},
+ file_type => q{grub_config},
+ filename => $args_ref->{'output_filename'},
}
- }
-
- # Either STDOUT or a tempfile...
- my ( $dst_fh, $dst_fn );
- if ( $dst eq q{-} ) {
- $dst_fh = IO::File->new();
- unless ( $dst_fh->fdopen( fileno(STDOUT), q{>} ) ) {
- croak qq{ERROR: fdopen STDOUT: $OS_ERROR};
- }
- $dst_fn = $dst;
- }
- else {
- $dst_fh = File::Temp->new( unlink => 0 ); # will croak() on error
- $dst_fn = $dst_fh->filename();
- }
-
- __write_array_to_filehandle( $dst_fh, $dst_fn, $lines_ref, qq{\n} );
-
- if ( $dst ne q{-} ) {
- __move_if_different( $dst_fn, $dst );
- }
+ );
return 1;
}
@@ -1333,8 +1300,7 @@
);
# Get cmdline, remove leading and trailing whitespace
- ( my $cmdline = join q{ }, get_cmdline_from_host_ref($host_ref) )
- =~ s{ \A \s* (\S*) \s* \z}{$1}xms;
+ my $cmdline = get_complete_cmdline_from_host_ref($host_ref);
my $vars_ref = {
'iface' => $iface,
@@ -1928,6 +1894,104 @@
return $lines_ref;
}
+=head2 __fix_grub_kopt($args_ref)
+
+Reads a GRUB configuration file, fixes the kopt configuration, and returns a
+reference to the list of (possibly updated) lines. Takes the following named
+arguments:
+
+=over
+
+=item I<hostname> the host name
+
+=item I<site_name> (optional) the site name
+
+=item I<input_filename> the file to read from (optional, a default value is
+provided in $global_config FIXME...)
+
+=item I<grub_version> the GRUB version (1 or 2)
+
+=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
+
+sub __fix_grub_kopt {
+ my ($args_ref) = @_;
+
+ check_mandatory_true_args_type(
+ $args_ref, q{},
+ qw( hostname grub_version )
+ );
+
+ check_optional_args_type(
+ $args_ref, q{},
+ qw( site_name input_filename )
+ );
+
+ check_mandatory_args_type(
+ $args_ref, q{HASH},
+ qw( global_config pf_config )
+ );
+
+ my ($hostname, $site_name, $input_filename, $grub_version, $global_config,
+ $pf_config
+ )
+ = @{$args_ref}{qw( hostname site_name input_filename grub_version global_config )};
+
+ if ( $grub_version != 1 and $grub_version != 2 ) {
+ croak qq{ERROR: Invalid grub_version '$grub_version'};
+ }
+
+ my $host_ref = get_host_config( $hostname, $global_config, $site_name );
+ my $mode = $host_ref->{'deployment'}->{'mode'};
+
+ my $cmdline = get_complete_cmdline_from_host_ref($host_ref);
+
+ my $grub_key = $grub_version == 1 ? q{} : $grub_version;
+ $input_filename ||= $pf_config->{$mode}->{"grub$grub_key"};
+
+ my $lines_ref = __read_file_in_array( $input_filename, 1 );
+
+ # If no $cmdline, nothing to change
+ if ($cmdline) {
+ foreach my $line ( @{$lines_ref} ) {
+ if ( $grub_version == 1
+ and $line =~ m{ \A [#] [ ] kopt= \s* (.*) \s* \z }xms )
+ {
+ my $old_kopt = $1;
+ if ( $old_kopt =~ m{ \Q$cmdline\E }xms ) {
+ next;
+ }
+
+ my $new_kopt
+ = trim_surrounding_whitespace( join q{ }, $old_kopt,
+ $cmdline );
+ $line = qq{# kopt=$new_kopt};
+ }
+
+ if ( $grub_version == 2
+ and $line =~ m{ \A GRUB_CMDLINE_LINUX_DEFAULT="(.*)" \z }xms )
+ {
+ my $old_kopt = $1;
+ if ( $old_kopt =~ m{ \Q$cmdline\E }xms ) {
+ next;
+ }
+
+ my $new_kopt
+ = trim_surrounding_whitespace( join q{ }, $old_kopt,
+ $cmdline );
+ $line = qq{GRUB_CMDLINE_LINUX_DEFAULT="$new_kopt"};
+ }
+ }
+ }
+
+ return $lines_ref;
+}
+
=head2 __build_dhcpd_conf($args_ref)
Builds the I<dhcpd.conf> content for a given site. Returns a reference to the
@@ -2295,6 +2359,7 @@
my %build_content_for = (
q{dhcpd.conf} => \&__build_dhcpd_conf,
etc_hosts => \&__fix_etc_hosts,
+ grub_config => \&__fix_grub_kopt,
interfaces => \&__build_interfaces,
preseed => \&__build_preseed,
pxe_boot => \&__build_pxe_boot,
diff -r 418635e2d6eb -r c8d956f0d489 sbin/mk_grubopt
--- a/sbin/mk_grubopt Mon Jan 03 12:05:47 2011 +0100
+++ b/sbin/mk_grubopt Fri Jan 07 18:54:58 2011 +0100
@@ -26,33 +26,27 @@
use Sys::Hostname;
use PFTools::Structqueries;
-use PFTools::Utils;
+use PFTools::Utils qw( Init_TOOLS fix_grub_kopt );
####################################################
# Vars
my @options_specs = (
+ 'config|c=s',
+ 'grub-version|v=s',
'help',
'host|h=s',
+ 'input|i=s',
+ 'output|o=s',
'site|s=s',
- 'src=s',
- 'grub=s',
- 'output|o=s',
- 'config|c=s',
'store=s',
);
my $options = {
- 'config' => '',
+ 'grub-version' => 2,
'host' => hostname,
- 'help' => 0,
- 'grub' => 2,
- 'src' => '',
'output' => '-',
};
-
-my $PF_CONFIG = {};
-my $GLOBAL_STRUCT = {};
my $program = basename $PROGRAM_NAME;
@@ -63,14 +57,16 @@
print STDERR << "# ENDHELP";
Usage: $program [options]
- -h --help : print help and exit
- -h --host : the hostname for which you want to build grub configuration
- -s --site : the site where the hostname is defined
- --src : the source for grub configuration
- --grub : version of grub default value is 2
- -o --output : destination for modified GRUB configuration default is STDOUT
+ -c --config : path for accessing pf-tools.conf file
+ --help : print help and exit
+ -h, --host : the hostname for which you want to build grub configuration
+ -v, --grub-version
+ : version of grub [default: 2]
+ -i, --input : the grub configuration file to read
+ -o, --output: the grub configuration file to write [default: STDOUT]
+ -s, --site : the site where the hostname is defined
--store : path for accessing storable file containing the global configuration
- -c --config : path for accessing pf-tools.conf file
+
# ENDHELP
}
@@ -85,35 +81,25 @@
exit 0;
}
-( $PF_CONFIG, $GLOBAL_STRUCT ) = Init_TOOLS(
+my ( $pf_config, $global_config ) = Init_TOOLS(
$options->{'host'},
$options->{'config'},
$options->{'store'}
);
-unless ( $options->{'site'} ) {
- $options->{'site'} = $PF_CONFIG->{'location'}->{'site'}
- || get_uniq_site_from_hostname( $options->{'host'}, $GLOBAL_STRUCT );
-}
+$options->{'site'} ||= $pf_config->{'location'}->{'site'}
+ || get_uniq_site_from_hostname( $options->{'host'}, $global_config );
-# FIXME get_site_dhcp_config()
-unless ( $GLOBAL_STRUCT->{'DHCP'}->{'BY_SITE'}->{ $options->{'site'} } ) {
- die "Unknown DHCP site $options->{'site'}";
-}
-
-if (
- !Change_kopt_for_hostname(
- $options->{'host'},
- $options->{'site'},
- $options->{'src'},
- $options->{'output'},
- $options->{'grub'},
- $GLOBAL_STRUCT,
- $PF_CONFIG
- )
- )
-{
- die "Unable to change kernel options(s) into file $options->{'src'}";
-}
+fix_grub_kopt(
+ {
+ hostname => $options->{'host'},
+ site_name => $options->{'site'},
+ input_filename => $options->{'input'},
+ output_filename => $options->{'output'},
+ grub_version => $options->{'grub-version'},
+ global_config => $global_config,
+ pf_config => $pf_config,
+ }
+);
exit 0;
diff -r 418635e2d6eb -r c8d956f0d489 t/10.whitespace.t
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/t/10.whitespace.t Fri Jan 07 18:54:58 2011 +0100
@@ -0,0 +1,34 @@
+#!perl
+
+use strict;
+use warnings;
+
+use English qw( -no_match_vars ); # Avoids regex performance penalty
+use Test::Exception;
+use Test::More qw( no_plan );
+
+use PFTools;
+
+########################################################################
+note('Testing PFTools::trim_surrounding_whitespace');
+can_ok( 'PFTools', qw( trim_surrounding_whitespace ) );
+
+throws_ok { trim_surrounding_whitespace( {} ); }
+ qr{\A ERROR: [ ] Invalid [ ] non-scalar [ ] string [ ] }xms
+ => 'Non-scalar argument';
+
+is trim_surrounding_whitespace(), undef
+ => q{Returns undef if no arg};
+
+is trim_surrounding_whitespace( q{} ), q{}
+ => q{Returns empty string if empty string};
+
+is trim_surrounding_whitespace( qq{ \t } ), q{}
+ => q{Returns empty string if only whitespace};
+
+is trim_surrounding_whitespace( q{ abc } ), q{abc}
+ => q{One word};
+
+is trim_surrounding_whitespace( q{ a b c } ), q{a b c}
+ => q{Several words separated by whitespace};
+
diff -r 418635e2d6eb -r c8d956f0d489 t/13.conf.cfg1/config-export/MODEL/model-rdeploy
--- a/t/13.conf.cfg1/config-export/MODEL/model-rdeploy Mon Jan 03 12:05:47 2011 +0100
+++ b/t/13.conf.cfg1/config-export/MODEL/model-rdeploy Fri Jan 07 18:54:58 2011 +0100
@@ -7,7 +7,7 @@
kerneluml = linux-uml-elf-2.4.26-gr1.9.15
pxefilename = pxelinux.0
console = default
-# cmdline = pci=bfsort
+ cmdline = pci=bfsort
[dns]
shortname = vlan-systeme
diff -r 418635e2d6eb -r c8d956f0d489 t/13.conf.t
--- a/t/13.conf.t Mon Jan 03 12:05:47 2011 +0100
+++ b/t/13.conf.t Fri Jan 07 18:54:58 2011 +0100
@@ -1103,7 +1103,8 @@
'kerneluml' =>
'linux-uml-elf-2.4.26-gr1.9.15',
'pxefilename' => 'pxelinux.0',
- 'console' => 'default'
+ 'console' => 'default',
+ 'cmdline' => 'pci=bfsort',
},
'interfaces' => {
'eth0' => {
@@ -1133,7 +1134,8 @@
'kerneluml' =>
'linux-uml-elf-2.4.26-gr1.9.15',
'pxefilename' => 'pxelinux.0',
- 'console' => 'default'
+ 'console' => 'default',
+ 'cmdline' => 'pci=bfsort',
},
'interfaces' => {
'eth0' => {
@@ -1403,16 +1405,16 @@
=> q{Dies if non-scalar $hostname};
throws_ok { get_hosttype_from_hostname('hostname') }
-qr{ \A ERROR: [ ] Invalid [ ] empty [ ] [\$] global_config }xms
+qr{ \A ERROR: [ ] Invalid [ ] non-hash [ ] reference [ ] global_config }xms
=> 'Dies if no global_config';
throws_ok { get_hosttype_from_hostname( 'hostname', 'global_config' ) }
-qr{ \A ERROR: [ ] Invalid [ ] non-hashref [ ] [\$] global_config }xms
+qr{ \A ERROR: [ ] Invalid [ ] non-hash [ ] reference [ ] global_config }xms
=> q{Dies if non-hashref $global_config};
throws_ok { get_hosttype_from_hostname( 'hostname', {}, {} ) }
-qr{ \A ERROR: [ ] Invalid [ ] non-scalar [ ] [\$] site }xms
- => q{Dies if non-scalar $site};
+qr{ \A ERROR: [ ] Invalid [ ] non-scalar [ ] site_name }xms
+ => q{Dies if non-scalar $site_name};
throws_ok { get_hosttype_from_hostname( 'hostname', {} ) }
qr{ \A ERROR: [ ] Unknown [ ] hostname }xms
@@ -1800,7 +1802,8 @@
'kernel' => 'vmlinuz-2.6.26.5-universal-grm2.1.12',
'kerneluml' => 'linux-uml-elf-2.4.26-gr1.9.15',
'pxefilename' => 'pxelinux.0',
- 'console' => 'default'
+ 'console' => 'default',
+ 'cmdline' => 'pci=bfsort',
},
'interfaces' => {
'eth0' => {
@@ -1828,7 +1831,8 @@
'kernel' => 'vmlinuz-2.6.26.5-universal-grm2.1.12',
'kerneluml' => 'linux-uml-elf-2.4.26-gr1.9.15',
'pxefilename' => 'pxelinux.0',
- 'console' => 'default'
+ 'console' => 'default',
+ 'cmdline' => 'pci=bfsort',
},
'interfaces' => {
'eth0' => {
@@ -1926,37 +1930,144 @@
my @expected_result = qw( rdeploy00 vlan-systeme private );
is_deeply \@result, \@expected_result
=> q{Returns the correct result for rdeploy00.vlan-systeme.private}
- or note explain @result;
+ or note explain \@result;
@result = get_hostshort_hostvlan_zone_from_fqdn( q{cbv4-rdeploy00.vlan-systeme.private} );
@expected_result = qw( cbv4-rdeploy00 vlan-systeme private );
is_deeply \@result, \@expected_result
=> q{Returns the correct result for cbv4-rdeploy00.vlan-systeme.private}
- or note explain @result;
+ or note explain \@result;
@result = get_hostshort_hostvlan_zone_from_fqdn( q{cbv4-rdeploy00.vlan-systeme} );
@expected_result = ( q{cbv4-rdeploy00}, q{vlan-systeme}, undef );
is_deeply \@result, \@expected_result
=> q{Returns the correct result for cbv4-rdeploy00.vlan-systeme}
- or note explain @result;
+ or note explain \@result;
@result = get_hostshort_hostvlan_zone_from_fqdn( q{cbv4-rdeploy00} );
@expected_result = ( q{cbv4-rdeploy00}, undef, undef );
is_deeply \@result, \@expected_result
=> q{Returns the correct result for cbv4-rdeploy00}
- or note explain @result;
+ or note explain \@result;
@result = get_hostshort_hostvlan_zone_from_fqdn( q{rdeploy00.vlan-systeme.private}, q{private} );
@expected_result = qw( rdeploy00 vlan-systeme private );
is_deeply \@result, \@expected_result
=> q{Returns the correct result for rdeploy00.vlan-systeme.private with explicit correct zone }
- or note explain @result;
+ or note explain \@result;
@result = get_hostshort_hostvlan_zone_from_fqdn( q{rdeploy00.vlan-systeme.private}, q{public} );
@expected_result = ();
is_deeply \@result, \@expected_result
=> q{Returns the correct result for rdeploy00.vlan-systeme.private with incorrect zone }
- or note explain @result;
+ or note explain \@result;
+
+########################################################################
+note('Testing PFTools::Structqueries::get_cmdline_from_host_ref');
+can_ok( 'PFTools::Structqueries', qw( get_cmdline_from_host_ref ) );
+
+throws_ok { get_cmdline_from_host_ref(); }
+qr{ \A ERROR: [ ] Invalid [ ] non-hash [ ] reference [ ] host_ref }xms
+ => 'No argument';
+
+throws_ok { get_cmdline_from_host_ref( q{host_ref} ); }
+qr{ \A ERROR: [ ] Invalid [ ] non-hash [ ] reference [ ] host_ref }xms
+ => 'Scalar argument';
+
+my $host_ref = get_host_config( q{cbv4-rdeploy01}, $global_config, q{cbv4} );
+ at result = get_cmdline_from_host_ref($host_ref);
+ at expected_result = ( q{pci=bfsort}, q{} );
+is_deeply \@result, \@expected_result
+ => q{Correct result for cbv4-rdeploy01}
+ or note explain \@result;
+
+my $host_ref = get_host_config( q{cbv4-spawn01}, $global_config, q{cbv4-pfds} );
+ at result = get_cmdline_from_host_ref($host_ref);
+ at expected_result = ( q{}, q{bonding.mode=active-backup bonding.miimon=100} );
+is_deeply \@result, \@expected_result
+ => q{Correct result for cbv4-spawn01}
+ or note explain \@result;
+
+########################################################################
+note('Testing PFTools::Structqueries::get_complete_cmdline_from_host_ref');
+can_ok( 'PFTools::Structqueries', qw( get_complete_cmdline_from_host_ref ) );
+
+throws_ok { get_complete_cmdline_from_host_ref(); }
+qr{ \A ERROR: [ ] Invalid [ ] non-hash [ ] reference [ ] host_ref }xms
+ => 'No argument';
+
+throws_ok { get_complete_cmdline_from_host_ref( q{host_ref} ); }
+qr{ \A ERROR: [ ] Invalid [ ] non-hash [ ] reference [ ] host_ref }xms
+ => 'Scalar argument';
+
+throws_ok { get_complete_cmdline_from_host_ref( [ q{host_ref} ] ); }
+qr{ \A ERROR: [ ] Invalid [ ] non-hash [ ] reference [ ] host_ref }xms
+ => 'Scalar argument';
+
+$host_ref = get_host_config( q{cbv4-rdeploy01}, $global_config, q{cbv4} );
+$result = get_complete_cmdline_from_host_ref($host_ref);
+$expected_result = q{pci=bfsort};
+
+is $result, $expected_result
+ => q{cbv4-rdeploy01}
+ or diag explain $result;
+
+########################################################################
+note('Testing PFTools::Structqueries::get_distrib_from_host_ref');
+can_ok( 'PFTools::Structqueries', qw( get_distrib_from_host_ref ) );
+
+throws_ok { get_distrib_from_host_ref(); }
+qr{ \A ERROR: [ ] Invalid [ ] non-hash [ ] reference [ ] host_ref }xms
+ => 'No argument';
+
+$host_ref = get_host_config( q{cbv4-rdeploy01}, $global_config, q{cbv4} );
+$result = get_distrib_from_host_ref($host_ref);
+$expected_result = q{lenny};
+
+is $result, $expected_result
+ => q{cbv4-rdeploy01}
+ or diag explain $result;
+
+########################################################################
+note('Testing PFTools::Structqueries::get_host_config');
+can_ok( 'PFTools::Structqueries', qw( get_host_config ) );
+
+my @args = ();
+throws_ok { get_host_config(@args); }
+qr{ \A ERROR: [ ] Invalid [ ] empty [ ] hostname }xms
+ => q{No arguments};
+
+ at args = ( {} );
+throws_ok { get_host_config(@args); }
+qr{ \A ERROR: [ ] Invalid [ ] non-scalar [ ] hostname }xms
+ => q{Non-scalar hostname};
+
+ at args = qw( hostname );
+throws_ok { get_host_config(@args); }
+qr{ \A ERROR: [ ] Invalid [ ] non-hash [ ] reference [ ] global_config }xms
+ => q{No global_config};
+
+ at args = ( q{hostname}, q{global_config} );
+throws_ok { get_host_config(@args); }
+qr{ \A ERROR: [ ] Invalid [ ] non-hash [ ] reference [ ] global_config }xms
+ => q{No global_config};
+
+ at args = ( q{hostname}, $global_config, { site => q{name} } );
+throws_ok { get_host_config(@args); }
+qr{ \A ERROR: [ ] Invalid [ ] non-scalar [ ] site_name }xms
+ => q{Non-scalar site_name};
+
+ at args = ( q{hostname}, $global_config );
+throws_ok { get_host_config(@args); }
+qr{ \A ERROR: [ ] Unknown [ ] hostname [ ] 'hostname' [ ] }xms
+ => q{Unknown hostname};
+
+#$result = get_host_config( q{cbv4-rdeploy01}, $global_config, q{cbv4} );
+#$expected_result = q{lenny};
+
+#is $result, $expected_result
+# => q{cbv4-rdeploy01}
+# or diag explain $result;
#TODO: {
diff -r 418635e2d6eb -r c8d956f0d489 t/20.files.fix_grub_kopt.input.1
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/t/20.files.fix_grub_kopt.input.1 Fri Jan 07 18:54:58 2011 +0100
@@ -0,0 +1,10 @@
+# foo
+foo
+
+# kopt=root=/dev/foo ro
+# kopt=
+
+kopt=foobar
+
+# the end
+
diff -r 418635e2d6eb -r c8d956f0d489 t/20.files.fix_grub_kopt.input.2
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/t/20.files.fix_grub_kopt.input.2 Fri Jan 07 18:54:58 2011 +0100
@@ -0,0 +1,12 @@
+# foo
+foo
+
+# GRUB_CMDLINE_LINUX_DEFAULT="foo"
+
+GRUB_CMDLINE_LINUX_DEFAULT=
+GRUB_CMDLINE_LINUX_DEFAULT=""
+GRUB_CMDLINE_LINUX_DEFAULT=foo
+GRUB_CMDLINE_LINUX_DEFAULT="foo bar baz"
+
+# the end
+
diff -r 418635e2d6eb -r c8d956f0d489 t/20.files.t
--- a/t/20.files.t Mon Jan 03 12:05:47 2011 +0100
+++ b/t/20.files.t Fri Jan 07 18:54:58 2011 +0100
@@ -414,7 +414,7 @@
$args_ref->{'hostname'} = q{foo};
$args_ref->{'site_name'} = q{cbv4-pfds};
throws_ok { PFTools::Utils::__build_interfaces($args_ref); }
-qr{ \A ERROR: [ ] Unknown [ ] hostname [ ] foo [ ] on [ ] site [ ] cbv4-pfds }xms
+qr{ \A ERROR: [ ] Unknown [ ] hostname [ ] 'foo' [ ] on [ ] site [ ] 'cbv4-pfds' }xms
=> q{Dies if unknown $site_name};
$args_ref->{'hostname'} = q{cbv4-spawn01};
@@ -425,7 +425,7 @@
$args_ref->{'site_name'} = q{cbv4};
throws_ok { PFTools::Utils::__build_interfaces($args_ref); }
-qr{ \A ERROR: [ ] Unknown [ ] hostname [ ] cbv4-spawn01 [ ] on [ ] site [ ] cbv4 }xms
+qr{ \A ERROR: [ ] Unknown [ ] hostname [ ] 'cbv4-spawn01' [ ] on [ ] site [ ] 'cbv4' }xms
=> q{Dies if unknown $hostname};
$args_ref->{'site_name'} = q{cbv4-pfds};
@@ -876,11 +876,11 @@
LABEL install
\tkernel debian-installer/lenny/amd64/linux
-\tappend DEBCONF_PRIORITY=critical vga=normal auto=true initrd=debian-installer/lenny/amd64/initrd.gz interface=eth0 netcfg/no_default_route=true url=http://vip-deploy.vlan-systeme.private/preseed_cbv4-rdeploy01 url/checksum=aa03e0e1599f6da3149aa94027d116a0 -- default
+\tappend DEBCONF_PRIORITY=critical vga=normal auto=true initrd=debian-installer/lenny/amd64/initrd.gz interface=eth0 netcfg/no_default_route=true url=http://vip-deploy.vlan-systeme.private/preseed_cbv4-rdeploy01 url/checksum=aa03e0e1599f6da3149aa94027d116a0 -- default pci=bfsort
LABEL linux
\tkernel vmlinuz-2.6.26.5-universal-grm2.1.12
-\tappend vga=normal root=/dev/sda2 -- default
+\tappend vga=normal root=/dev/sda2 -- default pci=bfsort
PROMPT 1
TIMEOUT 100
@@ -978,15 +978,15 @@
my $output_filename = q{t/20.files.etc.hosts.output};
$args_ref = {
- hostname => q{cbv4-rdeploy01},
- site_name => q{cbv4},
- ip_type => q{ipv4},
- global_config => $global_config,
- pf_config => $pf_config,
+ hostname => q{cbv4-rdeploy01},
+ site_name => q{cbv4},
+ output_filename => $output_filename,
+ ip_type => q{ipv4},
+ global_config => $global_config,
+ pf_config => $pf_config,
};
$args_ref->{'input_filename'} = undef;
-$args_ref->{'output_filename'} = undef;
throws_ok { fix_etc_hosts($args_ref); }
qr{ \A ERROR: [ ] Invalid [ ] false [ ] argument [ ] input_filename }xms
=> q{Dies if undefined input_filename};
@@ -997,6 +997,7 @@
=> q{Dies if non-scalar input_filename};
$args_ref->{'input_filename'} = $input_filename;
+$args_ref->{'output_filename'} = undef;
throws_ok { fix_etc_hosts($args_ref); }
qr{ \A ERROR: [ ] Invalid [ ] false [ ] argument [ ] output_filename }xms
@@ -1235,3 +1236,70 @@
=> q{Correct result for filter_type 'ip' and DUPLICATE separator}
or note explain $result;
+########################################################################
+note('Testing PFTools::Utils::fix_grub_kopt');
+can_ok( 'PFTools::Utils', qw( fix_grub_kopt ) );
+
+$input_filename = q{t/20.files.fix_grub_kopt.input.1};
+$output_filename = q{test.output};
+
+$args_ref = {
+# hostname => q{cbv4-rdeploy01},
+ site_name => q{cbv4},
+ input_filename => $input_filename,
+ output_filename => $output_filename,
+ grub_version => 42,
+ global_config => $global_config,
+ pf_config => $pf_config,
+};
+
+throws_ok { fix_grub_kopt($args_ref); }
+qr{ \A ERROR: [ ] Mandatory [ ] argument [ ] hostname [ ] not [ ] found [ ] }xms
+ => q{Dies if no hostname};
+
+$args_ref->{'hostname'} = q{cbv4-rdeploy01};
+throws_ok { fix_grub_kopt($args_ref); }
+qr{ \A ERROR: [ ] Invalid [ ] grub_version [ ] '42' [ ] }xms
+ => q{Invalid grub_version};
+
+$args_ref->{'grub_version'} = 1;
+ok fix_grub_kopt($args_ref);
+$result = PFTools::Utils::__read_file_in_array($output_filename, 1);
+$expected_result = [ split qr{ \n }xms, <<"EOT", -1 ];
+# foo
+foo
+
+# kopt=root=/dev/foo ro pci=bfsort
+# kopt=pci=bfsort
+
+kopt=foobar
+
+# the end
+EOT
+
+is_deeply $result, $expected_result
+ => q{Correct result for GRUB version 1}
+ or note explain $result;
+
+$args_ref->{'input_filename'} = q{t/20.files.fix_grub_kopt.input.2};
+$args_ref->{'grub_version'} = 2;
+ok fix_grub_kopt($args_ref);
+$result = PFTools::Utils::__read_file_in_array($output_filename, 1);
+$expected_result = [ split qr{ \n }xms, <<"EOT", -1 ];
+# foo
+foo
+
+# GRUB_CMDLINE_LINUX_DEFAULT="foo"
+
+GRUB_CMDLINE_LINUX_DEFAULT=
+GRUB_CMDLINE_LINUX_DEFAULT="pci=bfsort"
+GRUB_CMDLINE_LINUX_DEFAULT=foo
+GRUB_CMDLINE_LINUX_DEFAULT="foo bar baz pci=bfsort"
+
+# the end
+EOT
+
+is_deeply $result, $expected_result
+ => q{Correct result for GRUB version 2}
+ or note explain $result;
+
More information about the pf-tools-commits
mailing list