blob: e56458c79ab49010bff0d590d9368b43f2ed72d1 [file] [log] [blame]
George Keishinge7e91712021-09-03 11:28:44 -05001#!/usr/bin/env python3
Michael Walsh47f2e4c2017-09-12 17:21:00 -05002
3r"""
Michael Walsh410b1782019-10-22 15:56:18 -05004This program will get the system serial number from an OBMC machine and print it to stdout.
Michael Walsh47f2e4c2017-09-12 17:21:00 -05005"""
6
George Keishinge635ddc2022-12-08 07:38:02 -06007import os
Patrick Williams20f38712022-12-08 06:18:26 -06008import sys
9
George Keishinge635ddc2022-12-08 07:38:02 -060010import requests
11
Michael Walsh47f2e4c2017-09-12 17:21:00 -050012save_path_0 = sys.path[0]
13del sys.path[0]
14
Patrick Williams20f38712022-12-08 06:18:26 -060015from gen_arg import * # NOQA
16from gen_print import * # NOQA
17from gen_valid import * # NOQA
George Keishing37c58c82022-12-08 07:42:54 -060018
Michael Walsh47f2e4c2017-09-12 17:21:00 -050019# Restore sys.path[0].
20sys.path.insert(0, save_path_0)
21
22logging.captureWarnings(True)
23
24parser = argparse.ArgumentParser(
Patrick Williams20f38712022-12-08 06:18:26 -060025 usage="%(prog)s [OPTIONS]",
Patrick Williamsa57fef42022-12-03 07:00:14 -060026 description="%(prog)s will get the system serial number from an OBMC"
27 + " machine and print it to stdout as follows:\n\n"
28 + "mch_ser_num:<ser num>",
Michael Walshd0741f82017-12-21 14:04:21 -060029 formatter_class=argparse.ArgumentDefaultsHelpFormatter,
Patrick Williams20f38712022-12-08 06:18:26 -060030 prefix_chars="-+",
31)
Michael Walsh47f2e4c2017-09-12 17:21:00 -050032
33parser.add_argument(
Patrick Williams20f38712022-12-08 06:18:26 -060034 "--openbmc_username",
Michael Walsh47f2e4c2017-09-12 17:21:00 -050035 default="root",
Patrick Williams20f38712022-12-08 06:18:26 -060036 help="The username for communicating with the OpenBMC machine.",
37)
Michael Walsh47f2e4c2017-09-12 17:21:00 -050038
39parser.add_argument(
Patrick Williams20f38712022-12-08 06:18:26 -060040 "--openbmc_password",
Michael Walsh47f2e4c2017-09-12 17:21:00 -050041 default="0penBmc",
Patrick Williams20f38712022-12-08 06:18:26 -060042 help="The password for communicating with the OpenBMC machine.",
43)
Michael Walsh47f2e4c2017-09-12 17:21:00 -050044
45parser.add_argument(
Patrick Williams20f38712022-12-08 06:18:26 -060046 "openbmc_host", help="The host name or IP address of the OpenBMC machine."
47)
Michael Walsh47f2e4c2017-09-12 17:21:00 -050048
49# Populate stock_list with options we want.
50stock_list = [("test_mode", 0), ("quiet", 1)]
51
52
Patrick Williams20f38712022-12-08 06:18:26 -060053def exit_function(signal_number=0, frame=None):
Michael Walsh47f2e4c2017-09-12 17:21:00 -050054 r"""
Michael Walsh410b1782019-10-22 15:56:18 -050055 Execute whenever the program ends normally or with the signals that we catch (i.e. TERM, INT).
Michael Walsh47f2e4c2017-09-12 17:21:00 -050056 """
57
58 dprint_executing()
59 dprint_var(signal_number)
60
61 qprint_pgm_footer()
62
63
Patrick Williams20f38712022-12-08 06:18:26 -060064def signal_handler(signal_number, frame):
Michael Walsh47f2e4c2017-09-12 17:21:00 -050065 r"""
Michael Walsh410b1782019-10-22 15:56:18 -050066 Handle signals. Without a function to catch a SIGTERM or SIGINT, our program would terminate immediately
67 with return code 143 and without calling our exit_function.
Michael Walsh47f2e4c2017-09-12 17:21:00 -050068 """
69
Michael Walsh410b1782019-10-22 15:56:18 -050070 # Our convention is to set up exit_function with atexit.register() so there is no need to explicitly
71 # call exit_function from here.
Michael Walsh47f2e4c2017-09-12 17:21:00 -050072
73 dprint_executing()
74
Michael Walsh410b1782019-10-22 15:56:18 -050075 # Calling exit prevents us from returning to the code that was running when we received the signal.
Michael Walsh47f2e4c2017-09-12 17:21:00 -050076 exit(0)
77
78
79def validate_parms():
Michael Walsh47f2e4c2017-09-12 17:21:00 -050080 r"""
Michael Walsh410b1782019-10-22 15:56:18 -050081 Validate program parameters, etc. Return True or False (i.e. pass/fail) accordingly.
Michael Walsh47f2e4c2017-09-12 17:21:00 -050082 """
83
84 gen_post_validation(exit_function, signal_handler)
85
86 return True
87
88
89def create_http_prefix(host):
Michael Walsh47f2e4c2017-09-12 17:21:00 -050090 r"""
91 Create and return an http prefix string.
92
93 Description of argument(s):
Michael Walsh410b1782019-10-22 15:56:18 -050094 host The host being communicated with via the curl command.
Michael Walsh47f2e4c2017-09-12 17:21:00 -050095 """
96
97 return "https://" + host + "/"
98
99
100def main():
Michael Walsh47f2e4c2017-09-12 17:21:00 -0500101 if not gen_get_options(parser, stock_list):
102 return False
103
104 if not validate_parms():
105 return False
106
107 qprint_pgm_header()
108
109 session = requests.Session()
110
111 http_prefix = create_http_prefix(openbmc_host)
112
Patrick Williams20f38712022-12-08 06:18:26 -0600113 command = http_prefix + "login"
Michael Walsh47f2e4c2017-09-12 17:21:00 -0500114 qprint_issuing(command)
Patrick Williams20f38712022-12-08 06:18:26 -0600115 resp = session.post(
116 command,
117 json={"data": [openbmc_username, openbmc_password]},
118 verify=False,
119 )
120 if resp.json()["status"] != "ok":
Michael Walsh47f2e4c2017-09-12 17:21:00 -0500121 json = resp.json()
Michael Walshc2762f62019-05-17 15:21:35 -0500122 print_error_report("http request failed:\n" + sprint_var(command))
Michael Walsh47f2e4c2017-09-12 17:21:00 -0500123 raise Exception("Login failed.\n")
124
125 command = http_prefix + "xyz/openbmc_project/inventory/system"
126 qprint_issuing(command)
127 resp = session.get(command, verify=False)
128 json = resp.json()
Patrick Williams20f38712022-12-08 06:18:26 -0600129 if json["status"] != "ok":
Michael Walshc2762f62019-05-17 15:21:35 -0500130 print_error_report("http request failed:\n" + sprint_var(command))
Michael Walsh47f2e4c2017-09-12 17:21:00 -0500131 raise Exception("http request failed.\n")
132
Michael Walsh34162372017-09-18 13:31:31 -0500133 try:
Patrick Williams20f38712022-12-08 06:18:26 -0600134 mch_ser_num = json["data"]["SerialNumber"]
Michael Walsh34162372017-09-18 13:31:31 -0500135 except KeyError:
Patrick Williams20f38712022-12-08 06:18:26 -0600136 print_error_report(
137 "Failed to find 'SerialNumber' key in the"
138 + " following data:\n"
139 + sprint_var(json)
140 )
Michael Walsh34162372017-09-18 13:31:31 -0500141 return False
Michael Walshc2762f62019-05-17 15:21:35 -0500142 print_var(mch_ser_num, 0, 0, 0)
Michael Walsh47f2e4c2017-09-12 17:21:00 -0500143
144 return True
145
146
147# Main
148
149if not main():
150 exit(1)