Skip to content

Commit

Permalink
Ruff adjustments
Browse files Browse the repository at this point in the history
  • Loading branch information
rustydb committed Aug 15, 2023
1 parent ebf8609 commit 81d0f0a
Show file tree
Hide file tree
Showing 40 changed files with 284 additions and 81 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -56,3 +56,4 @@ venv/
ENV/
env.bak/
venv.bak/
.ruff_cache/
2 changes: 1 addition & 1 deletion GNUmakefile
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ help:
@echo ''

clean:
rm -rf build dist
rm -rf build dist .ruff_cache

#############################################################################
# RPM targets
Expand Down
1 change: 1 addition & 0 deletions libcsm/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
"""libCSM."""
11 changes: 6 additions & 5 deletions libcsm/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,18 +37,17 @@


class AuthException(Exception):

"""
An exception for authorization problems from the api.Auth object.
"""

def __init__(self, message) -> None:
"""Initialize an AuthException class."""
self.message = message
super().__init__(self.message)


class Auth:

"""
Class for handling CSM API credentials stored in Kubernetes secrets.
Expand All @@ -58,13 +57,15 @@ class Auth:
"""

def __init__(self, **kwargs) -> None:
"""Initialize an Auth class."""
config.load_kube_config(**kwargs)
self.core = client.CoreV1Api()
self._token = None

def _get_secret(self) -> dict:
"""
Fetches the Kubernetes SECRET for admin authentication.
Fetch the Kubernetes SECRET for admin authentication.
:return: The data dict from the resolved V1Secret.
"""
try:
Expand All @@ -79,7 +80,7 @@ def _get_secret(self) -> dict:

def refresh_token(self) -> None:
"""
Refreshes the authentication token.
Refresh the authentication token.
"""
del self.token
credentials = self._get_secret()
Expand Down Expand Up @@ -111,6 +112,6 @@ def token(self) -> str:
@token.deleter
def token(self) -> None:
"""
Handles deleting the authentication token.
Handle deleting the authentication token.
"""
self._token = None
1 change: 1 addition & 0 deletions libcsm/bss/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
"""libCSM BSS."""
4 changes: 1 addition & 3 deletions libcsm/bss/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@


class API:

"""
Class for providing API to interact with BSS.
"""
Expand Down Expand Up @@ -85,13 +84,12 @@ def patch_bss_bootparams(self, xname : str, bss_json) -> None:
print('BSS entry patched')

def set_bss_image(self, xname: str, image_dict: dict) -> None:

"""
Set the images in BSS for a specific xname.
The inputs are the node's xname and a dictionary containing initrd, kernel, and roofs
image paths that will be set in BSS.
"""

