Skip to content

Commit

Permalink
Adapt existing modules for "transitions"
Browse files Browse the repository at this point in the history
Signed-off-by: Tobias Wolf <[email protected]>
  • Loading branch information
NotTheEvilOne committed May 9, 2024
1 parent 07be5c7 commit 61013ca
Show file tree
Hide file tree
Showing 11 changed files with 79 additions and 77 deletions.
9 changes: 1 addition & 8 deletions src/rookify/modules/analyze_ceph/__init__.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,3 @@
# -*- coding: utf-8 -*-
# type: ignore

from .main import AnalyzeCephHandler

MODULE_NAME = "analyze_ceph"
HANDLER_CLASS = AnalyzeCephHandler
REQUIRES = []
AFTER = []
PREFLIGHT_REQUIRES = []
from .main import AnalyzeCephHandler as ModuleHandler # noqa
35 changes: 25 additions & 10 deletions src/rookify/modules/analyze_ceph/main.py
Original file line number Diff line number Diff line change
@@ -1,29 +1,44 @@
# -*- coding: utf-8 -*-

from ..module import ModuleHandler
from typing import Any, Dict
from ..machine import Machine
from ..module import ModuleHandler


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

results: Dict[str, Any] = dict()
state = self.machine.get_state("AnalyzeCephHandler")
state.data: Dict[str, Any] = {} # type: ignore

for command in commands:
parts = command.split(" ")
leaf = results
leaf = state.data
for idx, part in enumerate(parts):
if idx < len(parts) - 1:
leaf[part] = dict()
leaf[part] = {}
else:
leaf[part] = self.ceph.mon_command(command)
leaf = leaf[part]

self.logger.info("Dictionary created")
results["ssh"] = dict()
results["ssh"]["osd"] = dict()
for node, values in results["node"]["ls"]["osd"].items():
self.logger.debug("AnalyzeCephHandler commands executed")

state.data["ssh"] = {}
state.data["ssh"]["osd"] = {}

for node, values in state.data["node"]["ls"]["osd"].items():
devices = self.ssh.command(node, "find /dev/ceph-*/*").stdout.splitlines()
results["ssh"]["osd"][node] = {"devices": devices}
state.data["ssh"]["osd"][node] = {"devices": devices}

self.logger.info("AnalyzeCephHandler ran successfully.")
return results

@classmethod
def register_state(
_, machine: Machine, config: Dict[str, Any], **kwargs: Any
) -> None:
"""
Register state for transitions
"""

super().register_state(machine, config, tags=["data"])
9 changes: 1 addition & 8 deletions src/rookify/modules/cephx_auth_config/__init__.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,3 @@
# -*- coding: utf-8 -*-
# type: ignore

from .main import CephXAuthHandler

MODULE_NAME = "cephx_auth_config"
HANDLER_CLASS = CephXAuthHandler
REQUIRES = []
AFTER = []
PREFLIGHT_REQUIRES = []
from .main import CephXAuthHandler as ModuleHandler # noqa
41 changes: 32 additions & 9 deletions src/rookify/modules/cephx_auth_config/main.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,39 @@
# -*- coding: utf-8 -*-

from ..module import ModuleHandler
from typing import Any
from typing import Any, Dict
from ..machine import Machine
from ..module import ModuleException, ModuleHandler


class CephXAuthHandler(ModuleHandler):
def run(self) -> Any:
self.logger.debug("Reconfiguring Ceph to expect cephx auth")
def preflight(self) -> Any:
if not self.is_cephx_set(self.ceph.conf_get("auth_cluster_required")):
raise ModuleException(
"Ceph config value auth_cluster_required does not contain cephx"
)

self.ceph.conf_set("auth_cluster_required", "cephx")
self.ceph.conf_set("auth_service_required", "cephx")
self.ceph.conf_set("auth_client_required", "cephx")
if not self.is_cephx_set(self.ceph.conf_get("auth_service_required")):
raise ModuleException(
"Ceph config value auth_service_required does not contain cephx"
)

self.logger.info("Reconfigured Ceph to expect cephx auth")
return {"reconfigured": True}
if not self.is_cephx_set(self.ceph.conf_get("auth_client_required")):
raise ModuleException(
"Ceph config value auth_client_required does not contain cephx"
)

self.machine.get_state("CephXAuthHandler").verified = True
self.logger.info("Validated Ceph to expect cephx auth")

def is_cephx_set(self, values: str) -> Any:
return "cephx" in [value.strip() for value in values.split(",")]

@classmethod
def register_state(
_, machine: Machine, config: Dict[str, Any], **kwargs: Any
) -> None:
"""
Register state for transitions
"""

