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 f14f01b commit ab9249e
Show file tree
Hide file tree
Showing 3 changed files with 85 additions and 8 deletions.
51 changes: 49 additions & 2 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 @@ -280,6 +280,7 @@ def _execute(self, data, parent_data, common_data: PluginCommonData):
# target_host_objs 的长度通常为1或2,此处也不必担心时间复杂度问题
# 指定 target_host 主要用于远程采集的场景,常见于第三方插件,如拨测
for host in target_host_objs:
bk_biz_id = host.bk_biz_id
bk_host_id = host.bk_host_id
os_type = host.os_type.lower()
cpu_arch = host.cpu_arch
Expand All @@ -293,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 @@ -329,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 Down Expand Up @@ -899,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
40 changes: 34 additions & 6 deletions apps/backend/subscription/steps/adapter.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

from django.db.models import Max, Subquery, Value
from django.utils.translation import ugettext as _
from packaging import version
from rest_framework import exceptions, serializers

from apps.backend.subscription import errors
Expand Down Expand Up @@ -285,13 +286,17 @@ def format2policy_packages_new(
self, plugin_id: int, plugin_name: str, plugin_version: str, config_templates: List[Dict[str, Any]]
) -> List[Dict[str, Any]]:
latest_flag: str = "latest"
is_tag: bool = Tag.objects.filter(
stable_flag: str = "stable"
is_latest_tag: bool = Tag.objects.filter(
target_id=plugin_id, name=latest_flag, target_type=TargetType.PLUGIN.value
).exists()
is_stable_tag: bool = Tag.objects.filter(
target_id=plugin_id, name=stable_flag, target_type=TargetType.PLUGIN.value
).exists()

if plugin_version != latest_flag or is_tag:
if plugin_version not in (latest_flag, stable_flag) or is_latest_tag or is_stable_tag:
# 如果 latest 是 tag,走取指定版本的逻辑
packages = models.Packages.objects.filter(project=plugin_name, version=plugin_version)
packages = self.get_packages(plugin_name, plugin_version)
else:
max_pkg_ids: List[int] = self.max_ids_by_key(
list(models.Packages.objects.filter(project=plugin_name).values("id", "os", "cpu_arch"))
Expand All @@ -306,11 +311,11 @@ def format2policy_packages_new(
os_cpu__config_templates_map = defaultdict(list)
for template in config_templates:
is_main_template = template["is_main"]
if template["version"] != latest_flag or is_tag:
if template["version"] not in (latest_flag, stable_flag) or is_latest_tag or is_stable_tag:
plugin_version_set = {plugin_version, "*"}
else:
latest_packages_version_set = set(packages.values_list("version", flat=True))
plugin_version_set = latest_packages_version_set | {"*"}
tag_packages_version_set = set(packages.values_list("version", flat=True))
plugin_version_set = tag_packages_version_set | {"*"}

max_config_tmpl_ids: typing.List[int] = self.max_ids_by_key(
list(
Expand Down Expand Up @@ -468,3 +473,26 @@ 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_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")
version_packages = {pkg["id"]: pkg for pkg in all_packages if pkg["version"] == plugin_version}
package_ids = set(version_packages.keys())
for os in constants.PLUGIN_OS_TUPLE:
for cpu_arch in constants.CPU_TUPLE:
if not any(
pkg["os"] == os and pkg["cpu_arch"] == cpu_arch
for pkg in all_packages
if pkg["version"] == plugin_version
):
max_pkg_ids: List[int] = self.max_ids_by_key(
[pkg for pkg in all_packages if pkg["os"] == os and pkg["cpu_arch"] == cpu_arch]
)
package_ids.update(max_pkg_ids)
packages = models.Packages.objects.filter(id__in=package_ids)
if biz_version:
packages = packages.filter(
id__in=[pkg.id for pkg in packages if version.Version(pkg.version) <= version.Version(biz_version)]
)
return packages
2 changes: 2 additions & 0 deletions apps/node_man/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,8 @@ class KeyEnum(Enum):
AUTO_SELECT_INSTALL_CHANNEL_ONLY_DIRECT_AREA = "AUTO_SELECT_INSTALL_CHANNEL_ONLY_DIRECT_AREA"
# 安装通道ID与网段列表映射
INSTALL_CHANNEL_ID_NETWORK_SEGMENT = "INSTALL_CHANNEL_ID_NETWORK_SEGMENT"
# 业务最大插件版本
PLUGIN_VERSION_CONFIG = "PLUGIN_VERSION_CONFIG"

key = models.CharField(_("键"), max_length=255, db_index=True, primary_key=True)
v_json = JSONField(_("值"))
Expand Down

0 comments on commit ab9249e

Please sign in to comment.