if 'initrd' not in image_dict or 'kernel' not in image_dict or 'rootfs' not in image_dict:
raise ValueError(f"ERROR set_bss_image has inputs 'xname' and 'image_dictonary' where" \
f"'image_dictionary' is a dictionary containing values for 'initrd', 'kernel', " \
Expand Down
2 changes: 0 additions & 2 deletions libcsm/bss/set_image.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,7 @@
@click.option('--endpoint-url', required=False, type=str, default='http://rgw-vip',
help='Address of the Rados-gateway endpoint.')
def main(**kwargs) -> None:

"""Set the kernel, rootfs, and initrd images in BSS for specified node(s) given an image-id."""

hsm_role_subrole = kwargs['hsm_role_subrole']
xnames = kwargs['xnames']
bucket = kwargs['bucket']
Expand Down
22 changes: 22 additions & 0 deletions libcsm/hsm/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# MIT License
#
# (C) Copyright 2023 Hewlett Packard Enterprise Development LP
#
# Permission is hereby granted, free of charge, to any person obtaining a
# copy of this software and associated documentation files (the "Software"),
# to deal in the Software without restriction, including without limitation
# the rights to use, copy, modify, merge, publish, distribute, sublicense,
# and/or sell copies of the Software, and to permit persons to whom the
# Software is furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included
# in all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
# THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
# OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
# ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
# OTHER DEALINGS IN THE SOFTWARE.
"""libCSM HSM."""
3 changes: 2 additions & 1 deletion libcsm/hsm/components.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,11 @@

ROLE_SUBROLES = ["Management_Master", "Management_Worker", "Management_Storage"]


def get_components(role_subrole: str, api_gateway_address="api-gw-service-nmn.local") \
-> requests.Response:
"""
Function to get management components from HSM based on their role and subrole.
Get management components from HSM based on their role and subrole.
"""
auth = api.Auth()
auth.refresh_token()
Expand Down
8 changes: 6 additions & 2 deletions libcsm/hsm/xnames.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,15 +27,19 @@
from typing import List
from libcsm.hsm import components


def get_by_role_subrole(role_subrole: str) -> List[str]:
"""
Function to get xnames by subrole from HSM.
Get xnames by subrole from HSM.
"""
components_response = components.get_components(role_subrole)
xnames = []
if components_response is not None:
for component in components_response.json()['Components']:
xnames.append(component['ID'])
else:
print(f'ERROR no components were found with hsm_role_subrole: {role_subrole}')
print(
f'ERROR no components were found with hsm_role_subrole: '
f'{role_subrole}'
)
return xnames
6 changes: 5 additions & 1 deletion libcsm/logger.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@

class Logger(logging.Logger):
"""
Custom logger.
Inherits from logging.Logger, configuring custom settings on
initialization.
"""
Expand All @@ -44,6 +46,8 @@ def __init__(
log_level: int = logging.INFO
) -> None:
"""
Customize the ``Logger`` object.
:param module_name: Pass __name__ here or whatever name you want to
define the Logger as.
:param log_level: Level of logging (default: INFO).
Expand All @@ -60,7 +64,7 @@ def __init__(
print(
f'Failed to make {os.path.dirname(LOG_PATH)}\n{error}\n'
f'writing logs to present working directory.'
)
)
handler = RotatingFileHandler(os.path.basename(LOG_PATH))
handler.setFormatter(formatter)
self.addHandler(handler)
18 changes: 13 additions & 5 deletions libcsm/os.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,19 +43,21 @@
class _CLI:
"""
An object to abstract the return result from ``run_command``.
"""

_stdout = b''
_stderr = b''
_return_code = None
_duration = None

def __init__(self, args: [str, list], shell: bool = False) -> None:
"""
Create a ``Popen`` object.
If shell==True then the arguments will be converted to a string if
a list was passed.
The conversion is recommended by Popen's documentation:
https://docs.python.org/3/library/subprocess.html
https://docs.python.org/3/library/subprocess.html.
:param args: The arguments (as a list or string) to run with Popen.
:param shell: Whether to run Popen in a shell (default: False)
Expand All @@ -69,6 +71,8 @@ def __init__(self, args: [str, list], shell: bool = False) -> None:

def _run(self) -> None:
"""
Invoke the loaded command with ``Popen``.
Run the arguments and set the object's class variables with the
results.
"""
Expand Down Expand Up @@ -121,7 +125,7 @@ def stderr(self) -> [str, bytes]:
@property
def return_code(self) -> int:
"""
return code from the command.
Return code from the command.
"""
return self._return_code

Expand All @@ -134,6 +138,8 @@ def duration(self) -> float:

def decode(self, charset: str) -> None:
"""
Decode ``self.stdout`` and ``self.stderr``.
Decodes ``self._stdout`` and ``self._stderr`` with the given ``charset``.
:param charset: The character set to decode with.
"""
Expand All @@ -156,7 +162,9 @@ def decode(self, charset: str) -> None:
@contextmanager
def chdir(directory: str, create: bool = False) -> None:
"""
Changes into a given directory and returns to the original directory on
Change directories and run a command.
Change into a given directory and returns to the original directory on
exit.
.. note::
Expand Down Expand Up @@ -195,7 +203,7 @@ def run_command(
charset: str = None,
) -> _CLI:
"""
Runs a given command or list of commands by instantiating a ``CLI`` object.
Run a given command or list of commands by instantiating a ``CLI`` object.
.. code-block:: python
Expand Down
1 change: 1 addition & 0 deletions libcsm/requests/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
"""libCSM requests."""
22 changes: 22 additions & 0 deletions libcsm/s3/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# MIT License
#
# (C) Copyright 2023 Hewlett Packard Enterprise Development LP
#
# Permission is hereby granted, free of charge, to any person obtaining a
# copy of this software and associated documentation files (the "Software"),
# to deal in the Software without restriction, including without limitation
# the rights to use, copy, modify, merge, publish, distribute, sublicense,
# and/or sell copies of the Software, and to permit persons to whom the
# Software is furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included
# in all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
# THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
# OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
# ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
# OTHER DEALINGS IN THE SOFTWARE.
"""libCSM S3."""
6 changes: 4 additions & 2 deletions libcsm/s3/images.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,26 +28,28 @@
import json
from libcsm.s3 import s3object


class ImageFormatException(Exception):

"""
An exception for problems getting "initrd", "kernel", "rootfs"
image paths from an image in S3.
"""

def __init__(self, message) -> None:
self.message = message
super().__init__(self.message)


def get_s3_image_info(bucket_name, image_id, endpoint_url) -> dict:
"""
Function to get the initrd, rootfs, and kernel image paths given an s3 bucket and image ID.
Get the initrd, rootfs, and kernel image paths given an s3 bucket and image ID.
This returns a dictionary
{
rootfs: "<rootfs_image_path>"
kernel: "<kernal_image_path>"
initrd: "<initrd_image_path>"
}
}.
"""
image_manifest = image_id + "/manifest.json"
image_object = s3object.S3Object(bucket_name, image_manifest)
Expand Down
5 changes: 2 additions & 3 deletions libcsm/s3/s3object.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,18 +34,17 @@
S3_READ_TIMEOUT=1

class RGWAdminException(Exception):

"""
An exception for problems running radosgw-admin commands.
"""

def __init__(self, message) -> None:
self.message = message
super().__init__(self.message)

class S3Object:

"""
Class for getting s3 object infomation given a bucket and bject name.
Class for getting s3 object infomation given a bucket and bject name.
"""

def __init__(self, bucket: str, object_name: str):
Expand Down
22 changes: 22 additions & 0 deletions libcsm/sls/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# MIT License
#
# (C) Copyright 2023 Hewlett Packard Enterprise Development LP
#
# Permission is hereby granted, free of charge, to any person obtaining a
# copy of this software and associated documentation files (the "Software"),
# to deal in the Software without restriction, including without limitation
# the rights to use, copy, modify, merge, publish, distribute, sublicense,
# and/or sell copies of the Software, and to permit persons to whom the
# Software is furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included
# in all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
# THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
# OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
# ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
# OTHER DEALINGS IN THE SOFTWARE.
"""libCSM SLS."""
7 changes: 3 additions & 4 deletions libcsm/sls/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@


class API:

"""
Class for providing API to interact with SLS.
"""
Expand All @@ -46,7 +45,7 @@ def __init__(self, api_gateway_address="api-gw-service-nmn.local"):

def get_management_components_from_sls(self) -> requests.Response:
"""
Function to retrieve all management components from SLS.
Retrieve all management components from SLS.
"""
session = get_session()
try:
Expand All @@ -64,7 +63,7 @@ def get_management_components_from_sls(self) -> requests.Response:

def get_xname(self, hostname: str) -> str:
"""
Function to get the xname of a node from SLS based on a provided hostname.
Get the xname of a node from SLS based on a provided hostname.
"""
try:
components_response = (self.get_management_components_from_sls()).json()
Expand All @@ -84,7 +83,7 @@ def get_xname(self, hostname: str) -> str:

def get_hostname(self, xname: str) -> str:
"""
Function to get the hostname of a management node from SLS based on a provided xname.
Get the hostname of a management node from SLS based on a provided xname.
"""
try:
components_response = (self.get_management_components_from_sls()).json()
Expand Down
Loading

0 comments on commit 81d0f0a

Please sign in to comment.