|
6 | 6 | import json
|
7 | 7 | import subprocess
|
8 | 8 | from swsssdk import ConfigDBConnector
|
| 9 | +from minigraph import parse_device_desc_xml |
9 | 10 |
|
10 | 11 | SONIC_CFGGEN_PATH = "sonic-cfggen"
|
11 | 12 | MINIGRAPH_PATH = "/etc/sonic/minigraph.xml"
|
@@ -82,6 +83,14 @@ def _switch_bgp_session_status(ipaddr_or_hostname, status, verbose):
|
82 | 83 | raise click.Abort
|
83 | 84 | _switch_bgp_session_status_by_addr(ipaddress, status, verbose)
|
84 | 85 |
|
| 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 | + |
85 | 94 | # Callback for confirmation prompt. Aborts if user enters "n"
|
86 | 95 | def _abort_if_false(ctx, param, value):
|
87 | 96 | if not value:
|
@@ -112,6 +121,48 @@ def load(filename):
|
112 | 121 | command = "{} -j {} --write-to-db".format(SONIC_CFGGEN_PATH, filename)
|
113 | 122 | run_command(command, display_cmd=True)
|
114 | 123 |
|
| 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 | + |
115 | 166 | #
|
116 | 167 | # 'bgp' group
|
117 | 168 | #
|
|
0 commit comments