Skip to content

Commit

Permalink
fix: add logger class to module handler
Browse files Browse the repository at this point in the history
Signed-off-by: Rafael te Boekhorst <[email protected]>
  • Loading branch information
boekhorstb1 committed Mar 27, 2024
1 parent 0d83869 commit 7d2283d
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 14 deletions.
8 changes: 2 additions & 6 deletions src/rookify/modules/analyze_ceph/main.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,12 @@
# -*- coding: utf-8 -*-

from ..module import ModuleHandler

from typing import Any, Dict

from rookify.logger import getLogger


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

results: Dict[str, Any] = dict()
for command in commands:
Expand All @@ -23,11 +19,11 @@ def run(self) -> Any:
leaf[part] = self.ceph.mon_command(command)
leaf = leaf[part]

log.info("Dictionary created")
self.logger.info("Dictionary created")
results["ssh"] = dict()
results["ssh"]["osd"] = dict()
for node, values in results["node"]["ls"]["osd"].items():
devices = self.ssh.command(node, "find /dev/ceph-*/*").stdout.splitlines()
results["ssh"]["osd"][node] = {"devices": devices}
log.info("AnalyzeCephHandler ran successfully.")
self.logger.info("AnalyzeCephHandler ran successfully.")
return results
4 changes: 1 addition & 3 deletions src/rookify/modules/migrate_monitors/main.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
# -*- coding: utf-8 -*-

from ..module import ModuleHandler
from rookify.logger import getLogger
from typing import Dict, Any


class MigrateMonitorsHandler(ModuleHandler):
def run(self) -> Dict[str, Any]:
log = getLogger()
log.info("MigrateMonitorsHandler ran successfully.")
self.logger.info("MigrateMonitorsHandler ran successfully.")
return {}
6 changes: 1 addition & 5 deletions src/rookify/modules/migrate_osds/main.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,8 @@
# -*- coding: utf-8 -*-

from ..module import ModuleHandler

from typing import Any, Dict

from rookify.logger import getLogger


class MigrateOSDsHandler(ModuleHandler):
def preflight(self) -> None:
Expand All @@ -14,7 +11,6 @@ def preflight(self) -> None:
# raise ModuleException('test error')

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

log.info(osd_config)
self.logger.info(osd_config)
return {}
20 changes: 20 additions & 0 deletions src/rookify/modules/module.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
import kubernetes
import fabric
import jinja2
import structlog
from rookify.logger import configure_logging
from typing import Any, Dict, List, Optional


Expand All @@ -20,6 +22,17 @@ class ModuleHandler:
ModuleHandler is an abstract class that modules have to extend.
"""

class __Logger:
def __init__(self, config: Dict[str, Any]):
try:
configure_logging(config)
except ImportError as e:
raise ModuleException(f"Error initializing logger: {e}")

@property
def getLogger(self) -> structlog.getLogger:
return structlog.getLogger

class __Ceph:
def __init__(self, config: Dict[str, Any]):
try:
Expand Down Expand Up @@ -162,6 +175,7 @@ def __init__(self, config: Dict[str, Any], data: Dict[str, Any], module_path: st
self.__ceph: Optional[ModuleHandler.__Ceph] = None
self.__k8s: Optional[ModuleHandler.__K8s] = None
self.__ssh: Optional[ModuleHandler.__SSH] = None
self.__logger: Optional[structlog.getLogger] = None

@abc.abstractmethod
def preflight(self) -> None:
Expand Down Expand Up @@ -197,6 +211,12 @@ def ssh(self) -> __SSH:
self.__ssh = ModuleHandler.__SSH(self._config["ssh"])
return self.__ssh

@property
def logger(self) -> structlog.getLogger:
if self.__logger is None:
self.__logger = ModuleHandler.__Logger(self._config)
return self.__logger.getLogger()

def load_template(self, filename: str, **variables: Any) -> __Template:
template_path = os.path.join(self.__module_path, "templates", filename)
template = ModuleHandler.__Template(template_path)
Expand Down

0 comments on commit 7d2283d

Please sign in to comment.