Skip to content

Commit

Permalink
feat: 提供 Agent 包管理后台基础接口 (closed #1683)
Browse files Browse the repository at this point in the history
  • Loading branch information
ping15 committed Nov 11, 2024
1 parent 7c92cff commit aaa1aef
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 69 deletions.
48 changes: 4 additions & 44 deletions apps/node_man/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -1244,48 +1244,8 @@ def _get_member__alias_map(cls) -> Dict[Enum, str]:
}


BUILT_IN_TAG_DESCRIPTIONS: List[str] = [_("稳定版本"), _("最新版本"), _("测试版本")]
BUILT_IN_TAG_DESCRIPTIONS: List[str] = ["稳定版本", "最新版本", "测试版本"]
BUILT_IN_TAG_NAMES: List[str] = ["stable", "latest", "test"]
# TAG_NAME_MAP = dict(zip(BUILT_IN_TAG_DESCRIPTIONS, BUILT_IN_TAG_NAMES))
# TAG_NAME_MAP.update({name: name for name in BUILT_IN_TAG_NAMES})
# TAG_DESCRIPTION_MAP = {
# "stable": _("稳定版本"),
# "latest": _("最新版本"),
# "test": _("测试版本"),
# "稳定版本": _("稳定版本"),
# "最新版本": _("最新版本"),
# "测试版本": _("测试版本"),
# }
STABLE_DESCRIPTION = _("稳定版本")


A = {
"stable": "stable",
"latest": "latest",
"test": "test",
"稳定版本": "stable",
"最新版本": "latest",
"测试版本": "test",
}

B = {
"stable": "稳定版本",
"latest": "最新版本",
"test": "测试版本",
"稳定版本": "稳定版本",
"最新版本": "最新版本",
"测试版本": "测试版本",
}

C = {
"AGENT": "gse_agent",
}

D = {
"stable": "stable",
"latest": "latest",
"test": "test",
"稳定版本": "stable",
"最新版本": "latest",
"测试版本": "test",
}
TAG_NAME__TAG_DESCRIPTION = dict(zip(BUILT_IN_TAG_NAMES, BUILT_IN_TAG_DESCRIPTIONS))
TAG_DESCRIPTION__TAG_NAME = dict(zip(BUILT_IN_TAG_DESCRIPTIONS, BUILT_IN_TAG_NAMES))
STABLE_DESCRIPTION = "稳定版本"
37 changes: 16 additions & 21 deletions apps/node_man/handlers/gse_package.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,7 @@

from apps.core.tag.constants import TargetType
from apps.core.tag.models import Tag
from apps.node_man.constants import (
BUILT_IN_TAG_NAMES,
A,
GsePackageCacheKey,
GsePackageCode,
)
from apps.node_man import constants
from apps.node_man.models import GsePackageDesc, GsePackages
from apps.node_man.tools.gse_package import GsePackageTools

Expand Down Expand Up @@ -53,7 +48,7 @@ def clear_caches(self):

def _init_project_version__tags_map(self):
"""初始化项目版本标签映射"""
for project in GsePackageCode.values():
for project in constants.GsePackageCode.values():
tags: QuerySet = self.get_tag_objs(project).values("name", "description", "target_version")

for tag in tags:
Expand All @@ -65,7 +60,7 @@ def _init_project_version__tags_map(self):

def _init_project__description_map(self):
"""初始化项目描述映射"""
for project in GsePackageCode.values():
for project in constants.GsePackageCode.values():
description = GsePackageDesc.objects.filter(project=project).first().description
cache_key = self.get_description_cache_key(project)
self.cache[self.PROJECT__DESCRIPTION_MAP][cache_key] = description
Expand All @@ -75,12 +70,12 @@ def _init_project__description_map(self):
@classmethod
def get_tags_cache_key(cls, project: str, version: str) -> str:
"""获取标签缓存key"""
return f"{GsePackageCacheKey.TAGS_PREFIX.value}:{project}:{version}"
return f"{constants.GsePackageCacheKey.TAGS_PREFIX.value}:{project}:{version}"

@classmethod
def get_description_cache_key(cls, project: str) -> str:
"""获取描述缓存key"""
return f"{GsePackageCacheKey.DESCRIPTION_PREFIX.value}:{project}"
return f"{constants.GsePackageCacheKey.DESCRIPTION_PREFIX.value}:{project}"

