Skip to content

Commit 14c483f

Browse files
authored
[CLI] Move hostname, mgmt interface/vrf config to hostcfgd (#2173)
- Why I did it To be able to configure the management interface and hostname standalone by changing database config at runtime. From the CLI perspective fo view, the following behavior is the same. But now you have two ways of configuring it: CLI, directly through the database. - How I did it Moved configuration part of the interface and hostname to "hostcfgd". - How to verify it Built an image Flash it to the switch Run CLI commands Signed-off-by: Yevhen Fastiuk <[email protected]>
1 parent 673f0fd commit 14c483f

File tree

2 files changed

+41
-61
lines changed

2 files changed

+41
-61
lines changed

config/main.py

+7-61
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
from sonic_py_common import device_info, multi_asic
2323
from sonic_py_common.interface import get_interface_table_name, get_port_table_name, get_intf_longname
2424
from utilities_common import util_base
25+
from swsscommon import swsscommon
2526
from swsscommon.swsscommon import SonicV2Connector, ConfigDBConnector
2627
from utilities_common.db import Db
2728
from utilities_common.intf_filter import parse_interface_in_filter
@@ -1884,19 +1885,11 @@ def hostname(new_hostname):
18841885

18851886
config_db = ConfigDBConnector()
18861887
config_db.connect()
1887-
config_db.mod_entry('DEVICE_METADATA' , 'localhost', {"hostname" : new_hostname})
1888-
try:
1889-
command = "service hostname-config restart"
1890-
clicommon.run_command(command, display_cmd=True)
1891-
except SystemExit as e:
1892-
click.echo("Restarting hostname-config service failed with error {}".format(e))
1893-
raise
1888+
config_db.mod_entry(swsscommon.CFG_DEVICE_METADATA_TABLE_NAME, 'localhost',
1889+
{'hostname': new_hostname})
18941890

1895-
# Reload Monit configuration to pick up new hostname in case it changed
1896-
click.echo("Reloading Monit configuration ...")
1897-
clicommon.run_command("sudo monit reload")
1898-
1899-
click.echo("Please note loaded setting will be lost after system reboot. To preserve setting, run `config save`.")
1891+
click.echo('Please note loaded setting will be lost after system reboot. To'
1892+
' preserve setting, run `config save`.')
19001893

19011894
#
19021895
# 'synchronous_mode' command ('config synchronous_mode ...')
@@ -2837,22 +2830,6 @@ def warm_restart_bgp_eoiu(ctx, enable):
28372830
db = ctx.obj['db']
28382831
db.mod_entry('WARM_RESTART', 'bgp', {'bgp_eoiu': enable})
28392832

2840-
def mvrf_restart_services():
2841-
"""Restart interfaces-config service and NTP service when mvrf is changed"""
2842-
"""
2843-
When mvrf is enabled, eth0 should be moved to mvrf; when it is disabled,
2844-
move it back to default vrf. Restarting the "interfaces-config" service
2845-
will recreate the /etc/network/interfaces file and restart the
2846-
"networking" service that takes care of the eth0 movement.
2847-
NTP service should also be restarted to rerun the NTP service with or
2848-
without "cgexec" accordingly.
2849-
"""
2850-
cmd="service ntp stop"
2851-
os.system (cmd)
2852-
cmd="systemctl restart interfaces-config"
2853-
os.system (cmd)
2854-
cmd="service ntp start"
2855-
os.system (cmd)
28562833

28572834
def vrf_add_management_vrf(config_db):
28582835
"""Enable management vrf in config DB"""
@@ -2862,22 +2839,7 @@ def vrf_add_management_vrf(config_db):
28622839
click.echo("ManagementVRF is already Enabled.")
28632840
return None
28642841
config_db.mod_entry('MGMT_VRF_CONFIG', "vrf_global", {"mgmtVrfEnabled": "true"})
2865-
mvrf_restart_services()
2866-
"""
2867-
The regular expression for grep in below cmd is to match eth0 line in /proc/net/route, sample file:
2868-
$ cat /proc/net/route
2869-
Iface Destination Gateway Flags RefCnt Use Metric Mask MTU Window IRTT
2870-
eth0 00000000 01803B0A 0003 0 0 202 00000000 0 0 0
2871-
"""
2872-
cmd = r"cat /proc/net/route | grep -E \"eth0\s+00000000\s+[0-9A-Z]+\s+[0-9]+\s+[0-9]+\s+[0-9]+\s+202\" | wc -l"
2873-
proc = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE)
2874-
output = proc.communicate()
2875-
if int(output[0]) >= 1:
2876-
cmd="ip -4 route del default dev eth0 metric 202"
2877-
proc = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE)
2878-
proc.communicate()
2879-
if proc.returncode != 0:
2880-
click.echo("Could not delete eth0 route")
2842+
28812843

