Skip to content

Commit

Permalink
redfish: added lenovo support
Browse files Browse the repository at this point in the history
tested on ThinkSystem SR650 V2 server
  • Loading branch information
MrBr-github committed Mar 11, 2024
1 parent be66f74 commit 2da693b
Showing 1 changed file with 51 additions and 21 deletions.
72 changes: 51 additions & 21 deletions ailib/kfish/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,22 @@ def get_info(url, user, password):
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 'Oem' in v1data:
oem = v1data['Oem']
if 'Dell' in oem:
model = 'dell'
elif 'Hpe' in oem or 'Hp' in oem:
model = 'hp'
elif 'Supermicro' in oem:
model = 'supermicro'
else:
model = 'N/A'
elif 'Vendor' in v1data:
vendor = v1data['Vendor']
if vendor == 'Lenovo':
model = 'lenovo'
if '://' not in url:
if model in ['hp', 'supermicro']:
if model in ['hp', 'supermicro', 'lenovo']:
url = f"https://{url}/redfish/v1/Systems/1"
elif model == 'dell':
url = f"https://{url}/redfish/v1/Systems/System.Embedded.1"
Expand Down Expand Up @@ -118,35 +130,48 @@ def get_iso_status(self):
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']
ret_data = f"{self.baseurl}{t}"
if self.model == 'lenovo':
ret_data = f"{iso_url}"
else:
actions = json.loads(urlopen(request).read())['Actions']
target = '#IsoConfig.UnMount' if self.model == 'supermicro' and self.legacy else '#VirtualMedia.EjectMedia'
t = actions[target]['target']
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']
ret_data = f"{self.baseurl}{t}"
if self.model == 'lenovo':
ret_data = f"{iso_url}"
else:
actions = json.loads(urlopen(request).read())['Actions']
target = '#IsoConfig.Mount' if self.model == 'supermicro' and self.legacy else '#VirtualMedia.InsertMedia'
t = actions[target]['target']
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()
data = json.dumps({}).encode('utf-8')
if self.model == 'supermicro' and self.legacy:
headers['Content-Length'] = 0
# data = {}
eject_url = self.get_iso_eject_url()
if self.debug:
print(f"Sending POST to {eject_url} with empty data")
request = Request(eject_url, headers=headers, method='POST', data=data)
if self.model == 'lenovo':
data = {"Inserted": False}
data = json.dumps(data).encode('utf-8')
request = Request(eject_url, data=data, headers=headers, method='PATCH')
if self.debug:
print(f"Sending PATCH to {eject_url} with data {data}")
else:
data = json.dumps({}).encode('utf-8')
if self.model == 'supermicro' and self.legacy:
headers['Content-Length'] = 0
# data = {}
request = Request(eject_url, headers=headers, method='POST', data=data)
if self.debug:
print(f"Sending POST to {eject_url} with empty data")
return urlopen(request)

def insert_iso(self, iso_url):
Expand All @@ -164,10 +189,15 @@ def insert_iso(self, iso_url):
headers['Content-Length'] = 0
data = {"Image": iso_url, "Inserted": True}
insert_url = self.get_iso_insert_url()
if self.debug:
print(f"Sending POST to {insert_url} with data {data}")
data = json.dumps(data).encode('utf-8')
request = Request(insert_url, data=data, headers=headers)
if self.model == 'lenovo':
request = Request(insert_url, data=data, headers=headers, method='PATCH')
if self.debug:
print(f"Sending PATCH to {insert_url} with data {data}")
else:
request = Request(insert_url, data=data, headers=headers)
if self.debug:
print(f"Sending POST to {insert_url} with data {data}")
return urlopen(request)

def set_iso_once(self):
Expand Down

0 comments on commit 2da693b

Please sign in to comment.