Skip to content

Commit

Permalink
feat: 安装预设插件锁定版本(closed #2482)
Browse files Browse the repository at this point in the history
  • Loading branch information
jpyoung3 committed Dec 20, 2024
1 parent 27b560a commit f62ff72
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 40 deletions.
10 changes: 7 additions & 3 deletions apps/backend/components/collections/plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,8 @@ 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)
plugin_name = common_data.plugin_name
package = policy_step_adapter.get_matching_package_obj(host.os_type, host.cpu_arch, host.bk_biz_id, plugin_name)
return package

def get_plugin_root_by_process_status(
Expand Down Expand Up @@ -285,7 +286,9 @@ 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, bk_biz_id, plugin_name
)
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 Down Expand Up @@ -342,10 +345,11 @@ def get_package(
os_type: str,
cpu_arch: str,
bk_biz_id: int,
plugin_name: str,
) -> 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, bk_biz_id, plugin_name)
except errors.PackageNotExists as error:
# 插件包不支持或不存在时,记录异常信息,此实例不参与后续流程
self.move_insts_to_failed([subscription_instance.id], str(error))
Expand Down
79 changes: 43 additions & 36 deletions apps/backend/subscription/steps/adapter.py
Original file line number Diff line number Diff line change
Expand Up @@ -445,46 +445,26 @@ 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, bk_biz_id: int, plugin_name: str
) -> models.Packages:
try:
package = self.os_key_pkg_map[self.get_os_key(os_type, cpu_arch)]
except KeyError:
# 如果不存在某个系统架构的版本,则获取最大id的版本
package = (
models.Packages.objects.filter(project=plugin_name, os_type=os_type, cpu_arch=cpu_arch)
.order_by("-id")
.first()
)
if not package:
package = (
models.Packages.objects.filter(project=package.project, os_type=os_type, cpu_arch=cpu_arch)
.order_by("-id")
.first()
)

# 如果设定了业务最大版本,则判断当前版本是否大于业务设定的最大版本
plugin_version_config = self.plugin_version_config()
if package and 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,
msg = _("插件 [{name}] 不支持 系统:{os_type}-架构:{cpu_arch}-版本:{plugin_version}").format(
name=self.plugin_name,
os_type=os_type,
cpu_arch=cpu_arch,
plugin_version=self.get_matching_package_dict(os_type, cpu_arch)["version"],
)
if biz_version:
version_str = getattr(package, "version", "")
tag_name__obj_map: Dict[str, Tag] = PluginTargetHelper.get_tag_name__obj_map(
target_id=self.plugin_desc.id,
)
if version_str in tag_name__obj_map:
version_str = tag_name__obj_map[version_str].target_version
if version.Version(version_str) > version.Version(biz_version):
package = self.get_biz_max_package(package.project, os_type, cpu_arch, biz_version)
except KeyError:
msg = _("插件 [{name}] 不支持 系统:{os_type}-架构:{cpu_arch}-版本:{plugin_version}").format(
name=self.plugin_name,
os_type=os_type,
cpu_arch=cpu_arch,
plugin_version=self.get_matching_package_dict(os_type, cpu_arch)["version"],
)
raise errors.PackageNotExists(msg)
raise errors.PackageNotExists(msg)
else:
if not package.is_ready:
msg = _("插件 [{name}] 系统:{os_type}-架构:{cpu_arch}-版本:{plugin_version} 未启用").format(
Expand All @@ -494,7 +474,10 @@ def get_matching_package_obj(self, os_type: str, cpu_arch: str, bk_biz_id: int)
plugin_version=self.get_matching_package_dict(os_type, cpu_arch)["version"],
)
raise errors.PluginValidationError(msg)
return package

# 如果设定了业务最大版本,则判断当前版本是否大于业务设定的最大版本
package = self.check_biz_version(package, bk_biz_id)
return package

def get_matching_config_tmpl_objs(
self, os_type: str, cpu_arch: str, package: models.Packages = None, bk_biz_id: int = None, config: Dict = None
Expand All @@ -517,6 +500,30 @@ def get_matching_config_tmpl_objs(
return plugin_config_templates
return self.config_tmpl_obj_gby_os_key.get(self.get_os_key(os_type, cpu_arch), [])

def check_biz_version(self, package: models.Packages, bk_biz_id: int):
# 如果设定了业务最大版本,则判断当前版本是否大于业务设定的最大版本
plugin_version_config = self.plugin_version_config()
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,
)
if biz_version:
version_str = getattr(package, "version", "")
tag_name__obj_map: Dict[str, Tag] = PluginTargetHelper.get_tag_name__obj_map(
target_id=self.plugin_desc.id,
)
if version_str in tag_name__obj_map:
version_str = tag_name__obj_map[version_str].target_version
if version.Version(version_str) > version.Version(biz_version):
package = self.get_biz_max_package(package.project, package.os, package.cpu_arch, biz_version)
return package

@staticmethod
def get_biz_max_package(plugin_name: str, os_type: str, cpu_arch: str, biz_version: str):
"""获取业务锁定版本的插件包"""
Expand Down
2 changes: 1 addition & 1 deletion env/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@
)
BKAPP_DEFAULT_INSTALL_CHANNEL_ID = get_type_env(key="BKAPP_DEFAULT_INSTALL_CHANNEL_ID", default=-1, _type=int)
BKAPP_AUTOMATIC_CHOICE_CLOUD_ID = get_type_env(key="BKAPP_AUTOMATIC_CHOICE_CLOUD_ID", default=-1, _type=int)
BKAPP_UNASSIGNED_CLOUD_ID = get_type_env(key="BKAPP_UNASSIGNED_CLOUD_ID", default=90000001, _type=int)
BKAPP_UNASSIGNED_CLOUD_ID = int(get_type_env(key="BKAPP_UNASSIGNED_CLOUD_ID", default=90000001, _type=float))

# ===============================================================================
# 日志
Expand Down

0 comments on commit f62ff72

Please sign in to comment.