-
Notifications
You must be signed in to change notification settings - Fork 816
[config] config reload should generate sysinfo if missing #3031
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -14,6 +14,7 @@ | |
| import time | ||
| import itertools | ||
| import copy | ||
| import tempfile | ||
|
|
||
| from jsonpatch import JsonPatchConflict | ||
| from jsonpointer import JsonPointerException | ||
|
|
@@ -142,6 +143,14 @@ def read_json_file(fileName): | |
| raise Exception(str(e)) | ||
| return result | ||
|
|
||
| # write given JSON file | ||
| def write_json_file(json_input, fileName): | ||
| try: | ||
| with open(fileName, 'w') as f: | ||
| json.dump(json_input, f, indent=4) | ||
| except Exception as e: | ||
| raise Exception(str(e)) | ||
|
|
||
| def _get_breakout_options(ctx, args, incomplete): | ||
| """ Provides dynamic mode option as per user argument i.e. interface name """ | ||
| all_mode_options = [] | ||
|
|
@@ -1525,6 +1534,12 @@ def reload(db, filename, yes, load_sysinfo, no_service_restart, force, file_form | |
| # Get the file from user input, else take the default file /etc/sonic/config_db{NS_id}.json | ||
| if cfg_files: | ||
| file = cfg_files[inst+1] | ||
| # Save to tmpfile in case of stdin input which can only be read once | ||
| if file == "/dev/stdin": | ||
| file_input = read_json_file(file) | ||
| (_, tmpfname) = tempfile.mkstemp(dir="/tmp", suffix="_configReloadStdin") | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Look around the login inside this function, it seems one time reading of file will be enough. Let's take further effort to reorg the existing code and optimize the logic.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. sonic-cfggen -j read it first. Then sonic-cfggen read it second. |
||
| write_json_file(file_input, tmpfname) | ||
| file = tmpfname | ||
| else: | ||
| if file_format == 'config_db': | ||
| if namespace is None: | ||
|
|
@@ -1540,6 +1555,19 @@ def reload(db, filename, yes, load_sysinfo, no_service_restart, force, file_form | |
| click.echo("The config file {} doesn't exist".format(file)) | ||
| continue | ||
|
|
||
| if file_format == 'config_db': | ||
| file_input = read_json_file(file) | ||
|
|
||
| platform = file_input.get("DEVICE_METADATA", {}).\ | ||
| get("localhost", {}).get("platform") | ||
| mac = file_input.get("DEVICE_METADATA", {}).\ | ||
| get("localhost", {}).get("mac") | ||
|
|
||
| if not platform or not mac: | ||
| log.log_warning("Input file does't have platform or mac. platform: {}, mac: {}" | ||
| .format(None if platform is None else platform, None if mac is None else mac)) | ||
| load_sysinfo = True | ||
|
|
||
| if load_sysinfo: | ||
| try: | ||
| command = [SONIC_CFGGEN_PATH, "-j", file, '-v', "DEVICE_METADATA.localhost.hwsku"] | ||
|
|
@@ -1598,6 +1626,13 @@ def reload(db, filename, yes, load_sysinfo, no_service_restart, force, file_form | |
| clicommon.run_command(command, display_cmd=True) | ||
| client.set(config_db.INIT_INDICATOR, 1) | ||
|
|
||
| if os.path.exists(file) and file.endswith("_configReloadStdin"): | ||
| # Remove tmpfile | ||
| try: | ||
| os.remove(file) | ||
| except OSError as e: | ||
| click.echo("An error occurred while removing the temporary file: {}".format(str(e)), err=True) | ||
|
|
||
| # Migrate DB contents to latest version | ||
| db_migrator='/usr/local/bin/db_migrator.py' | ||
| if os.path.isfile(db_migrator) and os.access(db_migrator, os.X_OK): | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
except and raise immediately does not add value. It is the same as no except at all.