chore: add migrate cli command
This commit is contained in:
+1
-6
@@ -8,12 +8,7 @@ logger = logging.getLogger()
|
||||
def git_push_changes(
|
||||
repo: git.Repo,
|
||||
branch_name: str,
|
||||
service_dir_rel_path: str,
|
||||
registry_file_rel_path: str,
|
||||
master_hla_names_rel_path: str,
|
||||
master_ioc_subs_rel_path: str,
|
||||
iocs_overview_rel_path: str,
|
||||
service_manager_ui_rel_path: str,
|
||||
files_to_stage: list[str],
|
||||
commit_msg: str,
|
||||
):
|
||||
"""
|
||||
|
||||
@@ -80,17 +80,20 @@ class ServiceCreator:
|
||||
master_ioc_subs.write_to_file()
|
||||
service_manager.write_to_file()
|
||||
|
||||
files_to_stage = [
|
||||
f"services/{service.dir_name}/current",
|
||||
"config/services_registry.yml",
|
||||
# TODO: maybe assert/get correct master service dirname from registry
|
||||
"services/master/current/app/config/hla_names.yml",
|
||||
"services/master/current/ioc/AGEBD-CPCL-MASTER_main.subs",
|
||||
"docs/ioc/ioc_overview.md",
|
||||
"qt/A_BD_ServiceManager.ui",
|
||||
]
|
||||
# TODO: can't push if branch already exists in remote
|
||||
git_push_changes(
|
||||
repo=repo,
|
||||
branch_name=branch_name,
|
||||
service_dir_rel_path=f"services/{service.dir_name}/current",
|
||||
registry_file_rel_path="config/services_registry.yml",
|
||||
# TODO: maybe assert/get correct master service dirname from registry
|
||||
master_hla_names_rel_path="services/master/current/app/config/hla_names.yml",
|
||||
master_ioc_subs_rel_path="services/master/current/ioc/AGEBD-CPCL-MASTER_main.subs",
|
||||
iocs_overview_rel_path="docs/ioc/ioc_overview.md",
|
||||
service_manager_ui_rel_path="qt/A_BD_ServiceManager.ui",
|
||||
files_to_stage=files_to_stage,
|
||||
commit_msg=f"feature: add new service {service.name_lower}",
|
||||
)
|
||||
switch_branch(repo, original_branch)
|
||||
@@ -104,6 +107,99 @@ class ServiceCreator:
|
||||
switch_branch(repo, original_branch)
|
||||
raise e
|
||||
|
||||
# TODO: remove
|
||||
def migrate_service(self, name: str):
|
||||
repo = git.Repo(REPO_ROOT)
|
||||
assert_clean_repo(repo)
|
||||
original_branch = repo.active_branch.name
|
||||
|
||||
# Keep old configs to revert in case of Exception
|
||||
registry_old = ServiceRegistry.read_from_config()
|
||||
registry = ServiceRegistry.read_from_config()
|
||||
service = registry.add_service(name)
|
||||
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)
|
||||
switch_branch(repo, branch_name)
|
||||
|
||||
def copy_files_from_current_service():
|
||||
# ui
|
||||
CURRENT_FILENAME = str(REPO_ROOT / ".." / "sls_bd_qt" / self.ui_filename)
|
||||
DEST_FILENAME = str(
|
||||
REPO_ROOT / "services" / service.dir_name / "current" / "qt" / self.ui_filename
|
||||
)
|
||||
copier.run_copy(src_path=CURRENT_FILENAME, dst_path=DEST_FILENAME)
|
||||
|
||||
# ioc subs
|
||||
CURRENT_FILENAME = str(
|
||||
REPO_ROOT
|
||||
/ ".."
|
||||
/ "A_BD"
|
||||
/ f"AGEBD-CPCL-{service.name_upper}"
|
||||
/ f"AGEBD-CPCL-{service.name_upper}_main.subs"
|
||||
)
|
||||
DEST_FILENAME = str(
|
||||
REPO_ROOT
|
||||
/ "services"
|
||||
/ service.dir_name
|
||||
/ "current"
|
||||
/ "ioc"
|
||||
/ f"AGEBD-CPCL-{service.name_upper}_main.subs"
|
||||
)
|
||||
copier.run_copy(src_path=CURRENT_FILENAME, dst_path=DEST_FILENAME)
|
||||
|
||||
# ioc template
|
||||
CURRENT_FILENAME = str(
|
||||
REPO_ROOT
|
||||
/ ".."
|
||||
/ "A_BD"
|
||||
/ f"AGEBD-CPCL-{service.name_upper}"
|
||||
/ f"{service.name_upper}.template"
|
||||
)
|
||||
DEST_FILENAME = str(
|
||||
REPO_ROOT
|
||||
/ "services"
|
||||
/ service.dir_name
|
||||
/ "current"
|
||||
/ "ioc"
|
||||
/ f"{service.name_upper}.template"
|
||||
)
|
||||
copier.run_copy(src_path=CURRENT_FILENAME, dst_path=DEST_FILENAME)
|
||||
|
||||
try:
|
||||
self._render_new_service_templates(
|
||||
service=service,
|
||||
user=self.user,
|
||||
)
|
||||
self._generate_uv_lock(service=service)
|
||||
registry.write_to_config()
|
||||
iocs_overview.write_to_file()
|
||||
|
||||
copy_files_from_current_service()
|
||||
|
||||
files_to_stage = [
|
||||
f"services/{service.dir_name}/current",
|
||||
"config/services_registry.yml",
|
||||
"docs/ioc/ioc_overview.md",
|
||||
]
|
||||
# TODO: can't push if branch already exists in remote
|
||||
git_push_changes(
|
||||
repo=repo,
|
||||
branch_name=branch_name,
|
||||
files_to_stage=files_to_stage,
|
||||
commit_msg=f"feature: add new service {service.name_lower}",
|
||||
)
|
||||
switch_branch(repo, original_branch)
|
||||
except Exception as e:
|
||||
shutil.rmtree(Path(SERVICE_DEST_DIR / service.dir_name), ignore_errors=True)
|
||||
registry_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,
|
||||
|
||||
@@ -39,6 +39,31 @@ def add(
|
||||
service_creator.add_service(name=name)
|
||||
|
||||
|
||||
# TODO: remove
|
||||
@service.command(no_args_is_help=True)
|
||||
def migrate(
|
||||
name: str = typer.Option(..., "--name", "-n"),
|
||||
user: str = typer.Option(..., "--user", "-u"),
|
||||
ioc_description: str = typer.Option(
|
||||
..., "--ioc-description", "-d", help="See <repo-root>/docs/ioc/ioc_overview.md for examples"
|
||||
),
|
||||
ui_filename: str = typer.Option(
|
||||
...,
|
||||
"--ui-filename",
|
||||
"-f",
|
||||
help="The <ui-filename> in 'A_BD_<ui-filename>.ui'; See <repo-root>/qt/A_BD_ServiceManager.ui for examples",
|
||||
),
|
||||
):
|
||||
init_logging()
|
||||
service_creator = ServiceCreator(
|
||||
user=user, # TODO: read from 'whoami'?
|
||||
ioc_description=ioc_description,
|
||||
ui_name="",
|
||||
ui_filename=ui_filename, # TODO: make this standard?
|
||||
)
|
||||
service_creator.add_service(name=name)
|
||||
|
||||
|
||||
@service.command(no_args_is_help=True)
|
||||
def list():
|
||||
registry = ServiceRegistry.read_from_config()
|
||||
|
||||
Reference in New Issue
Block a user