Skip to content

Commit

Permalink
Linting -- no true changes
Browse files Browse the repository at this point in the history
  • Loading branch information
mharding-hpe committed Dec 17, 2024
1 parent a595f44 commit cba7c55
Show file tree
Hide file tree
Showing 52 changed files with 933 additions and 568 deletions.
9 changes: 6 additions & 3 deletions src/bos/common/options.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@
from abc import ABC, abstractmethod
from typing import Any


# This is the source of truth for default option values. All other BOS
# code should either import this dict directly, or (preferably) access
# its values indirectly using a DefaultOptions object
Expand All @@ -48,6 +47,7 @@
'session_limit_required': False
}


class BaseOptions(ABC):
"""
Abstract base class for getting BOS option values
Expand Down Expand Up @@ -136,6 +136,7 @@ class DefaultOptions(BaseOptions):
"""
Returns the default value for each option
"""

def get_option(self, key: str) -> Any:
if key in DEFAULTS:
return DEFAULTS[key]
Expand All @@ -149,7 +150,8 @@ class OptionsCache(DefaultOptions, ABC):
This caches the options so that frequent use of these options do not all
result in network/DB calls.
"""
def __init__(self, update_on_create:bool=True):

def __init__(self, update_on_create: bool = True):
super().__init__()
if update_on_create:
self.update()
Expand All @@ -170,4 +172,5 @@ def get_option(self, key: str) -> Any:
try:
return super().get_option(key)
except KeyError as err:
raise KeyError(f'Option {key} not found and no default exists') from err
raise KeyError(
f'Option {key} not found and no default exists') from err
29 changes: 18 additions & 11 deletions src/bos/common/tenant_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,11 @@
LOGGER = logging.getLogger(__name__)

TENANT_HEADER = "Cray-Tenant-Name"
SERVICE_NAME = 'cray-tapms/v1alpha3' # CASMCMS-9125: Currently when TAPMS bumps this version, it
# breaks backwards compatiblity, so BOS needs to update this
# whenever TAPMS does.
SERVICE_NAME = 'cray-tapms/v1alpha3' # CASMCMS-9125: Currently when TAPMS bumps this version, it
# breaks backwards compatiblity, so BOS needs to update this
# whenever TAPMS does.
BASE_ENDPOINT = f"{PROTOCOL}://{SERVICE_NAME}"
TENANT_ENDPOINT = f"{BASE_ENDPOINT}/tenants" # CASMPET-6433 changed this from tenant to tenants
TENANT_ENDPOINT = f"{BASE_ENDPOINT}/tenants" # CASMPET-6433 changed this from tenant to tenants


class InvalidTenantException(Exception):
Expand Down Expand Up @@ -81,9 +81,11 @@ def get_tenant_data(tenant, session=None):
try:
response.raise_for_status()
except HTTPError as e:
LOGGER.error("Failed getting tenant data from tapms: %s", exc_type_msg(e))
LOGGER.error("Failed getting tenant data from tapms: %s",
exc_type_msg(e))
if response.status_code == 404:
raise InvalidTenantException(f"Data not found for tenant {tenant}") from e
raise InvalidTenantException(
f"Data not found for tenant {tenant}") from e
raise
return response.json()

Expand All @@ -93,7 +95,7 @@ def get_tenant_component_set(tenant: str) -> set:
data = get_tenant_data(tenant)
status = data.get("status", {})
for resource in status.get("tenantresources", []):
components.append(resource.get("xnames",[]))
components.append(resource.get("xnames", []))
return set().union(*components)


Expand All @@ -107,27 +109,32 @@ def validate_tenant_exists(tenant: str) -> bool:

def tenant_error_handler(func):
"""Decorator for returning errors if there is an exception when calling tapms"""

@functools.wraps(func)
def wrapper(*args, **kwargs):
try:
return func(*args, **kwargs)
except InvalidTenantException as e:
LOGGER.debug("Invalid tenant: %s", exc_type_msg(e))
return connexion.problem(
status=400, title='Invalid tenant',
detail=str(e))
return connexion.problem(status=400,
title='Invalid tenant',
detail=str(e))

return wrapper


def reject_invalid_tenant(func):
"""Decorator for preemptively validating the tenant exists"""

@functools.wraps(func)
def wrapper(*args, **kwargs):
tenant = get_tenant_from_header()
if tenant and not validate_tenant_exists(tenant):
LOGGER.debug("The provided tenant does not exist")
return connexion.problem(
status=400, title="Invalid tenant",
status=400,
title="Invalid tenant",
detail=str("The provided tenant does not exist"))
return func(*args, **kwargs)

return wrapper
41 changes: 28 additions & 13 deletions src/bos/common/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,8 @@
from requests_retry_session import requests_retry_session as base_requests_retry_session

PROTOCOL = 'http'
TIME_DURATION_PATTERN = re.compile(r"^(\d+?)(\D+?)$", re.M|re.S)
TIME_DURATION_PATTERN = re.compile(r"^(\d+?)(\D+?)$", re.M | re.S)


