mirror of
				https://github.com/xcat2/xcat-core.git
				synced 2025-11-04 05:12:30 +00:00 
			
		
		
		
	added more code for ntp
This commit is contained in:
		@@ -59,7 +59,7 @@ sub send_msg {
 | 
			
		||||
    elsif ( exists( $request->{callback} )) {
 | 
			
		||||
        my $callback = $request->{callback};
 | 
			
		||||
        $output{errorcode} = $ecode;
 | 
			
		||||
        $output{data} = $msg;
 | 
			
		||||
        $output{data}->[0] = $msg;
 | 
			
		||||
        $callback->( \%output );
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
@@ -175,21 +175,13 @@ sub preprocess_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;
 | 
			
		||||
        }
 | 
			
		||||
        $reqcopy->{_all}->[0] = 1;
 | 
			
		||||
    }
 | 
			
		||||
    if (exists($globalopt{verbose})) {
 | 
			
		||||
        $reqcopy->{_verbose}->[0] = 1;
 | 
			
		||||
    }
 | 
			
		||||
    push @result, $reqcopy;
 | 
			
		||||
 | 
			
		||||
    return \@result;
 | 
			
		||||
}
 | 
			
		||||
@@ -212,16 +204,33 @@ sub process_request {
 | 
			
		||||
    $request{callback} = $callback;
 | 
			
		||||
    $request{command}  = $req->{command}->[0];
 | 
			
		||||
 | 
			
		||||
    my $verbose;
 | 
			
		||||
    if ($req->{_verbose}->[0] == 1) {
 | 
			
		||||
	$verbose = 1;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    my @nodeinfo   = xCAT::NetworkUtils->determinehostname();
 | 
			
		||||
    my $nodename   = pop @nodeinfo; 
 | 
			
		||||
 | 
			
		||||
    if (xCAT::Utils->isMN()) {
 | 
			
		||||
        send_msg(\%request, 0, "configuring management node: $nodename." );
 | 
			
		||||
    } else {
 | 
			
		||||
        send_msg(\%request, 0, "configuring service node: $nodename." );
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    #check if ntp is installed or not
 | 
			
		||||
    if ($verbose) {
 | 
			
		||||
        send_msg(\%request, 0, " ...checking if nptd is installed." );
 | 
			
		||||
    }
 | 
			
		||||
    if (!-f "/usr/sbin/ntpd") {
 | 
			
		||||
        send_msg(\%request, 1, "Please make sure ntpd is installed on $nodename.");
 | 
			
		||||
        return 1;
 | 
			
		||||
    }
 | 
			
		||||
    
 | 
			
		||||
        
 | 
			
		||||
    #configure the ntp configuration file
 | 
			
		||||
    if ($verbose) {
 | 
			
		||||
        send_msg(\%request, 0, " ...backing up the ntp configuration file /etc/ntp.conf." );
 | 
			
		||||
    }
 | 
			
		||||
    my $ntpcfg = "/etc/ntp.conf";
 | 
			
		||||
    my $ntpcfgbackup     = "/etc/ntp.conf.orig";
 | 
			
		||||
    my $ntpxcatcfgbackup = "/etc/ntp.conf.xcatbackup";
 | 
			
		||||
@@ -231,17 +240,16 @@ sub process_request {
 | 
			
		||||
            my $cmd = "mv $ntpcfg $ntpcfgbackup";
 | 
			
		||||
            my $result = xCAT::Utils->runcmd($cmd, 0);
 | 
			
		||||
            if ($::RUNCMD_RC != 0) {
 | 
			
		||||
                send_msg(\%request, 1, "Error from command:$cmd");
 | 
			
		||||
                send_msg(\%request, 1, "Error from command:$cmd\n    $result");
 | 
			
		||||
                return 1;
 | 
			
		||||
            }
 | 
			
		||||
        }
 | 
			
		||||
        else {    
 | 
			
		||||
            # backup xcat cfg
 | 
			
		||||
            my $cmd = "mv $ntpcfg $ntpxcatcfgbackup";
 | 
			
		||||
            my $cmd = "rm $ntpxcatcfgbackup;mv $ntpcfg $ntpxcatcfgbackup";
 | 
			
		||||
            my $result = xCAT::Utils->runcmd($cmd, 0);
 | 
			
		||||
            system $cmd;
 | 
			
		||||
            if ($::RUNCMD_RC != 0) {
 | 
			
		||||
                send_msg(\%request, 1, "Error from command:$cmd");
 | 
			
		||||
                send_msg(\%request, 1, "Error from command:$cmd\n    $result.");
 | 
			
		||||
                return 1;
 | 
			
		||||
            }
 | 
			
		||||
        }
 | 
			
		||||
@@ -249,23 +257,35 @@ sub process_request {
 | 
			
		||||
 | 
			
		||||
    # get site.extntpservers for mn, for sn use mn as the server
 | 
			
		||||
    my $ntp_servers;
 | 
			
		||||
    my $ntp_master;
 | 
			
		||||
    my $ntp_attrib;
 | 
			
		||||
    if (xCAT::Utils->isMN()) {
 | 
			
		||||
        my @entries = xCAT::TableUtils->get_site_attribute("extntpservers");
 | 
			
		||||
        $ntp_servers = $entries[0];
 | 
			
		||||
        $ntp_attrib = "extntpservers";
 | 
			
		||||
    } else {
 | 
			
		||||
        $ntp_attrib = "ntpservers";
 | 
			
		||||
    }
 | 
			
		||||
    my @entries = xCAT::TableUtils->get_site_attribute($ntp_attrib);
 | 
			
		||||
    my $ntp_servers = $entries[0];
 | 
			
		||||
 | 
			
		||||
    if (!xCAT::Utils->isMN() && ((!$ntp_servers) || (($ntp_servers) && ($ntp_servers =~ /<xcatmaster>/)))) {
 | 
			
		||||
        my $retdata = xCAT::ServiceNodeUtils->readSNInfo($nodename);
 | 
			
		||||
        $ntp_servers = $retdata->{'master'};
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    if ($verbose) {
 | 
			
		||||
        send_msg(\%request, 0, " ...changing the ntpp configuration file /etc/ntp.conf.\n    ntp servers are: $ntp_servers" );
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    # create ntp server config file
 | 
			
		||||
    open(CFGFILE, ">$ntpcfg")
 | 
			
		||||
        or xCAT::MsgUtils->message('SE',
 | 
			
		||||
                                   "Cannot open $ntpcfg for NTP update. \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) {
 | 
			
		||||
            if (!$ntp_master) { $ntp_master = $ntps; }
 | 
			
		||||
            print CFGFILE "server ";
 | 
			
		||||
            print CFGFILE "$ntps\n";
 | 
			
		||||
        }
 | 
			
		||||
@@ -273,15 +293,88 @@ sub process_request {
 | 
			
		||||
    #add xCAT mn/sn itself as a server
 | 
			
		||||
    print CFGFILE "server 127.127.1.0\n";
 | 
			
		||||
    print CFGFILE "fudge 127.127.1.0 stratum 10\n";
 | 
			
		||||
 
 | 
			
		||||
    
 | 
			
		||||
    print CFGFILE "driftfile /var/lib/ntp/drift\n";
 | 
			
		||||
    close CFGFILE;
 | 
			
		||||
 | 
			
		||||
    #restart ntp
 | 
			
		||||
    my $rc=xCAT::Utils->startservice("ntpd");
 | 
			
		||||
    
 | 
			
		||||
    my $os = xCAT::Utils->osver("all");
 | 
			
		||||
    my $ntp_service = "ntpd";
 | 
			
		||||
    if ($os =~ /sles/) {
 | 
			
		||||
        $ntp_service = "ntp";
 | 
			
		||||
    }
 | 
			
		||||
    #stop ntpd
 | 
			
		||||
    if ($verbose) {
 | 
			
		||||
        send_msg(\%request, 0, " ...stopping $ntp_service" );
 | 
			
		||||
    }
 | 
			
		||||
    my $rc=xCAT::Utils->stopservice($ntp_service);
 | 
			
		||||
    if ($rc != 0) {
 | 
			
		||||
        send_msg(\%request, 1, "Failed to stop nptd on $nodename.");
 | 
			
		||||
        return 1;
 | 
			
		||||
    }
 | 
			
		||||
    
 | 
			
		||||
    #update the time now
 | 
			
		||||
    if ($ntp_master) {
 | 
			
		||||
        my $cmd;
 | 
			
		||||
        if ($os =~ /sles/) {
 | 
			
		||||
            $cmd = "sntp -P no -r $ntp_master";
 | 
			
		||||
        } else {
 | 
			
		||||
            $cmd = "ntpdate -t5 $ntp_master";
 | 
			
		||||
        }
 | 
			
		||||
        if ($verbose) {
 | 
			
		||||
            send_msg(\%request, 0, " ...updating the time now. $cmd");
 | 
			
		||||
        }
 | 
			
		||||
        my $result = xCAT::Utils->runcmd($cmd, 0);
 | 
			
		||||
        if ($verbose) {
 | 
			
		||||
            send_msg(\%request, 0, "    $result");
 | 
			
		||||
        }
 | 
			
		||||
        if ($::RUNCMD_RC != 0) {
 | 
			
		||||
            send_msg(\%request, 1, "Error from command $cmd\n    $result.");
 | 
			
		||||
            return 1;
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
    
 | 
			
		||||
    #start ntpd
 | 
			
		||||
    if ($verbose) {
 | 
			
		||||
        send_msg(\%request, 0, " ...starting $ntp_service" );
 | 
			
		||||
    }
 | 
			
		||||
    my $rc=xCAT::Utils->startservice($ntp_service);
 | 
			
		||||
    if ($rc != 0) {
 | 
			
		||||
        send_msg(\%request, 1, "Failed to start nptd on $nodename.");
 | 
			
		||||
        return 1;
 | 
			
		||||
    }
 | 
			
		||||
    
 | 
			
		||||
    #enable ntpd for node reboot
 | 
			
		||||
    if ($verbose) {
 | 
			
		||||
        send_msg(\%request, 0, " ...enabling $ntp_service" );
 | 
			
		||||
    }
 | 
			
		||||
    xCAT::Utils->enableservice($ntp_service);
 | 
			
		||||
 | 
			
		||||
    #now handle sn that has ntpserver=1 set in servicenode table.
 | 
			
		||||
    # this part is called by makentp -a.
 | 
			
		||||
    if ($req->{_all}->[0] == 1) { 
 | 
			
		||||
        my @servicenodes = xCAT::ServiceNodeUtils->getSNList('ntpserver'); 
 | 
			
		||||
        if (@servicenodes > 0) {
 | 
			
		||||
            send_msg(\%request, 0, "configuring servicenodes: @servicenodes" );
 | 
			
		||||
            my $ret =
 | 
			
		||||
                xCAT::Utils->runxcmd(
 | 
			
		||||
                    {
 | 
			
		||||
                        command => ['updatenode'],
 | 
			
		||||
                        node    => \@servicenodes,
 | 
			
		||||
                        arg     => ["-P", "setupntp"],
 | 
			
		||||
                    },
 | 
			
		||||
                    $sub_req, -1, 1
 | 
			
		||||
                );
 | 
			
		||||
            if ($verbose) {
 | 
			
		||||
                send_msg(\%request, 0, " ...updatnode returns the following result: $ret" );
 | 
			
		||||
            }
 | 
			
		||||
            if ($::RUNCMD_RC != 0) {
 | 
			
		||||
                send_msg(\%request, 1, "Error in updatenode: $ret" );
 | 
			
		||||
 | 
			
		||||
            }
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
    return;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
 
 | 
			
		||||
@@ -13,17 +13,16 @@ if [ "$(uname -s|tr 'A-Z' 'a-z')" = "linux" ];then
 | 
			
		||||
   . $str_dir_name/xcatlib.sh
 | 
			
		||||
fi
 | 
			
		||||
 | 
			
		||||
# if on the Management Node, exit
 | 
			
		||||
# if on the Management Node
 | 
			
		||||
if [ -e /etc/xCATMN ]; then
 | 
			
		||||
   logger -t xcat -p local4.info "setupntp:Running on the Management Node ,  exiting "
 | 
			
		||||
   exit 0
 | 
			
		||||
fi
 | 
			
		||||
# if on the Server Node, exit,  makentp will set up service node
 | 
			
		||||
if [ -e /etc/xCATSN ]; then
 | 
			
		||||
   logger -t xcat -p local4.info "setupntp:Running on the Server Node ,  exiting "
 | 
			
		||||
   logger -t xcat -p local4.info "setupntp: This postscript does not support running on the management node. Please run makentp command. exiting"
 | 
			
		||||
   exit 0
 | 
			
		||||
fi
 | 
			
		||||
 | 
			
		||||
#for service node, the makentp -a command will call this postscript
 | 
			
		||||
#so do not diable service node.
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
master=$MASTER
 | 
			
		||||
setup=0
 | 
			
		||||
sitemaster=$SITEMASTER
 | 
			
		||||
@@ -32,7 +31,6 @@ conf_file_org="/etc/ntp.conf.org"
 | 
			
		||||
conf_file_backup="/etc/ntp.conf.postbackup"
 | 
			
		||||
 | 
			
		||||
# pmatch determines if 1st argument string is matched by 2nd argument pattern
 | 
			
		||||
 | 
			
		||||
pmatch ()
 | 
			
		||||
{
 | 
			
		||||
  case $1 in
 | 
			
		||||
@@ -70,35 +68,10 @@ is_lsb_ubuntu ()
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
logger -t xcat "Install: Setup NTP"
 | 
			
		||||
# if master is the sitemaster, then use the ntpservers defined in the site
 | 
			
		||||
# table, if they exist. If they don't exist, do not setup ntp
 | 
			
		||||
# else use the master which should be a service node
 | 
			
		||||
if [ "$master" =  "$sitemaster" ]; then
 | 
			
		||||
  if [ $NTPSERVERS ]; then
 | 
			
		||||
        if [ "$NODESETSTATE" = "statelite" ]; then
 | 
			
		||||
            cp -a $conf_file $conf_file_org
 | 
			
		||||
            echo  "" > $conf_file
 | 
			
		||||
        else
 | 
			
		||||
            if [ !  -f $conf_file_org ]; then
 | 
			
		||||
                mv -f $conf_file $conf_file_org
 | 
			
		||||
            else
 | 
			
		||||
                mv -f $conf_file $conf_file_backup
 | 
			
		||||
            fi
 | 
			
		||||
        fi
 | 
			
		||||
        if [ "$NTPSERVERS" = "<xcatmaster>" ] || [ "$NTPSERVERS" = "<xcatmaster>" ]; then
 | 
			
		||||
            echo "server $master" >>$conf_file
 | 
			
		||||
        else
 | 
			
		||||
        	for i in $(echo $NTPSERVERS | tr ',' ' ')
 | 
			
		||||
        	do
 | 
			
		||||
         	   echo "server $i" >>$conf_file
 | 
			
		||||
        	   master=$i
 | 
			
		||||
        	done 
 | 
			
		||||
        fi
 | 
			
		||||
  else
 | 
			
		||||
	echo "server $master" >$conf_file
 | 
			
		||||
 | 
			
		||||
  fi
 | 
			
		||||
else
 | 
			
		||||
# Use the ntpservers defined in the site table, if they exist. 
 | 
			
		||||
# If the value of ntpservers is <xcatmaster> use the service node or
 | 
			
		||||
# the management node as the ntp server.
 | 
			
		||||
if [ $NTPSERVERS ]; then
 | 
			
		||||
    if [ "$NODESETSTATE" = "statelite" ]; then
 | 
			
		||||
        cp -a $conf_file $conf_file_org
 | 
			
		||||
        echo  "" > $conf_file
 | 
			
		||||
@@ -109,21 +82,17 @@ else
 | 
			
		||||
            mv -f $conf_file $conf_file_backup
 | 
			
		||||
        fi
 | 
			
		||||
    fi
 | 
			
		||||
 | 
			
		||||
    if [ $NTPSERVERS ]; then
 | 
			
		||||
        if [ "$NTPSERVERS" = "<xcatmaster>" ] || [ "$NTPSERVERS" = "<xcatmaster>" ]; then
 | 
			
		||||
            echo "server $master" >>$conf_file
 | 
			
		||||
        else
 | 
			
		||||
        	for i in $(echo $NTPSERVERS | tr ',' ' ')
 | 
			
		||||
        	do
 | 
			
		||||
         	   echo "server $i" >>$conf_file
 | 
			
		||||
        	   master=$i
 | 
			
		||||
        	done 
 | 
			
		||||
        fi
 | 
			
		||||
    if [ "$NTPSERVERS" = "<xcatmaster>" ] || [ "$NTPSERVERS" = "<xcatmaster>" ]; then
 | 
			
		||||
        echo "server $master" >>$conf_file
 | 
			
		||||
    else
 | 
			
		||||
        echo "server $master" >$conf_file
 | 
			
		||||
        for i in $(echo $NTPSERVERS | tr ',' ' ')
 | 
			
		||||
        do
 | 
			
		||||
         	echo "server $i" >>$conf_file
 | 
			
		||||
        	master=$i
 | 
			
		||||
        done 
 | 
			
		||||
    fi
 | 
			
		||||
 | 
			
		||||
else
 | 
			
		||||
	echo "server $master" >$conf_file
 | 
			
		||||
fi
 | 
			
		||||
 | 
			
		||||
OS_TYPE=`uname`
 | 
			
		||||
@@ -133,17 +102,10 @@ if [ $OS_TYPE = Linux ]; then
 | 
			
		||||
    echo "driftfile /var/lib/ntp/drift
 | 
			
		||||
disable auth
 | 
			
		||||
restrict 127.0.0.1" >>$conf_file
 | 
			
		||||
    # default service for redhat/fedora
 | 
			
		||||
    #SERVICE=ntpd
 | 
			
		||||
    #if ( pmatch $OSVER "sles*" ) || ( pmatch $OSVER "suse*" ) || [ -f /etc/SuSE-release ] || ( pmatch $OSVER "ubuntu*" ) || ( is_lsb_ubuntu ); then
 | 
			
		||||
    #    SERVICE=ntp
 | 
			
		||||
    #fi
 | 
			
		||||
 | 
			
		||||
    #ntpdate/sntp conflict with ntpd, stop the service first
 | 
			
		||||
#    service $SERVICE status
 | 
			
		||||
    checkservicestatus ntpserver
 | 
			
		||||
    if [ $? -eq 0 ];then
 | 
			
		||||
#        service $SERVICE stop
 | 
			
		||||
         stopservice ntpserver
 | 
			
		||||
    fi
 | 
			
		||||
    #ntpdate program is deprecated on SuSE
 | 
			
		||||
@@ -202,21 +164,8 @@ restrict 127.0.0.1" >>$conf_file
 | 
			
		||||
            restartservice cron
 | 
			
		||||
        fi
 | 
			
		||||
    fi
 | 
			
		||||
    #service $SERVICE start 
 | 
			
		||||
    startservice ntpserver
 | 
			
		||||
 | 
			
		||||
    #start ntp and crontab automaticlly
 | 
			
		||||
    #if [ -f "/etc/debian_version" ];then
 | 
			
		||||
    #    update-rc.d cron defaults
 | 
			
		||||
    #    update-rc.d $SERVICE defaults
 | 
			
		||||
    #else
 | 
			
		||||
    #    chkconfig --add  $SERVICE >/dev/null 2>&1
 | 
			
		||||
    #    chkconfig --level 345 $SERVICE on >/dev/null 2>&1
 | 
			
		||||
    #    if ( pmatch $OSVER "sles*" ) || ( pmatch $OSVER "suse*" ) || [ -f /etc/SuSE-release ];then
 | 
			
		||||
    #        chkconfig --add cron >/dev/null 2>&1
 | 
			
		||||
    #        chkconfig --level 345 cron on >/dev/null 2>&1
 | 
			
		||||
    #    fi
 | 
			
		||||
    #fi
 | 
			
		||||
    enableservice cron
 | 
			
		||||
    enableservice ntpserver
 | 
			
		||||
else
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user