|
| 1 | +import argparse |
| 2 | +import logging |
| 3 | +import os |
| 4 | + |
| 5 | +import sushy |
| 6 | + |
| 7 | +from understack_workflows.bmc_password_standard import standard_password |
| 8 | +from understack_workflows.helpers import credential |
| 9 | +from understack_workflows.helpers import setup_logger |
| 10 | + |
| 11 | +logger = setup_logger(__name__) |
| 12 | + |
| 13 | +logs = [ |
| 14 | + "__main__", |
| 15 | + "sushy.main", |
| 16 | + "sushy.resources.base", |
| 17 | + "sushy.connector", |
| 18 | + "urllib3.connectionpool", |
| 19 | +] |
| 20 | +for log_name in logs: |
| 21 | + log = logging.getLogger(log_name) |
| 22 | + log.setLevel("INFO") |
| 23 | + |
| 24 | + |
| 25 | +def main(): |
| 26 | + """Export RAID details for a BMC using Sushy. |
| 27 | +
|
| 28 | + - connect to the BMC using standard password |
| 29 | +
|
| 30 | + - Using Sushy, gather controller details: |
| 31 | + - controller name |
| 32 | + - list of drive references for raid configuration. |
| 33 | +
|
| 34 | + - output json object response. |
| 35 | + """ |
| 36 | + args = argument_parser().parse_args() |
| 37 | + |
| 38 | + ip_address = args.ip_address |
| 39 | + logger.debug("%s starting for ip_address=%s", __file__, ip_address) |
| 40 | + |
| 41 | + client = client_for_ip_address(ip_address=ip_address) |
| 42 | + |
| 43 | + # argo workflows captures stdout as the results which we can use |
| 44 | + # to return the device UUID |
| 45 | + print(parse_controller_details(client)) |
| 46 | + |
| 47 | + |
| 48 | +def argument_parser(): |
| 49 | + """Parse runtime arguments.""" |
| 50 | + parser = argparse.ArgumentParser( |
| 51 | + prog=os.path.basename(__file__), description="Gather RAID Device info." |
| 52 | + ) |
| 53 | + parser.add_argument("--ip-address", type=str, required=True, help="BMC IP") |
| 54 | + parser.add_argument("--password", type=str, required=False, help="Custom Password") |
| 55 | + return parser |
| 56 | + |
| 57 | + |
| 58 | +def parse_controller_details(client) -> dict: |
| 59 | + """Parse available RAID controller details for execution.""" |
| 60 | + result = {"controller": None, "physical_disks": []} |
| 61 | + system_objects = client.get_system_collection().get_members() |
| 62 | + system = system_objects[0] |
| 63 | + for c in system.storage.get_members(): |
| 64 | + if "RAID" in c.identity: |
| 65 | + result["controller"] = c.identity |
| 66 | + for d in c.drives: |
| 67 | + result["physical_disks"].append(d.identity) |
| 68 | + break |
| 69 | + return result |
| 70 | + |
| 71 | + |
| 72 | +def client_for_ip_address( |
| 73 | + ip_address: str, username: str = "root", password: str | None = None |
| 74 | +) -> sushy.Sushy: |
| 75 | + """Retreive a Sushy session for BMC return as client object. |
| 76 | +
|
| 77 | + If no password is supplied then we use a conventional BMC standard one |
| 78 | + which is derived from the IP address and the BMC_MASTER secret key. |
| 79 | +
|
| 80 | + If no username is supplied then the username "root" is used. |
| 81 | + """ |
| 82 | + if password is None: |
| 83 | + bmc_master = os.getenv("BMC_MASTER") or credential("bmc_master", "key") |
| 84 | + password = standard_password(ip_address, bmc_master) |
| 85 | + |
| 86 | + base_url = "https://" + ip_address |
| 87 | + client = sushy.Sushy( |
| 88 | + base_url=base_url, username=username, password=password, verify=False |
| 89 | + ) |
| 90 | + return client |
| 91 | + |
| 92 | + |
| 93 | +if __name__ == "__main__": |
| 94 | + main() |
0 commit comments