@classmethod
def get_tag_objs(cls, project: str, version: str = None) -> QuerySet:
Expand Down Expand Up @@ -175,7 +170,7 @@ def split_tags_into_builtin_and_custom(
"""将标签拆分为内置的和自定义的"""
built_in_tags, custom_tags = [], []
for tag in tags:
if tag["name"] in BUILT_IN_TAG_NAMES:
if tag["name"] in constants.BUILT_IN_TAG_NAMES:
built_in_tags.append(tag)
else:
custom_tags.append(tag)
Expand All @@ -191,10 +186,10 @@ def handle_add_tag(cls, tag_description: str, package_obj: GsePackages, package_
:param package_desc_obj: Gse包描述记录
"""
# 如果新增的是内置标签,将原有的内置标签中的target_version进行修改即可,否则创建一个新的标签
if tag_description in ["test", "latest", "stable", "测试版本", "最新版本", "稳定版本"]:
Tag.objects.filter(name=A[tag_description], target_id=package_desc_obj.id).update(
target_version=package_obj.version
)
if tag_description in constants.BUILT_IN_TAG_DESCRIPTIONS:
Tag.objects.filter(
name=constants.TAG_DESCRIPTION__TAG_NAME[tag_description], target_id=package_desc_obj.id
).update(target_version=package_obj.version)
else:
tag: Tag = Tag.objects.filter(description=tag_description, target_id=package_desc_obj.id).first()
Tag.objects.create(
Expand Down Expand Up @@ -224,12 +219,12 @@ def handle_update_tag(
# 如果目标标签为内置标签的话,将内置标签的target_version进行覆盖,并对原来的标签进行删除或者清空
# 如果目标标签为自定义标签,原有标签为内置标签的话,原有标签target_version置空,并新增自定义标签
# 否则(目标和原有都为自定义标签)将直接修改原有标签的target_version
if tag_description in ["test", "latest", "stable", "测试版本", "最新版本", "稳定版本"]:
Tag.objects.filter(name=A[tag_description], target_id=package_desc_obj.id).update(
target_version=package_obj.version
)
if tag_description in constants.BUILT_IN_TAG_DESCRIPTIONS:
Tag.objects.filter(
name=constants.TAG_DESCRIPTION__TAG_NAME[tag_description], target_id=package_desc_obj.id
).update(target_version=package_obj.version)
cls.handle_delete_tag(tag_obj.name, tag_obj)
elif tag_obj.name in ["test", "latest", "stable"]:
elif tag_obj.name in constants.BUILT_IN_TAG_NAMES:
tag_obj.target_version = ""
tag_obj.save()
cls.handle_add_tag(tag_description, package_obj, package_desc_obj)
Expand All @@ -247,7 +242,7 @@ def handle_delete_tag(cls, tag_name: str, tag_obj: Tag):
:param tag_obj: 待删除的标签记录
"""
# 如果是删除内置标签,将target_version置空即可,不需要删除
if tag_name in ["test", "latest", "stable"]:
if tag_name in constants.BUILT_IN_TAG_NAMES:
tag_obj.target_version = ""
tag_obj.save()
else:
Expand Down
5 changes: 2 additions & 3 deletions apps/node_man/tools/gse_package.py
Original file line number Diff line number Diff line change
Expand Up @@ -112,10 +112,9 @@ def create_agent_tags(cls, tag_descriptions, project):
project=project, category=CategoryType.official
)

if tag_description in ["stable", "latest", "test", "稳定版本", "最新版本", "测试版本"]:
if tag_description in constants.BUILT_IN_TAG_DESCRIPTIONS:
# 内置标签,手动指定name和description
name: str = constants.A[tag_description]
tag_description: str = constants.B[tag_description]
name: str = constants.TAG_DESCRIPTION__TAG_NAME[tag_description]
else:
# 自定义标签,自动生成name
name: str = GsePackageTools.generate_name_by_description(tag_description)
Expand Down
2 changes: 1 addition & 1 deletion apps/node_man/views/package_manage.py
Original file line number Diff line number Diff line change
Expand Up @@ -632,7 +632,7 @@ def version(self, request):
)

# 获取默认标签
if not default_version and any(_(tag["description"]) == STABLE_DESCRIPTION for tag in tags):
if not default_version and any(tag["description"] == STABLE_DESCRIPTION for tag in tags):
default_version = version

# 初始化某个版本的包
Expand Down

0 comments on commit aaa1aef

Please sign in to comment.