Skip to content

Commit c7f6ff6

Browse files
authored
Add support to reload mgmt conf file and minigraph (sonic-net#93)
1 parent e4f7161 commit c7f6ff6

File tree

1 file changed

+51
-0
lines changed

1 file changed

+51
-0
lines changed

config/main.py

+51
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import json
77
import subprocess
88
from swsssdk import ConfigDBConnector
9+
from minigraph import parse_device_desc_xml
910

1011
SONIC_CFGGEN_PATH = "sonic-cfggen"
1112
MINIGRAPH_PATH = "/etc/sonic/minigraph.xml"
@@ -82,6 +83,14 @@ def _switch_bgp_session_status(ipaddr_or_hostname, status, verbose):
8283
raise click.Abort
8384
_switch_bgp_session_status_by_addr(ipaddress, status, verbose)
8485

86+
def _change_hostname(hostname):
87+
current_hostname = os.uname()[1]
88+
if current_hostname != hostname:
89+
run_command('echo {} > /etc/hostname'.format(hostname), display_cmd=True)
90+
run_command('hostname -F /etc/hostname', display_cmd=True)
91+
run_command('sed -i "/\s{}$/d" /etc/hosts'.format(current_hostname), display_cmd=True)
92+
run_command('echo "127.0.0.1 {}" >> /etc/hosts'.format(hostname), display_cmd=True)
93+
8594
# Callback for confirmation prompt. Aborts if user enters "n"
8695
def _abort_if_false(ctx, param, value):
8796
if not value:
@@ -112,6 +121,48 @@ def load(filename):
112121
command = "{} -j {} --write-to-db".format(SONIC_CFGGEN_PATH, filename)
113122
run_command(command, display_cmd=True)
114123

124+
@cli.command()
125+
@click.option('-y', '--yes', is_flag=True, callback=_abort_if_false,
126+
expose_value=False, prompt='Reload mgmt config?')
127+
@click.argument('filename', default='/etc/sonic/device_desc.xml', type=click.Path(exists=True))
128+
def load_mgmt_config(filename):
129+
"""Reconfigure hostname and mgmt interface based on device description file."""
130+
command = "{} -M {} --write-to-db".format(SONIC_CFGGEN_PATH, filename)
131+
run_command(command, display_cmd=True)
132+
#FIXME: After config DB daemon for hostname and mgmt interface is implemented, we'll no longer need to do manual configuration here
133+
config_data = parse_device_desc_xml(filename)
134+
hostname = config_data['minigraph_hostname']
135+
_change_hostname(hostname)
136+
mgmt_conf = config_data['minigraph_mgmt_interface']
137+
command = "ifconfig eth0 {} netmask {}".format(str(mgmt_conf['addr']), str(mgmt_conf['mask']))
138+
run_command(command, display_cmd=True)
139+
command = "[ -f /var/run/dhclient.eth0.pid ] && kill `cat /var/run/dhclient.eth0.pid` && rm -f /var/run/dhclient.eth0.pid"
140+
run_command(command, display_cmd=True)
141+
142+
@cli.command()
143+
@click.option('-y', '--yes', is_flag=True, callback=_abort_if_false,
144+
expose_value=False, prompt='Reload config from minigraph?')
145+
def load_minigraph():
146+
"""Reconfigure based on minigraph."""
147+
command = "{} -m --write-to-db".format(SONIC_CFGGEN_PATH)
148+
run_command(command, display_cmd=True)
149+
command = "{} -m -v minigraph_hostname".format(SONIC_CFGGEN_PATH)
150+
p = subprocess.Popen(command, shell=True, stdout=subprocess.PIPE)
151+
p.wait()
152+
hostname = p.communicate()[0].strip()
153+
_change_hostname(hostname)
154+
#FIXME: After config DB daemon is implemented, we'll no longer need to restart every service.
155+
run_command("service interfaces-config restart", display_cmd=True)
156+
run_command("service ntp-config restart", display_cmd=True)
157+
run_command("service rsyslog-config restart", display_cmd=True)
158+
run_command("service swss restart", display_cmd=True)
159+
run_command("service bgp restart", display_cmd=True)
160+
run_command("service teamd restart", display_cmd=True)
161+
run_command("service pmon restart", display_cmd=True)
162+
run_command("service lldp restart", display_cmd=True)
163+
run_command("service snmp restart", display_cmd=True)
164+
run_command("service dhcp_relay restart", display_cmd=True)
165+
115166
#
116167
# 'bgp' group
117168
#

0 commit comments

Comments
 (0)