From b89478cd6430ab1d33c756b7ab8ae96e90aa2c07 Mon Sep 17 00:00:00 2001 From: linggao Date: Thu, 2 Apr 2009 21:38:45 +0000 Subject: [PATCH] fix makehosts command to be able to specify if long name comes before short name or not. And remove the duplicate entries. And enable adding hosts for different domains git-svn-id: https://svn.code.sf.net/p/xcat/code/xcat-core/trunk@3072 8638fb3e-16cb-4fca-ae20-7b5d299a9bcd --- xCAT-client/pods/man8/makehosts.8.pod | 7 ++- xCAT-server/lib/xcat/plugins/hosts.pm | 85 +++++++++++++++++++++------ 2 files changed, 73 insertions(+), 19 deletions(-) diff --git a/xCAT-client/pods/man8/makehosts.8.pod b/xCAT-client/pods/man8/makehosts.8.pod index c7b6480cb..090d7777c 100644 --- a/xCAT-client/pods/man8/makehosts.8.pod +++ b/xCAT-client/pods/man8/makehosts.8.pod @@ -4,7 +4,7 @@ B - sets up /etc/hosts from the xCAT hosts table. =head1 SYNOPSIS -B [B<-n>] [I] +B [B<-n>] [I] [B<-l>|B<--longnamefirst>] B {B<-h>|B<--help>} @@ -23,6 +23,11 @@ in the hosts table and then easily generate /etc/hosts using makehosts. Completely replace the /etc/hosts file, losing any previous content. If this option is not specified, it will only replace the lines in the file that correspond to the nodes in the specified noderange. +=item B<-l>|B<--longnamefirst> + +The long name of the host will appear before the short name for each host in the /etc/hosts file. +The default is short name first. + =back =head1 EXAMPLES diff --git a/xCAT-server/lib/xcat/plugins/hosts.pm b/xCAT-server/lib/xcat/plugins/hosts.pm index 2e7d6f166..07ada7fd9 100644 --- a/xCAT-server/lib/xcat/plugins/hosts.pm +++ b/xCAT-server/lib/xcat/plugins/hosts.pm @@ -5,12 +5,15 @@ use warnings; use xCAT::Table; use Data::Dumper; use File::Copy; +use Getopt::Long; + my @hosts; #Hold /etc/hosts data to be written back +my $LONGNAME; my %usage=( - makehosts => "Usage: makehosts [-n] ", + makehosts => "Usage: makehosts [-n] [-l]\n makehosts -h", ); sub handled_commands { return { @@ -31,26 +34,76 @@ sub addnode { while ($idx <= $#hosts) { if ($hosts[$idx] =~ /^${ip}\s/ or $hosts[$idx] =~ /^\d+\.\d+\.\d+\.\d+\s+${node}\s/) { #TODO: if foundone, delete a dupe - if ($domain and $node !~ /\./) { - $hosts[$idx] = "$ip $node.$domain $node $othernames\n"; - } else { - $hosts[$idx] = "$ip $node $othernames\n"; - } + $hosts[$idx]=build_line($ip, $node, $domain, $othernames); $foundone=1; + return; } $idx++; } - if ($foundone) { return;} - if ($domain and $node !~ /\./) { - push @hosts,"$ip $node.$domain $node $othernames\n"; - } else { - push @hosts,"$ip $node $othernames\n"; - } - push @hosts,"$ip $node $othernames\n"; + + my $line=build_line($ip, $node, $domain, $othernames); + push @hosts, $line; } + +sub build_line { + my $ip=shift; + my $node=shift; + my $domain=shift; + my $othernames=shift; + my @o_names=split(/,| /, $othernames); + my $longname; + foreach (@o_names) { + if (($_ eq $node) || ( $domain && ($_ eq "$node.$domain"))) { + $longname="$node.$domain"; + $_=""; + } elsif ( $_ =~ /\./) { + if (!$longname) { + $longname=$_; + $_=""; + } + } + } + + if ($domain && !$longname) { + $longname="$node.$domain"; + } + + $othernames=join(' ', @o_names); + if ($LONGNAME) { return "$ip $longname $node $othernames\n"; } + else { return "$ip $node $longname $othernames\n"; } +} + + sub process_request { + Getopt::Long::Configure("bundling") ; + $Getopt::Long::ignorecase=0; + Getopt::Long::Configure("no_pass_through"); + my $req = shift; my $callback = shift; + my $HELP; + my $REMOVE; + + # parse the options + if ($req && $req->{arg}) {@ARGV = @{$req->{arg}};} + else { @ARGV = (); } + print "argv=@ARGV\n"; + if(!GetOptions( + 'h|help' => \$HELP, + 'n' => \$REMOVE, + 'l|longnamefirst' => \$LONGNAME,)) + { + $callback->({data=>$usage{makehosts}}); + return; + } + + # display the usage if -h + if ($HELP) { + $callback->({data=>$usage{makehosts}}); + return; + } + + my $hoststab = xCAT::Table->new('hosts'); my $sitetab = xCAT::Table->new('site'); my $domain; @@ -62,11 +115,7 @@ sub process_request { } @hosts = (); - if (grep /-h/,@{$req->{arg}}) { - $callback->({data=>$usage{makehosts}}); - return; - } - if (grep /-n/,@{$req->{arg}}) { + if ($REMOVE) { if (-e "/etc/hosts") { my $bakname = "/etc/hosts.xcatbak"; rename("/etc/hosts",$bakname);