feature: dynamicallt allocate port and save in service registry. Update iocs overview on service creation
This commit is contained in:
+41
-5
@@ -15,10 +15,13 @@ MASTER_SERVICE_DIR = REPO_ROOT / "services" / "000-master" / "current"
|
||||
MASTER_SERVICE_CONFIG_DIR = MASTER_SERVICE_DIR / "app" / "config"
|
||||
MASTER_HLA_NAMES_FILENAME = MASTER_SERVICE_CONFIG_DIR / "hla_names.yml"
|
||||
|
||||
IOCS_OVERVIEW_FILENAME = REPO_ROOT / "docs" / "ioc" / "ioc_overview.md"
|
||||
|
||||
|
||||
class Service(BaseModel):
|
||||
id: int
|
||||
name: str
|
||||
ioc_port: int
|
||||
status: ServiceStatus
|
||||
|
||||
@property
|
||||
@@ -39,7 +42,7 @@ class Service(BaseModel):
|
||||
return f"{self.id:03d}"
|
||||
|
||||
def to_dict(self):
|
||||
return {"name": self.name_lower, "status": str(self.status)}
|
||||
return {"name": self.name_lower, "ioc_port": self.ioc_port, "status": str(self.status)}
|
||||
|
||||
class Config:
|
||||
use_enum_values = True
|
||||
@@ -56,7 +59,8 @@ class ServiceRegistry(BaseModel):
|
||||
config_path: ClassVar[str] = str(SERVICE_REGISTRY_FILENAME)
|
||||
|
||||
next_available_id: int
|
||||
services: dict[int, dict]
|
||||
next_available_ioc_port: int
|
||||
services: dict[str, dict]
|
||||
|
||||
@classmethod
|
||||
def read_from_config(cls) -> "ServiceRegistry":
|
||||
@@ -67,12 +71,18 @@ class ServiceRegistry(BaseModel):
|
||||
return registry
|
||||
|
||||
def get_services(self) -> list[Service]:
|
||||
return [Service(id=svc_id, **svc_data) for svc_id, svc_data in self.services.items()]
|
||||
return [Service(id=int(svc_id), **svc_data) for svc_id, svc_data in self.services.items()]
|
||||
|
||||
def add_service(self, name: str) -> Service:
|
||||
svc = Service(id=self.next_available_id, name=name, status=ServiceStatus.ACTIVE)
|
||||
self.services.update({svc.id: svc.to_dict()})
|
||||
svc = Service(
|
||||
id=self.next_available_id,
|
||||
name=name,
|
||||
ioc_port=self.next_available_ioc_port,
|
||||
status=ServiceStatus.ACTIVE,
|
||||
)
|
||||
self.services.update({svc.id_str: svc.to_dict()})
|
||||
self.next_available_id += 1
|
||||
self.next_available_ioc_port += 1
|
||||
|
||||
return svc
|
||||
|
||||
@@ -100,3 +110,29 @@ class MasterHLANames(BaseModel):
|
||||
def write_to_config(self) -> None:
|
||||
with open(self.config_path, "w") as f:
|
||||
yaml.safe_dump(self.model_dump(), f, indent=4)
|
||||
|
||||
|
||||
class IocsOverview(BaseModel):
|
||||
config_path: ClassVar[str] = str(IOCS_OVERVIEW_FILENAME)
|
||||
|
||||
content: str
|
||||
|
||||
@classmethod
|
||||
def read_from_file(cls) -> "IocsOverview":
|
||||
with open(cls.config_path, "r", encoding="utf-8") as f:
|
||||
s = f.read()
|
||||
|
||||
return IocsOverview(content=s)
|
||||
|
||||
def add_ioc(self, service: Service, description: str):
|
||||
new_row = f"| AGEBD-CPCL-{service.name_upper} | {service.ioc_port} | {description} |\n"
|
||||
|
||||
# Ensuring it starts on a clean new line if the file didn't end with one
|
||||
if not self.content.endswith("\n"):
|
||||
self.content += "\n"
|
||||
|
||||
self.content += new_row
|
||||
|
||||
def write_to_file(self):
|
||||
with open(self.config_path, "w") as f:
|
||||
f.write(self.content)
|
||||
|
||||
@@ -14,7 +14,7 @@ from core.git import (
|
||||
git_push_changes,
|
||||
switch_branch,
|
||||
)
|
||||
from core.models import MasterHLANames, Service, ServiceRegistry
|
||||
from core.models import IocsOverview, MasterHLANames, Service, ServiceRegistry
|
||||
from core.utils import get_git_root
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
@@ -30,13 +30,9 @@ MASTER_IOC_SUBS_FILE = MASTER_SERVICE_DIR / "ioc" / "AGEBD-CPCL-MASTER_main.subs
|
||||
|
||||
|
||||
class ServiceCreator:
|
||||
def __init__(
|
||||
self,
|
||||
ioc_port: int,
|
||||
user: str,
|
||||
) -> None:
|
||||
self.ioc_port = ioc_port
|
||||
def __init__(self, user: str, ioc_description: str) -> None:
|
||||
self.user = user
|
||||
self.ioc_description = ioc_description
|
||||
|
||||
def add_service(self, name: str):
|
||||
repo = git.Repo(REPO_ROOT)
|
||||
@@ -50,6 +46,9 @@ class ServiceCreator:
|
||||
hla_names_old = MasterHLANames.read_from_config()
|
||||
hla_names = MasterHLANames.read_from_config()
|
||||
hla_names.add_hla_name(service.name_upper)
|
||||
iocs_overview_old = IocsOverview.read_from_file()
|
||||
iocs_overview = IocsOverview.read_from_file()
|
||||
iocs_overview.add_ioc(service, description=self.ioc_description)
|
||||
|
||||
branch_name = f"feature/add-service-{service.dir_name}"
|
||||
delete_local_branch(repo, branch_name)
|
||||
@@ -58,12 +57,12 @@ class ServiceCreator:
|
||||
try:
|
||||
self._render_new_service_templates(
|
||||
service=service,
|
||||
ioc_port=self.ioc_port,
|
||||
user=self.user,
|
||||
)
|
||||
self._generate_uv_lock(service=service)
|
||||
registry.write_to_config()
|
||||
hla_names.write_to_config()
|
||||
iocs_overview.write_to_file()
|
||||
|
||||
self._append_to_master_ioc_subs(
|
||||
service_name=service.name_upper,
|
||||
@@ -87,13 +86,13 @@ class ServiceCreator:
|
||||
shutil.rmtree(Path(SERVICE_DEST_DIR / service.dir_name), ignore_errors=True)
|
||||
registry_old.write_to_config()
|
||||
hla_names_old.write_to_config()
|
||||
iocs_overview_old.write_to_file()
|
||||
switch_branch(repo, original_branch)
|
||||
raise e
|
||||
|
||||
def _render_new_service_templates(
|
||||
self,
|
||||
service: Service,
|
||||
ioc_port: int,
|
||||
user: str,
|
||||
):
|
||||
"""
|
||||
@@ -106,7 +105,6 @@ class ServiceCreator:
|
||||
"service_id": service.id_str,
|
||||
"service_name_upper": service.name_upper,
|
||||
"service_name_lower": service.name_lower,
|
||||
"ioc_port": ioc_port,
|
||||
"user": user,
|
||||
},
|
||||
)
|
||||
|
||||
Reference in New Issue
Block a user