Skip to content

Commit

Permalink
daemon: adjusted services to avoid using abstractmethod and provide s…
Browse files Browse the repository at this point in the history
…ane defaults, avoiding the need to verbosely define services and only define what matters
  • Loading branch information
bharnden committed Nov 17, 2023
1 parent 54dc7e6 commit a622b8e
Show file tree
Hide file tree
Showing 8 changed files with 58 additions and 244 deletions.
98 changes: 24 additions & 74 deletions daemon/core/services/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,12 +60,34 @@ class CoreService(abc.ABC):
Base class for creating services.
"""

# globally unique name for service
name: Optional[str] = None
# group to categorize service within
group: Optional[str] = None
# directories to create unique mount points for
directories: list[str] = []
# files to create for service
files: list[str] = []
# configurable values that this service can use, for file generation
default_configs: list[Configuration] = []
# executables that should exist on path, that this service depends on
executables: list[str] = []
# other services that this service depends on, defines service start order
dependencies: list[str] = []
# commands to run to start this service
startup: list[str] = []
# commands to run to validate this service
validate: list[str] = []
# commands to run to stop this service
shutdown: list[str] = []
# validation mode, blocking, non-blocking, and timer
validation_mode: ServiceMode = ServiceMode.BLOCKING
# predefined configuration value groupings
modes: dict[str, dict[str, str]] = {}
# validation period in seconds, how frequent validation is attempted
validation_period: float = 0.5

# time to wait in seconds for determining if service started successfully
validation_timer: int = 5

# directories to shadow and copy files from
shadow_directories: list[ShadowDir] = []

Expand Down Expand Up @@ -96,66 +118,6 @@ def clean_text(text: str) -> str:
"""
return inspect.cleandoc(text)

@property
@abc.abstractmethod
def name(self) -> str:
raise NotImplementedError

@property
@abc.abstractmethod
def group(self) -> str:
raise NotImplementedError

@property
@abc.abstractmethod
def directories(self) -> list[str]:
raise NotImplementedError

@property
@abc.abstractmethod
def files(self) -> list[str]:
raise NotImplementedError

@property
@abc.abstractmethod
def default_configs(self) -> list[Configuration]:
raise NotImplementedError

@property
@abc.abstractmethod
def modes(self) -> dict[str, dict[str, str]]:
raise NotImplementedError

@property
@abc.abstractmethod
def executables(self) -> list[str]:
raise NotImplementedError

@property
@abc.abstractmethod
def dependencies(self) -> list[str]:
raise NotImplementedError

@property
@abc.abstractmethod
def startup(self) -> list[str]:
raise NotImplementedError

@property
@abc.abstractmethod
def validate(self) -> list[str]:
raise NotImplementedError

@property
@abc.abstractmethod
def shutdown(self) -> list[str]:
raise NotImplementedError

@property
@abc.abstractmethod
def validation_mode(self) -> ServiceMode:
raise NotImplementedError

