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