| George Keishing | e7e9171 | 2021-09-03 11:28:44 -0500 | [diff] [blame] | 1 | #!/usr/bin/env python3 | 
| Michael Walsh | 47f2e4c | 2017-09-12 17:21:00 -0500 | [diff] [blame] | 2 |  | 
|  | 3 | r""" | 
| Michael Walsh | 410b178 | 2019-10-22 15:56:18 -0500 | [diff] [blame] | 4 | This program will get the system serial number from an OBMC machine and print it to stdout. | 
| Michael Walsh | 47f2e4c | 2017-09-12 17:21:00 -0500 | [diff] [blame] | 5 | """ | 
|  | 6 |  | 
| Patrick Williams | 5731818 | 2022-12-08 06:18:26 -0600 | [diff] [blame^] | 7 | import os | 
|  | 8 | import sys | 
|  | 9 |  | 
|  | 10 | import requests | 
| Sridevi Ramesh | 47375aa | 2022-12-08 05:05:31 -0600 | [diff] [blame] | 11 | from gen_arg import * | 
|  | 12 | from gen_print import * | 
|  | 13 | from gen_valid import * | 
|  | 14 |  | 
| Michael Walsh | 47f2e4c | 2017-09-12 17:21:00 -0500 | [diff] [blame] | 15 | save_path_0 = sys.path[0] | 
|  | 16 | del sys.path[0] | 
|  | 17 |  | 
| Michael Walsh | 47f2e4c | 2017-09-12 17:21:00 -0500 | [diff] [blame] | 18 | # Restore sys.path[0]. | 
|  | 19 | sys.path.insert(0, save_path_0) | 
|  | 20 |  | 
|  | 21 | logging.captureWarnings(True) | 
|  | 22 |  | 
|  | 23 | parser = argparse.ArgumentParser( | 
| Patrick Williams | 5731818 | 2022-12-08 06:18:26 -0600 | [diff] [blame^] | 24 | usage="%(prog)s [OPTIONS]", | 
| Patrick Williams | a57fef4 | 2022-12-03 07:00:14 -0600 | [diff] [blame] | 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 Walsh | d0741f8 | 2017-12-21 14:04:21 -0600 | [diff] [blame] | 28 | formatter_class=argparse.ArgumentDefaultsHelpFormatter, | 
| Patrick Williams | 5731818 | 2022-12-08 06:18:26 -0600 | [diff] [blame^] | 29 | prefix_chars="-+", | 
|  | 30 | ) | 
| Michael Walsh | 47f2e4c | 2017-09-12 17:21:00 -0500 | [diff] [blame] | 31 |  | 
|  | 32 | parser.add_argument( | 
| Patrick Williams | 5731818 | 2022-12-08 06:18:26 -0600 | [diff] [blame^] | 33 | "--openbmc_username", | 
| Michael Walsh | 47f2e4c | 2017-09-12 17:21:00 -0500 | [diff] [blame] | 34 | default="root", | 
| Patrick Williams | 5731818 | 2022-12-08 06:18:26 -0600 | [diff] [blame^] | 35 | help="The username for communicating with the OpenBMC machine.", | 
|  | 36 | ) | 
| Michael Walsh | 47f2e4c | 2017-09-12 17:21:00 -0500 | [diff] [blame] | 37 |  | 
|  | 38 | parser.add_argument( | 
| Patrick Williams | 5731818 | 2022-12-08 06:18:26 -0600 | [diff] [blame^] | 39 | "--openbmc_password", | 
| Michael Walsh | 47f2e4c | 2017-09-12 17:21:00 -0500 | [diff] [blame] | 40 | default="0penBmc", | 
| Patrick Williams | 5731818 | 2022-12-08 06:18:26 -0600 | [diff] [blame^] | 41 | help="The password for communicating with the OpenBMC machine.", | 
|  | 42 | ) | 
| Michael Walsh | 47f2e4c | 2017-09-12 17:21:00 -0500 | [diff] [blame] | 43 |  | 
|  | 44 | parser.add_argument( | 
| Patrick Williams | 5731818 | 2022-12-08 06:18:26 -0600 | [diff] [blame^] | 45 | "openbmc_host", help="The host name or IP address of the OpenBMC machine." | 
|  | 46 | ) | 
| Michael Walsh | 47f2e4c | 2017-09-12 17:21:00 -0500 | [diff] [blame] | 47 |  | 
|  | 48 | # Populate stock_list with options we want. | 
|  | 49 | stock_list = [("test_mode", 0), ("quiet", 1)] | 
|  | 50 |  | 
|  | 51 |  | 
| Patrick Williams | 5731818 | 2022-12-08 06:18:26 -0600 | [diff] [blame^] | 52 | def exit_function(signal_number=0, frame=None): | 
| Michael Walsh | 47f2e4c | 2017-09-12 17:21:00 -0500 | [diff] [blame] | 53 | r""" | 
| Michael Walsh | 410b178 | 2019-10-22 15:56:18 -0500 | [diff] [blame] | 54 | Execute whenever the program ends normally or with the signals that we catch (i.e. TERM, INT). | 
| Michael Walsh | 47f2e4c | 2017-09-12 17:21:00 -0500 | [diff] [blame] | 55 | """ | 
|  | 56 |  | 
|  | 57 | dprint_executing() | 
|  | 58 | dprint_var(signal_number) | 
|  | 59 |  | 
|  | 60 | qprint_pgm_footer() | 
|  | 61 |  | 
|  | 62 |  | 
| Patrick Williams | 5731818 | 2022-12-08 06:18:26 -0600 | [diff] [blame^] | 63 | def signal_handler(signal_number, frame): | 
| Michael Walsh | 47f2e4c | 2017-09-12 17:21:00 -0500 | [diff] [blame] | 64 | r""" | 
| Michael Walsh | 410b178 | 2019-10-22 15:56:18 -0500 | [diff] [blame] | 65 | 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 Walsh | 47f2e4c | 2017-09-12 17:21:00 -0500 | [diff] [blame] | 67 | """ | 
|  | 68 |  | 
| Michael Walsh | 410b178 | 2019-10-22 15:56:18 -0500 | [diff] [blame] | 69 | # 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 Walsh | 47f2e4c | 2017-09-12 17:21:00 -0500 | [diff] [blame] | 71 |  | 
|  | 72 | dprint_executing() | 
|  | 73 |  | 
| Michael Walsh | 410b178 | 2019-10-22 15:56:18 -0500 | [diff] [blame] | 74 | # Calling exit prevents us from returning to the code that was running when we received the signal. | 
| Michael Walsh | 47f2e4c | 2017-09-12 17:21:00 -0500 | [diff] [blame] | 75 | exit(0) | 
|  | 76 |  | 
|  | 77 |  | 
|  | 78 | def validate_parms(): | 
| Michael Walsh | 47f2e4c | 2017-09-12 17:21:00 -0500 | [diff] [blame] | 79 | r""" | 
| Michael Walsh | 410b178 | 2019-10-22 15:56:18 -0500 | [diff] [blame] | 80 | Validate program parameters, etc.  Return True or False (i.e. pass/fail) accordingly. | 
| Michael Walsh | 47f2e4c | 2017-09-12 17:21:00 -0500 | [diff] [blame] | 81 | """ | 
|  | 82 |  | 
|  | 83 | gen_post_validation(exit_function, signal_handler) | 
|  | 84 |  | 
|  | 85 | return True | 
|  | 86 |  | 
|  | 87 |  | 
|  | 88 | def create_http_prefix(host): | 
| Michael Walsh | 47f2e4c | 2017-09-12 17:21:00 -0500 | [diff] [blame] | 89 | r""" | 
|  | 90 | Create and return an http prefix string. | 
|  | 91 |  | 
|  | 92 | Description of argument(s): | 
| Michael Walsh | 410b178 | 2019-10-22 15:56:18 -0500 | [diff] [blame] | 93 | host                            The host being communicated with via the curl command. | 
| Michael Walsh | 47f2e4c | 2017-09-12 17:21:00 -0500 | [diff] [blame] | 94 | """ | 
|  | 95 |  | 
|  | 96 | return "https://" + host + "/" | 
|  | 97 |  | 
|  | 98 |  | 
|  | 99 | def main(): | 
| Michael Walsh | 47f2e4c | 2017-09-12 17:21:00 -0500 | [diff] [blame] | 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 |  | 
| Patrick Williams | 5731818 | 2022-12-08 06:18:26 -0600 | [diff] [blame^] | 112 | command = http_prefix + "login" | 
| Michael Walsh | 47f2e4c | 2017-09-12 17:21:00 -0500 | [diff] [blame] | 113 | qprint_issuing(command) | 
| Patrick Williams | 5731818 | 2022-12-08 06:18:26 -0600 | [diff] [blame^] | 114 | resp = session.post( | 
|  | 115 | command, | 
|  | 116 | json={"data": [openbmc_username, openbmc_password]}, | 
|  | 117 | verify=False, | 
|  | 118 | ) | 
|  | 119 | if resp.json()["status"] != "ok": | 
| Michael Walsh | 47f2e4c | 2017-09-12 17:21:00 -0500 | [diff] [blame] | 120 | json = resp.json() | 
| Michael Walsh | c2762f6 | 2019-05-17 15:21:35 -0500 | [diff] [blame] | 121 | print_error_report("http request failed:\n" + sprint_var(command)) | 
| Michael Walsh | 47f2e4c | 2017-09-12 17:21:00 -0500 | [diff] [blame] | 122 | 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 Williams | 5731818 | 2022-12-08 06:18:26 -0600 | [diff] [blame^] | 128 | if json["status"] != "ok": | 
| Michael Walsh | c2762f6 | 2019-05-17 15:21:35 -0500 | [diff] [blame] | 129 | print_error_report("http request failed:\n" + sprint_var(command)) | 
| Michael Walsh | 47f2e4c | 2017-09-12 17:21:00 -0500 | [diff] [blame] | 130 | raise Exception("http request failed.\n") | 
|  | 131 |  | 
| Michael Walsh | 3416237 | 2017-09-18 13:31:31 -0500 | [diff] [blame] | 132 | try: | 
| Patrick Williams | 5731818 | 2022-12-08 06:18:26 -0600 | [diff] [blame^] | 133 | mch_ser_num = json["data"]["SerialNumber"] | 
| Michael Walsh | 3416237 | 2017-09-18 13:31:31 -0500 | [diff] [blame] | 134 | except KeyError: | 
| Patrick Williams | 5731818 | 2022-12-08 06:18:26 -0600 | [diff] [blame^] | 135 | print_error_report( | 
|  | 136 | "Failed to find 'SerialNumber' key in the" | 
|  | 137 | + " following data:\n" | 
|  | 138 | + sprint_var(json) | 
|  | 139 | ) | 
| Michael Walsh | 3416237 | 2017-09-18 13:31:31 -0500 | [diff] [blame] | 140 | return False | 
| Michael Walsh | c2762f6 | 2019-05-17 15:21:35 -0500 | [diff] [blame] | 141 | print_var(mch_ser_num, 0, 0, 0) | 
| Michael Walsh | 47f2e4c | 2017-09-12 17:21:00 -0500 | [diff] [blame] | 142 |  | 
|  | 143 | return True | 
|  | 144 |  | 
|  | 145 |  | 
|  | 146 | # Main | 
|  | 147 |  | 
|  | 148 | if not main(): | 
|  | 149 | exit(1) |