diff --git a/src/cnaas_nms/db/git.py b/src/cnaas_nms/db/git.py index be070ea0..37d41cd4 100644 --- a/src/cnaas_nms/db/git.py +++ b/src/cnaas_nms/db/git.py @@ -20,6 +20,7 @@ SettingsSyntaxError, VlanConflictError, get_device_primary_groups, + get_group_settings_asdict, get_groups, rebuild_settings_cache, ) @@ -302,7 +303,7 @@ def _refresh_repo_task(repo_type: RepoType = RepoType.TEMPLATES, job_id: Optiona devtype: DeviceType for devtype, platform in updated_devtypes: Device.set_devtype_syncstatus(session, devtype, ret, "templates", platform, job_id) - refresh_existing_templates_worktrees(ret, job_id) + refresh_existing_templates_worktrees(ret, job_id, get_group_settings_asdict(), get_device_primary_groups()) return ret diff --git a/src/cnaas_nms/db/git_worktrees.py b/src/cnaas_nms/db/git_worktrees.py index 0832b284..1c2dcc14 100644 --- a/src/cnaas_nms/db/git_worktrees.py +++ b/src/cnaas_nms/db/git_worktrees.py @@ -5,8 +5,8 @@ import git.exc from cnaas_nms.app_settings import app_settings from cnaas_nms.db.device import Device +from cnaas_nms.db.groups import get_groups_using_branch from cnaas_nms.db.session import sqla_session -from cnaas_nms.db.settings import get_device_primary_groups, get_groups_using_branch from cnaas_nms.devicehandler.sync_history import add_sync_event from cnaas_nms.tools.log import get_logger from git import Repo @@ -16,7 +16,7 @@ class WorktreeError(Exception): pass -def refresh_existing_templates_worktrees(by: str, job_id: int): +def refresh_existing_templates_worktrees(by: str, job_id: int, group_settings: dict, device_primary_groups: dict): """Look for existing worktrees and refresh them""" logger = get_logger() updated_groups: List[str] = [] @@ -31,12 +31,12 @@ def refresh_existing_templates_worktrees(by: str, job_id: int): except Exception as e: logger.exception(e) shutil.rmtree("/tmp/worktrees/" + subdir, ignore_errors=True) - updated_groups.append(get_groups_using_branch(subdir)) + updated_groups.append(get_groups_using_branch(subdir, group_settings)) # find all devices that are using these branches and mark them as unsynchronized updated_hostnames: List[str] = [] with sqla_session() as session: - for hostname, primary_group in get_device_primary_groups(): + for hostname, primary_group in device_primary_groups: if hostname in updated_hostnames: continue if primary_group in updated_groups: diff --git a/src/cnaas_nms/db/groups.py b/src/cnaas_nms/db/groups.py new file mode 100644 index 00000000..47945d00 --- /dev/null +++ b/src/cnaas_nms/db/groups.py @@ -0,0 +1,14 @@ +from typing import List + +# TODO: move all group related things here from settings +# make new settings_helper.py with (verify_dir_structure etc) and separate settings_groups for get_settigns groups? +# use get_group_settings_asdict instead of passing dict in get_groups_using_branch below + + +def get_groups_using_branch(branch_name: str, group_settings: dict) -> List[str]: + """Returns a list of group names that use the specified branch name""" + groups = [] + for group_name, group_data in group_settings.items(): + if group_data.get("templates_branch") == branch_name: + groups.append(group_name) + return groups diff --git a/src/cnaas_nms/db/settings.py b/src/cnaas_nms/db/settings.py index 26a249f8..c6354f85 100644 --- a/src/cnaas_nms/db/settings.py +++ b/src/cnaas_nms/db/settings.py @@ -767,15 +767,6 @@ def get_group_templates_branch(group_name: str) -> Optional[str]: return get_group_settings_asdict().get(group_name, {}).get("templates_branch") -def get_groups_using_branch(branch_name: str) -> List[str]: - """Returns a list of group names that use the specified branch name""" - groups = [] - for group_name, group_data in get_group_settings_asdict().items(): - if group_data.get("templates_branch") == branch_name: - groups.append(group_name) - return groups - - @redis_lru_cache def get_group_settings_asdict() -> Dict[str, Dict[str, Any]]: """Returns a dict with group name as key and other parameters as values"""