Skip to content

Commit

Permalink
feat: 安装预设插件锁定版本(closed #2482)
Browse files Browse the repository at this point in the history
  • Loading branch information
jpyoung3 committed Nov 12, 2024
1 parent 1354fd7 commit 9f21f98
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 33 deletions.
61 changes: 29 additions & 32 deletions apps/backend/components/collections/plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
from django.db.models import F, Q
from django.utils import timezone
from django.utils.translation import ugettext as _
from packaging import version

from apps.backend.api.constants import (
GSE_RUNNING_TASK_CODE,
Expand Down Expand Up @@ -285,7 +284,7 @@ def _execute(self, data, parent_data, common_data: PluginCommonData):
os_type = host.os_type.lower()
cpu_arch = host.cpu_arch
group_id = create_group_id(subscription, subscription_instance.instance_info)
package = self.get_package(subscription_instance, policy_step_adapter, os_type, cpu_arch)
package = self.get_package(subscription_instance, policy_step_adapter, os_type, cpu_arch, bk_biz_id)
ap_config = self.get_ap_config(ap_id_obj_map, host)
setup_path, pid_path, log_path, data_path = self.get_plugins_paths(
package, plugin_name, ap_config, group_id, subscription
Expand All @@ -294,9 +293,6 @@ def _execute(self, data, parent_data, common_data: PluginCommonData):
version_str = getattr(package, "version", "")
if version_str in tag_name__obj_map:
version_str = tag_name__obj_map[package.version].target_version
biz_version = self.get_biz_version(package, bk_biz_id)
if biz_version and version.Version(version_str) > version.Version(biz_version):
version_str = biz_version
process_status_property = dict(
bk_host_id=bk_host_id,
name=plugin_name,
Expand Down Expand Up @@ -348,32 +344,32 @@ def _execute(self, data, parent_data, common_data: PluginCommonData):
# version_str = getattr(package, "version", "")
# return version_str

@staticmethod
def get_biz_version(package: models.Packages, bk_biz_id: int):
plugin_version_config = models.GlobalSettings.get_config(
models.GlobalSettings.KeyEnum.PLUGIN_VERSION_CONFIG.value
)
biz_version = None
if str(bk_biz_id) in plugin_version_config:
biz_version_config = plugin_version_config[str(bk_biz_id)]
biz_version = next(
(
biz_plugin_version
for biz_plugin_name, biz_plugin_version in biz_version_config.items()
if package.project == biz_plugin_name
),
None,
)
# os_cpu__biz_pkg_map = {}
# if biz_version and version.Version(package.version) > version.Version(biz_version):
# packages = self.get_packages(package.project, biz_version, biz_version)
# os_cpu__biz_pkg_map = {self.get_os_key(package.os, package.cpu_arch): package for package in packages}
# if not os_cpu__biz_pkg_map:
# raise errors.PluginValidationError(
# msg="插件 [{name}-{versions}] 不存在".format(name=self.plugin_name, versions=biz_version)
# )
# return os_cpu__biz_pkg_map
return biz_version
# @staticmethod
# def get_biz_version(package: models.Packages, bk_biz_id: int):
# plugin_version_config = models.GlobalSettings.get_config(
# models.GlobalSettings.KeyEnum.PLUGIN_VERSION_CONFIG.value
# )
# biz_version = None
# if str(bk_biz_id) in plugin_version_config:
# biz_version_config = plugin_version_config[str(bk_biz_id)]
# biz_version = next(
# (
# biz_plugin_version
# for biz_plugin_name, biz_plugin_version in biz_version_config.items()
# if package.project == biz_plugin_name
# ),
# None,
# )
# # os_cpu__biz_pkg_map = {}
# # if biz_version and version.Version(package.version) > version.Version(biz_version):
# # packages = self.get_packages(package.project, biz_version, biz_version)
# # os_cpu__biz_pkg_map = {self.get_os_key(package.os, package.cpu_arch): package for package in packages}
# # if not os_cpu__biz_pkg_map:
# # raise errors.PluginValidationError(
# # msg="插件 [{name}-{versions}] 不存在".format(name=self.plugin_name, versions=biz_version)
# # )
# # return os_cpu__biz_pkg_map
# return biz_version

def inputs_format(self):
return self.inputs_format() + [
Expand All @@ -386,10 +382,11 @@ def get_package(
policy_step_adapter: PolicyStepAdapter,
os_type: str,
cpu_arch: str,
bk_biz_id: int,
) -> models.Packages:
"""获取插件包对象"""
try:
return policy_step_adapter.get_matching_package_obj(os_type, cpu_arch)
return policy_step_adapter.get_matching_package_obj(os_type, cpu_arch, bk_biz_id)
except errors.PackageNotExists as error:
# 插件包不支持或不存在时,记录异常信息,此实例不参与后续流程
self.move_insts_to_failed([subscription_instance.id], str(error))
Expand Down
39 changes: 38 additions & 1 deletion apps/backend/subscription/steps/adapter.py
Original file line number Diff line number Diff line change
Expand Up @@ -449,9 +449,18 @@ def get_matching_step_params(self, os_type: str = None, cpu_arch: str = None, os
return self.os_key_params_map.get(os_key)
return self.os_key_params_map.get(self.get_os_key(os_type, cpu_arch), {})

def get_matching_package_obj(self, os_type: str, cpu_arch: str) -> models.Packages:
def get_matching_package_obj(self, os_type: str, cpu_arch: str, bk_biz_id: int) -> models.Packages:
try:
package = self.os_key_pkg_map[self.get_os_key(os_type, cpu_arch)]
tag_name__obj_map: Dict[str, Tag] = PluginTargetHelper.get_tag_name__obj_map(
target_id=self.plugin_desc.id,
)
version_str = getattr(package, "version", "")
if version_str in tag_name__obj_map:
version_str = tag_name__obj_map[package.version].target_version
biz_version = self.get_biz_version(package, bk_biz_id)
if biz_version and version.Version(version_str) > version.Version(biz_version):
package = self.get_biz_package(package.project, os_type, cpu_arch, biz_version)
except KeyError:
msg = _("插件 [{name}] 不支持 系统:{os_type}-架构:{cpu_arch}-版本:{plugin_version}").format(
name=self.plugin_name,
Expand All @@ -474,6 +483,34 @@ def get_matching_package_obj(self, os_type: str, cpu_arch: str) -> models.Packag
def get_matching_config_tmpl_objs(self, os_type: str, cpu_arch: str) -> List[models.PluginConfigTemplate]:
return self.config_tmpl_obj_gby_os_key.get(self.get_os_key(os_type, cpu_arch), [])

def get_biz_package(self, plugin_name: str, os_type: str, cpu_arch: str, biz_version: str):
packages = self.get_packages(plugin_name, biz_version, biz_version)
os_cpu__biz_pkg_map = {self.get_os_key(package.os, package.cpu_arch): package for package in packages}
if not os_cpu__biz_pkg_map:
raise errors.PluginValidationError(
msg="插件 [{name}-{versions}] 不存在".format(name=self.plugin_name, versions=biz_version)
)
package = os_cpu__biz_pkg_map[self.get_os_key(os_type, cpu_arch)]
return package

@staticmethod
def get_biz_version(package: models.Packages, bk_biz_id: int):
plugin_version_config = models.GlobalSettings.get_config(
models.GlobalSettings.KeyEnum.PLUGIN_VERSION_CONFIG.value
)
biz_version = None
if str(bk_biz_id) in plugin_version_config:
biz_version_config = plugin_version_config[str(bk_biz_id)]
biz_version = next(
(
biz_plugin_version
for biz_plugin_name, biz_plugin_version in biz_version_config.items()
if package.project == biz_plugin_name
),
None,
)
return biz_version

def get_packages(self, plugin_name: str, plugin_version: str, biz_version: str = None):
# 对于不存的版本取最大 id 的 package
all_packages = models.Packages.objects.filter(project=plugin_name).values("id", "os", "cpu_arch", "version")
Expand Down

0 comments on commit 9f21f98

Please sign in to comment.