Skip to content

Commit

Permalink
sprintfix: 修复安装 P-Agent 场景下安装包分发不准确的问题 (fixed #1315
Browse files Browse the repository at this point in the history
  • Loading branch information
ZhuoZhuoCrayon committed Apr 25, 2023
1 parent 9ee7aa4 commit 6117bfe
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 32 deletions.
37 changes: 32 additions & 5 deletions apps/backend/components/collections/agent_new/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
)

import wrapt
from django.conf import settings
from django.utils.translation import ugettext_lazy as _

from apps.backend.agent.tools import InstallationTools, batch_gen_commands
Expand Down Expand Up @@ -104,12 +105,19 @@ def get_general_node_type(cls, node_type: str) -> str:
]

@classmethod
def get_agent_pkg_name(cls, common_data: "AgentCommonData", host: models.Host, is_upgrade: bool = False) -> str:
def get_agent_pkg_name(
cls,
common_data: "AgentCommonData",
host: models.Host,
is_upgrade: bool = False,
return_name_with_cpu_tmpl: bool = False,
) -> str:
"""
获取 Agent 升级包名称
:param common_data: AgentCommonData
:param host: models.Host
:param is_upgrade: bool 是否升级包
:param return_name_with_cpu_tmpl: 是否返回带 cpu 模板的名称
:return:
"""
# GSE2.0 安装包和升级包复用同一个包
Expand All @@ -132,13 +140,32 @@ def get_agent_pkg_name(cls, common_data: "AgentCommonData", host: models.Host, i
)
if not major_version_number:
raise OsVersionPackageValidationError(os_version=host.os_version, os_type=host.os_type)
agent_upgrade_package_name = (
f"gse_{package_type}-{host.os_type.lower()}{major_version_number}-{host.cpu_arch}{pkg_suffix}.tgz"
agent_pkg_name = (
f"gse_{package_type}-{host.os_type.lower()}{major_version_number}-" + "{cpu_arch}" + f"{pkg_suffix}.tgz"
)
else:
agent_upgrade_package_name = f"gse_{package_type}-{host.os_type.lower()}-{host.cpu_arch}{pkg_suffix}.tgz"
agent_pkg_name = f"gse_{package_type}-{host.os_type.lower()}-" + "{cpu_arch}" + f"{pkg_suffix}.tgz"

return agent_upgrade_package_name
return (agent_pkg_name.format(cpu_arch=host.cpu_arch), agent_pkg_name)[return_name_with_cpu_tmpl]

@classmethod
def get_agent_pkg_dir(cls, common_data: "AgentCommonData", host: models.Host) -> str:
"""
获取 Agent 安装包目录
:param common_data: AgentCommonData
:param host: models.Host
:return:
"""
host_ap = common_data.host_id__ap_map[host.bk_host_id]
download_path = host_ap.nginx_path or settings.DOWNLOAD_PATH
if common_data.agent_step_adapter.is_legacy:
# 旧版本 Agent 安装包位于下载目录
agent_path = download_path
else:
# 新版本 Agent 目录规则为 agent/{os}/{cpu_arch}/
# 具体参考:apps/backend/agent/artifact_builder/base.py make_and_upload_package
agent_path = constants.LINUX_SEP.join([download_path, "agent", host.os_type.lower()])
return agent_path

@staticmethod
def get_cloud_id__proxies_map(bk_cloud_ids: Iterable[int]) -> Dict[int, List[models.Host]]:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,6 @@
from dataclasses import dataclass, fields
from typing import Dict, List

from django.conf import settings

from apps.core.files.storage import get_storage
from apps.node_man import constants, models

Expand Down Expand Up @@ -60,36 +58,36 @@ def get_target_servers(self, data, common_data: PushAgentPkgCommonData, host: mo
target_servers["host_id_list"].append(install_channel_host.bk_host_id)
else:
for proxy in common_data.cloud_id__proxies_map[host.bk_cloud_id]:
target_servers["ip_list"].append({"bk_cloud_id": proxy.bk_cloud_id, "ip": host.inner_ip})
# 只对存活的 Proxy 分发文件,提高任务成功率
if proxy.status != constants.ProcStateType.RUNNING:
continue
target_servers["ip_list"].append({"bk_cloud_id": proxy.bk_cloud_id, "ip": proxy.inner_ip})
target_servers["host_id_list"].append(proxy.bk_host_id)
return target_servers

def get_job_file_params(self, data, common_data: AgentCommonData, host: models.Host):
"""
GSE2.0 Agent 包目录按以下进行约束
├── agent
│ ├── linux
│ │ ├── x86_64
│ │ │ └── gse_agent-2.0.0.tgz
│ │ └── aarch64
│ │ └── gse_agent-2.0.0.tgz
│ └── windows
│ └── x86_64
│ └── gse_agent-2.0.0.tgz
"""
pkg_name = self.get_agent_pkg_name(common_data, host)
host_ap = common_data.host_id__ap_map[host.bk_host_id]
download_path = host_ap.nginx_path or settings.DOWNLOAD_PATH
agent_path = constants.LINUX_SEP.join([download_path, "agent", host.os_type.lower()])
storage = get_storage()
cpu_arch_list, _ = storage.listdir(agent_path)
return [
{
"file_list": ["/".join([agent_path, cpu_arch, pkg_name])],
"file_target_path": "/".join([agent_path, cpu_arch]),
}
for cpu_arch in cpu_arch_list
]
agent_dir = self.get_agent_pkg_dir(common_data, host)
pkg_name = self.get_agent_pkg_name(common_data, host, return_name_with_cpu_tmpl=True)

if common_data.agent_step_adapter.is_legacy:
file_list = []
# 由于实际安装前 CPU 架构未知,此处对同操作系统的各类架构包进行一并下发
for cpu_arch in constants.CPU_TUPLE:
pkg_path = "/".join([agent_dir, pkg_name.format(cpu_arch=cpu_arch)])
if storage.exists(pkg_path):
file_list.append(pkg_path)
job_file_params = [{"file_list": file_list, "file_target_path": agent_dir}]
else:
cpu_arch_list, _ = storage.listdir(agent_dir)
job_file_params = [
{
"file_list": ["/".join([agent_dir, cpu_arch, pkg_name])],
"file_target_path": "/".join([agent_dir, cpu_arch]),
}
for cpu_arch in cpu_arch_list
]
return job_file_params

def get_job_param_os_type(self, host: models.Host) -> str:
# Proxy 或者安装通道跳板机 要求是 Linux 机器
Expand Down

0 comments on commit 6117bfe

Please sign in to comment.