SubprocessType,
)
from knot_resolver_manager.kresd_controller.systemd.dbus_api import (
- GC_SERVICE_NAME,
+ GC_SERVICE_BASE_NAME,
SystemdType,
Unit,
+ create_service_name,
is_service_name_ours,
kres_id_from_service_name,
list_units,
reset_failed_unit,
restart_unit,
- service_name_from_kres_id,
start_transient_kresd_unit,
stop_unit,
)
)
async def _stop(self):
- await compat.asyncio.to_thread(stop_unit, self._systemd_type, service_name_from_kres_id(self.id))
+ await compat.asyncio.to_thread(stop_unit, self._systemd_type, create_service_name(self.id, self._config))
async def _restart(self):
- await compat.asyncio.to_thread(restart_unit, self._systemd_type, service_name_from_kres_id(self.id))
+ await compat.asyncio.to_thread(restart_unit, self._systemd_type, create_service_name(self.id, self._config))
class SystemdSubprocessController(SubprocessController):
res: List[SystemdSubprocess] = []
units = await compat.asyncio.to_thread(list_units, self._systemd_type)
for unit in units:
- if is_service_name_ours(unit.name):
+ if is_service_name_ours(unit.name, self._controller_config):
if unit.state == "failed":
# if a unit is failed, remove it from the system by reseting its state
logger.warning("Unit '%s' is already failed, resetting its state and ignoring it", unit.name)
res.append(
SystemdSubprocess(
self._controller_config,
- SubprocessType.GC if unit.name == GC_SERVICE_NAME else SubprocessType.KRESD,
+ SubprocessType.GC
+ if unit.name == self._controller_config.server.groupid + GC_SERVICE_BASE_NAME
+ else SubprocessType.KRESD,
self._systemd_type,
- custom_id=kres_id_from_service_name(unit.name),
+ custom_id=kres_id_from_service_name(unit.name, self._controller_config),
)
)
async def create_subprocess(self, subprocess_config: KresConfig, subprocess_type: SubprocessType) -> Subprocess:
assert self._controller_config is not None
- custom_id = KresID.from_string(GC_SERVICE_NAME) if subprocess_type == SubprocessType.GC else None
+
+ custom_id = None
+ if subprocess_type == SubprocessType.GC:
+ custom_id = KresID.from_string(subprocess_config.server.groupid + GC_SERVICE_BASE_NAME)
+
return SystemdSubprocess(subprocess_config, subprocess_type, self._systemd_type, custom_id=custom_id)
async def get_subprocess_status(self) -> Dict[KresID, SubprocessStatus]:
return SubprocessStatus.UNKNOWN
data: List[Unit] = await to_thread(list_units, self._systemd_type)
- our_data = filter(lambda u: is_service_name_ours(u.name), data)
- return {kres_id_from_service_name(u.name): convert(u.state) for u in our_data}
+
+ our_data = filter(lambda u: is_service_name_ours(u.name, self._controller_config), data) # type: ignore
+ return {kres_id_from_service_name(u.name, self._controller_config): convert(u.state) for u in our_data}
logger = logging.getLogger(__name__)
-_PREFIX = "mkres"
-GC_SERVICE_NAME = f"{_PREFIX}_cache_gc.service"
-KRESD_SERVICE_PATTERN = re.compile(rf"^{_PREFIX}d_([0-9]+).service$")
+GC_SERVICE_BASE_NAME = "kres_cache_gc.service"
+KRESD_SERVICE_BASE_PATTERN = re.compile(r"^kresd_([0-9]+).service$")
-def kres_id_from_service_name(service_name: str) -> KresID:
- kid = KRESD_SERVICE_PATTERN.search(service_name)
+def kres_id_from_service_name(service_name: str, config: KresConfig) -> KresID:
+ base_service_name = service_name
+ if service_name.startswith(config.server.groupid):
+ base_service_name = service_name[len(config.server.groupid) :] # noqa: E203
+ kid = KRESD_SERVICE_BASE_PATTERN.search(base_service_name)
if kid:
return KresID.from_string(kid.groups()[0])
return KresID.from_string(service_name)
-def service_name_from_kres_id(kid: KresID) -> str:
+def create_service_name(kid: KresID, config: KresConfig) -> str:
rep = str(kid)
if rep.isnumeric():
- return f"{_PREFIX}d_{rep}.service"
+ return f"{config.server.groupid}kresd_{rep}.service"
return rep
-def is_service_name_ours(service_name: str) -> bool:
- is_ours = service_name == GC_SERVICE_NAME
- is_ours |= bool(KRESD_SERVICE_PATTERN.match(service_name))
+def is_service_name_ours(service_name: str, config: KresConfig) -> bool:
+ if service_name.startswith(config.server.groupid):
+ service_name = service_name[len(config.server.groupid) :] # noqa: E203
+ is_ours = service_name == GC_SERVICE_BASE_NAME
+ is_ours |= bool(KRESD_SERVICE_BASE_PATTERN.match(service_name))
return is_ours
config: KresConfig, type_: SystemdType, kres_id: KresID, subprocess_type: SubprocessType
) -> None:
name, properties = {
- SubprocessType.KRESD: (service_name_from_kres_id(kres_id), _kresd_unit_properties(config, kres_id)),
- SubprocessType.GC: (service_name_from_kres_id(kres_id), _gc_unit_properties(config)),
+ SubprocessType.KRESD: (create_service_name(kres_id, config), _kresd_unit_properties(config, kres_id)),
+ SubprocessType.GC: (create_service_name(kres_id, config), _gc_unit_properties(config)),
}[subprocess_type]
systemd = _create_manager_proxy(type_)