apply ruff

This commit is contained in:
2026-01-21 08:35:27 +01:00
parent 846ce706b6
commit b1e5369a33
2 changed files with 43 additions and 16 deletions

View File

@@ -28,7 +28,9 @@ class HostConfig:
# New attributes for Redfish stuff # New attributes for Redfish stuff
vendor: str | None = None vendor: str | None = None
session_token: str | None = None session_token: str | None = None
session_logout: str | None = None # SessionLocation like /redfish/v1/SessionService/Sessions/marco.lucarelli%40abacus.ch00000000xxx/ session_logout: str | None = (
None # SessionLocation like /redfish/v1/SessionService/Sessions/marco.lucarelli%40abacus.ch00000000xxx/
)
def should_skip(self) -> bool: def should_skip(self) -> bool:
"""Check if host is still in cool-down window""" """Check if host is still in cool-down window"""
@@ -93,13 +95,17 @@ async def fetch_with_retry(session, host: HostConfig, url: str) -> dict | None:
if not host.vendor: if not host.vendor:
try: try:
async with session.get(f"https://{host.fqdn}/redfish/v1/", ssl=False, timeout=10) as resp: async with session.get(
f"https://{host.fqdn}/redfish/v1/", ssl=False, timeout=10
) as resp:
if resp.status == 200: if resp.status == 200:
data = await resp.json() data = await resp.json()
host.vendor = data.get("Vendor", "") host.vendor = data.get("Vendor", "")
logging.debug("Detected vendor for %s: %s", host.fqdn, host.vendor) logging.debug("Detected vendor for %s: %s", host.fqdn, host.vendor)
else: else:
logging.warning("Vendor probe failed on %s: HTTP %s", host.fqdn, resp.status) logging.warning(
"Vendor probe failed on %s: HTTP %s", host.fqdn, resp.status
)
except Exception as e: except Exception as e:
logging.warning("Vendor probe failed for %s: %s", host.fqdn, e) logging.warning("Vendor probe failed for %s: %s", host.fqdn, e)
@@ -117,32 +123,50 @@ async def fetch_with_retry(session, host: HostConfig, url: str) -> dict | None:
else: else:
# Need to login and store new session token # Need to login and store new session token
# HPE Redfish login # HPE Redfish login
login_url = f"https://{host.fqdn}/redfish/v1/SessionService/Sessions" login_url = (
f"https://{host.fqdn}/redfish/v1/SessionService/Sessions"
)
payload = {"UserName": host.username, "Password": host.password} payload = {"UserName": host.username, "Password": host.password}
async with session.post(login_url, json=payload, ssl=False, timeout=10) as login_resp: async with session.post(
login_url, json=payload, ssl=False, timeout=10
) as login_resp:
if login_resp.status == 201: if login_resp.status == 201:
host.session_token = login_resp.headers.get("X-Auth-Token") # as response in header host.session_token = login_resp.headers.get(
"X-Auth-Token"
) # as response in header
if not host.session_token: if not host.session_token:
raise RuntimeError("No X-Auth-Token in login response") raise RuntimeError("No X-Auth-Token in login response")
host.session_logout = login_resp.headers.get("Location") # as response in header host.session_logout = login_resp.headers.get(
"Location"
) # as response in header
if not host.session_logout: if not host.session_logout:
raise RuntimeError("No Location in login response") raise RuntimeError("No Location in login response")
headers["X-Auth-Token"] = host.session_token headers["X-Auth-Token"] = host.session_token
logging.info("New session token obtained for %s", host.fqdn) logging.info("New session token obtained for %s", host.fqdn)
else: else:
logging.warning("Login failed for %s: HTTP %s", host.fqdn, login_resp.status) logging.warning(
"Login failed for %s: HTTP %s",
host.fqdn,
login_resp.status,
)
continue # retry login next attempt continue # retry login next attempt
async with session.get(url, headers=headers, ssl=False, timeout=10) as resp: async with session.get(
url, headers=headers, ssl=False, timeout=10
) as resp:
if resp.status == 200: if resp.status == 200:
host.mark_success() host.mark_success()
return await resp.json() return await resp.json()
elif resp.status in (401, 403): elif resp.status in (401, 403):
# Token expired or invalid, clear it and retry # Token expired or invalid, clear it and retry
logging.warning("Invalid token for %s, reauthenticating...", host.fqdn) logging.warning(
"Invalid token for %s, reauthenticating...", host.fqdn
)
host.session_token = None host.session_token = None
continue continue
logging.warning("HTTP %s from %s (attempt %d)", resp.status, host.fqdn, attempt) logging.warning(
"HTTP %s from %s (attempt %d)", resp.status, host.fqdn, attempt
)
else: else:
# Default: BasicAuth, like Supermicro and so # Default: BasicAuth, like Supermicro and so
@@ -225,6 +249,7 @@ async def get_power_data(session, host: HostConfig):
REQUEST_LATENCY.labels(host=host.fqdn).observe(time.monotonic() - start) REQUEST_LATENCY.labels(host=host.fqdn).observe(time.monotonic() - start)
async def logout_host(session, host): async def logout_host(session, host):
"""Clean logout for Redfish with session tokens""" """Clean logout for Redfish with session tokens"""
if not host.session_token: if not host.session_token:
@@ -242,7 +267,9 @@ async def logout_host(session, host):
if resp.status in (200, 204): if resp.status in (200, 204):
logging.info("Logged out from %s", host.fqdn) logging.info("Logged out from %s", host.fqdn)
else: else:
logging.warning("Logout failed for %s (HTTP %s)", host.fqdn, resp.status) logging.warning(
"Logout failed for %s (HTTP %s)", host.fqdn, resp.status
)
except Exception as e: except Exception as e:
logging.warning("Error during logout for %s: %s", host.fqdn, e) logging.warning("Error during logout for %s: %s", host.fqdn, e)
finally: finally: