2017-06-20 02:09:28 +01:00
|
|
|
"""
|
|
|
|
Definition of CoreService class that is subclassed to define
|
2013-08-29 15:21:13 +01:00
|
|
|
startup services and routing for nodes. A service is typically a daemon
|
2017-06-20 02:09:28 +01:00
|
|
|
program launched when a node starts that provides some sort of service.
|
|
|
|
The CoreServices class handles configuration messages for sending
|
2013-08-29 15:21:13 +01:00
|
|
|
a list of available services to the GUI and for configuring individual
|
|
|
|
services.
|
2017-06-20 02:09:28 +01:00
|
|
|
"""
|
|
|
|
|
2019-06-03 22:36:21 +01:00
|
|
|
import enum
|
2019-02-16 17:50:19 +00:00
|
|
|
import logging
|
2022-03-02 20:42:53 +00:00
|
|
|
import pkgutil
|
2018-06-20 20:59:07 +01:00
|
|
|
import time
|
2023-04-13 23:39:40 +01:00
|
|
|
from collections.abc import Iterable
|
2021-03-19 23:54:24 +00:00
|
|
|
from pathlib import Path
|
2023-04-13 23:39:40 +01:00
|
|
|
from typing import TYPE_CHECKING, Optional, Union
|
2018-06-20 20:59:07 +01:00
|
|
|
|
2022-03-02 20:42:53 +00:00
|
|
|
from core import services as core_services
|
2019-09-28 07:29:15 +01:00
|
|
|
from core import utils
|
2019-04-30 07:31:47 +01:00
|
|
|
from core.emulator.data import FileData
|
2019-12-20 18:51:52 +00:00
|
|
|
from core.emulator.enumerations import ExceptionLevels, MessageFlags, RegisterTlvs
|
2020-08-27 18:46:55 +01:00
|
|
|
from core.errors import (
|
|
|
|
CoreCommandError,
|
|
|
|
CoreError,
|
|
|
|
CoreServiceBootError,
|
|
|
|
CoreServiceError,
|
|
|
|
)
|
2020-01-14 23:26:19 +00:00
|
|
|
from core.nodes.base import CoreNode
|
|
|
|
|
2021-04-22 05:09:35 +01:00
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
2020-01-14 23:26:19 +00:00
|
|
|
if TYPE_CHECKING:
|
|
|
|
from core.emulator.session import Session
|
2017-06-20 02:09:28 +01:00
|
|
|
|
2023-04-13 23:39:40 +01:00
|
|
|
CoreServiceType = Union["CoreService", type["CoreService"]]
|
2020-08-18 21:18:41 +01:00
|
|
|
|
2017-06-20 02:09:28 +01:00
|
|
|
|
2018-06-20 20:59:07 +01:00
|
|
|
class ServiceMode(enum.Enum):
|
|
|
|
BLOCKING = 0
|
|
|
|
NON_BLOCKING = 1
|
|
|
|
TIMER = 2
|
|
|
|
|
|
|
|
|
2019-10-23 17:31:07 +01:00
|
|
|
class ServiceDependencies:
|
2018-08-01 18:13:57 +01:00
|
|
|
"""
|
|
|
|
Can generate boot paths for services, based on their dependencies. Will validate
|
2020-06-14 06:25:38 +01:00
|
|
|
that all services will be booted and that all dependencies exist within the services
|
|
|
|
provided.
|
2018-08-01 18:13:57 +01:00
|
|
|
"""
|
|
|
|
|
2023-04-13 23:39:40 +01:00
|
|
|
def __init__(self, services: list["CoreServiceType"]) -> None:
|
|
|
|
self.visited: set[str] = set()
|
|
|
|
self.services: dict[str, "CoreServiceType"] = {}
|
|
|
|
self.paths: dict[str, list["CoreServiceType"]] = {}
|
|
|
|
self.boot_paths: list[list["CoreServiceType"]] = []
|
|
|
|
roots = {x.name for x in services}
|
2020-08-18 21:18:41 +01:00
|
|
|
for service in services:
|
|
|
|
self.services[service.name] = service
|
2020-08-21 19:34:12 +01:00
|
|
|
roots -= set(service.dependencies)
|
2023-04-13 23:39:40 +01:00
|
|
|
self.roots: list["CoreServiceType"] = [x for x in services if x.name in roots]
|
2020-08-21 19:34:12 +01:00
|
|
|
if services and not self.roots:
|
|
|
|
raise ValueError("circular dependency is present")
|
|
|
|
|
|
|
|
def _search(
|
|
|
|
self,
|
|
|
|
service: "CoreServiceType",
|
2023-04-13 23:39:40 +01:00
|
|
|
visiting: set[str] = None,
|
|
|
|
path: list[str] = None,
|
|
|
|
) -> list["CoreServiceType"]:
|
2020-08-18 21:18:41 +01:00
|
|
|
if service.name in self.visited:
|
2020-08-21 19:34:12 +01:00
|
|
|
return self.paths[service.name]
|
2020-08-18 21:18:41 +01:00
|
|
|
self.visited.add(service.name)
|
|
|
|
if visiting is None:
|
|
|
|
visiting = set()
|
|
|
|
visiting.add(service.name)
|
2020-08-21 19:34:12 +01:00
|
|
|
if path is None:
|
|
|
|
for dependency in service.dependencies:
|
|
|
|
path = self.paths.get(dependency)
|
|
|
|
if path is not None:
|
|
|
|
break
|
2020-08-18 21:18:41 +01:00
|
|
|
for dependency in service.dependencies:
|
|
|
|
service_dependency = self.services.get(dependency)
|
|
|
|
if not service_dependency:
|
|
|
|
raise ValueError(f"required dependency was not provided: {dependency}")
|
|
|
|
if dependency in visiting:
|
|
|
|
raise ValueError(f"circular dependency, already visited: {dependency}")
|
|
|
|
else:
|
2020-08-21 19:34:12 +01:00
|
|
|
path = self._search(service_dependency, visiting, path)
|
2020-08-18 21:18:41 +01:00
|
|
|
visiting.remove(service.name)
|
2020-08-21 19:34:12 +01:00
|
|
|
if path is None:
|
|
|
|
path = []
|
|
|
|
self.boot_paths.append(path)
|
|
|
|
path.append(service)
|
|
|
|
self.paths[service.name] = path
|
|
|
|
return path
|
|
|
|
|
2023-04-13 23:39:40 +01:00
|
|
|
def boot_order(self) -> list[list["CoreServiceType"]]:
|
2020-08-21 19:34:12 +01:00
|
|
|
for service in self.roots:
|
2020-08-18 21:18:41 +01:00
|
|
|
self._search(service)
|
2020-08-21 19:34:12 +01:00
|
|
|
return self.boot_paths
|
2018-08-01 18:13:57 +01:00
|
|
|
|
|
|
|
|
2019-10-23 17:31:07 +01:00
|
|
|
class ServiceManager:
|
2017-06-20 02:09:28 +01:00
|
|
|
"""
|
|
|
|
Manages services available for CORE nodes to use.
|
|
|
|
"""
|
2019-09-10 23:10:24 +01:00
|
|
|
|
2023-04-13 23:39:40 +01:00
|
|
|
services: dict[str, type["CoreService"]] = {}
|
2013-08-29 15:21:13 +01:00
|
|
|
|
2017-06-20 02:09:28 +01:00
|
|
|
@classmethod
|
2023-04-13 23:39:40 +01:00
|
|
|
def add(cls, service: type["CoreService"]) -> None:
|
2017-06-20 02:09:28 +01:00
|
|
|
"""
|
|
|
|
Add a service to manager.
|
|
|
|
|
2020-01-16 19:00:57 +00:00
|
|
|
:param service: service to add
|
2017-06-20 02:09:28 +01:00
|
|
|
:return: nothing
|
2019-09-30 17:33:17 +01:00
|
|
|
:raises ValueError: when service cannot be loaded
|
2017-06-20 02:09:28 +01:00
|
|
|
"""
|
2018-06-15 22:03:27 +01:00
|
|
|
name = service.name
|
2021-04-22 05:09:35 +01:00
|
|
|
logger.debug("loading service: class(%s) name(%s)", service.__name__, name)
|
2022-03-02 20:42:53 +00:00
|
|
|
# avoid services with no name
|
|
|
|
if name is None:
|
|
|
|
logger.debug("not loading class(%s) with no name", service.__name__)
|
|
|
|
return
|
2018-06-19 17:19:49 +01:00
|
|
|
# avoid duplicate services
|
2018-06-14 20:50:48 +01:00
|
|
|
if name in cls.services:
|
2022-03-02 20:42:53 +00:00
|
|
|
raise ValueError(f"duplicate service being added: {name}")
|
2018-06-19 17:19:49 +01:00
|
|
|
# validate dependent executables are present
|
|
|
|
for executable in service.executables:
|
2020-06-23 17:11:37 +01:00
|
|
|
try:
|
|
|
|
utils.which(executable, required=True)
|
|
|
|
except CoreError as e:
|
|
|
|
raise CoreError(f"service({name}): {e}")
|
2019-09-30 17:33:17 +01:00
|
|
|
# validate service on load succeeds
|
|
|
|
try:
|
|
|
|
service.on_load()
|
|
|
|
except Exception as e:
|
2021-04-22 05:09:35 +01:00
|
|
|
logger.exception("error during service(%s) on load", service.name)
|
2019-09-30 17:33:17 +01:00
|
|
|
raise ValueError(e)
|
2018-06-19 17:19:49 +01:00
|
|
|
# make service available
|
2018-06-14 20:50:48 +01:00
|
|
|
cls.services[name] = service
|
2017-06-20 02:09:28 +01:00
|
|
|
|
|
|
|
@classmethod
|
2023-04-13 23:39:40 +01:00
|
|
|
def get(cls, name: str) -> type["CoreService"]:
|
2017-06-20 02:09:28 +01:00
|
|
|
"""
|
|
|
|
Retrieve a service from the manager.
|
|
|
|
|
2020-01-16 19:00:57 +00:00
|
|
|
:param name: name of the service to retrieve
|
2017-06-20 02:09:28 +01:00
|
|
|
:return: service if it exists, None otherwise
|
2020-01-17 00:12:01 +00:00
|
|
|
"""
|
2020-08-27 18:43:13 +01:00
|
|
|
service = cls.services.get(name)
|
|
|
|
if service is None:
|
|
|
|
raise CoreServiceError(f"service({name}) does not exist")
|
|
|
|
return service
|
2014-04-30 22:27:41 +01:00
|
|
|
|
2017-08-04 00:33:54 +01:00
|
|
|
@classmethod
|
2023-04-13 23:39:40 +01:00
|
|
|
def add_services(cls, path: Path) -> list[str]:
|
2017-08-04 00:33:54 +01:00
|
|
|
"""
|
|
|
|
Method for retrieving all CoreServices from a given path.
|
|
|
|
|
2020-01-16 19:00:57 +00:00
|
|
|
:param path: path to retrieve services from
|
2018-06-19 17:19:49 +01:00
|
|
|
:return: list of core services that failed to load
|
2020-01-17 00:12:01 +00:00
|
|
|
"""
|
2018-06-19 17:19:49 +01:00
|
|
|
service_errors = []
|
2018-02-05 19:22:01 +00:00
|
|
|
services = utils.load_classes(path, CoreService)
|
|
|
|
for service in services:
|
2018-06-15 22:03:27 +01:00
|
|
|
if not service.name:
|
2018-04-21 05:40:55 +01:00
|
|
|
continue
|
2018-06-19 17:19:49 +01:00
|
|
|
try:
|
|
|
|
cls.add(service)
|
2020-06-23 17:11:37 +01:00
|
|
|
except (CoreError, ValueError) as e:
|
2018-06-19 17:19:49 +01:00
|
|
|
service_errors.append(service.name)
|
2021-04-22 05:09:35 +01:00
|
|
|
logger.debug("not loading service(%s): %s", service.name, e)
|
2018-06-19 17:19:49 +01:00
|
|
|
return service_errors
|
2017-08-04 00:33:54 +01:00
|
|
|
|
2022-03-02 20:42:53 +00:00
|
|
|
@classmethod
|
2023-04-13 23:39:40 +01:00
|
|
|
def load_locals(cls) -> list[str]:
|
2022-03-02 20:42:53 +00:00
|
|
|
errors = []
|
|
|
|
for module_info in pkgutil.walk_packages(
|
|
|
|
core_services.__path__, f"{core_services.__name__}."
|
|
|
|
):
|
|
|
|
services = utils.load_module(module_info.name, CoreService)
|
|
|
|
for service in services:
|
|
|
|
try:
|
|
|
|
cls.add(service)
|
|
|
|
except CoreError as e:
|
|
|
|
errors.append(service.name)
|
|
|
|
logger.debug("not loading service(%s): %s", service.name, e)
|
|
|
|
return errors
|
|
|
|
|
2017-06-20 02:09:28 +01:00
|
|
|
|
2019-10-23 17:31:07 +01:00
|
|
|
class CoreServices:
|
2017-06-20 02:09:28 +01:00
|
|
|
"""
|
|
|
|
Class for interacting with a list of available startup services for
|
|
|
|
nodes. Mostly used to convert a CoreService into a Config API
|
|
|
|
message. This class lives in the Session object and remembers
|
|
|
|
the default services configured for each node type, and any
|
|
|
|
custom service configuration. A CoreService is not a Configurable.
|
|
|
|
"""
|
2019-09-10 23:10:24 +01:00
|
|
|
|
2020-06-14 06:25:38 +01:00
|
|
|
name: str = "services"
|
|
|
|
config_type: RegisterTlvs = RegisterTlvs.UTILITY
|
2017-02-24 00:20:57 +00:00
|
|
|
|
2020-01-14 23:26:19 +00:00
|
|
|
def __init__(self, session: "Session") -> None:
|
2017-06-20 02:09:28 +01:00
|
|
|
"""
|
|
|
|
Creates a CoreServices instance.
|
|
|
|
|
2020-01-16 19:00:57 +00:00
|
|
|
:param session: session this manager is tied to
|
2017-06-20 02:09:28 +01:00
|
|
|
"""
|
2020-06-14 06:25:38 +01:00
|
|
|
self.session: "Session" = session
|
2013-08-29 15:21:13 +01:00
|
|
|
# dict of default services tuples, key is node type
|
2023-04-13 23:39:40 +01:00
|
|
|
self.default_services: dict[str, list[str]] = {
|
2020-06-14 06:25:38 +01:00
|
|
|
"mdr": ["zebra", "OSPFv3MDR", "IPForward"],
|
|
|
|
"PC": ["DefaultRoute"],
|
|
|
|
"prouter": [],
|
|
|
|
"router": ["zebra", "OSPFv2", "OSPFv3", "IPForward"],
|
|
|
|
"host": ["DefaultRoute", "SSH"],
|
2020-06-13 04:22:51 +01:00
|
|
|
}
|
2018-06-22 22:41:06 +01:00
|
|
|
# dict of node ids to dict of custom services by name
|
2023-04-13 23:39:40 +01:00
|
|
|
self.custom_services: dict[int, dict[str, "CoreService"]] = {}
|
2017-06-20 02:09:28 +01:00
|
|
|
|
2020-01-14 23:26:19 +00:00
|
|
|
def reset(self) -> None:
|
2017-06-20 02:09:28 +01:00
|
|
|
"""
|
|
|
|
Called when config message with reset flag is received
|
|
|
|
"""
|
2018-06-22 22:41:06 +01:00
|
|
|
self.custom_services.clear()
|
2017-06-20 02:09:28 +01:00
|
|
|
|
2020-01-14 23:26:19 +00:00
|
|
|
def get_service(
|
|
|
|
self, node_id: int, service_name: str, default_service: bool = False
|
|
|
|
) -> "CoreService":
|
2017-06-20 02:09:28 +01:00
|
|
|
"""
|
2020-01-14 23:26:19 +00:00
|
|
|
Get any custom service configured for the given node that matches the specified
|
|
|
|
service name. If no custom service is found, return the specified service.
|
2017-06-20 02:09:28 +01:00
|
|
|
|
2020-01-16 19:00:57 +00:00
|
|
|
:param node_id: object id to get service from
|
|
|
|
:param service_name: name of service to retrieve
|
|
|
|
:param default_service: True to return default service when custom does
|
2020-01-14 23:26:19 +00:00
|
|
|
not exist, False returns None
|
2017-06-20 02:09:28 +01:00
|
|
|
:return: custom service from the node
|
|
|
|
"""
|
2018-06-22 22:41:06 +01:00
|
|
|
node_services = self.custom_services.setdefault(node_id, {})
|
2018-06-15 22:03:27 +01:00
|
|
|
default = None
|
|
|
|
if default_service:
|
|
|
|
default = ServiceManager.get(service_name)
|
|
|
|
return node_services.get(service_name, default)
|
2013-08-29 15:21:13 +01:00
|
|
|
|
2020-01-14 23:26:19 +00:00
|
|
|
def set_service(self, node_id: int, service_name: str) -> None:
|
2017-06-20 02:09:28 +01:00
|
|
|
"""
|
|
|
|
Store service customizations in an instantiated service object
|
|
|
|
using a list of values that came from a config message.
|
|
|
|
|
2020-01-16 19:00:57 +00:00
|
|
|
:param node_id: object id to set custom service for
|
|
|
|
:param service_name: name of service to set
|
2018-06-15 22:03:27 +01:00
|
|
|
:return: nothing
|
2017-06-20 02:09:28 +01:00
|
|
|
"""
|
2021-04-22 05:09:35 +01:00
|
|
|
logger.debug("setting custom service(%s) for node: %s", service_name, node_id)
|
2018-06-22 22:41:06 +01:00
|
|
|
service = self.get_service(node_id, service_name)
|
2018-06-15 22:03:27 +01:00
|
|
|
if not service:
|
|
|
|
service_class = ServiceManager.get(service_name)
|
|
|
|
service = service_class()
|
2017-06-20 02:09:28 +01:00
|
|
|
|
2018-06-15 22:03:27 +01:00
|
|
|
# add the custom service to dict
|
2018-06-22 22:41:06 +01:00
|
|
|
node_services = self.custom_services.setdefault(node_id, {})
|
2018-06-15 22:03:27 +01:00
|
|
|
node_services[service.name] = service
|
2017-06-20 02:09:28 +01:00
|
|
|
|
2020-01-14 23:26:19 +00:00
|
|
|
def add_services(
|
2023-04-13 23:39:40 +01:00
|
|
|
self, node: CoreNode, model: str, services: list[str] = None
|
2020-01-14 23:26:19 +00:00
|
|
|
) -> None:
|
2017-06-20 02:09:28 +01:00
|
|
|
"""
|
2018-06-15 22:03:27 +01:00
|
|
|
Add services to a node.
|
2017-06-20 02:09:28 +01:00
|
|
|
|
2020-01-16 19:00:57 +00:00
|
|
|
:param node: node to add services to
|
2022-04-05 21:39:57 +01:00
|
|
|
:param model: node model type to add services for
|
2020-01-16 19:00:57 +00:00
|
|
|
:param services: names of services to add to node
|
2017-06-20 02:09:28 +01:00
|
|
|
:return: nothing
|
|
|
|
"""
|
2020-04-16 20:31:48 +01:00
|
|
|
if not services:
|
2021-04-22 05:09:35 +01:00
|
|
|
logger.info(
|
2022-04-05 21:39:57 +01:00
|
|
|
"using default services for node(%s) type(%s)", node.name, model
|
2019-09-10 23:10:24 +01:00
|
|
|
)
|
2022-04-05 21:39:57 +01:00
|
|
|
services = self.default_services.get(model, [])
|
2021-04-22 05:09:35 +01:00
|
|
|
logger.info("setting services for node(%s): %s", node.name, services)
|
2018-06-15 22:03:27 +01:00
|
|
|
for service_name in services:
|
2019-04-27 06:07:51 +01:00
|
|
|
service = self.get_service(node.id, service_name, default_service=True)
|
2018-06-15 22:03:27 +01:00
|
|
|
if not service:
|
2021-04-22 05:09:35 +01:00
|
|
|
logger.warning(
|
2019-09-10 23:10:24 +01:00
|
|
|
"unknown service(%s) for node(%s)", service_name, node.name
|
|
|
|
)
|
2018-06-15 22:03:27 +01:00
|
|
|
continue
|
2019-09-11 21:12:42 +01:00
|
|
|
node.services.append(service)
|
2013-08-29 15:21:13 +01:00
|
|
|
|
2023-04-13 23:39:40 +01:00
|
|
|
def all_configs(self) -> list[tuple[int, "CoreService"]]:
|
2017-06-20 02:09:28 +01:00
|
|
|
"""
|
2020-01-14 23:26:19 +00:00
|
|
|
Return (node_id, service) tuples for all stored configs. Used when reconnecting
|
|
|
|
to a session or opening XML.
|
2017-06-20 02:09:28 +01:00
|
|
|
|
|
|
|
:return: list of tuples of node ids and services
|
2020-01-17 00:12:01 +00:00
|
|
|
"""
|
2017-06-20 02:09:28 +01:00
|
|
|
configs = []
|
2019-05-06 05:23:43 +01:00
|
|
|
for node_id in self.custom_services:
|
|
|
|
custom_services = self.custom_services[node_id]
|
|
|
|
for name in custom_services:
|
|
|
|
service = custom_services[name]
|
2018-06-15 22:03:27 +01:00
|
|
|
configs.append((node_id, service))
|
2017-06-20 02:09:28 +01:00
|
|
|
return configs
|
2013-08-29 15:21:13 +01:00
|
|
|
|
2023-04-13 23:39:40 +01:00
|
|
|
def all_files(self, service: "CoreService") -> list[tuple[str, str]]:
|
2017-06-20 02:09:28 +01:00
|
|
|
"""
|
|
|
|
Return all customized files stored with a service.
|
2013-08-29 15:21:13 +01:00
|
|
|
Used when reconnecting to a session or opening XML.
|
2017-06-20 02:09:28 +01:00
|
|
|
|
2020-01-16 19:00:57 +00:00
|
|
|
:param service: service to get files for
|
2018-06-22 22:41:06 +01:00
|
|
|
:return: list of all custom service files
|
2020-01-17 00:12:01 +00:00
|
|
|
"""
|
2017-06-20 02:09:28 +01:00
|
|
|
files = []
|
2018-06-15 22:03:27 +01:00
|
|
|
if not service.custom:
|
2017-06-20 02:09:28 +01:00
|
|
|
return files
|
|
|
|
|
2018-06-15 22:03:27 +01:00
|
|
|
for filename in service.configs:
|
2018-06-22 22:41:06 +01:00
|
|
|
data = service.config_data.get(filename)
|
2013-08-29 15:21:13 +01:00
|
|
|
if data is None:
|
|
|
|
continue
|
2017-06-20 02:09:28 +01:00
|
|
|
files.append((filename, data))
|
|
|
|
|
|
|
|
return files
|
2013-08-29 15:21:13 +01:00
|
|
|
|
2020-01-14 23:26:19 +00:00
|
|
|
def boot_services(self, node: CoreNode) -> None:
|
2017-06-20 02:09:28 +01:00
|
|
|
"""
|
|
|
|
Start all services on a node.
|
|
|
|
|
2020-01-16 19:00:57 +00:00
|
|
|
:param node: node to start services on
|
2018-02-28 00:28:28 +00:00
|
|
|
:return: nothing
|
2017-06-20 02:09:28 +01:00
|
|
|
"""
|
2020-08-21 19:34:12 +01:00
|
|
|
boot_paths = ServiceDependencies(node.services).boot_order()
|
|
|
|
funcs = []
|
|
|
|
for boot_path in boot_paths:
|
|
|
|
args = (node, boot_path)
|
|
|
|
funcs.append((self._boot_service_path, args, {}))
|
|
|
|
result, exceptions = utils.threadpool(funcs)
|
|
|
|
if exceptions:
|
2020-08-27 18:46:55 +01:00
|
|
|
raise CoreServiceBootError(*exceptions)
|
2020-08-21 19:34:12 +01:00
|
|
|
|
2023-04-13 23:39:40 +01:00
|
|
|
def _boot_service_path(self, node: CoreNode, boot_path: list["CoreServiceType"]):
|
2021-04-22 05:09:35 +01:00
|
|
|
logger.info(
|
2019-09-11 23:05:05 +01:00
|
|
|
"booting node(%s) services: %s",
|
|
|
|
node.name,
|
2020-08-21 19:34:12 +01:00
|
|
|
" -> ".join([x.name for x in boot_path]),
|
2019-09-10 23:10:24 +01:00
|
|
|
)
|
2020-08-21 19:34:12 +01:00
|
|
|
for service in boot_path:
|
2019-12-14 02:17:42 +00:00
|
|
|
service = self.get_service(node.id, service.name, default_service=True)
|
2018-09-12 20:15:53 +01:00
|
|
|
try:
|
|
|
|
self.boot_service(node, service)
|
2020-08-18 21:18:41 +01:00
|
|
|
except Exception as e:
|
2021-04-22 05:09:35 +01:00
|
|
|
logger.exception("exception booting service: %s", service.name)
|
2020-08-27 18:46:55 +01:00
|
|
|
raise CoreServiceBootError(e)
|
2018-06-21 19:20:08 +01:00
|
|
|
|
2020-08-18 21:18:41 +01:00
|
|
|
def boot_service(self, node: CoreNode, service: "CoreServiceType") -> None:
|
2017-06-20 02:09:28 +01:00
|
|
|
"""
|
|
|
|
Start a service on a node. Create private dirs, generate config
|
|
|
|
files, and execute startup commands.
|
|
|
|
|
2020-01-16 19:00:57 +00:00
|
|
|
:param node: node to boot services on
|
|
|
|
:param service: service to start
|
2017-06-20 02:09:28 +01:00
|
|
|
:return: nothing
|
|
|
|
"""
|
2021-04-22 05:09:35 +01:00
|
|
|
logger.info(
|
2019-09-10 23:10:24 +01:00
|
|
|
"starting node(%s) service(%s) validation(%s)",
|
|
|
|
node.name,
|
|
|
|
service.name,
|
|
|
|
service.validation_mode.name,
|
|
|
|
)
|
2018-06-20 20:59:07 +01:00
|
|
|
|
|
|
|
# create service directories
|
2018-06-15 22:03:27 +01:00
|
|
|
for directory in service.dirs:
|
2021-03-19 23:54:24 +00:00
|
|
|
dir_path = Path(directory)
|
2018-10-10 17:43:32 +01:00
|
|
|
try:
|
2021-09-17 22:34:37 +01:00
|
|
|
node.create_dir(dir_path)
|
2021-03-19 23:54:24 +00:00
|
|
|
except (CoreCommandError, CoreError) as e:
|
2021-04-22 05:09:35 +01:00
|
|
|
logger.warning(
|
2019-09-10 23:10:24 +01:00
|
|
|
"error mounting private dir '%s' for service '%s': %s",
|
|
|
|
directory,
|
|
|
|
service.name,
|
|
|
|
e,
|
|
|
|
)
|
2018-10-10 17:43:32 +01:00
|
|
|
|
2018-06-20 20:59:07 +01:00
|
|
|
# create service files
|
2018-06-22 23:47:02 +01:00
|
|
|
self.create_service_files(node, service)
|
2017-06-20 02:09:28 +01:00
|
|
|
|
2018-06-20 20:59:07 +01:00
|
|
|
# run startup
|
|
|
|
wait = service.validation_mode == ServiceMode.BLOCKING
|
2018-06-22 23:47:02 +01:00
|
|
|
status = self.startup_service(node, service, wait)
|
2018-06-20 20:59:07 +01:00
|
|
|
if status:
|
2020-08-27 18:46:55 +01:00
|
|
|
raise CoreServiceBootError(
|
2023-04-13 23:39:40 +01:00
|
|
|
f"node({node.name}) service({service.name}) error during startup"
|
2019-09-10 23:10:24 +01:00
|
|
|
)
|
2017-06-20 02:09:28 +01:00
|
|
|
|
2018-09-26 22:20:19 +01:00
|
|
|
# blocking mode is finished
|
|
|
|
if wait:
|
|
|
|
return
|
|
|
|
|
|
|
|
# timer mode, sleep and return
|
|
|
|
if service.validation_mode == ServiceMode.TIMER:
|
2018-06-20 20:59:07 +01:00
|
|
|
time.sleep(service.validation_timer)
|
2018-09-26 22:20:19 +01:00
|
|
|
# non-blocking, attempt to validate periodically, up to validation_timer time
|
|
|
|
elif service.validation_mode == ServiceMode.NON_BLOCKING:
|
2019-12-06 17:42:41 +00:00
|
|
|
start = time.monotonic()
|
2018-09-26 22:20:19 +01:00
|
|
|
while True:
|
|
|
|
status = self.validate_service(node, service)
|
|
|
|
if not status:
|
|
|
|
break
|
|
|
|
|
2019-12-06 17:42:41 +00:00
|
|
|
if time.monotonic() - start > service.validation_timer:
|
2018-09-26 22:20:19 +01:00
|
|
|
break
|
|
|
|
|
|
|
|
time.sleep(service.validation_period)
|
2017-06-20 02:09:28 +01:00
|
|
|
|
2018-06-20 20:59:07 +01:00
|
|
|
if status:
|
2020-08-27 18:46:55 +01:00
|
|
|
raise CoreServiceBootError(
|
2023-04-13 23:39:40 +01:00
|
|
|
f"node({node.name}) service({service.name}) failed validation"
|
2019-09-10 23:10:24 +01:00
|
|
|
)
|
2017-06-20 02:09:28 +01:00
|
|
|
|
2021-03-19 23:54:24 +00:00
|
|
|
def copy_service_file(self, node: CoreNode, file_path: Path, cfg: str) -> bool:
|
2017-06-20 02:09:28 +01:00
|
|
|
"""
|
|
|
|
Given a configured service filename and config, determine if the
|
2013-08-29 15:21:13 +01:00
|
|
|
config references an existing file that should be copied.
|
|
|
|
Returns True for local files, False for generated.
|
2017-06-20 02:09:28 +01:00
|
|
|
|
2020-01-16 19:00:57 +00:00
|
|
|
:param node: node to copy service for
|
2021-03-19 23:54:24 +00:00
|
|
|
:param file_path: file name for a configured service
|
2020-01-16 19:00:57 +00:00
|
|
|
:param cfg: configuration string
|
2017-06-20 02:09:28 +01:00
|
|
|
:return: True if successful, False otherwise
|
2020-01-17 00:12:01 +00:00
|
|
|
"""
|
2019-09-10 23:10:24 +01:00
|
|
|
if cfg[:7] == "file://":
|
2013-08-29 15:21:13 +01:00
|
|
|
src = cfg[7:]
|
2019-09-10 23:10:24 +01:00
|
|
|
src = src.split("\n")[0]
|
2018-03-02 17:15:52 +00:00
|
|
|
src = utils.expand_corepath(src, node.session, node)
|
2013-08-29 15:21:13 +01:00
|
|
|
# TODO: glob here
|
2021-09-17 22:34:37 +01:00
|
|
|
node.copy_file(src, file_path, mode=0o644)
|
2013-08-29 15:21:13 +01:00
|
|
|
return True
|
|
|
|
return False
|
|
|
|
|
2020-08-18 21:18:41 +01:00
|
|
|
def validate_service(self, node: CoreNode, service: "CoreServiceType") -> int:
|
2017-06-20 02:09:28 +01:00
|
|
|
"""
|
|
|
|
Run the validation command(s) for a service.
|
|
|
|
|
2020-01-16 19:00:57 +00:00
|
|
|
:param node: node to validate service for
|
|
|
|
:param service: service to validate
|
2017-06-20 02:09:28 +01:00
|
|
|
:return: service validation status
|
2020-01-17 00:12:01 +00:00
|
|
|
"""
|
2021-04-22 05:09:35 +01:00
|
|
|
logger.debug("validating node(%s) service(%s)", node.name, service.name)
|
2018-06-20 20:59:07 +01:00
|
|
|
cmds = service.validate
|
|
|
|
if not service.custom:
|
2018-06-22 23:47:02 +01:00
|
|
|
cmds = service.get_validate(node)
|
2017-06-20 02:09:28 +01:00
|
|
|
|
|
|
|
status = 0
|
2018-06-20 20:59:07 +01:00
|
|
|
for cmd in cmds:
|
2021-04-22 05:09:35 +01:00
|
|
|
logger.debug("validating service(%s) using: %s", service.name, cmd)
|
2018-06-15 22:03:27 +01:00
|
|
|
try:
|
2019-10-19 07:28:09 +01:00
|
|
|
node.cmd(cmd)
|
2018-09-26 22:20:19 +01:00
|
|
|
except CoreCommandError as e:
|
2021-04-22 05:09:35 +01:00
|
|
|
logger.debug(
|
2019-09-10 23:10:24 +01:00
|
|
|
"node(%s) service(%s) validate failed", node.name, service.name
|
|
|
|
)
|
2021-04-22 05:09:35 +01:00
|
|
|
logger.debug("cmd(%s): %s", e.cmd, e.output)
|
2018-06-15 22:03:27 +01:00
|
|
|
status = -1
|
2018-09-26 22:20:19 +01:00
|
|
|
break
|
2017-06-20 02:09:28 +01:00
|
|
|
|
2014-03-05 16:28:32 +00:00
|
|
|
return status
|
2017-06-20 02:09:28 +01:00
|
|
|
|
2020-01-14 23:26:19 +00:00
|
|
|
def stop_services(self, node: CoreNode) -> None:
|
2017-06-20 02:09:28 +01:00
|
|
|
"""
|
|
|
|
Stop all services on a node.
|
|
|
|
|
2020-01-16 19:00:57 +00:00
|
|
|
:param node: node to stop services on
|
2017-06-20 02:09:28 +01:00
|
|
|
:return: nothing
|
|
|
|
"""
|
2018-06-22 16:16:59 +01:00
|
|
|
for service in node.services:
|
2018-06-22 23:47:02 +01:00
|
|
|
self.stop_service(node, service)
|
2017-06-20 02:09:28 +01:00
|
|
|
|
2020-08-18 21:18:41 +01:00
|
|
|
def stop_service(self, node: CoreNode, service: "CoreServiceType") -> int:
|
2017-06-20 02:09:28 +01:00
|
|
|
"""
|
|
|
|
Stop a service on a node.
|
|
|
|
|
2020-01-16 19:00:57 +00:00
|
|
|
:param node: node to stop a service on
|
|
|
|
:param service: service to stop
|
2017-06-20 02:09:28 +01:00
|
|
|
:return: status for stopping the services
|
|
|
|
"""
|
2018-06-20 20:59:07 +01:00
|
|
|
status = 0
|
2018-06-15 22:03:27 +01:00
|
|
|
for args in service.shutdown:
|
|
|
|
try:
|
2019-10-19 07:28:09 +01:00
|
|
|
node.cmd(args)
|
2019-12-20 18:51:52 +00:00
|
|
|
except CoreCommandError as e:
|
|
|
|
self.session.exception(
|
|
|
|
ExceptionLevels.ERROR,
|
|
|
|
"services",
|
|
|
|
f"error stopping service {service.name}: {e.stderr}",
|
2020-04-01 00:39:27 +01:00
|
|
|
node.id,
|
2019-12-20 18:51:52 +00:00
|
|
|
)
|
2021-04-22 05:09:35 +01:00
|
|
|
logger.exception("error running stop command %s", args)
|
2018-06-20 20:59:07 +01:00
|
|
|
status = -1
|
2014-03-05 16:28:32 +00:00
|
|
|
return status
|
2013-08-29 15:21:13 +01:00
|
|
|
|
2020-03-31 17:41:29 +01:00
|
|
|
def get_service_file(
|
|
|
|
self, node: CoreNode, service_name: str, filename: str
|
|
|
|
) -> FileData:
|
2017-06-20 02:09:28 +01:00
|
|
|
"""
|
|
|
|
Send a File Message when the GUI has requested a service file.
|
2013-08-29 15:21:13 +01:00
|
|
|
The file data is either auto-generated or comes from an existing config.
|
2017-06-20 02:09:28 +01:00
|
|
|
|
2020-01-16 19:00:57 +00:00
|
|
|
:param node: node to get service file from
|
|
|
|
:param service_name: service to get file from
|
|
|
|
:param filename: file name to retrieve
|
2020-03-31 17:41:29 +01:00
|
|
|
:return: file data
|
2017-06-20 02:09:28 +01:00
|
|
|
"""
|
2018-06-15 22:03:27 +01:00
|
|
|
# get service to get file from
|
2019-04-27 06:07:51 +01:00
|
|
|
service = self.get_service(node.id, service_name, default_service=True)
|
2018-06-15 22:03:27 +01:00
|
|
|
if not service:
|
|
|
|
raise ValueError("invalid service: %s", service_name)
|
|
|
|
|
|
|
|
# retrieve config files for default/custom service
|
|
|
|
if service.custom:
|
|
|
|
config_files = service.configs
|
2013-08-29 15:21:13 +01:00
|
|
|
else:
|
2018-06-22 23:47:02 +01:00
|
|
|
config_files = service.get_configs(node)
|
2018-06-15 22:03:27 +01:00
|
|
|
|
|
|
|
if filename not in config_files:
|
2019-09-10 23:10:24 +01:00
|
|
|
raise ValueError(
|
|
|
|
"unknown service(%s) config file: %s", service_name, filename
|
|
|
|
)
|
2017-06-20 02:09:28 +01:00
|
|
|
|
2013-08-29 15:21:13 +01:00
|
|
|
# get the file data
|
2018-06-22 22:41:06 +01:00
|
|
|
data = service.config_data.get(filename)
|
2013-08-29 15:21:13 +01:00
|
|
|
if data is None:
|
2023-04-13 23:39:40 +01:00
|
|
|
data = service.generate_config(node, filename)
|
2013-08-29 15:21:13 +01:00
|
|
|
else:
|
2023-04-13 23:39:40 +01:00
|
|
|
data = data
|
2017-06-20 02:09:28 +01:00
|
|
|
|
2023-04-13 23:39:40 +01:00
|
|
|
filetypestr = f"service:{service.name}"
|
2017-07-11 23:51:56 +01:00
|
|
|
return FileData(
|
2020-03-13 06:12:17 +00:00
|
|
|
message_type=MessageFlags.ADD,
|
2019-04-27 06:07:51 +01:00
|
|
|
node=node.id,
|
2017-07-11 23:51:56 +01:00
|
|
|
name=filename,
|
|
|
|
type=filetypestr,
|
2019-09-10 23:10:24 +01:00
|
|
|
data=data,
|
2017-07-11 23:51:56 +01:00
|
|
|
)
|
2017-06-20 02:09:28 +01:00
|
|
|
|
2020-01-14 23:26:19 +00:00
|
|
|
def set_service_file(
|
|
|
|
self, node_id: int, service_name: str, file_name: str, data: str
|
|
|
|
) -> None:
|
2017-06-20 02:09:28 +01:00
|
|
|
"""
|
|
|
|
Receive a File Message from the GUI and store the customized file
|
2013-08-29 15:21:13 +01:00
|
|
|
in the service config. The filename must match one from the list of
|
|
|
|
config files in the service.
|
2017-06-20 02:09:28 +01:00
|
|
|
|
2020-01-16 19:00:57 +00:00
|
|
|
:param node_id: node id to set service file
|
|
|
|
:param service_name: service name to set file for
|
|
|
|
:param file_name: file name to set
|
2017-06-20 02:09:28 +01:00
|
|
|
:param data: data for file to set
|
|
|
|
:return: nothing
|
|
|
|
"""
|
2018-06-15 22:03:27 +01:00
|
|
|
# attempt to set custom service, if needed
|
2018-06-22 22:41:06 +01:00
|
|
|
self.set_service(node_id, service_name)
|
2018-06-15 22:03:27 +01:00
|
|
|
|
|
|
|
# retrieve custom service
|
2018-06-22 22:41:06 +01:00
|
|
|
service = self.get_service(node_id, service_name)
|
2018-06-20 20:59:07 +01:00
|
|
|
if service is None:
|
2021-04-22 05:09:35 +01:00
|
|
|
logger.warning("received file name for unknown service: %s", service_name)
|
2013-08-29 15:21:13 +01:00
|
|
|
return
|
2018-06-15 22:03:27 +01:00
|
|
|
|
|
|
|
# validate file being set is valid
|
2018-06-22 23:47:02 +01:00
|
|
|
config_files = service.configs
|
|
|
|
if file_name not in config_files:
|
2021-04-22 05:09:35 +01:00
|
|
|
logger.warning(
|
2019-09-10 23:10:24 +01:00
|
|
|
"received unknown file(%s) for service(%s)", file_name, service_name
|
|
|
|
)
|
2013-08-29 15:21:13 +01:00
|
|
|
return
|
2018-06-15 22:03:27 +01:00
|
|
|
|
|
|
|
# set custom service file data
|
2018-06-22 23:47:02 +01:00
|
|
|
service.config_data[file_name] = data
|
2018-06-15 22:03:27 +01:00
|
|
|
|
2020-01-14 23:26:19 +00:00
|
|
|
def startup_service(
|
2020-08-18 21:18:41 +01:00
|
|
|
self, node: CoreNode, service: "CoreServiceType", wait: bool = False
|
2020-01-14 23:26:19 +00:00
|
|
|
) -> int:
|
2018-06-15 22:03:27 +01:00
|
|
|
"""
|
|
|
|
Startup a node service.
|
|
|
|
|
2020-01-16 19:00:57 +00:00
|
|
|
:param node: node to reconfigure service for
|
|
|
|
:param service: service to reconfigure
|
|
|
|
:param wait: determines if we should wait to validate startup
|
2018-06-15 22:03:27 +01:00
|
|
|
:return: status of startup
|
2020-01-17 00:12:01 +00:00
|
|
|
"""
|
2018-06-20 20:59:07 +01:00
|
|
|
cmds = service.startup
|
|
|
|
if not service.custom:
|
2018-06-22 23:47:02 +01:00
|
|
|
cmds = service.get_startup(node)
|
2018-06-15 22:03:27 +01:00
|
|
|
|
2018-06-20 20:59:07 +01:00
|
|
|
status = 0
|
|
|
|
for cmd in cmds:
|
2018-06-15 22:03:27 +01:00
|
|
|
try:
|
2019-10-19 07:28:09 +01:00
|
|
|
node.cmd(cmd, wait)
|
2018-06-15 22:03:27 +01:00
|
|
|
except CoreCommandError:
|
2021-04-22 05:09:35 +01:00
|
|
|
logger.exception("error starting command")
|
2018-06-20 20:59:07 +01:00
|
|
|
status = -1
|
2018-06-15 22:03:27 +01:00
|
|
|
return status
|
|
|
|
|
2020-08-18 21:18:41 +01:00
|
|
|
def create_service_files(self, node: CoreNode, service: "CoreServiceType") -> None:
|
2018-06-20 20:59:07 +01:00
|
|
|
"""
|
|
|
|
Creates node service files.
|
|
|
|
|
2020-01-16 19:00:57 +00:00
|
|
|
:param node: node to reconfigure service for
|
|
|
|
:param service: service to reconfigure
|
2018-06-20 20:59:07 +01:00
|
|
|
:return: nothing
|
|
|
|
"""
|
|
|
|
# get values depending on if custom or not
|
2018-06-22 23:47:02 +01:00
|
|
|
config_files = service.configs
|
2018-06-20 20:59:07 +01:00
|
|
|
if not service.custom:
|
2018-06-22 23:47:02 +01:00
|
|
|
config_files = service.get_configs(node)
|
|
|
|
for file_name in config_files:
|
2021-03-19 23:54:24 +00:00
|
|
|
file_path = Path(file_name)
|
2021-04-22 05:09:35 +01:00
|
|
|
logger.debug(
|
2019-12-14 02:17:42 +00:00
|
|
|
"generating service config custom(%s): %s", service.custom, file_name
|
|
|
|
)
|
2018-06-20 20:59:07 +01:00
|
|
|
if service.custom:
|
2018-06-22 22:41:06 +01:00
|
|
|
cfg = service.config_data.get(file_name)
|
2018-06-20 20:59:07 +01:00
|
|
|
if cfg is None:
|
2018-06-22 23:47:02 +01:00
|
|
|
cfg = service.generate_config(node, file_name)
|
2018-06-20 20:59:07 +01:00
|
|
|
# cfg may have a file:/// url for copying from a file
|
|
|
|
try:
|
2021-03-19 23:54:24 +00:00
|
|
|
if self.copy_service_file(node, file_path, cfg):
|
2018-06-20 20:59:07 +01:00
|
|
|
continue
|
2023-04-13 23:39:40 +01:00
|
|
|
except OSError:
|
2021-04-22 05:09:35 +01:00
|
|
|
logger.exception("error copying service file: %s", file_name)
|
2018-06-20 20:59:07 +01:00
|
|
|
continue
|
|
|
|
else:
|
2018-06-22 23:47:02 +01:00
|
|
|
cfg = service.generate_config(node, file_name)
|
2021-09-17 22:34:37 +01:00
|
|
|
node.create_file(file_path, cfg)
|
2018-06-20 20:59:07 +01:00
|
|
|
|
2020-01-14 23:26:19 +00:00
|
|
|
def service_reconfigure(self, node: CoreNode, service: "CoreService") -> None:
|
2018-06-15 22:03:27 +01:00
|
|
|
"""
|
|
|
|
Reconfigure a node service.
|
|
|
|
|
2020-01-16 19:00:57 +00:00
|
|
|
:param node: node to reconfigure service for
|
|
|
|
:param service: service to reconfigure
|
2017-06-20 02:09:28 +01:00
|
|
|
:return: nothing
|
|
|
|
"""
|
2018-06-22 23:47:02 +01:00
|
|
|
config_files = service.configs
|
2018-06-20 20:59:07 +01:00
|
|
|
if not service.custom:
|
2018-06-22 23:47:02 +01:00
|
|
|
config_files = service.get_configs(node)
|
|
|
|
for file_name in config_files:
|
2021-03-19 23:54:24 +00:00
|
|
|
file_path = Path(file_name)
|
2018-06-20 20:59:07 +01:00
|
|
|
if file_name[:7] == "file:///":
|
2018-06-15 22:03:27 +01:00
|
|
|
# TODO: implement this
|
|
|
|
raise NotImplementedError
|
2018-06-22 22:41:06 +01:00
|
|
|
cfg = service.config_data.get(file_name)
|
2018-06-15 22:03:27 +01:00
|
|
|
if cfg is None:
|
2018-06-22 23:47:02 +01:00
|
|
|
cfg = service.generate_config(node, file_name)
|
2021-09-17 22:34:37 +01:00
|
|
|
node.create_file(file_path, cfg)
|
2017-06-20 02:09:28 +01:00
|
|
|
|
2013-08-29 15:21:13 +01:00
|
|
|
|
2019-10-23 17:31:07 +01:00
|
|
|
class CoreService:
|
2017-06-20 02:09:28 +01:00
|
|
|
"""
|
|
|
|
Parent class used for defining services.
|
|
|
|
"""
|
2019-09-10 23:10:24 +01:00
|
|
|
|
2013-08-29 15:21:13 +01:00
|
|
|
# service name should not include spaces
|
2020-06-14 06:25:38 +01:00
|
|
|
name: Optional[str] = None
|
2018-06-14 20:50:48 +01:00
|
|
|
|
2018-06-19 17:19:49 +01:00
|
|
|
# executables that must exist for service to run
|
2023-04-13 23:39:40 +01:00
|
|
|
executables: tuple[str, ...] = ()
|
2018-06-19 17:19:49 +01:00
|
|
|
|
2018-06-20 02:36:53 +01:00
|
|
|
# sets service requirements that must be started prior to this service starting
|
2023-04-13 23:39:40 +01:00
|
|
|
dependencies: tuple[str, ...] = ()
|
2018-06-20 02:36:53 +01:00
|
|
|
|
2013-08-29 15:21:13 +01:00
|
|
|
# group string allows grouping services together
|
2020-06-14 06:25:38 +01:00
|
|
|
group: Optional[str] = None
|
2018-06-14 20:50:48 +01:00
|
|
|
|
2013-08-29 15:21:13 +01:00
|
|
|
# private, per-node directories required by this service
|
2023-04-13 23:39:40 +01:00
|
|
|
dirs: tuple[str, ...] = ()
|
2018-06-14 20:50:48 +01:00
|
|
|
|
2013-08-29 15:21:13 +01:00
|
|
|
# config files written by this service
|
2023-04-13 23:39:40 +01:00
|
|
|
configs: tuple[str, ...] = ()
|
2018-06-14 20:50:48 +01:00
|
|
|
|
2018-06-22 22:41:06 +01:00
|
|
|
# config file data
|
2023-04-13 23:39:40 +01:00
|
|
|
config_data: dict[str, str] = {}
|
2018-06-22 22:41:06 +01:00
|
|
|
|
2013-08-29 15:21:13 +01:00
|
|
|
# list of startup commands
|
2023-04-13 23:39:40 +01:00
|
|
|
startup: tuple[str, ...] = ()
|
2018-06-14 20:50:48 +01:00
|
|
|
|
2013-08-29 15:21:13 +01:00
|
|
|
# list of shutdown commands
|
2023-04-13 23:39:40 +01:00
|
|
|
shutdown: tuple[str, ...] = ()
|
2018-06-14 20:50:48 +01:00
|
|
|
|
2013-08-29 15:21:13 +01:00
|
|
|
# list of validate commands
|
2023-04-13 23:39:40 +01:00
|
|
|
validate: tuple[str, ...] = ()
|
2018-06-14 20:50:48 +01:00
|
|
|
|
2018-06-20 20:59:07 +01:00
|
|
|
# validation mode, used to determine startup success
|
2020-06-14 06:25:38 +01:00
|
|
|
validation_mode: ServiceMode = ServiceMode.NON_BLOCKING
|
2018-06-20 20:59:07 +01:00
|
|
|
|
2018-09-26 22:20:19 +01:00
|
|
|
# time to wait in seconds for determining if service started successfully
|
2020-06-14 06:25:38 +01:00
|
|
|
validation_timer: int = 5
|
2018-09-26 22:20:19 +01:00
|
|
|
|
|
|
|
# validation period in seconds, how frequent validation is attempted
|
2020-06-14 06:25:38 +01:00
|
|
|
validation_period: float = 0.5
|
2018-06-20 20:59:07 +01:00
|
|
|
|
2013-08-29 15:21:13 +01:00
|
|
|
# metadata associated with this service
|
2020-06-14 06:25:38 +01:00
|
|
|
meta: Optional[str] = None
|
2018-06-14 20:50:48 +01:00
|
|
|
|
2013-08-29 15:21:13 +01:00
|
|
|
# custom configuration text
|
2020-06-14 06:25:38 +01:00
|
|
|
custom: bool = False
|
|
|
|
custom_needed: bool = False
|
2013-08-29 15:21:13 +01:00
|
|
|
|
2020-01-14 23:26:19 +00:00
|
|
|
def __init__(self) -> None:
|
2017-06-20 02:09:28 +01:00
|
|
|
"""
|
|
|
|
Services are not necessarily instantiated. Classmethods may be used
|
|
|
|
against their config. Services are instantiated when a custom
|
|
|
|
configuration is used to override their default parameters.
|
|
|
|
"""
|
2020-06-14 06:25:38 +01:00
|
|
|
self.custom: bool = True
|
2023-04-13 23:39:40 +01:00
|
|
|
self.config_data: dict[str, str] = self.__class__.config_data.copy()
|
2017-06-20 02:09:28 +01:00
|
|
|
|
2017-08-08 00:40:39 +01:00
|
|
|
@classmethod
|
2020-01-14 23:26:19 +00:00
|
|
|
def on_load(cls) -> None:
|
2017-08-08 00:40:39 +01:00
|
|
|
pass
|
|
|
|
|
2013-08-29 15:21:13 +01:00
|
|
|
@classmethod
|
2020-01-14 23:26:19 +00:00
|
|
|
def get_configs(cls, node: CoreNode) -> Iterable[str]:
|
2017-06-20 02:09:28 +01:00
|
|
|
"""
|
|
|
|
Return the tuple of configuration file filenames. This default method
|
|
|
|
returns the cls._configs tuple, but this method may be overriden to
|
|
|
|
provide node-specific filenames that may be based on other services.
|
|
|
|
|
2020-01-16 19:00:57 +00:00
|
|
|
:param node: node to generate config for
|
2018-06-21 22:56:30 +01:00
|
|
|
:return: configuration files
|
2020-01-17 00:12:01 +00:00
|
|
|
"""
|
2018-06-15 22:03:27 +01:00
|
|
|
return cls.configs
|
2017-06-20 02:09:28 +01:00
|
|
|
|
2013-08-29 15:21:13 +01:00
|
|
|
@classmethod
|
2023-04-13 23:39:40 +01:00
|
|
|
def generate_config(cls, node: CoreNode, filename: str) -> str:
|
2017-06-20 02:09:28 +01:00
|
|
|
"""
|
|
|
|
Generate configuration file given a node object. The filename is
|
2018-06-22 22:41:06 +01:00
|
|
|
provided to allow for multiple config files.
|
2017-06-20 02:09:28 +01:00
|
|
|
Return the configuration string to be written to a file or sent
|
|
|
|
to the GUI for customization.
|
|
|
|
|
2020-01-16 19:00:57 +00:00
|
|
|
:param node: node to generate config for
|
|
|
|
:param filename: file name to generate config for
|
2023-04-13 23:39:40 +01:00
|
|
|
:return: generated config
|
2017-06-20 02:09:28 +01:00
|
|
|
"""
|
2013-08-29 15:21:13 +01:00
|
|
|
raise NotImplementedError
|
2017-06-20 02:09:28 +01:00
|
|
|
|
2013-08-29 15:21:13 +01:00
|
|
|
@classmethod
|
2020-01-14 23:26:19 +00:00
|
|
|
def get_startup(cls, node: CoreNode) -> Iterable[str]:
|
2017-06-20 02:09:28 +01:00
|
|
|
"""
|
|
|
|
Return the tuple of startup commands. This default method
|
2018-06-22 22:41:06 +01:00
|
|
|
returns the cls.startup tuple, but this method may be
|
2017-06-20 02:09:28 +01:00
|
|
|
overridden to provide node-specific commands that may be
|
|
|
|
based on other services.
|
|
|
|
|
2020-01-16 19:00:57 +00:00
|
|
|
:param node: node to get startup for
|
2017-06-20 02:09:28 +01:00
|
|
|
:return: startup commands
|
2020-01-17 00:12:01 +00:00
|
|
|
"""
|
2018-06-15 22:03:27 +01:00
|
|
|
return cls.startup
|
2013-08-29 15:21:13 +01:00
|
|
|
|
|
|
|
@classmethod
|
2020-01-14 23:26:19 +00:00
|
|
|
def get_validate(cls, node: CoreNode) -> Iterable[str]:
|
2017-06-20 02:09:28 +01:00
|
|
|
"""
|
|
|
|
Return the tuple of validate commands. This default method
|
2018-06-22 22:41:06 +01:00
|
|
|
returns the cls.validate tuple, but this method may be
|
|
|
|
overridden to provide node-specific commands that may be
|
2017-06-20 02:09:28 +01:00
|
|
|
based on other services.
|
|
|
|
|
2020-01-16 19:00:57 +00:00
|
|
|
:param node: node to validate
|
2017-06-20 02:09:28 +01:00
|
|
|
:return: validation commands
|
2020-01-17 00:12:01 +00:00
|
|
|
"""
|
2018-06-15 22:03:27 +01:00
|
|
|
return cls.validate
|