Skip to content

Commit

Permalink
feature: Agent升级功能支持 2.x -> 2.x 的升级 (close TencentBlueKing#1311)
Browse files Browse the repository at this point in the history
  • Loading branch information
zhangzhw8 authored and ZhuoZhuoCrayon committed Dec 22, 2022
1 parent d6332fc commit 85e43a0
Show file tree
Hide file tree
Showing 6 changed files with 56 additions and 7 deletions.
8 changes: 6 additions & 2 deletions apps/backend/components/collections/agent_new/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -104,13 +104,18 @@ def get_general_node_type(cls, node_type: str) -> str:
]

@classmethod
def get_agent_upgrade_pkg_name(cls, host: models.Host) -> str:
def get_agent_upgrade_pkg_name(cls, common_data: "AgentCommonData", host: models.Host) -> str:
"""
获取 Agent 升级包名称
:param common_data:
:param host:
:return:
"""
package_type = ("client", "proxy")[host.node_type == constants.NodeType.PROXY]
agent_step_adapter = common_data.agent_step_adapter
if not agent_step_adapter.is_legacy:
setup_info = agent_step_adapter.get_setup_info()
return f"{setup_info.name}-{setup_info.version}.tgz"
if host.os_version:
major_version_number = None
if host.os_type == constants.OsType.AIX:
Expand Down Expand Up @@ -299,7 +304,6 @@ def maintain_agent_proc_status_uniqueness(self, bk_host_ids: Set[int]) -> None:

@dataclass
class AgentCommonData(CommonData):

# 默认接入点
default_ap: models.AccessPoint
# 主机ID - 接入点 映射关系
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,20 @@ class PushUpgradeFileService(AgentTransferFileService):
def get_file_target_path(self, data, common_data: AgentCommonData, host: models.Host) -> str:
return common_data.host_id__ap_map[host.bk_host_id].get_agent_config(host.os_type)["temp_path"]

def get_file_list(self, data, common_data: AgentCommonData, host: models.Host) -> List[str]:

def get_upgrade_package_source_path(self, common_data: AgentCommonData, host: models.Host):
"""
获取升级包源路径
"""
# 1.x 升级到 1.x,使用老到路径,升级包直接放在 download 目录下
download_path = common_data.host_id__ap_map[host.bk_host_id].nginx_path or settings.DOWNLOAD_PATH
agent_upgrade_package_name = self.get_agent_upgrade_pkg_name(host=host)
if not common_data.agent_step_adapter.is_legacy:
# 2.x 升级到 2.x,根据操作系统、CPU 架构等组合路径
download_path = os.path.join(download_path, "agent", host.os_type.lower(), host.cpu_arch.lower())
return download_path

def get_file_list(self, data, common_data: AgentCommonData, host: models.Host) -> List[str]:
download_path = self.get_upgrade_package_source_path(common_data, host)
agent_upgrade_package_name = self.get_agent_upgrade_pkg_name(common_data, host=host)

file_names: List[str] = [agent_upgrade_package_name]

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ def script_name(self):
return "upgrade_command"

def get_script_content(self, data, common_data: AgentCommonData, host: models.Host) -> str:
agent_upgrade_pkg_name = self.get_agent_upgrade_pkg_name(host)
agent_upgrade_pkg_name = self.get_agent_upgrade_pkg_name(common_data, host)
general_node_type = self.get_general_node_type(host.node_type)
agent_config = common_data.host_id__ap_map[host.bk_host_id].get_agent_config(host.os_type)

Expand Down
4 changes: 3 additions & 1 deletion apps/backend/subscription/steps/agent.py
Original file line number Diff line number Diff line change
Expand Up @@ -272,7 +272,9 @@ def _generate_activities(self, agent_manager: AgentManager):

class UpgradeAgent(ReinstallAgent):
"""
升级Agent
升级Agent,仅适用于 1.x 升级到 1.x 或 2.x 升级到 2.x ,不支持 1.x 升级到 2.x 的场景
1.x 升级到 2.x 到场景,使用 InstallAgent2 覆盖安装
TODO 2.x 升级到 2.x 的场景暂时沿用替换二进制后 reload 的方案,待 GSE 提供自升级方案后再调整
"""

ACTION_NAME = backend_const.ActionNameType.UPGRADE_AGENT
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
from abc import ABC

from django.conf import settings
from django.utils.crypto import get_random_string

from apps.backend.components.collections.agent_new import components
from apps.node_man import constants, models
Expand Down Expand Up @@ -39,6 +40,35 @@ def tearDown(self) -> None:
super().tearDown()


class PushUpgradePackageV2TestCase(base.JobBaseTestCase):
@classmethod
def get_default_case_name(cls) -> str:
return "下发 Linux 升级包 2.0 成功"

@classmethod
def setUpTestData(cls):
super().setUpTestData()
models.Host.objects.filter(bk_host_id__in=cls.obj_factory.bk_host_ids).update(bk_agent_id=get_random_string())
sub_step_obj: models.SubscriptionStep = cls.obj_factory.sub_step_objs[0]
sub_step_obj.config.update({"name": "gse_agent", "version": "2.0.0"})
sub_step_obj.save()

def component_cls(self):
return components.PushUpgradePackageComponent

def tearDown(self) -> None:
record = self.job_api_mock_client.call_recorder.record
fast_transfer_file_query_params = record[JobApi.fast_transfer_file][0].args[0]
self.assertEqual(
fast_transfer_file_query_params["file_source_list"][0]["file_list"],
[os.path.join(settings.DOWNLOAD_PATH, "agent/linux/x86_64/gse_agent-2.0.0.tgz")],
)
super().tearDown()

def test_component(self):
super().test_component()


class PushAixUpgradePackageSuccessTestCase(base.JobBaseTestCase):
@classmethod
def get_default_case_name(cls) -> str:
Expand Down
3 changes: 3 additions & 0 deletions dev_log/2.2.32/durant_202212081048.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
---
feature:
- "Agent升级功能支持 2.x -> 2.x 的升级 (close #1311)"

0 comments on commit 85e43a0

Please sign in to comment.