Skip to content

Commit

Permalink
Merge pull request #51 from MrBr-github/main
Browse files Browse the repository at this point in the history
Enhanced debug output of redfish
  • Loading branch information
karmab committed Mar 11, 2024
2 parents 0aaede0 + be66f74 commit b149d9c
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 13 deletions.
4 changes: 3 additions & 1 deletion ailib/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
from urllib.parse import urlparse
from urllib.request import urlretrieve
import yaml

import traceback

default_cluster_params = {"openshift_version": "4.15", "base_dns_domain": "karmalabs.corp",
"vip_dhcp_allocation": False}
Expand Down Expand Up @@ -68,6 +68,8 @@ def boot_hosts(overrides, hostnames=[], debug=False):
red.set_iso(iso_url)
except Exception as e:
warning(f"Hit {e} when plugging iso to host {msg}")
if debug:
traceback.print_exception(e)
return 1
else:
warning(f"Skipping entry {index} because either bmc_url, bmc_user or bmc_password is not set")
Expand Down
54 changes: 42 additions & 12 deletions ailib/kfish/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import re
import sys
from uuid import UUID
import traceback


def valid_uuid(uuid):
Expand All @@ -28,8 +29,13 @@ def get_info(url, user, password):
oem_url = f"https://{url}" if '://' not in url else url
p = urlparse(oem_url)
headers = {'Accept': 'application/json'}
request = Request(f"https://{p.netloc}/redfish/v1", headers=headers)
v1data = json.loads(urlopen(request).read())
request_url = f"https://{p.netloc}/redfish/v1"
request = Request(request_url, headers=headers)
try:
v1data = json.loads(urlopen(request).read())
except Exception as e:
print(f"Error: tried accessing URL {request_url}")
raise e
oem = v1data['Oem']
model = "dell" if 'Dell' in oem else "hp" if 'Hpe' in oem else 'supermicro' if 'Supermicro' in oem else 'N/A'
if '://' not in url:
Expand All @@ -40,7 +46,8 @@ def get_info(url, user, password):
user = user or 'root'
password = password or 'calvin'
else:
print(f"Invalid url {url}")
print(f"Failed to autodetect redfish URL for '{url}'")
print(f"JSON query to {request_url} returned '{model}' as ['Oem'] value")
sys.exit(1)
return model, url, user, password

Expand Down Expand Up @@ -69,7 +76,10 @@ def __init__(self, url, user='root', password='calvin', insecure=True, debug=Fal
def get_manager_url(self):
request = Request(self.url, headers=self.headers)
response = json.loads(urlopen(request).read())
return f"{self.baseurl}{response['Links']['ManagedBy'][0]['@odata.id']}"
ret_data = f"{self.baseurl}{response['Links']['ManagedBy'][0]['@odata.id']}"
if self.debug:
print(f"Manager URL is {ret_data}")
return ret_data

def get_iso_url(self):
manager_url = self.get_manager_url()
Expand All @@ -88,31 +98,43 @@ def get_iso_url(self):
odata = member['@odata.id']
if odata.endswith('CD') or odata.endswith('Cd') or odata.endswith('2'):
break
return f'{self.baseurl}{odata}'
ret_data = f'{self.baseurl}{odata}'
if self.debug:
print(f"ISO URL is {ret_data}")
return ret_data

def get_iso_status(self):
iso_url = self.get_iso_url()
if self.debug:
print(f"Getting {iso_url}")
request = Request(iso_url, headers=self.headers)
response = json.loads(urlopen(request).read())
return f"{response['Image']}"
ret_data = f"{response['Image']}"
if self.debug:
print(f"ISO status is {ret_data}")
return ret_data

def get_iso_eject_url(self):
iso_url = self.get_iso_url()
request = Request(iso_url, headers=self.headers)
actions = json.loads(urlopen(request).read())['Actions']
target = '#IsoConfig.UnMount' if self.model == 'supermicro' and self.legacy else '#VirtualMedia.EjectMedia'
t = actions[target]['target']
return f"{self.baseurl}{t}"
ret_data = f"{self.baseurl}{t}"
if self.debug:
print(f"ISO eject URL is {ret_data}")
return ret_data

def get_iso_insert_url(self):
iso_url = self.get_iso_url()
request = Request(iso_url, headers=self.headers)
actions = json.loads(urlopen(request).read())['Actions']
target = '#IsoConfig.Mount' if self.model == 'supermicro' and self.legacy else '#VirtualMedia.InsertMedia'
t = actions[target]['target']
return f"{self.baseurl}{t}"
ret_data = f"{self.baseurl}{t}"
if self.debug:
print(f"ISO insert URL is {ret_data}")
return ret_data

def eject_iso(self):
headers = self.headers.copy()
Expand Down Expand Up @@ -215,17 +237,25 @@ def reset(self):
return urlopen(request)

def set_iso(self, iso_url):
result = None
try:
self.eject_iso()
except:
pass
result = self.insert_iso(iso_url)
except Exception as e:
if self.debug:
traceback.print_exception(e)
try:
result = self.insert_iso(iso_url)
except Exception as e:
if self.debug:
traceback.print_exception(e)
if result.code not in [200, 202, 204]:
print(f"Hit {result.reason} When plugging {iso_url}")
sys.exit(1)
try:
self.set_iso_once()
except:
except Exception as e:
if self.debug:
traceback.print_exception(e)
self.set_iso_once()
self.restart()

Expand Down

0 comments on commit b149d9c

Please sign in to comment.