Skip to content

Commit

Permalink
Add support to reload mgmt conf file and minigraph (sonic-net#93)
Browse files Browse the repository at this point in the history
  • Loading branch information
taoyl-ms authored Aug 7, 2017
1 parent e4f7161 commit c7f6ff6
Showing 1 changed file with 51 additions and 0 deletions.
51 changes: 51 additions & 0 deletions config/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import json
import subprocess
from swsssdk import ConfigDBConnector
from minigraph import parse_device_desc_xml

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

def _change_hostname(hostname):
current_hostname = os.uname()[1]
if current_hostname != hostname:
run_command('echo {} > /etc/hostname'.format(hostname), display_cmd=True)
run_command('hostname -F /etc/hostname', display_cmd=True)
run_command('sed -i "/\s{}$/d" /etc/hosts'.format(current_hostname), display_cmd=True)
run_command('echo "127.0.0.1 {}" >> /etc/hosts'.format(hostname), display_cmd=True)

# Callback for confirmation prompt. Aborts if user enters "n"
def _abort_if_false(ctx, param, value):
if not value:
Expand Down Expand Up @@ -112,6 +121,48 @@ def load(filename):
command = "{} -j {} --write-to-db".format(SONIC_CFGGEN_PATH, filename)
run_command(command, display_cmd=True)

@cli.command()
@click.option('-y', '--yes', is_flag=True, callback=_abort_if_false,
expose_value=False, prompt='Reload mgmt config?')
@click.argument('filename', default='/etc/sonic/device_desc.xml', type=click.Path(exists=True))
def load_mgmt_config(filename):
"""Reconfigure hostname and mgmt interface based on device description file."""
command = "{} -M {} --write-to-db".format(SONIC_CFGGEN_PATH, filename)
run_command(command, display_cmd=True)
#FIXME: After config DB daemon for hostname and mgmt interface is implemented, we'll no longer need to do manual configuration here
config_data = parse_device_desc_xml(filename)
hostname = config_data['minigraph_hostname']
_change_hostname(hostname)
mgmt_conf = config_data['minigraph_mgmt_interface']
command = "ifconfig eth0 {} netmask {}".format(str(mgmt_conf['addr']), str(mgmt_conf['mask']))
run_command(command, display_cmd=True)
command = "[ -f /var/run/dhclient.eth0.pid ] && kill `cat /var/run/dhclient.eth0.pid` && rm -f /var/run/dhclient.eth0.pid"
run_command(command, display_cmd=True)

@cli.command()
@click.option('-y', '--yes', is_flag=True, callback=_abort_if_false,
expose_value=False, prompt='Reload config from minigraph?')
def load_minigraph():
"""Reconfigure based on minigraph."""
command = "{} -m --write-to-db".format(SONIC_CFGGEN_PATH)
run_command(command, display_cmd=True)
command = "{} -m -v minigraph_hostname".format(SONIC_CFGGEN_PATH)
p = subprocess.Popen(command, shell=True, stdout=subprocess.PIPE)
p.wait()
hostname = p.communicate()[0].strip()
_change_hostname(hostname)
#FIXME: After config DB daemon is implemented, we'll no longer need to restart every service.
run_command("service interfaces-config restart", display_cmd=True)
run_command("service ntp-config restart", display_cmd=True)
run_command("service rsyslog-config restart", display_cmd=True)
run_command("service swss restart", display_cmd=True)
run_command("service bgp restart", display_cmd=True)
run_command("service teamd restart", display_cmd=True)
run_command("service pmon restart", display_cmd=True)
run_command("service lldp restart", display_cmd=True)
run_command("service snmp restart", display_cmd=True)
run_command("service dhcp_relay restart", display_cmd=True)

#
# 'bgp' group
#
Expand Down

0 comments on commit c7f6ff6

Please sign in to comment.