Michael Walsh | 47f2e4c | 2017-09-12 17:21:00 -0500 | [diff] [blame] | 1 | #!/usr/bin/env python |
| 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 | |
| 7 | import sys |
| 8 | import os |
| 9 | import requests |
| 10 | |
| 11 | save_path_0 = sys.path[0] |
| 12 | del sys.path[0] |
| 13 | |
| 14 | from gen_arg import * |
| 15 | from gen_print import * |
| 16 | from gen_valid import * |
| 17 | |
| 18 | # Restore sys.path[0]. |
| 19 | sys.path.insert(0, save_path_0) |
| 20 | |
| 21 | logging.captureWarnings(True) |
| 22 | |
| 23 | parser = 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 Walsh | d0741f8 | 2017-12-21 14:04:21 -0600 | [diff] [blame] | 28 | formatter_class=argparse.ArgumentDefaultsHelpFormatter, |
Michael Walsh | 47f2e4c | 2017-09-12 17:21:00 -0500 | [diff] [blame] | 29 | prefix_chars='-+') |
| 30 | |
| 31 | parser.add_argument( |
| 32 | '--openbmc_username', |
| 33 | default="root", |
| 34 | help='The username for communicating with the OpenBMC machine.') |
| 35 | |
| 36 | parser.add_argument( |
| 37 | '--openbmc_password', |
| 38 | default="0penBmc", |
| 39 | help='The password for communicating with the OpenBMC machine.') |
| 40 | |
| 41 | parser.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. |
| 46 | stock_list = [("test_mode", 0), ("quiet", 1)] |
| 47 | |
| 48 | |
| 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 | |
| 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(): |
| 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 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() |
| 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: |
| 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 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) |