blob: 9f207b1d8798122fcb1fb3bd90532afd92d4c107 [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
Patrick Williams57318182022-12-08 06:18:26 -06007import os
8import sys
9
10import requests
Sridevi Ramesh47375aa2022-12-08 05:05:31 -060011from gen_arg import *
12from gen_print import *
13from gen_valid import *
14
Michael Walsh47f2e4c2017-09-12 17:21:00 -050015save_path_0 = sys.path[0]
16del sys.path[0]
17
Michael Walsh47f2e4c2017-09-12 17:21:00 -050018# Restore sys.path[0].
19sys.path.insert(0, save_path_0)
20
21logging.captureWarnings(True)
22
23parser = argparse.ArgumentParser(
Patrick Williams57318182022-12-08 06:18:26 -060024 usage="%(prog)s [OPTIONS]",
Patrick Williamsa57fef42022-12-03 07:00:14 -060025 description="%(prog)s will get the system serial number from an OBMC"
26 + " machine and print it to stdout as follows:\n\n"
27 + "mch_ser_num:<ser num>",
Michael Walshd0741f82017-12-21 14:04:21 -060028 formatter_class=argparse.ArgumentDefaultsHelpFormatter,
Patrick Williams57318182022-12-08 06:18:26 -060029 prefix_chars="-+",
30)
Michael Walsh47f2e4c2017-09-12 17:21:00 -050031
32parser.add_argument(
Patrick Williams57318182022-12-08 06:18:26 -060033 "--openbmc_username",
Michael Walsh47f2e4c2017-09-12 17:21:00 -050034 default="root",
Patrick Williams57318182022-12-08 06:18:26 -060035 help="The username for communicating with the OpenBMC machine.",
36)
Michael Walsh47f2e4c2017-09-12 17:21:00 -050037
38parser.add_argument(
Patrick Williams57318182022-12-08 06:18:26 -060039 "--openbmc_password",
Michael Walsh47f2e4c2017-09-12 17:21:00 -050040 default="0penBmc",
Patrick Williams57318182022-12-08 06:18:26 -060041 help="The password for communicating with the OpenBMC machine.",
42)
Michael Walsh47f2e4c2017-09-12 17:21:00 -050043
44parser.add_argument(
Patrick Williams57318182022-12-08 06:18:26 -060045 "openbmc_host", help="The host name or IP address of the OpenBMC machine."
46)
Michael Walsh47f2e4c2017-09-12 17:21:00 -050047
48# Populate stock_list with options we want.
49stock_list = [("test_mode", 0), ("quiet", 1)]
50
51
Patrick Williams57318182022-12-08 06:18:26 -060052def exit_function(signal_number=0, frame=None):
Michael Walsh47f2e4c2017-09-12 17:21:00 -050053 r"""
Michael Walsh410b1782019-10-22 15:56:18 -050054 Execute whenever the program ends normally or with the signals that we catch (i.e. TERM, INT).
Michael Walsh47f2e4c2017-09-12 17:21:00 -050055 """
56
57 dprint_executing()
58 dprint_var(signal_number)
59
60 qprint_pgm_footer()
61
62
Patrick Williams57318182022-12-08 06:18:26 -060063def signal_handler(signal_number, frame):
Michael Walsh47f2e4c2017-09-12 17:21:00 -050064 r"""
Michael Walsh410b1782019-10-22 15:56:18 -050065 Handle signals. Without a function to catch a SIGTERM or SIGINT, our program would terminate immediately
66 with return code 143 and without calling our exit_function.
Michael Walsh47f2e4c2017-09-12 17:21:00 -050067 """
68
Michael Walsh410b1782019-10-22 15:56:18 -050069 # Our convention is to set up exit_function with atexit.register() so there is no need to explicitly
70 # call exit_function from here.
Michael Walsh47f2e4c2017-09-12 17:21:00 -050071
72 dprint_executing()
73
Michael Walsh410b1782019-10-22 15:56:18 -050074 # Calling exit prevents us from returning to the code that was running when we received the signal.
Michael Walsh47f2e4c2017-09-12 17:21:00 -050075 exit(0)
76
77
78def validate_parms():
Michael Walsh47f2e4c2017-09-12 17:21:00 -050079 r"""
Michael Walsh410b1782019-10-22 15:56:18 -050080 Validate program parameters, etc. Return True or False (i.e. pass/fail) accordingly.
Michael Walsh47f2e4c2017-09-12 17:21:00 -050081 """
82
83 gen_post_validation(exit_function, signal_handler)
84
85 return True
86
87
88def create_http_prefix(host):
Michael Walsh47f2e4c2017-09-12 17:21:00 -050089 r"""
90 Create and return an http prefix string.
91
92 Description of argument(s):
Michael Walsh410b1782019-10-22 15:56:18 -050093 host The host being communicated with via the curl command.
Michael Walsh47f2e4c2017-09-12 17:21:00 -050094 """
95
96 return "https://" + host + "/"
97
98
99def main():
Michael Walsh47f2e4c2017-09-12 17:21:00 -0500100 if not gen_get_options(parser, stock_list):
101 return False
102
103 if not validate_parms():
104 return False
105
106 qprint_pgm_header()
107
108 session = requests.Session()
109
110 http_prefix = create_http_prefix(openbmc_host)
111
Patrick Williams57318182022-12-08 06:18:26 -0600112 command = http_prefix + "login"
Michael Walsh47f2e4c2017-09-12 17:21:00 -0500113 qprint_issuing(command)
Patrick Williams57318182022-12-08 06:18:26 -0600114 resp = session.post(
115 command,
116 json={"data": [openbmc_username, openbmc_password]},
117 verify=False,
118 )
119 if resp.json()["status"] != "ok":
Michael Walsh47f2e4c2017-09-12 17:21:00 -0500120 json = resp.json()
Michael Walshc2762f62019-05-17 15:21:35 -0500121 print_error_report("http request failed:\n" + sprint_var(command))
Michael Walsh47f2e4c2017-09-12 17:21:00 -0500122 raise Exception("Login failed.\n")
123
124 command = http_prefix + "xyz/openbmc_project/inventory/system"
125 qprint_issuing(command)
126 resp = session.get(command, verify=False)
127 json = resp.json()
Patrick Williams57318182022-12-08 06:18:26 -0600128 if json["status"] != "ok":
Michael Walshc2762f62019-05-17 15:21:35 -0500129 print_error_report("http request failed:\n" + sprint_var(command))
Michael Walsh47f2e4c2017-09-12 17:21:00 -0500130 raise Exception("http request failed.\n")
131
Michael Walsh34162372017-09-18 13:31:31 -0500132 try:
Patrick Williams57318182022-12-08 06:18:26 -0600133 mch_ser_num = json["data"]["SerialNumber"]
Michael Walsh34162372017-09-18 13:31:31 -0500134 except KeyError:
Patrick Williams57318182022-12-08 06:18:26 -0600135 print_error_report(
136 "Failed to find 'SerialNumber' key in the"
137 + " following data:\n"
138 + sprint_var(json)
139 )
Michael Walsh34162372017-09-18 13:31:31 -0500140 return False
Michael Walshc2762f62019-05-17 15:21:35 -0500141 print_var(mch_ser_num, 0, 0, 0)
Michael Walsh47f2e4c2017-09-12 17:21:00 -0500142
143 return True
144
145
146# Main
147
148if not main():
149 exit(1)