From 1914cef2dd21426881f0bad5dbe97d3ea142aea9 Mon Sep 17 00:00:00 2001 From: Saikrishna Arcot Date: Tue, 8 Oct 2024 19:06:45 -0700 Subject: [PATCH 1/2] Add options to configure new NTP servers When adding new NTP servers via the CLI, allow options such as iburst, version, and the association type to be specified. Signed-off-by: Saikrishna Arcot --- config/main.py | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/config/main.py b/config/main.py index bfa6dccadc..b5d7d0a210 100644 --- a/config/main.py +++ b/config/main.py @@ -7055,8 +7055,11 @@ def ntp(ctx): @ntp.command('add') @click.argument('ntp_ip_address', metavar='', required=True) +@click.option('--association-type', type=click.Choice(["server", "pool"], case_sensitive=False), default="server", help="Define the association type for this NTP server") +@click.option('--iburst', is_flag=True, help="Enable iburst for this NTP server") +@click.option('--version', type=int, help="Specify the version for this NTP server") @click.pass_context -def add_ntp_server(ctx, ntp_ip_address): +def add_ntp_server(ctx, ntp_ip_address, association_type, iburst, version): """ Add NTP server IP """ if ADHOC_VALIDATION: if not clicommon.is_ipaddress(ntp_ip_address): @@ -7067,10 +7070,15 @@ def add_ntp_server(ctx, ntp_ip_address): click.echo("NTP server {} is already configured".format(ntp_ip_address)) return else: + ntp_server_options = {} + if version: + ntp_server_options['version'] = version + if association_type != "server": + ntp_server_options['association_type'] = association_type + if iburst: + ntp_server_options['iburst'] = "on" try: - db.set_entry('NTP_SERVER', ntp_ip_address, - {'resolve_as': ntp_ip_address, - 'association_type': 'server'}) + db.set_entry('NTP_SERVER', ntp_ip_address, ntp_server_options) except ValueError as e: ctx.fail("Invalid ConfigDB. Error: {}".format(e)) click.echo("NTP server {} added to configuration".format(ntp_ip_address)) From 8a2705d1362a2954b283ecfb515669b712611542 Mon Sep 17 00:00:00 2001 From: Saikrishna Arcot Date: Tue, 8 Oct 2024 19:07:34 -0700 Subject: [PATCH 2/2] Replace ntp-config and ntpd with chrony Signed-off-by: Saikrishna Arcot --- config/main.py | 12 ++++++------ show/main.py | 21 +++++++-------------- tests/conftest.py | 1 - 3 files changed, 13 insertions(+), 21 deletions(-) diff --git a/config/main.py b/config/main.py index b5d7d0a210..14ad672259 100644 --- a/config/main.py +++ b/config/main.py @@ -7083,10 +7083,10 @@ def add_ntp_server(ctx, ntp_ip_address, association_type, iburst, version): ctx.fail("Invalid ConfigDB. Error: {}".format(e)) click.echo("NTP server {} added to configuration".format(ntp_ip_address)) try: - click.echo("Restarting ntp-config service...") - clicommon.run_command(['systemctl', 'restart', 'ntp-config'], display_cmd=False) + click.echo("Restarting chrony service...") + clicommon.run_command(['systemctl', 'restart', 'chrony'], display_cmd=False) except SystemExit as e: - ctx.fail("Restart service ntp-config failed with error {}".format(e)) + ctx.fail("Restart service chrony failed with error {}".format(e)) @ntp.command('del') @click.argument('ntp_ip_address', metavar='', required=True) @@ -7107,10 +7107,10 @@ def del_ntp_server(ctx, ntp_ip_address): else: ctx.fail("NTP server {} is not configured.".format(ntp_ip_address)) try: - click.echo("Restarting ntp-config service...") - clicommon.run_command(['systemctl', 'restart', 'ntp-config'], display_cmd=False) + click.echo("Restarting chrony service...") + clicommon.run_command(['systemctl', 'restart', 'chrony'], display_cmd=False) except SystemExit as e: - ctx.fail("Restart service ntp-config failed with error {}".format(e)) + ctx.fail("Restart service chrony failed with error {}".format(e)) # # 'sflow' group ('config sflow ...') diff --git a/show/main.py b/show/main.py index b7e75b24cf..c06398f459 100755 --- a/show/main.py +++ b/show/main.py @@ -1928,22 +1928,15 @@ def bgp(verbose): @click.option('--verbose', is_flag=True, help="Enable verbose output") def ntp(ctx, verbose): """Show NTP information""" - from pkg_resources import parse_version - ntpstat_cmd = ["ntpstat"] - ntpcmd = ["ntpq", "-p", "-n"] + chronyc_tracking_cmd = ["chronyc", "tracking"] + chronyc_sources_cmd = ["chronyc", "sources"] if is_mgmt_vrf_enabled(ctx) is True: - #ManagementVRF is enabled. Call ntpq using "ip vrf exec" or cgexec based on linux version - os_info = os.uname() - release = os_info[2].split('-') - if parse_version(release[0]) > parse_version("4.9.0"): - ntpstat_cmd = ['sudo', 'ip', 'vrf', 'exec', 'mgmt', 'ntpstat'] - ntpcmd = ['sudo', 'ip', 'vrf', 'exec', 'mgmt', 'ntpq', '-p', '-n'] - else: - ntpstat_cmd = ['sudo', 'cgexec', '-g', 'l3mdev:mgmt', 'ntpstat'] - ntpcmd = ['sudo', 'cgexec', '-g', 'l3mdev:mgmt', 'ntpq', '-p', '-n'] + #ManagementVRF is enabled. Call chronyc using "ip vrf exec" based on linux version + chronyc_tracking_cmd = ["sudo", "ip", "vrf", "exec", "mgmt"] + chronyc_tracking_cmd + chronyc_sources_cmd = ["sudo", "ip", "vrf", "exec", "mgmt"] + chronyc_sources_cmd - run_command(ntpstat_cmd, display_cmd=verbose) - run_command(ntpcmd, display_cmd=verbose) + run_command(chronyc_tracking_cmd, display_cmd=verbose) + run_command(chronyc_sources_cmd, display_cmd=verbose) # # 'uptime' command ("show uptime") diff --git a/tests/conftest.py b/tests/conftest.py index 3874668a67..41c9901e86 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -27,7 +27,6 @@ sys.path.insert(0, modules_path) generated_services_list = [ - 'ntp-config.service', 'warmboot-finalizer.service', 'watchdog-control.service', 'rsyslog-config.service',