super().register_state(machine, config, tags=["verified"])
13 changes: 1 addition & 12 deletions src/rookify/modules/example/__init__.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,3 @@
# -*- coding: utf-8 -*-
# type: ignore

from .main import ExampleHandler

MODULE_NAME = "example" # Name of the module
HANDLER_CLASS = ExampleHandler # Define the handler class for this module
REQUIRES = [] # A list of modules that are required to run before this module. Modules in this list will be imported, even if they are not configured
AFTER = [
"migrate_monitors"
] # A list of modules that should be run before this module, if they are defined in config
PREFLIGHT_REQUIRES = [
"analyze_ceph"
] # A list of modules that are required to run the preflight_check of this module. Modules in this list will be imported and run in preflight stage.
from .main import ExampleHandler as ModuleHandler # noqa
6 changes: 4 additions & 2 deletions src/rookify/modules/example/main.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
# -*- coding: utf-8 -*-

from ..module import ModuleHandler, ModuleException

from typing import Any
from ..module import ModuleHandler, ModuleException


class ExampleHandler(ModuleHandler):
# A list of modules that are required to run the preflight_check of this module. Modules in this list will be imported and run in preflight stage.
PREFLIGHT_REQUIRES = ["analyze_ceph"]

def preflight(self) -> None:
# Do something for checking if all needed preconditions are met else throw ModuleException
raise ModuleException("Example module was loaded, so aborting!")
Expand Down
9 changes: 1 addition & 8 deletions src/rookify/modules/migrate_monitors/__init__.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,3 @@
# -*- coding: utf-8 -*-
# type: ignore

from .main import MigrateMonitorsHandler

MODULE_NAME = "migrate_monitors"
HANDLER_CLASS = MigrateMonitorsHandler
REQUIRES = []
AFTER = []
PREFLIGHT_REQUIRES = ["analyze_ceph"]
from .main import MigrateMonitorsHandler as ModuleHandler # noqa
4 changes: 3 additions & 1 deletion src/rookify/modules/migrate_monitors/main.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
# -*- coding: utf-8 -*-

from ..module import ModuleHandler
from typing import Dict, Any
from ..module import ModuleHandler


class MigrateMonitorsHandler(ModuleHandler):
REQUIRES = ["analyze_ceph"]

def run(self) -> Dict[str, Any]:
self.logger.info("MigrateMonitorsHandler ran successfully.")
return {}
9 changes: 1 addition & 8 deletions src/rookify/modules/migrate_osds/__init__.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,3 @@
# -*- coding: utf-8 -*-
# type: ignore

from .main import MigrateOSDsHandler

MODULE_NAME = "migrate_osds"
HANDLER_CLASS = MigrateOSDsHandler
REQUIRES = []
AFTER = ["migrate_monitors"]
PREFLIGHT_REQUIRES = ["analyze_ceph"]
from .main import MigrateOSDsHandler as ModuleHandler # noqa
18 changes: 8 additions & 10 deletions src/rookify/modules/migrate_osds/main.py
Original file line number Diff line number Diff line change
@@ -1,23 +1,22 @@
# -*- coding: utf-8 -*-

from ..module import ModuleHandler
from typing import Any, Dict
from ..module import ModuleHandler


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

def run(self) -> Any:
osd_config: Dict[str, Any] = dict()
for node, osds in self._data["analyze_ceph"]["node"]["ls"]["osd"].items():
osd_config: Dict[str, Any] = {}
state_data = self.machine.get_state("AnalyzeCephHandler").data

for node, osds in state_data["node"]["ls"]["osd"].items():
osd_config[node] = {"osds": {}}
for osd in osds:
osd_config[node]["osds"][osd] = dict()

for osd in self._data["analyze_ceph"]["osd"]["dump"]["osds"]:
for osd in state_data["osd"]["dump"]["osds"]:
number = osd["osd"]
uuid = osd["uuid"]
for host in osd_config.values():
Expand All @@ -26,12 +25,11 @@ def run(self) -> Any:
break

for node, values in osd_config.items():
devices = self._data["analyze_ceph"]["ssh"]["osd"][node]["devices"]
devices = state_data["ssh"]["osd"][node]["devices"]
for osd in values["osds"].values():
for device in devices:
if osd["uuid"] in device:
osd["device"] = device
break

self.logger.info(osd_config)
return {}
3 changes: 2 additions & 1 deletion tests/modules/test_example.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,10 @@
import pytest

from rookify.modules.example.main import ExampleHandler
from rookify.modules.machine import Machine
from rookify.modules.module import ModuleException


def test_preflight() -> None:
with pytest.raises(ModuleException):
ExampleHandler({}, {}, "").preflight()
ExampleHandler(Machine(), {}).preflight()

0 comments on commit 61013ca

Please sign in to comment.