Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Prs/add mypy to pre commit checks #35

Merged
merged 4 commits into from
Mar 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,10 @@ repos:
- id: mixed-line-ending
- id: end-of-file-fixer
- id: trailing-whitespace
- repo: https://github.com/pre-commit/mirrors-mypy
rev: 'v1.9.0'
hooks:
- id: mypy
args: [--strict, --ignore-missing-imports, --check-untyped-defs]
additional_dependencies:
- types-PyYAML
9 changes: 5 additions & 4 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,15 @@
limitations under the License.
"""

from typing import Dict, Any

try:
from setuptools import find_packages, setup
except ImportError:
from distutils import find_packages, setup
#
from distutils import find_packages, setup # type: ignore[attr-defined, unused-ignore]


def get_version():
def get_version() -> str:
"""
Returns the version currently in development.

Expand All @@ -36,7 +37,7 @@ def get_version():

#

_setup = {
_setup: Dict[str, Any] = {
"version": get_version()[1:],
"data_files": [("docs", ["LICENSE", "README.md"])],
"test_suite": "tests",
Expand Down
2 changes: 1 addition & 1 deletion src/rookify/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
from .yaml import load_yaml, save_yaml


def main():
def main() -> None:
try:
config = load_yaml("config.yaml")
except FileNotFoundError as err:
Expand Down
29 changes: 20 additions & 9 deletions src/rookify/modules/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import importlib
import types
from typing import List

from collections import OrderedDict
from .module import ModuleHandler
Expand All @@ -23,7 +24,9 @@ def __init__(self, module_name: str, message: str):
self.message = message


def load_modules(module_names: list) -> tuple[list, list]:
def load_modules(
module_names: List[str],
) -> tuple[List[types.ModuleType], List[types.ModuleType]]:
"""
Dynamically loads modules from the 'modules' package.

Expand All @@ -32,7 +35,7 @@ def load_modules(module_names: list) -> tuple[list, list]:
"""

# Sanity checks for modules
def check_module_sanity(module_name: str, module: types.ModuleType):
def check_module_sanity(module_name: str, module: types.ModuleType) -> None:
for attr_type, attr_name in (
(ModuleHandler, "HANDLER_CLASS"),
(str, "MODULE_NAME"),
Expand All @@ -52,9 +55,11 @@ def check_module_sanity(module_name: str, module: types.ModuleType):
)

# Load the modules in the given list and recursivley load required modules
required_modules = OrderedDict()
required_modules: OrderedDict[str, types.ModuleType] = OrderedDict()

def load_required_modules(modules_out: OrderedDict, module_names: list) -> None:
def load_required_modules(
modules_out: OrderedDict[str, types.ModuleType], module_names: List[str]
) -> None:
for module_name in module_names:
if module_name in modules_out:
continue
Expand All @@ -70,10 +75,12 @@ def load_required_modules(modules_out: OrderedDict, module_names: list) -> None:
load_required_modules(required_modules, module_names)

# Recursively load the modules in the PREFLIGHT_REQUIRES attribute of the given modules
preflight_modules = OrderedDict()
preflight_modules: OrderedDict[str, types.ModuleType] = OrderedDict()

def load_preflight_modules(
modules_in: OrderedDict, modules_out: OrderedDict, module_names: list
modules_in: OrderedDict[str, types.ModuleType],
modules_out: OrderedDict[str, types.ModuleType],
module_names: List[str],
) -> None:
for module_name in module_names:
if module_name in modules_out:
Expand All @@ -94,13 +101,17 @@ def load_preflight_modules(
if module_name not in modules_in:
modules_out[module_name] = module

load_preflight_modules(required_modules, preflight_modules, required_modules.keys())
load_preflight_modules(
required_modules, preflight_modules, list(required_modules.keys())
)

# Sort the modules by the AFTER keyword
modules = OrderedDict()
modules: OrderedDict[str, types.ModuleType] = OrderedDict()

def sort_modules(
modules_in: OrderedDict, modules_out: OrderedDict, module_names: list
modules_in: OrderedDict[str, types.ModuleType],
modules_out: OrderedDict[str, types.ModuleType],
module_names: List[str],
) -> None:
for module_name in module_names:
if module_name not in modules_in:
Expand Down
1 change: 1 addition & 0 deletions src/rookify/modules/analyze_ceph/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
# -*- coding: utf-8 -*-
# type: ignore

from .main import AnalyzeCephHandler

Expand Down
7 changes: 4 additions & 3 deletions src/rookify/modules/analyze_ceph/main.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
# -*- coding: utf-8 -*-


from ..module import ModuleHandler

from typing import Any, Dict


class AnalyzeCephHandler(ModuleHandler):
def run(self) -> dict:
def run(self) -> Dict[str, Any]:
commands = ["mon dump", "osd dump", "device ls", "fs dump", "node ls"]

results = dict()
results: Dict[str, Any] = dict()
for command in commands:
parts = command.split(" ")
leaf = results
Expand Down
1 change: 1 addition & 0 deletions src/rookify/modules/example/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
# -*- coding: utf-8 -*-
# type: ignore

from .main import ExampleHandler

Expand Down
8 changes: 5 additions & 3 deletions src/rookify/modules/example/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,14 @@

from ..module import ModuleHandler, ModuleException

from typing import Any, Dict


class ExampleHandler(ModuleHandler):
def preflight_check(self):
def preflight_check(self) -> None:
# Do something for checking if all needed preconditions are met else throw ModuleException
raise ModuleException("Example module was loaded, so aborting!")

def run(self) -> dict:
def run(self) -> Dict[str, Any]:
# Run the migration tasks
pass
return {}
1 change: 1 addition & 0 deletions src/rookify/modules/migrate_monitors/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
# -*- coding: utf-8 -*-
# type: ignore

from .main import MigrateMonitorsHandler

Expand Down
1 change: 1 addition & 0 deletions src/rookify/modules/migrate_osds/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
# -*- coding: utf-8 -*-
# type: ignore

from .main import MigrateOSDsHandler

Expand Down
9 changes: 6 additions & 3 deletions src/rookify/modules/migrate_osds/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,17 @@

from ..module import ModuleHandler

from typing import Any, Dict


class MigrateOSDsHandler(ModuleHandler):
def preflight_check(self):
def preflight_check(self) -> None:
pass
# result = self.ceph.mon_command("osd dump")
# raise ModuleException('test error')

def run(self) -> dict:
osd_config = dict()
def run(self) -> Dict[str, Any]:
osd_config: Dict[str, Any] = dict()
for node, osds in self._data["analyze_ceph"]["node"]["ls"]["osd"].items():
osd_config[node] = {"osds": {}}
for osd in osds:
Expand All @@ -33,3 +35,4 @@ def run(self) -> dict:
break

print(osd_config)
return {}
33 changes: 19 additions & 14 deletions src/rookify/modules/module.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import rados
import kubernetes
import fabric
from typing import Any, Dict, List, Optional


class ModuleException(Exception):
Expand All @@ -17,7 +18,7 @@ class ModuleHandler:
"""

class __Ceph:
def __init__(self, config: dict):
def __init__(self, config: Dict[str, Any]):
try:
self.__ceph = rados.Rados(
conffile=config["conf_file"], conf={"keyring": config["keyring"]}
Expand All @@ -26,16 +27,20 @@ def __init__(self, config: dict):
except rados.ObjectNotFound as err:
raise ModuleException(f"Could not connect to ceph: {err}")

def mon_command(self, command: str, **kwargs) -> dict:
def mon_command(
self, command: str, **kwargs: str
) -> Dict[str, Any] | List[Any]:
cmd = {"prefix": command, "format": "json"}
cmd.update(kwargs)
cmd.update(**kwargs)
result = self.__ceph.mon_command(json.dumps(cmd), b"")
if result[0] != 0:
raise ModuleException(f"Ceph did return an error: {result}")
return json.loads(result[1])
data = json.loads(result[1])
assert isinstance(data, dict) or isinstance(data, list)
return data

class __K8s:
def __init__(self, config: dict):
def __init__(self, config: Dict[str, Any]):
k8s_config = kubernetes.client.Configuration()
k8s_config.api_key = config["api_key"]
k8s_config.host = config["host"]
Expand All @@ -54,7 +59,7 @@ def NodeV1Api(self) -> kubernetes.client.NodeV1Api:
return kubernetes.client.NodeV1Api(self.__client)

class __SSH:
def __init__(self, config: dict):
def __init__(self, config: Dict[str, Any]):
self.__config = config

def command(self, host: str, command: str) -> fabric.runners.Result:
Expand All @@ -77,7 +82,7 @@ def command(self, host: str, command: str) -> fabric.runners.Result:
).run(command, hide=True)
return result

def __init__(self, config: dict, data: dict):
def __init__(self, config: Dict[str, Any], data: Dict[str, Any]):
"""
Construct a new 'ModuleHandler' object.

Expand All @@ -86,9 +91,9 @@ def __init__(self, config: dict, data: dict):
"""
self._config = config
self._data = data
self.__ceph = None
self.__k8s = None
self.__ssh = None
self.__ceph: Optional[ModuleHandler.__Ceph] = None
self.__k8s: Optional[ModuleHandler.__K8s] = None
self.__ssh: Optional[ModuleHandler.__SSH] = None

@abc.abstractmethod
def preflight_check(self) -> None:
Expand All @@ -98,7 +103,7 @@ def preflight_check(self) -> None:
pass

@abc.abstractmethod
def run(self) -> dict:
def run(self) -> Dict[str, Any]:
"""
Run the modules tasks

Expand All @@ -109,17 +114,17 @@ def run(self) -> dict:
@property
def ceph(self) -> __Ceph:
if self.__ceph is None:
self.__ceph = self.__Ceph(self._config["ceph"])
self.__ceph = ModuleHandler.__Ceph(self._config["ceph"])
return self.__ceph

@property
def k8s(self) -> __K8s:
if self.__k8s is None:
self.__k8s = self.__K8s(self._config["kubernetes"])
self.__k8s = ModuleHandler.__K8s(self._config["kubernetes"])
return self.__k8s

@property
def ssh(self) -> __SSH:
if self.__ssh is None:
self.__ssh = self.__SSH(self._config["ssh"])
self.__ssh = ModuleHandler.__SSH(self._config["ssh"])
return self.__ssh
9 changes: 6 additions & 3 deletions src/rookify/yaml.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
# -*- coding: utf-8 -*-

import yaml
from typing import Any, Dict


def load_yaml(path: str) -> dict:
def load_yaml(path: str) -> Dict[str, Any]:
with open(path, "r") as file:
return yaml.safe_load(file)
data = yaml.safe_load(file)
assert isinstance(data, dict)
return data


def save_yaml(path: str, data: dict) -> None:
def save_yaml(path: str, data: Dict[str, Any]) -> None:
with open(path, "w") as file:
yaml.safe_dump(data, file)
Loading