# Common date and timestamps functions so that timezones and formats are handled consistently.
def get_current_time() -> datetime.datetime:
Expand All @@ -54,21 +55,28 @@ def duration_to_timedelta(timestamp: str):
Converts a <digit><duration string> to a timedelta object.
"""
# Calculate the corresponding multiplier for each time value
seconds_table = {'s': 1,
'm': 60,
'h': 60*60,
'd': 60*60*24,
'w': 60*60*24*7}
seconds_table = {
's': 1,
'm': 60,
'h': 60 * 60,
'd': 60 * 60 * 24,
'w': 60 * 60 * 24 * 7
}
timeval, durationval = TIME_DURATION_PATTERN.search(timestamp).groups()
timeval = float(timeval)
seconds = timeval * seconds_table[durationval]
return datetime.timedelta(seconds=seconds)


requests_retry_session = partial(base_requests_retry_session,
retries=10, backoff_factor=0.5,
retries=10,
backoff_factor=0.5,
status_forcelist=(500, 502, 503, 504),
connect_timeout=3, read_timeout=10,
session=None, protocol=PROTOCOL)
connect_timeout=3,
read_timeout=10,
session=None,
protocol=PROTOCOL)


def compact_response_text(response_text: str) -> str:
"""
Expand All @@ -77,7 +85,7 @@ def compact_response_text(response_text: str) -> str:
trailing whitespace from each line, and then returns it.
"""
if response_text:
return ' '.join([ line.strip() for line in response_text.split('\n') ])
return ' '.join([line.strip() for line in response_text.split('\n')])
return str(response_text)


Expand All @@ -88,14 +96,16 @@ def exc_type_msg(exc: Exception) -> str:
"""
return ''.join(traceback.format_exception_only(type(exc), exc))


def get_image_id(component: str) -> str:
"""
Extract the IMS image ID from the path to the kernel
We expect it to look something like this:
s3://boot-images/fbcc5b02-b6a4-46a8-9402-2b7138adc327/kernel
"""
# Get kernel's path
boot_artifacts = component.get('desired_state', {}).get('boot_artifacts', {})
boot_artifacts = component.get('desired_state',
{}).get('boot_artifacts', {})
kernel = boot_artifacts.get('kernel')
image_id = get_image_id_from_kernel(kernel)
return image_id
Expand All @@ -108,6 +118,7 @@ def get_image_id_from_kernel(kernel_path: str) -> str:
image_id = match.group(1)
return image_id


def using_sbps(component: str) -> bool:
"""
If the component is using the Scalable Boot Provisioning Service (SBPS) to
Expand All @@ -120,10 +131,12 @@ def using_sbps(component: str) -> bool:
Return True if it is and False if it is not.
"""
# Get the kernel boot parameters
boot_artifacts = component.get('desired_state', {}).get('boot_artifacts', {})
boot_artifacts = component.get('desired_state',
{}).get('boot_artifacts', {})
kernel_parameters = boot_artifacts.get('kernel_parameters')
return using_sbps_check_kernel_parameters(kernel_parameters)


def using_sbps_check_kernel_parameters(kernel_parameters: str) -> bool:
"""
Check the kernel boot parameters to see if the image is using the
Expand All @@ -137,6 +150,7 @@ def using_sbps_check_kernel_parameters(kernel_parameters: str) -> bool:
# Check for the 'root=sbps-s3' string.
return "root=sbps-s3" in kernel_parameters


def components_by_id(components: List[dict]) -> dict:
"""
Input:
Expand All @@ -148,7 +162,8 @@ def components_by_id(components: List[dict]) -> dict:
Purpose: It makes searching more efficient because you can
index by component name.
"""
return { component["id"]: component for component in components }
return {component["id"]: component for component in components}


def reverse_components_by_id(components_by_id_map: dict) -> List[dict]:
"""
Expand Down
13 changes: 4 additions & 9 deletions src/bos/common/values.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ class Phase:
configuring = "configuring"
none = ""


# Actions
class Action:
power_on = "powering_on"
Expand All @@ -37,6 +38,7 @@ class Action:
session_setup = "session_setup"
newly_discovered = "newly_discovered"


# Status
class Status:
power_on_pending = "power_on_pending"
Expand All @@ -50,16 +52,9 @@ class Status:
on_hold = "on_hold"


EMPTY_BOOT_ARTIFACTS = {
"kernel": "",
"kernel_parameters": "",
"initrd": ""
}
EMPTY_BOOT_ARTIFACTS = {"kernel": "", "kernel_parameters": "", "initrd": ""}

EMPTY_ACTUAL_STATE = {
"boot_artifacts": EMPTY_BOOT_ARTIFACTS,
"bss_token": ""
}
EMPTY_ACTUAL_STATE = {"boot_artifacts": EMPTY_BOOT_ARTIFACTS, "bss_token": ""}

EMPTY_DESIRED_STATE = {
"configuration": "",
Expand Down
11 changes: 6 additions & 5 deletions src/bos/operators/actual_state_cleanup.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,16 +55,17 @@ def filters(self):
return [
BOSQuery(),
ActualBootStateIsSet(),
ActualStateAge(
seconds=duration_to_timedelta(options.component_actual_state_ttl).total_seconds()
)
ActualStateAge(seconds=duration_to_timedelta(
options.component_actual_state_ttl).total_seconds())
]

def _act(self, components):
data = []
for component_id in [component['id'] for component in components]:
data.append({'id': component_id,
'actual_state': EMPTY_ACTUAL_STATE})
data.append({
'id': component_id,
'actual_state': EMPTY_ACTUAL_STATE
})
if data:
LOGGER.info('Found %d components that require updates', len(data))
LOGGER.debug('Calling to update with payload: %s', data)
Expand Down
Loading

0 comments on commit cba7c55

Please sign in to comment.