def give_client(): try: # Create a Redfish client object rf_obj = RedfishClient(base_url=SYSTEM_URL, username=LOGIN_ACCOUNT, \ password=LOGIN_PASSWORD) # Login with the Redfish client rf_obj.login() except ServerDownOrUnreachableError as excp: sys.stderr.write("ERROR: server not reachable or does not support RedFish.\n") sys.exit() return rf_obj
# account name, and password to send https requests # SYSTEM_URL acceptable examples: # "https://10.0.0.100" # "https://ilo.hostname" SYSTEM_URL = "https://10.0.0.100" LOGIN_ACCOUNT = "admin" LOGIN_PASSWORD = "******" ESKM_USERNAME = "******" ESKM_PASSWORD = "******" ESKM_ACCOUNTGROUP = "group" ESKM_PRIMARYKEYSERVER_ADDR = "192.168.1.10" ESKM_PRIMARYKEYSERVER_PORT = 5927 # flag to force disable resource directory. Resource directory and associated operations are # intended for HPE servers. DISABLE_RESOURCE_DIR = False try: # Create a Redfish client object REDFISHOBJ = RedfishClient(base_url=SYSTEM_URL, username=LOGIN_ACCOUNT, \ password=LOGIN_PASSWORD) # Login with the Redfish client REDFISHOBJ.login() except ServerDownOrUnreachableError as excp: sys.stderr.write("ERROR: server not reachable or does not support RedFish.\n") sys.exit() ESKM_username_pass(REDFISHOBJ, ESKM_USERNAME, ESKM_PASSWORD, ESKM_ACCOUNTGROUP, \ ESKM_PRIMARYKEYSERVER_ADDR, ESKM_PRIMARYKEYSERVER_PORT) REDFISHOBJ.logout()
class RedfishObject(object): def __init__(self, host, login_account, login_password): """ This function is to create redfish object Arguments: host {string} -- host url login_account {string} -- host username login_password {string} -- host password """ # Creating instace for RedfishClient clasee by providing ilo_url, ilo_username, ilo_password # Note:redfish.ris.tpdefs is not supported by python-ilorest-library 3.0 try: self.redfish_client = RedfishClient(base_url=host, username=login_account, password=login_password) except: raise self.redfish_client.login(auth=AuthMethod.SESSION) self.SYSTEMS_RESOURCES = self.ex1_get_resource_directory() self.MESSAGE_REGISTRIES = self.ex2_get_base_registry() def delete_obj(self): """ This function is to logout """ try: self.redfish_client.logout() except AttributeError as excp: pass def search_for_type(self, type): """ This function is to get redifsh resources """ instances = [] for item in self.SYSTEMS_RESOURCES["resources"]: foundsettings = False if "@odata.type" in item and type.lower( ) in item["@odata.type"].lower(): for entry in self.SYSTEMS_RESOURCES["resources"]: if (item["@odata.id"] + "/settings/").lower() == ( entry["@odata.id"]).lower(): foundsettings = True if not foundsettings: instances.append(item) if not instances: log_info("Resource or feature is not supported on this system:" + type) return instances def error_handler(self, response): """ This function is to display extnded info of error message Arguments: response {dict}: response of a rest call on specified host """ if not self.MESSAGE_REGISTRIES: log_info("ERROR: No message registries found.") try: message = json.loads(response.text) newmessage = message["error"]["@Message.ExtendedInfo"][0][ "MessageId"].split(".") except: log_info("No extended error information returned by iLO.") return for err_mesg in self.MESSAGE_REGISTRIES: if err_mesg != newmessage[0]: continue else: for err_entry in self.MESSAGE_REGISTRIES[err_mesg]: if err_entry == newmessage[3]: log_info( "iLO return code " + str(message["error"]["@Message.ExtendedInfo"][0]) + " : " + str(self.MESSAGE_REGISTRIES[err_mesg] [err_entry]["Description"])) def redfish_get(self, suburi): """ This function is to get redfish resources based on the specified uri Arguments: suburi {string}: redfish uri for the specified host """ return self.redfish_client.get(path=suburi) def redfish_patch(self, suburi, request_body): """ REDFISH PATCH Arguments: suburi {string}: redfish uri for the specified host request_body {dict}: resource content """ log_debug("PATCH " + str(request_body) + " to " + suburi) response = self.redfish_client.patch(path=suburi, body=request_body) log_debug("PATCH response: " + str(response.status)) return response def redfish_put(self, suburi, request_body): """ REDFISH PUT Arguments: suburi {string}: redfish uri for the specified host request_body {dict}: resource content """ log_debug("PUT " + str(request_body) + " to " + suburi) response = self.redfish_client.put(path=suburi, body=request_body) log_info("PUT response: " + str(response.status)) return response def redfish_post(self, suburi, request_body): """ REDFISH POST Arguments: suburi {string}: redfish uri for the specified host request_body {dict}: resource content """ log_info("POST " + str(request_body) + " to " + suburi) response = self.redfish_client.post(path=suburi, body=request_body) log_info("POST response: " + str(response.status)) return response def redfish_delete(self, suburi): """ REDFISH DELETE Arguments: suburi {string}: redfish uri for the specified host request_body {dict}: resource content """ log_info("DELETE " + suburi) response = self.redfish_client.delete(path=suburi) log_info("DELETE response: " + str(response.status)) return response def ex1_get_resource_directory(self): """ This function is to get resources of resource directory """ response = self.redfish_get("/redfish/v1/resourcedirectory/") resources = {} if response.status == 200: resources["resources"] = response.dict["Instances"] return resources else: log_info("\tResource directory missing at " \ "/redfish/v1/resourcedirectory" + "\n") def ex2_get_base_registry(self): """ This function is to get Registries resources """ response = self.redfish_get("/redfish/v1/Registries/") messages = {} location = None for entry in response.dict["Members"]: if not [x for x in ["/Base/", "/iLO/"] if x in entry["@odata.id"]]: continue else: registry = self.redfish_get(entry["@odata.id"]) for location in registry.dict["Location"]: if "extref" in location["Uri"]: location = location["Uri"]["extref"] else: location = location["Uri"] reg_resp = self.redfish_get(location) if reg_resp.status == 200: messages[reg_resp.dict["RegistryPrefix"]] = \ reg_resp.dict["Messages"] else: log_info("\t" + reg_resp.dict["RegistryPrefix"] + \ " not found at " + location + "\n") return messages
# Upload the firmware file to the iLO Repository update_repo_flag = True # Update the system with the firmware file update_target_flag = False comp_type = 'C' tpm_flag = True # flag to force disable resource directory. Resource directory and associated operations are # intended for HPE servers. DISABLE_RESOURCE_DIR = True try: # Create a Redfish client object redfish_obj = RedfishClient(base_url=system_url, username=options.ilo_user, password=options.ilo_pass) # Login with the Redfish client redfish_obj.login() except ServerDownOrUnreachableError as excp: sys.stderr.write( "ERROR: server not reachable or does not support RedFish.\n") sys.exit() upload_firmware(redfish_obj, options.comp_path, comp_type, update_repo_flag, update_target_flag) if comp_type == 'C': create_task(redfish_obj, options.comp_path, tpm_flag) redfish_obj.logout()
#LOGIN_PASSWORD = None # When running remotely connect using the secured (https://) address, # account name, and password to send https requests # SYSTEM_URL acceptable examples: # "https://10.0.0.100" # "https://ilo.hostname" SYSTEM_URL = "https://10.0.0.100" LOGIN_ACCOUNT = "admin" LOGIN_PASSWORD = "******" # Create a REDFISH object try: REDFISH_OBJ = RedfishClient(base_url=SYSTEM_URL, username=LOGIN_ACCOUNT, \ password=LOGIN_PASSWORD) REDFISH_OBJ.login() except ServerDownOrUnreachableError: sys.stderr.write( "ERROR: server not reachable or doesn't support Redfish.\n") sys.exit() resources = get_resource_directory(REDFISH_OBJ) for resource in resources: try: sys.stdout.write("\t" + str(resource["@odata.type"]) + \ "\n\t\t" + str(resource["@odata.id"]) + "\n") except KeyError: pass REDFISH_OBJ.logout()