Skip to content

Commit

Permalink
Fix circular imports for now, group things should be moved out of
Browse files Browse the repository at this point in the history
settings module at some point
  • Loading branch information
indy-independence committed Oct 28, 2024
1 parent 4ea0b6a commit c578cc4
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 14 deletions.
3 changes: 2 additions & 1 deletion src/cnaas_nms/db/git.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
SettingsSyntaxError,
VlanConflictError,
get_device_primary_groups,
get_group_settings_asdict,
get_groups,
rebuild_settings_cache,
)
Expand Down Expand Up @@ -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

Expand Down
8 changes: 4 additions & 4 deletions src/cnaas_nms/db/git_worktrees.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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] = []
Expand All @@ -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:
Expand Down
14 changes: 14 additions & 0 deletions src/cnaas_nms/db/groups.py
Original file line number Diff line number Diff line change
@@ -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
9 changes: 0 additions & 9 deletions src/cnaas_nms/db/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"""
Expand Down

0 comments on commit c578cc4

Please sign in to comment.