blob: 96fa7e02bad4d43d2a7d499daf12e6112c986dec [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
7import sys
8import os
9import requests
10
11save_path_0 = sys.path[0]
12del sys.path[0]
13
14from gen_arg import *
15from gen_print import *
16from gen_valid import *
17
18# Restore sys.path[0].
19sys.path.insert(0, save_path_0)
20
21logging.captureWarnings(True)
22
23parser = argparse.ArgumentParser(
24 usage='%(prog)s [OPTIONS]',
25 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,
Michael Walsh47f2e4c2017-09-12 17:21:00 -050029 prefix_chars='-+')
30
31parser.add_argument(
32 '--openbmc_username',
33 default="root",
34 help='The username for communicating with the OpenBMC machine.')
35
36parser.add_argument(
37 '--openbmc_password',
38 default="0penBmc",
39 help='The password for communicating with the OpenBMC machine.')
40
41parser.add_argument(
42 'openbmc_host',
43 help='The host name or IP address of the OpenBMC machine.')
44
45# Populate stock_list with options we want.
46stock_list = [("test_mode", 0), ("quiet", 1)]
47
48
49def 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
61def 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():
99
100 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
112 command = http_prefix + 'login'
113 qprint_issuing(command)
114 resp = session.post(command,
115 json={'data': [openbmc_username, openbmc_password]},
116 verify=False)
117 if resp.json()['status'] != 'ok':
118 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()
126 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:
131 mch_ser_num = json['data']['SerialNumber']
132 except KeyError:
133 print_error_report("Failed to find 'SerialNumber' key in the" +
134 " following data:\n" + sprint_var(json))
135 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)