Skip to content

Commit 59b3ef3

Browse files
authored
Use more generic JSON reboot cause extra info file (#1631)
Use a more structured file format to hold reboot cause information. Also match the cause and user to that used in the tests. Cherry picking changes from sonic-net/sonic-buildimage#22638 <!-- Please make sure you've read and understood our contributing guidelines: https://github.com/Azure/SONiC/blob/gh-pages/CONTRIBUTING.md ** Make sure all your commits include a signature generated with `git commit -s` ** If this is a bug fix, make sure your description includes "fixes #xxxx", or "closes #xxxx" or "resolves #xxxx" Please provide the following information: --> #### Why I did it During design discussion, it was pointed out that having a file with no schema to hold the reboot cause info is a bad idea. This patch addresses that change. ##### Work item tracking - Microsoft ADO **(number only)**: #### How I did it #### How to verify it <!-- If PR needs to be backported, then the PR must be tested against the base branch and the earliest backport release branch and provide tested image version on these two branches. For example, if the PR is requested for master, 202211 and 202012, then the requester needs to provide test results on master and 202012. --> #### Which release branch to backport (provide reason below if selected) <!-- - Note we only backport fixes to a release branch, *not* features! - Please also provide a reason for the backporting below. - e.g. - [x] 202006 --> - [ ] 201811 - [ ] 201911 - [ ] 202006 - [ ] 202012 - [ ] 202106 - [ ] 202111 - [ ] 202205 - [ ] 202211 #### Tested branch (Please provide the tested image version) <!-- - Please provide tested image version - e.g. - [x] 20201231.100 --> - [ ] <!-- image version 1 --> - [ ] <!-- image version 2 --> #### Description for the changelog <!-- Write a short (one line) summary that describes the changes in this pull request for inclusion in the changelog: --> <!-- Ensure to add label/tag for the feature raised. example - PR#2174 under sonic-utilities repo. where, Generic Config and Update feature has been labelled as GCU. --> #### Link to config_db schema for YANG module changes <!-- Provide a link to config_db schema for the table for which YANG model is defined Link should point to correct section on https://github.com/Azure/sonic-buildimage/blob/master/src/sonic-yang-models/doc/Configuration.md --> #### A picture of a cute animal (not mandatory but encouraged) Signed-off-by: Mohan Yelugoti <[email protected]>
1 parent a152e6b commit 59b3ef3

File tree

1 file changed

+14
-8
lines changed

1 file changed

+14
-8
lines changed
Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,25 @@
11
#!/usr/bin/env python3
22

3+
import json
34
import os
45
from datetime import datetime, timezone
56

67
REBOOT_CAUSE_DIR = "/host/reboot-cause/"
7-
REBOOT_LC_BY_SUPERVISOR_FILE = os.path.join(REBOOT_CAUSE_DIR, "reboot-lc-by-supervisor.txt")
8+
REBOOT_CAUSE_PLATFORM_DIR = "/host/reboot-cause/platform"
9+
REBOOT_EXTRA_INFO_FILE = os.path.join(REBOOT_CAUSE_PLATFORM_DIR, "reboot-extra-info.json")
810

911
def main():
10-
reboot_time = datetime.now(timezone.utc).strftime("%a %b %d %I:%M:%S %p %Z %Y")
11-
12-
if os.path.exists(REBOOT_LC_BY_SUPERVISOR_FILE):
13-
os.remove(REBOOT_LC_BY_SUPERVISOR_FILE)
14-
with open(os.path.join(REBOOT_CAUSE_DIR, "reboot-cause.txt"), 'w') as reboot_cause_file:
15-
reboot_msg = "User issued 'Reboot from Supervisor' command [User: Supervisor, Time: {}]".format(reboot_time)
16-
reboot_cause_file.write(reboot_msg)
12+
if os.path.exists(REBOOT_EXTRA_INFO_FILE):
13+
with open(REBOOT_EXTRA_INFO_FILE, 'r', encoding='utf-8') as f:
14+
data = json.load(f)
15+
os.remove(REBOOT_EXTRA_INFO_FILE)
16+
who = data.get('from', None)
17+
when = data.get('when', None)
18+
if who is not None:
19+
who = who.capitalize()
20+
with open(os.path.join(REBOOT_CAUSE_DIR, "reboot-cause.txt"), 'w') as reboot_cause_file:
21+
reboot_msg = f"User issued 'Reboot from {who}' command [User: {who}, Time: {when}]"
22+
reboot_cause_file.write(reboot_msg)
1723

1824
if __name__ == "__main__":
1925
main()

0 commit comments

Comments
 (0)