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