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

Add migrate mgrs module #55

Merged
merged 3 commits into from
Sep 3, 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
3 changes: 3 additions & 0 deletions src/rookify/modules/migrate_mgrs/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# -*- coding: utf-8 -*-

from .main import MigrateMgrsHandler as ModuleHandler # noqa
92 changes: 92 additions & 0 deletions src/rookify/modules/migrate_mgrs/main.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
# -*- coding: utf-8 -*-

from time import sleep
from typing import Any
from ..exception import ModuleException
from ..machine import Machine
from ..module import ModuleHandler


class MigrateMgrsHandler(ModuleHandler):
REQUIRES = ["analyze_ceph", "create_cluster"]

def execute(self) -> None:
state_data = self.machine.get_preflight_state("AnalyzeCephHandler").data

for node, _ in state_data["node"]["ls"]["mgr"].items():
self._migrate_mgr(node)

def _migrate_mgr(self, mgr_host: str) -> None:
migrated_mgrs = getattr(
self.machine.get_execution_state("MigrateMgrsHandler"), "migrated_mgrs", []
)
if mgr_host in migrated_mgrs:
return

self.logger.debug("Migrating Ceph mgr daemon '{0}'".format(mgr_host))

result = self.ssh.command(
mgr_host, "sudo systemctl disable --now ceph-mgr.target"
)

if result.failed:
raise ModuleException(
"Disabling original Ceph mgr daemon at host {0} failed: {1}".format(
mgr_host, result.stderr
)
)

self.logger.debug(
"Waiting for disabled original Ceph mgr daemon '{0}' to disconnect".format(
mgr_host
)
)

while True:
result = self.ceph.mon_command("node ls")

if mgr_host not in result["mgr"]:
break

sleep(2)

self.logger.info(
"Disabled Ceph mgr daemon '{0}' and enabling Rook based Ceph mgr daemon '{0}'".format(
mgr_host
)
)

node_patch = {"metadata": {"labels": {"ceph-mgr-placement": "enabled"}}}

if (
"ceph-mgr-placement"
not in self.k8s.core_v1_api.patch_node(mgr_host, node_patch).metadata.labels
):
raise ModuleException(
"Failed to patch k8s node for Ceph mgr daemon '{0}'".format(mgr_host)
)

migrated_mgrs.append(mgr_host)

self.machine.get_execution_state(
"MigrateMgrsHandler"
).migrated_mgrs = migrated_mgrs

self.logger.debug("Waiting for 3 Ceph mgr daemons to be available")

while True:
result = self.ceph.mon_command("node ls")
if len(result["mgr"]) >= 3:
break

sleep(2)

self.logger.debug("3 Ceph mgr daemons are available")

@staticmethod
def register_execution_state(
machine: Machine, state_name: str, handler: ModuleHandler, **kwargs: Any
) -> None:
ModuleHandler.register_execution_state(
machine, state_name, handler, tags=["migrated_mgrs"]
)
Loading