apply ruff
This commit is contained in:
@@ -28,7 +28,9 @@ class HostConfig:
|
||||
# New attributes for Redfish stuff
|
||||
vendor: 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:
|
||||
"""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:
|
||||
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:
|
||||
data = await resp.json()
|
||||
host.vendor = data.get("Vendor", "")
|
||||
logging.debug("Detected vendor for %s: %s", host.fqdn, host.vendor)
|
||||
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:
|
||||
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:
|
||||
# Need to login and store new session token
|
||||
# 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}
|
||||
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:
|
||||
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:
|
||||
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:
|
||||
raise RuntimeError("No Location in login response")
|
||||
headers["X-Auth-Token"] = host.session_token
|
||||
logging.info("New session token obtained for %s", host.fqdn)
|
||||
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
|
||||
|
||||
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:
|
||||
host.mark_success()
|
||||
return await resp.json()
|
||||
elif resp.status in (401, 403):
|
||||
# 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
|
||||
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:
|
||||
# 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)
|
||||
|
||||
|
||||
async def logout_host(session, host):
|
||||
"""Clean logout for Redfish with session tokens"""
|
||||
if not host.session_token:
|
||||
@@ -242,7 +267,9 @@ async def logout_host(session, host):
|
||||
if resp.status in (200, 204):
|
||||
logging.info("Logged out from %s", host.fqdn)
|
||||
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:
|
||||
logging.warning("Error during logout for %s: %s", host.fqdn, e)
|
||||
finally:
|
||||
|
||||
Reference in New Issue
Block a user