@@ -77,6 +77,27 @@ def signal_handler(signum, frame):
7777
7878 sys .exit (0 )
7979
80+ def get_ntp_service_name ():
81+ '''!
82+ Detect which NTP service is available on the system.
83+ Checks in priority order: chrony, ntp
84+
85+ Returns:
86+ str: Name of the available NTP service, or None if none found
87+ '''
88+ ntp_services = ['chrony' , 'ntp' ]
89+ for service in ntp_services :
90+ try :
91+ rc = runCommand ('systemctl is-enabled ' + service , capture_stdout = True )
92+ if rc == 0 :
93+ logger .info ('Detected NTP service: %s' % service )
94+ return service
95+ except :
96+ continue
97+
98+ logger .warning ('No NTP service detected (ntp/ntpsec/chrony)' )
99+ return None
100+
80101class ZTPEngine ():
81102 '''!
82103 \b rief This class performs core functions of ZTP service.
@@ -795,6 +816,25 @@ def __forceRestartDiscovery(self, msg):
795816 # Restart link-scan
796817 self .__intf_state = dict ()
797818
819+ def __restart_network_services (self ):
820+ '''!
821+ Restart the necessary services: stop and start NTP, and restart interfaces-config.
822+ Detects which NTP service is available (ntp, ntpsec, or chrony).
823+ '''
824+ ntp_service = get_ntp_service_name ()
825+
826+ if ntp_service :
827+ logger .info ('Stopping %s service...' % ntp_service )
828+ runCommand ('systemctl stop ' + ntp_service , capture_stdout = False )
829+
830+ logger .info ('Restarting interfaces-config service...' )
831+ runCommand ('systemctl restart interfaces-config' , capture_stdout = False )
832+
833+ if ntp_service :
834+ logger .info ('Starting %s service...' % ntp_service )
835+ runCommand ('systemctl start ' + ntp_service , capture_stdout = False )
836+ logger .info ('%s service restarted successfully.' % ntp_service )
837+
798838 def executeLoop (self , test_mode = False ):
799839 '''!
800840 ZTP service loop which peforms provisioning data discovery and initiates processing.
@@ -860,7 +900,7 @@ def executeLoop(self, test_mode=False):
860900 if self .__link_scan ():
861901 updateActivity ('Restarting network discovery after link scan' )
862902 logger .info ('Restarting network discovery after link scan.' )
863- runCommand ( 'systemctl restart interfaces-config' , capture_stdout = False )
903+ self . __restart_network_services ( )
864904 logger .info ('Restarted network discovery after link scan.' )
865905 _start_time = time .time ()
866906 continue
@@ -876,7 +916,7 @@ def executeLoop(self, test_mode=False):
876916 # Remove existing leases to source new provisioning data
877917 self .__cleanup_dhcp_leases ()
878918 logger .info ('Restarting network discovery.' )
879- runCommand ( 'systemctl restart interfaces-config' , capture_stdout = False )
919+ self . __restart_network_services ( )
880920 logger .info ('Restarted network discovery.' )
881921 _start_time = time .time ()
882922 continue
0 commit comments