def start(self) -> None:
"""
Creates services files/directories, runs startup, and validates based on
Expand Down Expand Up @@ -511,18 +473,6 @@ def set_config(self, data: dict[str, str]) -> None:


class CustomCoreService(CoreService):
name: str = ""
group: str = ""
directories: list[str] = []
files: list[str] = []
executables: list[str] = []
dependencies: list[str] = []
startup: list[str] = []
validate: list[str] = []
shutdown: list[str] = []
validation_mode: ServiceMode = ServiceMode.NON_BLOCKING
default_configs: list[Configuration] = []
modes: dict[str, dict[str, str]] = {}
defined_templates: dict[str, str] = {}

def get_templates(self) -> dict[str, str]:
Expand Down
16 changes: 1 addition & 15 deletions daemon/core/services/defaults/frrservices/services.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
import abc
from typing import Any

from core.config import Configuration
from core.emane.nodes import EmaneNet
from core.nodes.base import CoreNodeBase, NodeBase
from core.nodes.interface import DEFAULT_MTU, CoreInterface
from core.nodes.network import PtpNet, WlanNode
from core.nodes.physical import Rj45Node
from core.nodes.wireless import WirelessNode
from core.services.base import CoreService, ServiceMode
from core.services.base import CoreService

GROUP: str = "FRR"
FRR_STATE_DIR: str = "/var/run/frr"
Expand Down Expand Up @@ -90,13 +89,9 @@ class FRRZebra(CoreService):
"/usr/local/etc/frr/daemons",
]
executables: list[str] = ["zebra"]
dependencies: list[str] = []
startup: list[str] = ["bash frrboot.sh zebra"]
validate: list[str] = ["pidof zebra"]
shutdown: list[str] = ["killall zebra"]
validation_mode: ServiceMode = ServiceMode.BLOCKING
default_configs: list[Configuration] = []
modes: dict[str, dict[str, str]] = {}

def data(self) -> dict[str, Any]:
frr_conf = self.files[0]
Expand Down Expand Up @@ -146,16 +141,7 @@ def data(self) -> dict[str, Any]:

class FrrService(abc.ABC):
group: str = GROUP
directories: list[str] = []
files: list[str] = []
executables: list[str] = []
dependencies: list[str] = ["FRRzebra"]
startup: list[str] = []
validate: list[str] = []
shutdown: list[str] = []
validation_mode: ServiceMode = ServiceMode.BLOCKING
default_configs: list[Configuration] = []
modes: dict[str, dict[str, str]] = {}
ipv4_routing: bool = False
ipv6_routing: bool = False

Expand Down
37 changes: 1 addition & 36 deletions daemon/core/services/defaults/nrlservices/services.py
Original file line number Diff line number Diff line change
@@ -1,25 +1,19 @@
from typing import Any

from core import utils
from core.config import Configuration
from core.services.base import CoreService, ServiceMode
from core.services.base import CoreService

GROUP: str = "ProtoSvc"


class MgenSinkService(CoreService):
name: str = "MGEN_Sink"
group: str = GROUP
directories: list[str] = []
files: list[str] = ["mgensink.sh", "sink.mgen"]
executables: list[str] = ["mgen"]
dependencies: list[str] = []
startup: list[str] = ["bash mgensink.sh"]
validate: list[str] = ["pidof mgen"]
shutdown: list[str] = ["killall mgen"]
validation_mode: ServiceMode = ServiceMode.BLOCKING
default_configs: list[Configuration] = []
modes: dict[str, dict[str, str]] = {}

def data(self) -> dict[str, Any]:
ifnames = []
Expand All @@ -32,16 +26,11 @@ def data(self) -> dict[str, Any]:
class NrlNhdp(CoreService):
name: str = "NHDP"
group: str = GROUP
directories: list[str] = []
files: list[str] = ["nrlnhdp.sh"]
executables: list[str] = ["nrlnhdp"]
dependencies: list[str] = []
startup: list[str] = ["bash nrlnhdp.sh"]
validate: list[str] = ["pidof nrlnhdp"]
shutdown: list[str] = ["killall nrlnhdp"]
validation_mode: ServiceMode = ServiceMode.BLOCKING
default_configs: list[Configuration] = []
modes: dict[str, dict[str, str]] = {}

def data(self) -> dict[str, Any]:
has_smf = "SMF" in self.node.services
Expand All @@ -54,16 +43,11 @@ def data(self) -> dict[str, Any]:
class NrlSmf(CoreService):
name: str = "SMF"
group: str = GROUP
directories: list[str] = []
files: list[str] = ["startsmf.sh"]
executables: list[str] = ["nrlsmf", "killall"]
dependencies: list[str] = []
startup: list[str] = ["bash startsmf.sh"]
validate: list[str] = ["pidof nrlsmf"]
shutdown: list[str] = ["killall nrlsmf"]
validation_mode: ServiceMode = ServiceMode.BLOCKING
default_configs: list[Configuration] = []
modes: dict[str, dict[str, str]] = {}

def data(self) -> dict[str, Any]:
has_nhdp = "NHDP" in self.node.services
Expand All @@ -84,16 +68,11 @@ def data(self) -> dict[str, Any]:
class NrlOlsr(CoreService):
name: str = "OLSR"
group: str = GROUP
directories: list[str] = []
files: list[str] = ["nrlolsrd.sh"]
executables: list[str] = ["nrlolsrd"]
dependencies: list[str] = []
startup: list[str] = ["bash nrlolsrd.sh"]
validate: list[str] = ["pidof nrlolsrd"]
shutdown: list[str] = ["killall nrlolsrd"]
validation_mode: ServiceMode = ServiceMode.BLOCKING
default_configs: list[Configuration] = []
modes: dict[str, dict[str, str]] = {}

def data(self) -> dict[str, Any]:
has_smf = "SMF" in self.node.services
Expand All @@ -108,16 +87,11 @@ def data(self) -> dict[str, Any]:
class NrlOlsrv2(CoreService):
name: str = "OLSRv2"
group: str = GROUP
directories: list[str] = []
files: list[str] = ["nrlolsrv2.sh"]
executables: list[str] = ["nrlolsrv2"]
dependencies: list[str] = []
startup: list[str] = ["bash nrlolsrv2.sh"]
validate: list[str] = ["pidof nrlolsrv2"]
shutdown: list[str] = ["killall nrlolsrv2"]
validation_mode: ServiceMode = ServiceMode.BLOCKING
default_configs: list[Configuration] = []
modes: dict[str, dict[str, str]] = {}

def data(self) -> dict[str, Any]:
has_smf = "SMF" in self.node.services
Expand All @@ -133,13 +107,9 @@ class OlsrOrg(CoreService):
directories: list[str] = ["/etc/olsrd"]
files: list[str] = ["olsrd.sh", "/etc/olsrd/olsrd.conf"]
executables: list[str] = ["olsrd"]
dependencies: list[str] = []
startup: list[str] = ["bash olsrd.sh"]
validate: list[str] = ["pidof olsrd"]
shutdown: list[str] = ["killall olsrd"]
validation_mode: ServiceMode = ServiceMode.BLOCKING
default_configs: list[Configuration] = []
modes: dict[str, dict[str, str]] = {}

def data(self) -> dict[str, Any]:
has_smf = "SMF" in self.node.services
Expand All @@ -152,13 +122,8 @@ def data(self) -> dict[str, Any]:
class MgenActor(CoreService):
name: str = "MgenActor"
group: str = GROUP
directories: list[str] = []
files: list[str] = ["start_mgen_actor.sh"]
executables: list[str] = ["mgen"]
dependencies: list[str] = []
startup: list[str] = ["bash start_mgen_actor.sh"]
validate: list[str] = ["pidof mgen"]
shutdown: list[str] = ["killall mgen"]
validation_mode: ServiceMode = ServiceMode.BLOCKING
default_configs: list[Configuration] = []
modes: dict[str, dict[str, str]] = {}
16 changes: 1 addition & 15 deletions daemon/core/services/defaults/quaggaservices/services.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,13 @@
import logging
from typing import Any

from core.config import Configuration
from core.emane.nodes import EmaneNet
from core.nodes.base import CoreNodeBase, NodeBase
from core.nodes.interface import DEFAULT_MTU, CoreInterface
from core.nodes.network import PtpNet, WlanNode
from core.nodes.physical import Rj45Node
from core.nodes.wireless import WirelessNode
from core.services.base import CoreService, ServiceMode
from core.services.base import CoreService

logger = logging.getLogger(__name__)
GROUP: str = "Quagga"
Expand Down Expand Up @@ -91,13 +90,9 @@ class Zebra(CoreService):
"/usr/local/etc/quagga/vtysh.conf",
]
executables: list[str] = ["zebra"]
dependencies: list[str] = []
startup: list[str] = ["bash quaggaboot.sh zebra"]
validate: list[str] = ["pidof zebra"]
shutdown: list[str] = ["killall zebra"]
validation_mode: ServiceMode = ServiceMode.BLOCKING
default_configs: list[Configuration] = []
modes: dict[str, dict[str, str]] = {}

def data(self) -> dict[str, Any]:
quagga_bin_search = self.node.session.options.get(
Expand Down Expand Up @@ -153,16 +148,7 @@ def data(self) -> dict[str, Any]:

class QuaggaService(abc.ABC):
group: str = GROUP
directories: list[str] = []
files: list[str] = []
executables: list[str] = []
dependencies: list[str] = ["zebra"]
startup: list[str] = []
validate: list[str] = []
shutdown: list[str] = []
validation_mode: ServiceMode = ServiceMode.BLOCKING
default_configs: list[Configuration] = []
modes: dict[str, dict[str, str]] = {}
ipv4_routing: bool = False
ipv6_routing: bool = False

Expand Down
Loading

0 comments on commit a622b8e

Please sign in to comment.