Skip to content

Commit b36dd4e

Browse files
committed
Restart chrony/ntp service on interfaces-config restart
Signed-off-by: Anand Mehra (anamehra) <anamehra@cisco.com>
1 parent f8fa833 commit b36dd4e

File tree

2 files changed

+78
-6
lines changed

2 files changed

+78
-6
lines changed

src/usr/lib/ztp/ztp-engine.py

Lines changed: 42 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -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+
80101
class ZTPEngine():
81102
'''!
82103
\brief 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

src/usr/lib/ztp/ztp-profile.sh

Lines changed: 36 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,18 @@ PLATFORM=`sonic-cfggen -H -v DEVICE_METADATA.localhost.platform`
4141
PRESET=(`head -n 1 /usr/share/sonic/device/$PLATFORM/default_sku`)
4242
HW_KEY=${PRESET[0]}
4343

44+
# Function to detect which NTP service is available
45+
# Different SONiC releases use: ntp, ntpsec, or chrony
46+
get_ntp_service() {
47+
for service in chrony ntp; do
48+
if systemctl is-enabled $service >/dev/null 2>&1; then
49+
echo "$service"
50+
return 0
51+
fi
52+
done
53+
return 1
54+
}
55+
4456
# Command usage and help
4557
usage()
4658
{
@@ -221,9 +233,20 @@ if [ "$CMD" = "install" ] ; then
221233
# Restart interface configuration again to pickup newly created interfaces
222234
# to start DHCP discovery
223235
if [ "$(ztp status -c)" = "4:IN-PROGRESS" ]; then
224-
echo "Restarting network configuration."
225-
updateActivity "Restarting network configuration"
236+
NTP_SERVICE=$(get_ntp_service)
237+
if [ -n "$NTP_SERVICE" ]; then
238+
echo "Stopping $NTP_SERVICE."
239+
updateActivity "Stopping $NTP_SERVICE"
240+
systemctl stop $NTP_SERVICE
241+
fi
242+
echo "Restarting interfaces-config."
243+
updateActivity "Restarting interfaces-config"
244+
226245
systemctl restart interfaces-config
246+
if [ -n "$NTP_SERVICE" ]; then
247+
systemctl start $NTP_SERVICE
248+
echo "Restarted $NTP_SERVICE."
249+
fi
227250
echo "Restarted network configuration."
228251
fi
229252
fi
@@ -259,9 +282,18 @@ if [ "$CMD" = "remove" ] ; then
259282
sonic-db-cli CONFIG_DB DEL "ZTP|mode" > /dev/null
260283
fi
261284

262-
updateActivity "Restarting network configuration"
263-
# Restart interface configuration to stop DHCP
285+
NTP_SERVICE=$(get_ntp_service)
286+
if [ -n "$NTP_SERVICE" ]; then
287+
updateActivity "Restarting network configuration and $NTP_SERVICE"
288+
# Restart interface configuration to stop DHCP
289+
systemctl stop $NTP_SERVICE
290+
else
291+
updateActivity "Restarting network configuration"
292+
fi
264293
systemctl restart interfaces-config
294+
if [ -n "$NTP_SERVICE" ]; then
295+
systemctl start $NTP_SERVICE
296+
fi
265297
fi
266298

267299
# Remove ZTP DHCP policy

0 commit comments

Comments
 (0)