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 0ffd84a commit 6766453
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 35 deletions.
57 changes: 51 additions & 6 deletions apps/backend/components/collections/plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
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 @@ -69,7 +70,6 @@

@dataclass
class PluginCommonData(CommonData):

# 进程状态列表
process_statuses: List[models.ProcessStatus]
# 目标主机列表,用于远程采集场景
Expand Down Expand Up @@ -203,7 +203,7 @@ def get_package_by_process_status(
"""通过进程状态得到插件包对象"""
host = self.get_host_by_process_status(process_status, common_data)
policy_step_adapter = common_data.policy_step_adapter
package = policy_step_adapter.get_matching_package_obj(host.os_type, host.cpu_arch, host.bk_biz_id)
package = policy_step_adapter.get_matching_package_obj(host.os_type, host.cpu_arch)
return package

def get_plugin_root_by_process_status(
Expand Down Expand Up @@ -285,7 +285,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, bk_biz_id)
package = self.get_package(subscription_instance, policy_step_adapter, os_type, cpu_arch)
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,6 +294,11 @@ 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 = self.get_biz_package_version(
policy_step_adapter, package.project, os_type, cpu_arch, biz_version
)
process_status_property = dict(
bk_host_id=bk_host_id,
name=plugin_name,
Expand Down Expand Up @@ -330,6 +335,48 @@ def _execute(self, data, parent_data, common_data: PluginCommonData):
batch_size=self.batch_size,
)

def get_biz_package_version(
self, policy_step_adapter: PolicyStepAdapter, plugin_name: str, os_type: str, cpu_arch: str, biz_version: str
):
packages = policy_step_adapter.get_packages(plugin_name, biz_version, biz_version)
os_cpu__biz_pkg_map = {
policy_step_adapter.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[policy_step_adapter.get_os_key(os_type, cpu_arch)]
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

def inputs_format(self):
return self.inputs_format() + [
Service.InputItem(name="action", key="action", type="str", required=True),
Expand All @@ -341,11 +388,10 @@ 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, bk_biz_id)
return policy_step_adapter.get_matching_package_obj(os_type, cpu_arch)
except errors.PackageNotExists as error:
# 插件包不支持或不存在时,记录异常信息,此实例不参与后续流程
self.move_insts_to_failed([subscription_instance.id], str(error))
Expand Down Expand Up @@ -901,7 +947,6 @@ def get_job_instance_status(
# 其它 job_status 则认为任务已结束,进一步查询IP的 JOB 日志,并进行端口分配
multi_allocate_params = []
for process_status in process_statuses:

bk_host_id = process_status.bk_host_id
host = host_id_obj_map.get(bk_host_id)
subscription_instance = group_id_instance_map.get(process_status.group_id)
Expand Down
30 changes: 1 addition & 29 deletions apps/backend/subscription/steps/adapter.py
Original file line number Diff line number Diff line change
Expand Up @@ -449,16 +449,9 @@ 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, bk_biz_id: int) -> models.Packages:
def get_matching_package_obj(self, os_type: str, cpu_arch: str) -> models.Packages:
try:
package = self.os_key_pkg_map[self.get_os_key(os_type, cpu_arch)]
plugin_version_config = models.GlobalSettings.get_config(
models.GlobalSettings.KeyEnum.PLUGIN_VERSION_CONFIG.value
)
if str(bk_biz_id) in plugin_version_config:
os_cpu__biz_pkg_map = self.get_biz_version(package, plugin_version_config, bk_biz_id)
if os_cpu__biz_pkg_map:
package = os_cpu__biz_pkg_map[self.get_os_key(os_type, cpu_arch)]
except KeyError:
msg = _("插件 [{name}] 不支持 系统:{os_type}-架构:{cpu_arch}-版本:{plugin_version}").format(
name=self.plugin_name,
Expand Down Expand Up @@ -503,24 +496,3 @@ def get_packages(self, plugin_name: str, plugin_version: str, biz_version: str =
id__in=[pkg.id for pkg in packages if version.Version(pkg.version) <= version.Version(biz_version)]
)
return packages

def get_biz_version(self, package: models.Packages, plugin_version_config: Dict[str, Dict], bk_biz_id: int):
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

0 comments on commit 6766453

Please sign in to comment.