chore: extract service creator class
This commit is contained in:
+27
-3
@@ -1,16 +1,19 @@
|
||||
import re
|
||||
from pathlib import Path
|
||||
from typing import ClassVar
|
||||
|
||||
import yaml
|
||||
from pydantic import BaseModel, field_validator
|
||||
|
||||
from core.enums import ServiceStatus
|
||||
from core.utils import get_git_root
|
||||
|
||||
HERE_DIR = Path(__file__).parent
|
||||
CONFIG_DIR = HERE_DIR / ".." / ".." / ".." / "config"
|
||||
REPO_ROOT = get_git_root(__file__)
|
||||
CONFIG_DIR = REPO_ROOT / "config"
|
||||
SERVICE_REGISTRY_FILENAME = CONFIG_DIR / "services_registry.yml"
|
||||
|
||||
MASTER_SERVICE_DIR = REPO_ROOT / "services" / "000-master"
|
||||
MASTER_SERVICE_CONFIG_DIR = MASTER_SERVICE_DIR / "app" / "config"
|
||||
|
||||
|
||||
class Service(BaseModel):
|
||||
id: int
|
||||
@@ -75,3 +78,24 @@ class ServiceRegistry(BaseModel):
|
||||
def write_to_config(self) -> None:
|
||||
with open(self.config_path, "w") as f:
|
||||
yaml.safe_dump(self.model_dump(), f)
|
||||
|
||||
|
||||
class MasterHLANames(BaseModel):
|
||||
config_path: ClassVar[str] = str(MASTER_SERVICE_CONFIG_DIR)
|
||||
|
||||
hla_apps: list[str]
|
||||
|
||||
@classmethod
|
||||
def read_from_config(cls) -> "MasterHLANames":
|
||||
with open(cls.config_path) as f:
|
||||
d = yaml.safe_load(f)
|
||||
|
||||
hla_names = MasterHLANames.model_validate(d)
|
||||
return hla_names
|
||||
|
||||
def add_hla_name(self, name: str):
|
||||
self.hla_apps.append(name)
|
||||
|
||||
def write_to_config(self) -> None:
|
||||
with open(self.config_path, "w") as f:
|
||||
yaml.safe_dump(self.model_dump(), f)
|
||||
|
||||
@@ -1,33 +0,0 @@
|
||||
import copier
|
||||
|
||||
from core.utils import get_git_root
|
||||
|
||||
REPO_ROOT = get_git_root(__file__)
|
||||
SERVICE_TEMPLATES_DIR = REPO_ROOT / "templates" / "service"
|
||||
SERVICE_DEST_DIR = REPO_ROOT / "services"
|
||||
|
||||
|
||||
def render_new_service_templates(
|
||||
service_id: str,
|
||||
service_name_upper: str,
|
||||
service_name_lower: str,
|
||||
ioc_host: str,
|
||||
ioc_port: int,
|
||||
user: str,
|
||||
):
|
||||
"""
|
||||
Render template files for a new service
|
||||
"""
|
||||
copier.run_copy(
|
||||
src_path=str(SERVICE_TEMPLATES_DIR),
|
||||
dst_path=str(SERVICE_DEST_DIR),
|
||||
data={
|
||||
"service_id": service_id,
|
||||
"service_name_upper": service_name_upper,
|
||||
"service_name_lower": service_name_lower,
|
||||
# TODO: restrict this, use enum
|
||||
"ioc_host": ioc_host,
|
||||
"ioc_port": ioc_port,
|
||||
"user": user,
|
||||
},
|
||||
)
|
||||
+4
-29
@@ -1,9 +1,7 @@
|
||||
import git
|
||||
import typer
|
||||
|
||||
from core.git import assert_clean_repo, git_push_changes, switch_branch
|
||||
from core.models import ServiceRegistry
|
||||
from core.render_templates import render_new_service_templates
|
||||
from core.service_creator import ServiceCreator
|
||||
from core.utils import get_git_root, init_logging
|
||||
|
||||
service = typer.Typer(no_args_is_help=True)
|
||||
@@ -16,35 +14,12 @@ def add(
|
||||
name: str = typer.Option(..., "--name", "-n"),
|
||||
):
|
||||
init_logging()
|
||||
|
||||
registry = ServiceRegistry.read_from_config()
|
||||
service = registry.add_service(name)
|
||||
|
||||
repo = git.Repo(REPO_ROOT)
|
||||
assert_clean_repo(repo)
|
||||
original_branch = repo.active_branch.name
|
||||
branch_name = f"feature/add-service-{service.name_lower}"
|
||||
switch_branch(repo, branch_name)
|
||||
|
||||
render_new_service_templates(
|
||||
service_id=service.id_str,
|
||||
service_name_upper=service.name_upper,
|
||||
service_name_lower=service.name_lower,
|
||||
# TODO: Maybe create a GitPusher context manager
|
||||
ServiceCreator(
|
||||
ioc_host="ex-host", # TODO
|
||||
ioc_port=9999, # TODO
|
||||
user="user", # TODO
|
||||
)
|
||||
registry.write_to_config()
|
||||
|
||||
git_push_changes(
|
||||
repo=repo,
|
||||
branch_name=f"feature/add-service-{service.name_lower}",
|
||||
service_dir_rel_path=f"services/{service.dir_name}",
|
||||
registry_file_rel_path="config/services_registry.yml",
|
||||
commit_msg=f"feature: add new service {service.name_lower}",
|
||||
)
|
||||
|
||||
switch_branch(repo, original_branch)
|
||||
).add_service(name=name)
|
||||
|
||||
|
||||
@service.command()
|
||||
|
||||
Reference in New Issue
Block a user