chore: add extracted service creator
This commit is contained in:
@@ -0,0 +1,94 @@
|
||||
import copier
|
||||
import git
|
||||
|
||||
from core.git import (
|
||||
assert_clean_repo,
|
||||
delete_local_branch,
|
||||
git_push_changes,
|
||||
switch_branch,
|
||||
)
|
||||
from core.models import MasterHLANames, Service, ServiceRegistry
|
||||
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"
|
||||
|
||||
|
||||
class ServiceCreator:
|
||||
def __init__(
|
||||
self,
|
||||
ioc_host: str,
|
||||
ioc_port: int,
|
||||
user: str,
|
||||
) -> None:
|
||||
self.ioc_host = ioc_host
|
||||
self.ioc_port = ioc_port
|
||||
self.user = user
|
||||
|
||||
def add_service(self, name: str):
|
||||
repo = git.Repo(REPO_ROOT)
|
||||
assert_clean_repo(repo)
|
||||
original_branch = repo.active_branch.name
|
||||
|
||||
try:
|
||||
registry = ServiceRegistry.read_from_config()
|
||||
service = registry.add_service(name)
|
||||
|
||||
branch_name = f"feature/add-service-{service.name_lower}"
|
||||
delete_local_branch(repo, branch_name)
|
||||
switch_branch(repo, branch_name)
|
||||
|
||||
self._render_new_service_templates(
|
||||
service=service,
|
||||
ioc_host=self.ioc_host,
|
||||
ioc_port=self.ioc_port,
|
||||
user=self.user,
|
||||
)
|
||||
registry.write_to_config()
|
||||
self._update_master_hla_names(name=service.name_upper)
|
||||
|
||||
git_push_changes(
|
||||
repo=repo,
|
||||
branch_name=branch_name,
|
||||
service_dir_rel_path=f"services/{service.dir_name}",
|
||||
registry_file_rel_path="config/services_registry.yml",
|
||||
master_hla_names_rel_path="services/000-master/app/config/hla_names.yml",
|
||||
commit_msg=f"feature: add new service {service.name_lower}",
|
||||
)
|
||||
switch_branch(repo, original_branch)
|
||||
except Exception as e:
|
||||
switch_branch(repo, original_branch)
|
||||
raise e
|
||||
|
||||
def _render_new_service_templates(
|
||||
self,
|
||||
service: Service,
|
||||
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_str,
|
||||
"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,
|
||||
},
|
||||
)
|
||||
|
||||
def _update_master_hla_names(self, name: str):
|
||||
"""
|
||||
Add new HLA name to MASTER so it can monitor it
|
||||
"""
|
||||
hla_names = MasterHLANames.read_from_config()
|
||||
hla_names.add_hla_name(name)
|
||||
hla_names.write_to_config()
|
||||
Reference in New Issue
Block a user