Skip to content

Commit

Permalink
Add support for config save and config load (sonic-net#88)
Browse files Browse the repository at this point in the history
config save will export current config db content into a json file (default /etc/sonic/config_db.json).
config load will import data from a json file into DB.
  • Loading branch information
taoyl-ms authored Jul 25, 2017
1 parent 5131e7f commit 6c7e223
Showing 1 changed file with 23 additions and 0 deletions.
23 changes: 23 additions & 0 deletions config/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,13 +91,36 @@ def _switch_bgp_session_status(ipaddr_or_hostname, status, verbose):
raise click.Abort
_switch_bgp_session_status_by_addr(ipaddress, status, verbose)

# Callback for confirmation prompt. Aborts if user enters "n"
def _abort_if_false(ctx, param, value):
if not value:
ctx.abort()

# This is our main entrypoint - the main 'config' command
@click.group()
def cli():
"""SONiC command line - 'config' command"""
if os.geteuid() != 0:
exit("Root privileges are required for this operation")

@cli.command()
@click.option('-y', '--yes', is_flag=True, callback=_abort_if_false,
expose_value=False, prompt='Existing file will be overwritten, continue?')
@click.argument('filename', default='/etc/sonic/config_db.json', type=click.Path())
def save(filename):
"""Export current config DB to a file on disk."""
command = "{} -d --print-data > {}".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 all config?')
@click.argument('filename', default='/etc/sonic/config_db.json', type=click.Path(exists=True))
def load(filename):
"""Import a previous saved config DB dump file."""
command = "{} -j {} --write-to-db".format(SONIC_CFGGEN_PATH, filename)
run_command(command, display_cmd=True)

#
# 'bgp' group
#
Expand Down

0 comments on commit 6c7e223

Please sign in to comment.