Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[config] Enhance the Sonic CLI command "config save" #998

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 18 additions & 4 deletions config/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import re
import syslog
import time
import tempfile
import netifaces
import threading

Expand Down Expand Up @@ -171,7 +172,7 @@ def execute_systemctl(list_of_services, action):
if e.is_set():
sys.exit(1)

def run_command(command, display_cmd=False, ignore_error=False):
def run_command(command, display_cmd=False, ignore_error=False, raise_exception_when_error=False):
"""Run bash command and print output to stdout
"""
if display_cmd == True:
Expand All @@ -184,6 +185,8 @@ def run_command(command, display_cmd=False, ignore_error=False):
click.echo(out)

if proc.returncode != 0 and not ignore_error:
if raise_exception_when_error:
raise Exception("Return code is not zero(exit_code={})".format(proc.returncode))
sys.exit(proc.returncode)

# Validate whether a given namespace name is valid in the device.
Expand Down Expand Up @@ -783,13 +786,24 @@ def save(filename):
else:
file = "/etc/sonic/config_db{}.json".format(inst)

tmp_file_hdl=tempfile.NamedTemporaryFile(suffix='.db', prefix='config{}'.format(inst), delete=False)
tmp_filename=tmp_file_hdl.name
tmp_file_hdl.close()

if namespace is None:
command = "{} -d --print-data > {}".format(SONIC_CFGGEN_PATH, file)
command = "{} -d --print-data > {}".format(SONIC_CFGGEN_PATH, tmp_filename)
else:
command = "{} -n {} -d --print-data > {}".format(SONIC_CFGGEN_PATH, namespace, file)
command = "{} -n {} -d --print-data > {}".format(SONIC_CFGGEN_PATH, namespace, tmp_filename)

log_info("'save' executing...")
run_command(command, display_cmd=True)
try:
run_command(command, display_cmd=True, raise_exception_when_error=True)
if os.path.isfile(file):
os.remove(file)
os.rename(tmp_filename, file)
except Exception as e:
os.remove(tmp_filename)
exit(str(e))

@config.command()
@click.option('-y', '--yes', is_flag=True)
Expand Down