Skip to content

Commit

Permalink
feature: 提供 Agent 包管理后台接口 (closed #1683)
Browse files Browse the repository at this point in the history
  • Loading branch information
wyyalt committed Nov 29, 2023
1 parent 7eda861 commit 9b1ea46
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 14 deletions.
62 changes: 48 additions & 14 deletions apps/backend/agent/artifact_builder/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -441,13 +441,57 @@ def _get_changelog(self, extract_dir: str) -> str:
changelog: str = changelog_fs.read()
return changelog

def update_or_create_record(self, artifact_meta_info: typing.Dict[str, typing.Any]):
def generate_location_path(self, upload_path: str, pkg_name: str) -> str:
if settings.STORAGE_TYPE == core_files_constants.StorageType.BLUEKING_ARTIFACTORY.value:
location_path: str = f"{settings.BKREPO_ENDPOINT_URL}/generic/blueking/bknodeman/{upload_path}/{pkg_name}"
else:
location_path: str = f"http://{settings.BKAPP_LAN_IP}/{upload_path}/{pkg_name}"

return location_path

def update_or_create_package_records(self, package_infos: typing.List[typing.Dict[str, typing.Any]]):
"""
创建或更新制品记录,待 Agent 包管理完善
:param artifact_meta_info:
创建或更新制品记录
:param package_infos:
:return:
"""
pass
for package_info in package_infos:
models.GsePackages.objects.update_or_create(
defaults={
"pkg_size": package_info["package_upload_info"]["pkg_size"],
"pkg_path": package_info["package_upload_info"]["pkg_path"],
"md5": package_info["package_upload_info"]["md5"],
"location": self.generate_location_path(
package_info["package_upload_info"]["pkg_path"],
package_info["package_upload_info"]["pkg_name"],
),
"version_log": package_info["artifact_meta_info"]["changelog"],
},
pkg_name=package_info["package_upload_info"]["pkg_name"],
version=package_info["artifact_meta_info"]["version"],
project=package_info["artifact_meta_info"]["name"],
os=package_info["package_dir_info"]["os"],
cpu_arch=package_info["package_dir_info"]["cpu_arch"],
)
logger.info(
f"[update_or_create_package_record] "
f"package name -> {package_info['package_upload_info']['pkg_name']} success"
)

if package_infos:
models.GsePackageDesc.objects.update_or_create(
defaults={
"description": package_infos[0]["artifact_meta_info"]["changelog"],
},
project=package_infos[0]["artifact_meta_info"]["name"],
category=constants.CategoryType.official,
)

logger.info(
f"[update_or_create_package_record] "
f"package desc -> {package_info['package_upload_info']['pkg_name']}, "
f"project -> {package_infos[0]['artifact_meta_info']['name']} success"
)

def update_or_create_tag(self, artifact_meta_info: typing.Dict[str, typing.Any]):
"""
Expand Down Expand Up @@ -517,14 +561,6 @@ def update_or_create_support_files(self, package_infos: typing.List[typing.Dict]
agent_name=self.NAME,
)

def update_or_create_package_records(self, v):
"""
创建或更新安装包记录,待 Agent 包管理完善
:param package_infos:
:return:
"""
pass

def get_artifact_meta_info(self, extract_dir: str) -> typing.Dict[str, typing.Any]:
"""
获取制品的基础信息、配置文件信息
Expand Down Expand Up @@ -591,8 +627,6 @@ def make(
artifact_meta_info["operator"] = operator
# Agent 包先导入文件源 -> 写配置文件 -> 创建包记录 -> 创建 Tag
self.update_or_create_support_files(package_infos)
# TODO update_or_create_record & update_or_create_package_records 似乎是一样的功能?
self.update_or_create_record(artifact_meta_info)
self.update_or_create_package_records(package_infos)
self.update_or_create_tag(artifact_meta_info)

Expand Down
13 changes: 13 additions & 0 deletions apps/backend/tests/agent/artifact_builder/test_agent.py
Original file line number Diff line number Diff line change
Expand Up @@ -106,12 +106,24 @@ def template_and_env_checker(self, version_str):

self.assertTrue(models.GseConfigTemplate.objects.filter(**filter_kwargs).exists())

def gse_package_and_desc_records_checker(self, version_str):
for package_os, cpu_arch in self.OS_CPU_CHOICES:
filter_kwargs: dict = {
"project": self.NAME,
"os": package_os,
"cpu_arch": cpu_arch,
"version": version_str,
}
self.assertTrue(models.GsePackages.objects.filter(**filter_kwargs).exists())
self.assertTrue(models.GsePackageDesc.objects.filter(**{"project": filter_kwargs.pop("project")}).exists())

def test_make(self):
"""测试安装包制作"""
with self.ARTIFACT_BUILDER_CLASS(initial_artifact_path=self.ARCHIVE_PATH) as builder:
builder.make()
self.pkg_checker(version_str=utils.VERSION)
self.template_and_env_checker(version_str=utils.VERSION)
self.gse_package_and_desc_records_checker(version_str=utils.VERSION)

def test_make__overwrite_version(self):
"""测试版本号覆盖"""
Expand All @@ -130,6 +142,7 @@ def test_make__overwrite_version(self):
self.template_and_env_checker(version_str=utils.VERSION)
self.pkg_checker(version_str=self.OVERWRITE_VERSION)
self.tag_checker(target_id=AGENT_NAME_TARGET_ID_MAP[self.NAME])
self.gse_package_and_desc_records_checker(version_str=utils.VERSION)


class BkRepoTestCase(FileSystemTestCase):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ def test_make(self):
self.assertTrue(models.UploadPackage.objects.all().exists())
self.pkg_checker(version_str=utils.VERSION)
self.template_and_env_checker(version_str=utils.VERSION)
self.gse_package_and_desc_records_checker(version_str=utils.VERSION)

def test_make__overwrite_version(self):
"""测试版本号覆盖"""
Expand All @@ -52,6 +53,7 @@ def test_make__overwrite_version(self):
self.template_and_env_checker(version_str=utils.VERSION)
self.pkg_checker(version_str=self.OVERWRITE_VERSION)
self.tag_checker(target_id=AGENT_NAME_TARGET_ID_MAP[self.NAME])
self.gse_package_and_desc_records_checker(version_str=utils.VERSION)


class BkRepoImportAgentTestCase(FileSystemImportAgentTestCase):
Expand Down
1 change: 1 addition & 0 deletions apps/backend/tests/agent/artifact_builder/test_proxy.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ def test_make(self):
builder.make()
self.pkg_checker(version_str=utils.VERSION)
self.template_and_env_checker(version_str=utils.VERSION)
self.gse_package_and_desc_records_checker(version_str=utils.VERSION)


class BkRepoTestCase(FileSystemTestCase):
Expand Down

0 comments on commit 9b1ea46

Please sign in to comment.