From 9d5acf5e6bb120a7449b164325b250609d5f2b71 Mon Sep 17 00:00:00 2001 From: Bing Sun Date: Thu, 31 Aug 2023 21:03:24 -0700 Subject: [PATCH 1/5] modify click ntp add command to include minpoll&maxpoll --- config/main.py | 79 +++++++++++++++++++++++++++++++--------- doc/Command-Reference.md | 18 +++++---- show/main.py | 7 +++- 3 files changed, 77 insertions(+), 27 deletions(-) diff --git a/config/main.py b/config/main.py index 5f7e41a36f..05a6b5a3d3 100644 --- a/config/main.py +++ b/config/main.py @@ -6536,28 +6536,71 @@ def ntp(ctx): @ntp.command('add') @click.argument('ntp_ip_address', metavar='', required=True) +@click.argument('minpoll_maxpoll', metavar='[minpoll maxpoll ]', required=False, type=click.Path()) @click.pass_context -def add_ntp_server(ctx, ntp_ip_address): +def add_ntp_server(ctx, ntp_ip_address, minpoll_maxpoll): """ Add NTP server IP """ + DEFAULT_MINPOLL = 6 + DEFAULT_MAXPOLL = 10 + MIN_POLL = 3 + MAX_POLL = 17 + + new_minpoll = DEFAULT_MINPOLL + new_maxpoll = DEFAULT_MAXPOLL + if ADHOC_VALIDATION: - if not clicommon.is_ipaddress(ntp_ip_address): + if not clicommon.is_ipaddress(ntp_ip_address): ctx.fail('Invalid IP address') - db = ValidatedConfigDBConnector(ctx.obj['db']) - ntp_servers = db.get_table("NTP_SERVER") - if ntp_ip_address in ntp_servers: - click.echo("NTP server {} is already configured".format(ntp_ip_address)) - return - else: - try: - db.set_entry('NTP_SERVER', ntp_ip_address, {'NULL': 'NULL'}) - except ValueError as e: - 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) - except SystemExit as e: - ctx.fail("Restart service ntp-config failed with error {}".format(e)) + + if len(minpoll_maxpoll) != 0 and len(minpoll_maxpoll) != 4: + ctx.fail('Invalid input for minpoll and maxpoll') + + if len(minpoll_maxpoll) != 0: + new_minpoll = int(minpoll_maxpoll[1]) + new_maxpoll = int(minpoll_maxpoll[3]) + + if new_minpoll not in range(MIN_POLL, MAX_POLL + 1) or new_maxpoll not in range(MIN_POLL, MAX_POLL + 1): + ctx.fail('minpoll and maxpoll must be in the range 3-17') + + db = ValidatedConfigDBConnector(ctx.obj['db']) + ntp_server_entry = db.get_entry('NTP_SERVER', ntp_ip_address) + + # init the curr_minpoll & curr_maxpoll with default values + curr_minpoll = DEFAULT_MINPOLL + curr_maxpoll = DEFAULT_MAXPOLL + + if ntp_server_entry: + if ntp_server_entry.get('minpoll'): + curr_minpoll = int(ntp_server_entry.get('minpoll')) + if ntp_server_entry and ntp_server_entry.get('maxpoll'): + curr_maxpoll = int(ntp_server_entry.get('maxpoll')) + + if (curr_minpoll == minpoll) and (curr_maxpoll == maxpoll): + click.echo("NTP server {} is already configured".format(ntp_ip_address)) + return + # if create or update + if new_minpoll >= new_maxpoll: + ctx.fail("Invalid minpoll:{} and maxpoll:{}".format(curr_minpoll, curr_maxpoll)) + + serverInfo = { + "minpoll": new_minpoll, + "maxpoll": new_maxpoll + } + try: + if ntp_server_entry: + db.mod_entry('NTP_SERVER', ntp_ip_address, serverInfo) + else: + db.set_entry('NTP_SERVER', ntp_ip_address, serverInfo) + + except ValueError as e: + ctx.fail("Invalid ConfigDB. Error: {}".format(e)) + click.echo("NTP server {} minpoll {} maxpoll {} added to configuration".format(ntp_ip_address, new_minpoll, new_maxpoll)) + try: + click.echo("Restarting ntp-config service...") + clicommon.run_command(['systemctl', 'restart', 'ntp-config'], display_cmd=False) + except SystemExit as e: + ctx.fail("Restart service ntp-config failed with error {}".format(e)) + @ntp.command('del') @click.argument('ntp_ip_address', metavar='', required=True) diff --git a/doc/Command-Reference.md b/doc/Command-Reference.md index 429fe39bbf..bda1aef07e 100644 --- a/doc/Command-Reference.md +++ b/doc/Command-Reference.md @@ -7458,19 +7458,24 @@ This sub-section of commands is used to add or remove the configured NTP servers **config ntp add** This command is used to add a NTP server IP address to the NTP server list. Note that more that one NTP server IP address can be added in the device. - +User may optionally configure minpoll and maxpoll for each NTP server. Minpoll and maxpoll is in the range from 3-17. When not configured, minpoll has default value "6" and maxpoll has default value "10" - Usage: ``` - config ntp add + config ntp add [minpoll] [maxpoll] ``` - Example: ``` admin@sonic:~$ sudo config ntp add 9.9.9.9 - NTP server 9.9.9.9 added to configuration + NTP server 9.9.9.9 {'minpoll': 6, 'maxpoll': 10} added to configuration Restarting ntp-config service... ``` + ``` + admin@sonic:~# config ntp add 8.8.8.8 12 16 + NTP server 8.8.8.8 {'minpoll': 12, 'maxpoll': 16} added to configuration + Restarting ntp-config service... + **config ntp delete** This command is used to delete a configured NTP server IP address. @@ -9600,11 +9605,10 @@ This command displays the running configuration of the ntp module. - Example: ``` - admin@sonic:~$ show runningconfiguration ntp NTP Servers - ------------- - 1.1.1.1 - 2.2.2.2 + ----------------------------- + 8.8.8.8 minpoll 12 maxpoll 16 + 9.9.9.9 minpoll 6 maxpoll 10 ``` **show runningconfiguration syslog** diff --git a/show/main.py b/show/main.py index 35303b6eda..2700dbeb30 100755 --- a/show/main.py +++ b/show/main.py @@ -1511,8 +1511,11 @@ def ntp(verbose): data = ntp_file.readlines() for line in data: if line.startswith("server "): - ntp_server = line.split(" ")[1] - ntp_servers.append(ntp_server) + info = line.split(" ") + ntp_server = info[1] + minpoll = info[4] + maxpoll = info[6] + ntp_servers.append(ntp_server + " minpoll " + minpoll + " maxpoll " + maxpoll) ntp_dict['NTP Servers'] = ntp_servers print(tabulate(ntp_dict, headers=list(ntp_dict.keys()), tablefmt="simple", stralign='left', missingval="")) From 81202b467b128ac6033005fb906f5a229fe029d8 Mon Sep 17 00:00:00 2001 From: Bing Sun Date: Thu, 31 Aug 2023 21:37:14 -0700 Subject: [PATCH 2/5] fix indentation etc --- config/main.py | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/config/main.py b/config/main.py index 05a6b5a3d3..361069f9a1 100644 --- a/config/main.py +++ b/config/main.py @@ -6536,7 +6536,7 @@ def ntp(ctx): @ntp.command('add') @click.argument('ntp_ip_address', metavar='', required=True) -@click.argument('minpoll_maxpoll', metavar='[minpoll maxpoll ]', required=False, type=click.Path()) +@click.argument('minpoll_maxpoll', metavar='[minpoll maxpoll ]', required=False, nargs=-1, type=click.Path()) @click.pass_context def add_ntp_server(ctx, ntp_ip_address, minpoll_maxpoll): """ Add NTP server IP """ @@ -6555,8 +6555,11 @@ def add_ntp_server(ctx, ntp_ip_address, minpoll_maxpoll): if len(minpoll_maxpoll) != 0 and len(minpoll_maxpoll) != 4: ctx.fail('Invalid input for minpoll and maxpoll') + if minpoll_maxpoll[0] != 'minpoll' and minpoll_maxpoll[2] != 'maxpoll': + ctx.fail('Invalid parameters') + if len(minpoll_maxpoll) != 0: - new_minpoll = int(minpoll_maxpoll[1]) + new_minpoll = int(minpoll_maxpoll[1]) new_maxpoll = int(minpoll_maxpoll[3]) if new_minpoll not in range(MIN_POLL, MAX_POLL + 1) or new_maxpoll not in range(MIN_POLL, MAX_POLL + 1): @@ -6575,7 +6578,7 @@ def add_ntp_server(ctx, ntp_ip_address, minpoll_maxpoll): if ntp_server_entry and ntp_server_entry.get('maxpoll'): curr_maxpoll = int(ntp_server_entry.get('maxpoll')) - if (curr_minpoll == minpoll) and (curr_maxpoll == maxpoll): + if (curr_minpoll == new_minpoll) and (curr_maxpoll == new_maxpoll): click.echo("NTP server {} is already configured".format(ntp_ip_address)) return # if create or update @@ -6598,7 +6601,7 @@ def add_ntp_server(ctx, ntp_ip_address, minpoll_maxpoll): try: click.echo("Restarting ntp-config service...") clicommon.run_command(['systemctl', 'restart', 'ntp-config'], display_cmd=False) - except SystemExit as e: + except SystemExit as e: ctx.fail("Restart service ntp-config failed with error {}".format(e)) From 4d4df8920382b11950fd71cd97255c0f3b3ebb16 Mon Sep 17 00:00:00 2001 From: Bing Sun Date: Thu, 31 Aug 2023 21:40:19 -0700 Subject: [PATCH 3/5] push again --- config/main.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/config/main.py b/config/main.py index 361069f9a1..e598c4cda2 100644 --- a/config/main.py +++ b/config/main.py @@ -6556,7 +6556,7 @@ def add_ntp_server(ctx, ntp_ip_address, minpoll_maxpoll): ctx.fail('Invalid input for minpoll and maxpoll') if minpoll_maxpoll[0] != 'minpoll' and minpoll_maxpoll[2] != 'maxpoll': - ctx.fail('Invalid parameters') + ctx.fail('Invalid minpoll or maxpoll parameters') if len(minpoll_maxpoll) != 0: new_minpoll = int(minpoll_maxpoll[1]) From eed051883e4864651a0a9ba5e3f110be86d75f78 Mon Sep 17 00:00:00 2001 From: Bing Sun Date: Thu, 31 Aug 2023 21:49:30 -0700 Subject: [PATCH 4/5] force push --- config/main.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/config/main.py b/config/main.py index e598c4cda2..95a227191a 100644 --- a/config/main.py +++ b/config/main.py @@ -6553,7 +6553,7 @@ def add_ntp_server(ctx, ntp_ip_address, minpoll_maxpoll): ctx.fail('Invalid IP address') if len(minpoll_maxpoll) != 0 and len(minpoll_maxpoll) != 4: - ctx.fail('Invalid input for minpoll and maxpoll') + ctx.fail('Invalid minpoll and maxpoll parameters') if minpoll_maxpoll[0] != 'minpoll' and minpoll_maxpoll[2] != 'maxpoll': ctx.fail('Invalid minpoll or maxpoll parameters') From 8b3b11ffd5ff60648e3d759be37b0208b496c83f Mon Sep 17 00:00:00 2001 From: bsun-sudo Date: Fri, 1 Sep 2023 22:24:36 -0700 Subject: [PATCH 5/5] comment --- config/main.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/config/main.py b/config/main.py index 95a227191a..2d3d65f29b 100644 --- a/config/main.py +++ b/config/main.py @@ -6556,7 +6556,7 @@ def add_ntp_server(ctx, ntp_ip_address, minpoll_maxpoll): ctx.fail('Invalid minpoll and maxpoll parameters') if minpoll_maxpoll[0] != 'minpoll' and minpoll_maxpoll[2] != 'maxpoll': - ctx.fail('Invalid minpoll or maxpoll parameters') + ctx.fail('Invalid minpoll or maxpoll parameter') if len(minpoll_maxpoll) != 0: new_minpoll = int(minpoll_maxpoll[1])