blob: ba620c8e1301f2ce6fb7c0f8fcf9825f1133eb5c [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 sys
8import os
9import requests
10
Michael Walsh47f2e4c2017-09-12 17:21:00 -050011save_path_0 = sys.path[0]
12del sys.path[0]
13
George Keishing37c58c82022-12-08 07:42:54 -060014from gen_arg import *
15from gen_print import *
16from gen_valid import *
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(
George Keishinge635ddc2022-12-08 07:38:02 -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,
George Keishinge635ddc2022-12-08 07:38:02 -060029 prefix_chars='-+')
Michael Walsh47f2e4c2017-09-12 17:21:00 -050030
31parser.add_argument(
George Keishinge635ddc2022-12-08 07:38:02 -060032 '--openbmc_username',
Michael Walsh47f2e4c2017-09-12 17:21:00 -050033 default="root",
George Keishinge635ddc2022-12-08 07:38:02 -060034 help='The username for communicating with the OpenBMC machine.')
Michael Walsh47f2e4c2017-09-12 17:21:00 -050035
36parser.add_argument(
George Keishinge635ddc2022-12-08 07:38:02 -060037 '--openbmc_password',
Michael Walsh47f2e4c2017-09-12 17:21:00 -050038 default="0penBmc",
George Keishinge635ddc2022-12-08 07:38:02 -060039 help='The password for communicating with the OpenBMC machine.')
Michael Walsh47f2e4c2017-09-12 17:21:00 -050040
41parser.add_argument(
George Keishinge635ddc2022-12-08 07:38:02 -060042 'openbmc_host',
43 help='The host name or IP address of the OpenBMC machine.')
Michael Walsh47f2e4c2017-09-12 17:21:00 -050044
45# Populate stock_list with options we want.
46stock_list = [("test_mode", 0), ("quiet", 1)]
47
48
George Keishinge635ddc2022-12-08 07:38:02 -060049def exit_function(signal_number=0,
50 frame=None):
Michael Walsh47f2e4c2017-09-12 17:21:00 -050051 r"""
Michael Walsh410b1782019-10-22 15:56:18 -050052 Execute whenever the program ends normally or with the signals that we catch (i.e. TERM, INT).
Michael Walsh47f2e4c2017-09-12 17:21:00 -050053 """
54
55 dprint_executing()
56 dprint_var(signal_number)
57
58 qprint_pgm_footer()
59
60
George Keishinge635ddc2022-12-08 07:38:02 -060061def signal_handler(signal_number,
62 frame):
Michael Walsh47f2e4c2017-09-12 17:21:00 -050063 r"""
Michael Walsh410b1782019-10-22 15:56:18 -050064 Handle signals. Without a function to catch a SIGTERM or SIGINT, our program would terminate immediately
65 with return code 143 and without calling our exit_function.
Michael Walsh47f2e4c2017-09-12 17:21:00 -050066 """
67
Michael Walsh410b1782019-10-22 15:56:18 -050068 # Our convention is to set up exit_function with atexit.register() so there is no need to explicitly
69 # call exit_function from here.
Michael Walsh47f2e4c2017-09-12 17:21:00 -050070
71 dprint_executing()
72
Michael Walsh410b1782019-10-22 15:56:18 -050073 # Calling exit prevents us from returning to the code that was running when we received the signal.
Michael Walsh47f2e4c2017-09-12 17:21:00 -050074 exit(0)
75
76
77def validate_parms():
Michael Walsh47f2e4c2017-09-12 17:21:00 -050078 r"""
Michael Walsh410b1782019-10-22 15:56:18 -050079 Validate program parameters, etc. Return True or False (i.e. pass/fail) accordingly.
Michael Walsh47f2e4c2017-09-12 17:21:00 -050080 """
81
82 gen_post_validation(exit_function, signal_handler)
83
84 return True
85
86
87def create_http_prefix(host):
Michael Walsh47f2e4c2017-09-12 17:21:00 -050088 r"""
89 Create and return an http prefix string.
90
91 Description of argument(s):
Michael Walsh410b1782019-10-22 15:56:18 -050092 host The host being communicated with via the curl command.
Michael Walsh47f2e4c2017-09-12 17:21:00 -050093 """
94
95 return "https://" + host + "/"
96
97
98def main():
George Keishinge635ddc2022-12-08 07:38:02 -060099
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
George Keishinge635ddc2022-12-08 07:38:02 -0600112 command = http_prefix + 'login'
Michael Walsh47f2e4c2017-09-12 17:21:00 -0500113 qprint_issuing(command)
George Keishinge635ddc2022-12-08 07:38:02 -0600114 resp = session.post(command,
115 json={'data': [openbmc_username, openbmc_password]},
116 verify=False)
117 if resp.json()['status'] != 'ok':
Michael Walsh47f2e4c2017-09-12 17:21:00 -0500118 json = resp.json()
Michael Walshc2762f62019-05-17 15:21:35 -0500119 print_error_report("http request failed:\n" + sprint_var(command))
Michael Walsh47f2e4c2017-09-12 17:21:00 -0500120 raise Exception("Login failed.\n")
121
122 command = http_prefix + "xyz/openbmc_project/inventory/system"
123 qprint_issuing(command)
124 resp = session.get(command, verify=False)
125 json = resp.json()
George Keishinge635ddc2022-12-08 07:38:02 -0600126 if json['status'] != 'ok':
Michael Walshc2762f62019-05-17 15:21:35 -0500127 print_error_report("http request failed:\n" + sprint_var(command))
Michael Walsh47f2e4c2017-09-12 17:21:00 -0500128 raise Exception("http request failed.\n")
129
Michael Walsh34162372017-09-18 13:31:31 -0500130 try:
George Keishinge635ddc2022-12-08 07:38:02 -0600131 mch_ser_num = json['data']['SerialNumber']
Michael Walsh34162372017-09-18 13:31:31 -0500132 except KeyError:
George Keishinge635ddc2022-12-08 07:38:02 -0600133 print_error_report("Failed to find 'SerialNumber' key in the"
134 + " following data:\n" + sprint_var(json))
Michael Walsh34162372017-09-18 13:31:31 -0500135 return False
Michael Walshc2762f62019-05-17 15:21:35 -0500136 print_var(mch_ser_num, 0, 0, 0)
Michael Walsh47f2e4c2017-09-12 17:21:00 -0500137
138 return True
139
140
141# Main
142
143if not main():
144 exit(1)