From 0ea24f1db932efd39b571975c36a20fe7ae8ba89 Mon Sep 17 00:00:00 2001 From: linggao Date: Thu, 11 Jun 2015 12:17:01 -0400 Subject: [PATCH] added new command makentp to setup ntp servers on mn and sn. --- perl-xCAT/xCAT/Schema.pm | 6 +- perl-xCAT/xCAT/Usage.pm | 4 + xCAT-client/debian/xcat-client.links | 2 + xCAT-client/xCAT-client.spec | 1 + xCAT-server/lib/xcat/plugins/AAsn.pm | 104 +-------- xCAT-server/lib/xcat/plugins/makentp.pm | 280 ++++++++++++++++++++++++ 6 files changed, 302 insertions(+), 95 deletions(-) create mode 100755 xCAT-server/lib/xcat/plugins/makentp.pm diff --git a/perl-xCAT/xCAT/Schema.pm b/perl-xCAT/xCAT/Schema.pm index 0949ea4b4..3a46009fe 100755 --- a/perl-xCAT/xCAT/Schema.pm +++ b/perl-xCAT/xCAT/Schema.pm @@ -1147,8 +1147,10 @@ site => { " give stable output. You can increase the timeout value by specifying \n". " '--min-rtt-timeout 1s'. xCAT will append the options defined here to \n". " the nmap command.\n\n". - " ntpservers: A comma delimited list of NTP servers for the cluster - often the\n". - " xCAT management node.\n\n". + " ntpservers: A comma delimited list of NTP servers for the node. Often the\n". + " xCAT management node is the NTP server.\n\n". + " extntpservers: A comma delimited list of external NTP servers for the xCAT\n". + " management node to sync with.\n\n". " svloglocal: if set to 1, syslog on the service node will not get forwarded to the\n". " mgmt node.\n\n". " timezone: (e.g. America/New_York)\n\n". diff --git a/perl-xCAT/xCAT/Usage.pm b/perl-xCAT/xCAT/Usage.pm index 2ef132eb3..4da60dab9 100755 --- a/perl-xCAT/xCAT/Usage.pm +++ b/perl-xCAT/xCAT/Usage.pm @@ -290,6 +290,9 @@ my %usage = ( "switchdiscover" => "Usage: switchdiscover [-h|--help|-v|--version] switchdiscover [|--range ipranges] [-s scan_methods] [-r|-x|-z] [-w] [-V|--verbose]", + "makentp" => +"Usage: makentp [-h|--help|-v|--version] + makentp [-a|--all] [-V|--verbose]", "rflash" => "Usage: rflash [ -h|--help|-v|--version] @@ -486,6 +489,7 @@ my %version = ( "rmvm" => "$vers", "lsslp" => "$vers", "switchdiscover" => "$vers", + "makentp" => "$vers", "rflash" => "$vers", "renergy" => "$vers", "lsflexnode" => "$vers", diff --git a/xCAT-client/debian/xcat-client.links b/xCAT-client/debian/xcat-client.links index 8ad216f98..5ec6f5230 100644 --- a/xCAT-client/debian/xcat-client.links +++ b/xCAT-client/debian/xcat-client.links @@ -122,3 +122,5 @@ opt/xcat/bin/xcatclientnnr opt/xcat/bin/rmzone opt/xcat/bin/xcatclientnnr opt/xcat/bin/slpdiscover opt/xcat/bin/xcatclient opt/xcat/bin/xCATWorld opt/xcat/bin/xcatclientnnr opt/xcat/bin/switchdiscover +opt/xcat/bin/xcatclientnnr opt/xcat/bin/makentp + diff --git a/xCAT-client/xCAT-client.spec b/xCAT-client/xCAT-client.spec index fe4d0c13f..8911d3d22 100644 --- a/xCAT-client/xCAT-client.spec +++ b/xCAT-client/xCAT-client.spec @@ -164,6 +164,7 @@ ln -sf ../bin/xcatclient $RPM_BUILD_ROOT/%{prefix}/bin/rmflexnode ln -sf ../bin/xcatclient $RPM_BUILD_ROOT/%{prefix}/bin/mkflexnode ln -sf ../bin/xcatclientnnr $RPM_BUILD_ROOT/%{prefix}/bin/lsslp ln -sf ../bin/xcatclientnnr $RPM_BUILD_ROOT/%{prefix}/bin/switchdiscover +ln -sf ../bin/xcatclientnnr $RPM_BUILD_ROOT/%{prefix}/bin/makentp ln -sf ../bin/xcatclient $RPM_BUILD_ROOT/%{prefix}/bin/imgcapture ln -sf ../bin/xcatclientnnr $RPM_BUILD_ROOT/%{prefix}/bin/swapnodes ln -sf ../bin/xcatclientnnr $RPM_BUILD_ROOT/%{prefix}/bin/nodegrpch diff --git a/xCAT-server/lib/xcat/plugins/AAsn.pm b/xCAT-server/lib/xcat/plugins/AAsn.pm index 0b4d03392..e09e33aba 100755 --- a/xCAT-server/lib/xcat/plugins/AAsn.pm +++ b/xCAT-server/lib/xcat/plugins/AAsn.pm @@ -128,6 +128,9 @@ sub init_plugin } } + if ($servicelist->{"ntpserver"} == 1) { + &setup_NTP($doreq); + } if ($servicelist->{"proxydhcp"} == 1) { &setup_proxydhcp(1); } else { @@ -168,7 +171,7 @@ sub init_plugin } if ($servicelist->{"ipforward"} == 1) - { + { # enable ip forwarding xCAT::NetworkUtils->setup_ip_forwarding(1); } @@ -870,107 +873,22 @@ sub setup_NFS #----------------------------------------------------------------------------- -=head3 setup_NTPsn +=head3 setup_NTP - Sets up NTP services on service node + Sets up NTP services on mn or sn =cut #----------------------------------------------------------------------------- -sub setup_NTPsn +sub setup_NTP { - my ($nodename) = @_; - my $rc = 0; - my $cmd; - my $master; - my $os; - my $arch; - my $ntpcfg = "/etc/ntp.conf"; + my $doreq = shift; + my $res = xCAT::Utils->runxcmd({ + command => ['makentp'],}, $doreq, 0, 1); - # read DB for nodeinfo - my $retdata = xCAT::ServiceNodeUtils->readSNInfo($nodename); - $master = $retdata->{'master'}; - $os = $retdata->{'os'}; - $arch = $retdata->{'arch'}; - if (!($arch)) - { # error - xCAT::MsgUtils->message("S", " Error reading service node info."); - return 1; - } - - # backup the existing config file - $rc = &backup_NTPconf(); - if ($rc == 0) - { - - # create config file - open(CFGFILE, ">$ntpcfg") - or xCAT::MsgUtils->message('SE', - "Cannot open $ntpcfg for NTP update. \n"); - print CFGFILE "server "; - print CFGFILE $master; - print CFGFILE "\n"; - print CFGFILE "driftfile /var/lib/ntp/drift\n"; - print CFGFILE "restrict 127.0.0.1\n"; - close CFGFILE; - - $rc = &start_NTP(); # restart ntp - } - return $rc; + return 0; } -#----------------------------------------------------------------------------- - -=head3 setup_NTPmn - - Sets up NTP services on Management Node - Get ntpservers from site table. If they do not exist, warn cannot setup NTP -=cut - -#----------------------------------------------------------------------------- -sub setup_NTPmn -{ - my $rc = 0; - my $ntpcfg = "/etc/ntp.conf"; - - # get timeservers from site table - my @ntpservers = xCAT::TableUtils->get_site_attribute("ntpservers"); - if ($ntpservers[0]) - { - - # backup the existing config file - $rc = &backup_NTPconf(); - if ($rc == 0) - { - - # add server names - open(CFGFILE, ">$ntpcfg") - or xCAT::MsgUtils->message('SE', - "Cannot open $ntpcfg for NTP update. \n"); - my @servers = split ',', $ntpservers[0]; - foreach my $addr (@servers) - { - print CFGFILE "server "; - print CFGFILE $addr; - print CFGFILE "\n"; - } - print CFGFILE "driftfile /var/lib/ntp/drift\n"; - print CFGFILE "restrict 127.0.0.1\n"; - close CFGFILE; - - $rc = &start_NTP(); # restart ntp - } - } - else - { # no servers defined - xCAT::MsgUtils->message( - "S", - "No NTP servers defined in the ntpservers attribute in the site table.\n" - ); - return 1; - } - return $rc; -} #----------------------------------------------------------------------------- diff --git a/xCAT-server/lib/xcat/plugins/makentp.pm b/xCAT-server/lib/xcat/plugins/makentp.pm new file mode 100755 index 000000000..b7060ac34 --- /dev/null +++ b/xCAT-server/lib/xcat/plugins/makentp.pm @@ -0,0 +1,280 @@ +#!/usr/bin/env perl +# IBM(c) 2015 EPL license http://www.eclipse.org/legal/epl-v10.html + +package xCAT_plugin::makentp; +BEGIN +{ + $::XCATROOT = $ENV{'XCATROOT'} ? $ENV{'XCATROOT'} : '/opt/xcat'; +} +use lib "$::XCATROOT/lib/perl"; + +use Getopt::Long; +use xCAT::Usage; +use xCAT::NetworkUtils; +use xCAT::TableUtils; +use xCAT::Utils; +use XML::Simple; +no strict; +use Data::Dumper; +use Socket; + +my %globalopt; + +#------------------------------------------------------------------------------- +=head1 xCAT_plugin:makentp +=head2 Package Description + Handles ntp server setup on a xCAT management node. +=cut +#------------------------------------------------------------------------------- + + +#-------------------------------------------------------------------------------- +=head3 send_msg + Invokes the callback with the specified message + Arguments: + request: request structure for plguin calls + ecode: error code. 0 for succeful. + msg: messages to be displayed. + Returns: + none +=cut +#-------------------------------------------------------------------------------- +sub send_msg { + + my $request = shift; + my $ecode = shift; + my $msg = shift; + my %output; + + # Called from child process - send to parent + if ( exists( $request->{pipe} )) { + my $out = $request->{pipe}; + + $output{errorcode} = $ecode; + $output{data} = \@_; + print $out freeze( [\%output] ); + print $out "\nENDOFFREEZE6sK4ci\n"; + } + # Called from parent - invoke callback directly + elsif ( exists( $request->{callback} )) { + my $callback = $request->{callback}; + $output{errorcode} = $ecode; + $output{data} = $msg; + $callback->( \%output ); + } +} + + +#-------------------------------------------------------------------------------- +=head3 handled_commands + It returns a list of commands handled by this plugin. + Arguments: + none + Returns: + a list of commands. +=cut +#-------------------------------------------------------------------------------- +sub handled_commands { + return( {makentp=>"makentp"} ); +} + + +#-------------------------------------------------------------------------------- +=head3 parse_args + Parse the command line options and operands. + Arguments: + request: the request structure for plugin + Returns: + Usage string or error message. + 0 if no user promp needed. + +=cut +#-------------------------------------------------------------------------------- +sub parse_args { + + my $request = shift; + my $args = $request->{arg}; + my $cmd = $request->{command}; + my %opt; + + # Responds with usage statement + local *usage = sub { + my $usage_string = xCAT::Usage->getUsage($cmd); + return( [$_[0], $usage_string] ); + }; + + # No command-line arguments - use defaults + if ( !defined( $args )) { + return(0); + } + + # Checks case in GetOptions, allows opts + # to be grouped (e.g. -vx), and terminates + # at the first unrecognized option. + @ARGV = @$args; + $Getopt::Long::ignorecase = 0; + Getopt::Long::Configure( "bundling" ); + + # Process command-line flags + if (!GetOptions( \%opt, + qw(h|help V|Verbose v|version a|all))) { + return( usage() ); + } + + # Option -V for verbose output + if ( exists( $opt{V} )) { + $globalopt{verbose} = 1; + } + + if ( exists( $opt{a} )) { + $globalopt{a} = 1; + } + + return; +} + + +#-------------------------------------------------------------------------------- +=head3 preprocess_request + Parse the arguments and display the usage or the version string. + +=cut +#-------------------------------------------------------------------------------- +sub preprocess_request { + my $req = shift; + if ($req->{_xcatpreprocessed}->[0] == 1) { return [$req]; } + my $callback=shift; + + my $command = $req->{command}->[0]; + my $extrargs = $req->{arg}; + my @exargs=($req->{arg}); + if (ref($extrargs)) { + @exargs=@$extrargs; + } + + # Build hash to pass around + my %request; + $request{arg} = $extrargs; + $request{callback} = $callback; + $request{command} = $command; + + my $usage_string=xCAT::Usage->parseCommand($command, @exargs); + if ($usage_string) { + $callback->({data=>[$usage_string]}); + $req = {}; + return; + } + + my $result = parse_args( \%request ); + if ( ref($result) eq 'ARRAY' ) { + send_msg( \%request, 1, @$result ); + return(1); + } + + # add current request + my @result = (); + my $reqcopy = {%$req}; + $reqcopy->{_xcatpreprocessed}->[0] = 1; + push @result, $reqcopy; + + + # if on mn and with -a flag, go to the service node that has + # ntpserver=1. + if (xCAT::Utils->isMN() && exists($globalopt{a})) { + my @servicenodes = xCAT::ServiceNodeUtils->getSNList('ntpserver'); + send_msg(\%request, 0, "servicenodes=@servicenodes" ); + foreach my $sn (@servicenodes) { + my $reqcopy = {%$req}; + $reqcopy->{'_xcatdest'}=$sn; + $reqcopy->{_xcatpreprocessed}->[0] = 1; + push @result, $reqcopy; + } + } + + return \@result; +} + +#-------------------------------------------------------------------------------- +=head3 process_request + Pasrse the arguments and call the correspondent functions + to do switch discovery. + +=cut +#-------------------------------------------------------------------------------- +sub process_request { + my $req = shift; + my $callback = shift; + my $sub_req = shift; + + # Build hash to pass around + my %request; + $request{arg} = $req->{arg}; + $request{callback} = $callback; + $request{command} = $req->{command}->[0]; + + + my $ntpcfg = "/etc/ntp.conf"; + my $ntpcfgbackup = "/etc/ntp.conf.orig"; + my $ntpxcatcfgbackup = "/etc/ntp.conf.xcatbackup"; + if (!-e $ntpcfgbackup) { + # if original backup does not already exist + my $cmd = "mv $ntpcfg $ntpcfgbackup"; + my $result = xCAT::Utils->runcmd($cmd, 0); + if ($::RUNCMD_RC != 0) { + send_msg(\%request, 1, "Error from command:$cmd"); + return 1; + } + } + else { + # backup xcat cfg + my $cmd = "mv $ntpcfg $ntpxcatcfgbackup"; + my $result = xCAT::Utils->runcmd($cmd, 0); + system $cmd; + if ($::RUNCMD_RC != 0) { + send_msg(\%request, 1, "Error from command:$cmd"); + return 1; + } + } + + # get site.extntpservers for mn, for sn use mn as the server + my $ntp_servers; + if (xCAT::Utils->isMN()) { + my @entries = xCAT::TableUtils->get_site_attribute("extntpservers"); + $ntp_servers = $entries[0]; + } else { + my @nodeinfo = xCAT::NetworkUtils->determinehostname(); + my $nodename = pop @nodeinfo; + my $retdata = xCAT::ServiceNodeUtils->readSNInfo($nodename); + $ntp_servers = $retdata->{'master'}; + } + + # create ntp server config file + open(CFGFILE, ">$ntpcfg") + or xCAT::MsgUtils->message('SE', + "Cannot open $ntpcfg for NTP update. \n"); + #print CFGFILE "restrict default ignore\n"; + + if (defined($ntp_servers) && $ntp_servers) { + my @npt_server_array = split(',', $ntp_servers); + # add ntp servers one by one + foreach my $ntps (@npt_server_array) { + print CFGFILE "server "; + print CFGFILE "$ntps\n"; + } + } + print CFGFILE "driftfile /var/lib/ntp/drift\n"; + #add xCAT mn/sn itself as a server + print CFGFILE "restrict 127.0.0.1\n"; + close CFGFILE; + + #restart ntp + my $rc=xCAT::Utils->startservice("ntpd"); + if ($rc != 0) { + return 1; + } + return; +} + + +1; +