28822844
def vrf_delete_management_vrf(config_db):
28832845
"""Disable management vrf in config DB"""
@@ -2887,7 +2849,7 @@ def vrf_delete_management_vrf(config_db):
28872849
click.echo("ManagementVRF is already Disabled.")
28882850
return None
28892851
config_db.mod_entry('MGMT_VRF_CONFIG', "vrf_global", {"mgmtVrfEnabled": "false"})
2890-
mvrf_restart_services()
2852+
28912853

28922854
@config.group(cls=clicommon.AbbreviationGroup)
28932855
@click.pass_context
@@ -4123,20 +4085,6 @@ def _get_all_mgmtinterface_keys():
41234085
config_db.connect()
41244086
return list(config_db.get_table('MGMT_INTERFACE').keys())
41254087

4126-
def mgmt_ip_restart_services():
4127-
"""Restart the required services when mgmt inteface IP address is changed"""
4128-
"""
4129-
Whenever the eth0 IP address is changed, restart the "interfaces-config"
4130-
service which regenerates the /etc/network/interfaces file and restarts
4131-
the networking service to make the new/null IP address effective for eth0.
4132-
"ntp-config" service should also be restarted based on the new
4133-
eth0 IP address since the ntp.conf (generated from ntp.conf.j2) is
4134-
made to listen on that particular eth0 IP address or reset it back.
4135-
"""
4136-
cmd="systemctl restart interfaces-config"
4137-
os.system (cmd)
4138-
cmd="systemctl restart ntp-config"
4139-
os.system (cmd)
41404088

41414089
#
41424090
# 'mtu' subcommand
@@ -4282,7 +4230,6 @@ def add(ctx, interface_name, ip_addr, gw):
42824230
config_db.set_entry("MGMT_INTERFACE", (interface_name, str(ip_address)), {"NULL": "NULL"})
42834231
else:
42844232
config_db.set_entry("MGMT_INTERFACE", (interface_name, str(ip_address)), {"gwaddr": gw})
4285-
mgmt_ip_restart_services()
42864233

42874234
return
42884235

@@ -4322,7 +4269,6 @@ def remove(ctx, interface_name, ip_addr):
43224269

43234270
if interface_name == 'eth0':
43244271
config_db.set_entry("MGMT_INTERFACE", (interface_name, str(ip_address)), None)
4325-
mgmt_ip_restart_services()
43264272
return
43274273

43284274
table_name = get_interface_table_name(interface_name)

tests/config_test.py

+34
Original file line numberDiff line numberDiff line change
@@ -1580,3 +1580,37 @@ def test_config_rate(self, get_cmd_module, setup_single_broadcom_asic):
15801580
def teardown_class(cls):
15811581
print("TEARDOWN")
15821582
os.environ['UTILITIES_UNIT_TESTING'] = "0"
1583+
1584+
1585+
class TestConfigHostname(object):
1586+
@classmethod
1587+
def setup_class(cls):
1588+
print("SETUP")
1589+
import config.main
1590+
importlib.reload(config.main)
1591+
1592+
@mock.patch('config.main.ConfigDBConnector')
1593+
def test_hostname_add(self, db_conn_patch, get_cmd_module):
1594+
db_conn_patch().mod_entry = mock.Mock()
1595+
(config, show) = get_cmd_module
1596+
1597+
runner = CliRunner()
1598+
result = runner.invoke(config.config.commands["hostname"],
1599+
["new_hostname"])
1600+
1601+
# Verify success
1602+
assert result.exit_code == 0
1603+
1604+
# Check was called
1605+
args_list = db_conn_patch().mod_entry.call_args_list
1606+
assert len(args_list) > 0
1607+
1608+
args, _ = args_list[0]
1609+
assert len(args) > 0
1610+
1611+
# Check new hostname was part of args
1612+
assert {'hostname': 'new_hostname'} in args
1613+
1614+
@classmethod
1615+
def teardown_class(cls):
1616+
print("TEARDOWN")

0 commit comments

Comments
 (0)