1*47f2e4c8SMichael Walsh#!/usr/bin/env python 2*47f2e4c8SMichael Walsh 3*47f2e4c8SMichael Walshr""" 4*47f2e4c8SMichael WalshThis program will get the system serial number from an OBMC machine and print 5*47f2e4c8SMichael Walshit to stdout. 6*47f2e4c8SMichael Walsh""" 7*47f2e4c8SMichael Walsh 8*47f2e4c8SMichael Walshimport sys 9*47f2e4c8SMichael Walshimport os 10*47f2e4c8SMichael Walshimport requests 11*47f2e4c8SMichael Walsh 12*47f2e4c8SMichael Walshsave_path_0 = sys.path[0] 13*47f2e4c8SMichael Walshdel sys.path[0] 14*47f2e4c8SMichael Walsh 15*47f2e4c8SMichael Walshfrom gen_arg import * 16*47f2e4c8SMichael Walshfrom gen_print import * 17*47f2e4c8SMichael Walshfrom gen_valid import * 18*47f2e4c8SMichael Walsh 19*47f2e4c8SMichael Walsh# Restore sys.path[0]. 20*47f2e4c8SMichael Walshsys.path.insert(0, save_path_0) 21*47f2e4c8SMichael Walsh 22*47f2e4c8SMichael Walshlogging.captureWarnings(True) 23*47f2e4c8SMichael Walsh 24*47f2e4c8SMichael Walshparser = argparse.ArgumentParser( 25*47f2e4c8SMichael Walsh usage='%(prog)s [OPTIONS]', 26*47f2e4c8SMichael Walsh description="%(prog)s will get the system serial number from an OBMC" + 27*47f2e4c8SMichael Walsh " machine and print it to stdout as follows:\n\n" + 28*47f2e4c8SMichael Walsh "mch_ser_num:<ser num>", 29*47f2e4c8SMichael Walsh formatter_class=argparse.RawTextHelpFormatter, 30*47f2e4c8SMichael Walsh prefix_chars='-+') 31*47f2e4c8SMichael Walsh 32*47f2e4c8SMichael Walshparser.add_argument( 33*47f2e4c8SMichael Walsh '--openbmc_username', 34*47f2e4c8SMichael Walsh default="root", 35*47f2e4c8SMichael Walsh help='The username for communicating with the OpenBMC machine.') 36*47f2e4c8SMichael Walsh 37*47f2e4c8SMichael Walshparser.add_argument( 38*47f2e4c8SMichael Walsh '--openbmc_password', 39*47f2e4c8SMichael Walsh default="0penBmc", 40*47f2e4c8SMichael Walsh help='The password for communicating with the OpenBMC machine.') 41*47f2e4c8SMichael Walsh 42*47f2e4c8SMichael Walshparser.add_argument( 43*47f2e4c8SMichael Walsh 'openbmc_host', 44*47f2e4c8SMichael Walsh help='The host name or IP address of the OpenBMC machine.') 45*47f2e4c8SMichael Walsh 46*47f2e4c8SMichael Walsh# Populate stock_list with options we want. 47*47f2e4c8SMichael Walshstock_list = [("test_mode", 0), ("quiet", 1)] 48*47f2e4c8SMichael Walsh 49*47f2e4c8SMichael Walsh 50*47f2e4c8SMichael Walshdef exit_function(signal_number=0, 51*47f2e4c8SMichael Walsh frame=None): 52*47f2e4c8SMichael Walsh 53*47f2e4c8SMichael Walsh r""" 54*47f2e4c8SMichael Walsh Execute whenever the program ends normally or with the signals that we 55*47f2e4c8SMichael Walsh catch (i.e. TERM, INT). 56*47f2e4c8SMichael Walsh """ 57*47f2e4c8SMichael Walsh 58*47f2e4c8SMichael Walsh dprint_executing() 59*47f2e4c8SMichael Walsh dprint_var(signal_number) 60*47f2e4c8SMichael Walsh 61*47f2e4c8SMichael Walsh qprint_pgm_footer() 62*47f2e4c8SMichael Walsh 63*47f2e4c8SMichael Walsh 64*47f2e4c8SMichael Walshdef signal_handler(signal_number, 65*47f2e4c8SMichael Walsh frame): 66*47f2e4c8SMichael Walsh 67*47f2e4c8SMichael Walsh r""" 68*47f2e4c8SMichael Walsh Handle signals. Without a function to catch a SIGTERM or SIGINT, our 69*47f2e4c8SMichael Walsh program would terminate immediately with return code 143 and without 70*47f2e4c8SMichael Walsh calling our exit_function. 71*47f2e4c8SMichael Walsh """ 72*47f2e4c8SMichael Walsh 73*47f2e4c8SMichael Walsh # Our convention is to set up exit_function with atexit.register() so 74*47f2e4c8SMichael Walsh # there is no need to explicitly call exit_function from here. 75*47f2e4c8SMichael Walsh 76*47f2e4c8SMichael Walsh dprint_executing() 77*47f2e4c8SMichael Walsh 78*47f2e4c8SMichael Walsh # Calling exit prevents us from returning to the code that was running 79*47f2e4c8SMichael Walsh # when we received the signal. 80*47f2e4c8SMichael Walsh exit(0) 81*47f2e4c8SMichael Walsh 82*47f2e4c8SMichael Walsh 83*47f2e4c8SMichael Walshdef validate_parms(): 84*47f2e4c8SMichael Walsh 85*47f2e4c8SMichael Walsh r""" 86*47f2e4c8SMichael Walsh Validate program parameters, etc. Return True or False (i.e. pass/fail) 87*47f2e4c8SMichael Walsh accordingly. 88*47f2e4c8SMichael Walsh """ 89*47f2e4c8SMichael Walsh 90*47f2e4c8SMichael Walsh gen_post_validation(exit_function, signal_handler) 91*47f2e4c8SMichael Walsh 92*47f2e4c8SMichael Walsh return True 93*47f2e4c8SMichael Walsh 94*47f2e4c8SMichael Walsh 95*47f2e4c8SMichael Walshdef create_http_prefix(host): 96*47f2e4c8SMichael Walsh 97*47f2e4c8SMichael Walsh r""" 98*47f2e4c8SMichael Walsh Create and return an http prefix string. 99*47f2e4c8SMichael Walsh 100*47f2e4c8SMichael Walsh Description of argument(s): 101*47f2e4c8SMichael Walsh host The host being communicated with via the 102*47f2e4c8SMichael Walsh curl command. 103*47f2e4c8SMichael Walsh """ 104*47f2e4c8SMichael Walsh 105*47f2e4c8SMichael Walsh return "https://" + host + "/" 106*47f2e4c8SMichael Walsh 107*47f2e4c8SMichael Walsh 108*47f2e4c8SMichael Walshdef main(): 109*47f2e4c8SMichael Walsh 110*47f2e4c8SMichael Walsh if not gen_get_options(parser, stock_list): 111*47f2e4c8SMichael Walsh return False 112*47f2e4c8SMichael Walsh 113*47f2e4c8SMichael Walsh if not validate_parms(): 114*47f2e4c8SMichael Walsh return False 115*47f2e4c8SMichael Walsh 116*47f2e4c8SMichael Walsh qprint_pgm_header() 117*47f2e4c8SMichael Walsh 118*47f2e4c8SMichael Walsh session = requests.Session() 119*47f2e4c8SMichael Walsh 120*47f2e4c8SMichael Walsh http_prefix = create_http_prefix(openbmc_host) 121*47f2e4c8SMichael Walsh 122*47f2e4c8SMichael Walsh command = http_prefix + 'login' 123*47f2e4c8SMichael Walsh qprint_issuing(command) 124*47f2e4c8SMichael Walsh resp = session.post(command, 125*47f2e4c8SMichael Walsh json={'data': [openbmc_username, openbmc_password]}, 126*47f2e4c8SMichael Walsh verify=False) 127*47f2e4c8SMichael Walsh if resp.json()['status'] != 'ok': 128*47f2e4c8SMichael Walsh json = resp.json() 129*47f2e4c8SMichael Walsh print_error_report("http request failed:\n" + sprint_var(command, 1)) 130*47f2e4c8SMichael Walsh raise Exception("Login failed.\n") 131*47f2e4c8SMichael Walsh 132*47f2e4c8SMichael Walsh command = http_prefix + "xyz/openbmc_project/inventory/system" 133*47f2e4c8SMichael Walsh qprint_issuing(command) 134*47f2e4c8SMichael Walsh resp = session.get(command, verify=False) 135*47f2e4c8SMichael Walsh json = resp.json() 136*47f2e4c8SMichael Walsh if json['status'] != 'ok': 137*47f2e4c8SMichael Walsh print_error_report("http request failed:\n" + sprint_var(command, 1)) 138*47f2e4c8SMichael Walsh raise Exception("http request failed.\n") 139*47f2e4c8SMichael Walsh 140*47f2e4c8SMichael Walsh mch_ser_num = json['data']['SerialNumber'] 141*47f2e4c8SMichael Walsh pvar(mch_ser_num, 0, 0, 0) 142*47f2e4c8SMichael Walsh 143*47f2e4c8SMichael Walsh return True 144*47f2e4c8SMichael Walsh 145*47f2e4c8SMichael Walsh 146*47f2e4c8SMichael Walsh# Main 147*47f2e4c8SMichael Walsh 148*47f2e4c8SMichael Walshif not main(): 149*47f2e4c8SMichael Walsh exit(1) 150