Skip to content

Commit

Permalink
feature: Linux 2.0 Agent / Proxy 支持配置进程托管模式 (closed TencentBlueKing#1698
Browse files Browse the repository at this point in the history
)
  • Loading branch information
ZhuoZhuoCrayon committed Jul 27, 2023
1 parent 91180f3 commit c9782a0
Show file tree
Hide file tree
Showing 12 changed files with 1,776 additions and 143 deletions.
18 changes: 16 additions & 2 deletions apps/backend/agent/artifact_builder/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
from apps.backend import exceptions
from apps.core.files import core_files_constants
from apps.core.files.storage import get_storage
from apps.node_man import constants
from apps.node_man import constants, models
from apps.utils import cache, files

logger = logging.getLogger("app")
Expand Down Expand Up @@ -211,7 +211,21 @@ def _inject_dependencies(self, extract_dir: str):
with self.storage.open(gsectl_file_path, mode="rb") as fs:
# mode 指定 w,覆盖现有文件
with open(gsectl_target_file_path, mode="wb") as local_fs:
local_fs.write(fs.read())
gsectl_file_content: str = fs.read().decode()
# TODO 后续如果 1.0 Agent 包也纳入管理,此处需要区分
# 1.0 Agent / Proxy 的 gsectl 采用固定的 rclocal 模式
if gsectl_filename == "gsectl" and "{{ AUTO_TYPE }}" in gsectl_file_content:
auto_type: str = models.GlobalSettings.get_config(
models.GlobalSettings.KeyEnum.GSE2_LINUX_AUTO_TYPE.value,
constants.GseLinuxAutoType.RCLOCAL.value,
)
logger.info(f"apply auto_type -> {auto_type} to gsectl")
gsectl_file_content = gsectl_file_content.replace("{{ AUTO_TYPE }}", auto_type)
if "{{ AUTO_TYPE }}" in gsectl_file_content:
raise exceptions.PkgMetaInfoValidationError(
_("渲染 AUTO_TYPE -> {auto_type} 到 gsectl 失败").format(auto_type=auto_type)
)
local_fs.write(gsectl_file_content.encode(encoding="utf-8"))
logger.info(f"copy gsectl -> {gsectl_file_path} to {gsectl_target_file_path} success.")

# 为二进制文件授予可执行权限
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,12 @@ def get_script_content(self, data, common_data: AgentCommonData, host: models.Ho
general_node_type = self.get_general_node_type(host.node_type)
setup_path = common_data.host_id__ap_map[host.bk_host_id].get_agent_config(host.os_type)["setup_path"]
agent_path = path_handler.join(setup_path, general_node_type, "bin")

return f"cd {agent_path} && ./gse_agent --reload"
if common_data.agent_step_adapter.is_legacy:
return f"cd {agent_path} && ./gse_agent --reload"
else:
# 趋势:gsectl 后续作为 Agent 操作的入口
# || 兼容旧版本 gsectl 没有 reload 操作
return f"cd {agent_path} && ./gsectl --reload agent || ./gse_agent --reload"

def update_host_upstream_nodes(self, common_data: AgentCommonData, gse_version: str):
hosts = [host for host in common_data.host_id_obj_map.values() if host.bk_cloud_id != constants.DEFAULT_CLOUD]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@

from django.conf import settings

from apps.backend.agent.artifact_builder.proxy import ProxyArtifactBuilder
from apps.node_man import constants, models

from .base import AgentCommonData, AgentExecuteScriptService
Expand Down Expand Up @@ -50,13 +49,27 @@
# Agent 重载配置命令模板
AGENT_RELOAD_CMD_TEMPLATE = "cd {setup_path}/{node_type}/bin && ./{procs} --reload || ./gsectl restart all"


# 进程拉起配置命令
PROCESS_PULL_CONFIGURATION_CMD = """setup_startup_scripts
remove_crontab
"""

# 节点类型 - 重载命令模板映射关系
NODE_TYPE__RELOAD_CMD_TPL_MAP = {
LEGACY_NODE_TYPE__RELOAD_CMD_TPL_MAP = {
constants.NodeType.PROXY.lower(): PROXY_RELOAD_CMD_TEMPLATE,
constants.NodeType.AGENT.lower(): AGENT_RELOAD_CMD_TEMPLATE,
}


# 新版本 Agent 具有 systemd / crontab 等多种拉起方式,systemd 模式下 reload 会导致进程拉不起来
# 鉴于 reload 和 start 逻辑均是采取干掉进程再恢复的逻辑,统一采用 restart
NODE_TYPE__RELOAD_CMD_TPL_MAP = {
constants.NodeType.PROXY.lower(): "cd {setup_path}/{node_type}/bin && ./gsectl restart all",
constants.NodeType.AGENT.lower(): "cd {setup_path}/{node_type}/bin && ./gsectl restart",
}


class RunUpgradeCommandService(AgentExecuteScriptService):
@property
def script_name(self):
Expand All @@ -79,23 +92,32 @@ def get_script_content(self, data, common_data: AgentCommonData, host: models.Ho
with open(tpl_path, encoding="utf-8") as fh:
scripts = fh.read()

if host.node_type == constants.NodeType.PROXY:
if common_data.agent_step_adapter.is_legacy:
if common_data.agent_step_adapter.is_legacy:
process_pull_configuration_cmd: str = PROCESS_PULL_CONFIGURATION_CMD

if host.node_type == constants.NodeType.PROXY:
procs: typing.List[str] = ["gse_agent", "gse_transit", "gse_btsvr", "gse_data"]
else:
procs: typing.List[str] = ["gse_agent"] + ProxyArtifactBuilder.PROXY_SVR_EXES
procs: typing.List[str] = ["gse_agent"]

reload_cmd = LEGACY_NODE_TYPE__RELOAD_CMD_TPL_MAP[general_node_type].format(
setup_path=agent_config["setup_path"], node_type=general_node_type, procs=" ".join(procs)
)

else:
procs: typing.List[str] = ["gse_agent"]
# 新版本 Agent,通过 gsectl 配置进程拉起方式
process_pull_configuration_cmd: str = ""
reload_cmd = NODE_TYPE__RELOAD_CMD_TPL_MAP[general_node_type].format(
setup_path=agent_config["setup_path"], node_type=general_node_type
)

reload_cmd = NODE_TYPE__RELOAD_CMD_TPL_MAP[general_node_type].format(
setup_path=agent_config["setup_path"], node_type=general_node_type, procs=" ".join(procs)
)
scripts = scripts.format(
setup_path=agent_config["setup_path"],
temp_path=agent_config["temp_path"],
package_name=agent_upgrade_pkg_name,
node_type=general_node_type,
reload_cmd=reload_cmd,
process_pull_configuration_cmd=process_pull_configuration_cmd,
pkg_cpu_arch=host.cpu_arch,
)
return scripts
5 changes: 5 additions & 0 deletions apps/backend/exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,3 +83,8 @@ class AgentConfigTemplateNotExistError(BackendBaseException):
class NotSemanticVersionError(BackendBaseException):
MESSAGE_TPL = _("版本号 -> {version} 不符合语义化版本规则")
ERROR_CODE = 13


class PkgMetaInfoValidationError(BackendBaseException):
MESSAGE = _("安装包元数据校验失败")
ERROR_CODE = 14
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
)
from apps.backend.components.collections.agent_new.run_upgrade_command import (
AGENT_RELOAD_CMD_TEMPLATE,
NODE_TYPE__RELOAD_CMD_TPL_MAP,
PROCESS_PULL_CONFIGURATION_CMD,
WINDOWS_UPGRADE_CMD_TEMPLATE,
)
from apps.node_man import constants, models
Expand All @@ -39,6 +41,7 @@ class RunUpgradeCommandSuccessTestCase(base.JobBaseTestCase):
package_name="gse_client-linux-x86_64_upgrade.tgz",
node_type="agent",
reload_cmd=AGENT_RELOAD_CMD_TEMPLATE.format(setup_path="/usr/local/gse", node_type="agent", procs="gse_agent"),
process_pull_configuration_cmd=PROCESS_PULL_CONFIGURATION_CMD,
pkg_cpu_arch="x86_64",
)
WINDOWS_TEST_SCRIPTS = WINDOWS_UPGRADE_CMD_TEMPLATE.format(
Expand Down Expand Up @@ -93,9 +96,8 @@ def tearDown(self) -> None:
temp_path="/tmp",
package_name="gse_agent-2.0.0.tgz",
node_type="agent",
reload_cmd=AGENT_RELOAD_CMD_TEMPLATE.format(
setup_path="/usr/local/gse", node_type="agent", procs="gse_agent"
),
reload_cmd=NODE_TYPE__RELOAD_CMD_TPL_MAP["agent"].format(setup_path="/usr/local/gse", node_type="agent"),
process_pull_configuration_cmd="",
pkg_cpu_arch="x86_64",
)
for record_result in record[JobApi.fast_execute_script]:
Expand Down
16 changes: 16 additions & 0 deletions apps/node_man/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -965,6 +965,22 @@ def _get_member__alias_map(cls) -> Dict[Enum, str]:
}


class GseLinuxAutoType(EnhanceEnum):
"""GSE Linux 进程拉起(托管方式),该配置在 Agent 打包时,渲染到 gsectl 中"""

CRONTAB = "crontab"
RCLOCAL = "rclocal"
SYSTEMD = "systemd"

@classmethod
def _get_member__alias_map(cls) -> Dict[Enum, str]:
return {
cls.CRONTAB: _("crontab"),
cls.RCLOCAL: _("rclocal"),
cls.SYSTEMD: _("systemd"),
}


########################################################################################################
# CMDB
########################################################################################################
Expand Down
2 changes: 2 additions & 0 deletions apps/node_man/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,8 @@ class KeyEnum(Enum):
PLUGIN_COMMON_CONSTANTS = "PLUGIN_COMMON_CONSTANTS"
# GSE 2.0 灰度列表
GSE2_GRAY_SCOPE_LIST = "GSE2_GRAY_SCOPE_LIST"
# GSE 2.0 Linux Agent/Proxy 进程托管(拉起)方式,默认为 rclocal
GSE2_LINUX_AUTO_TYPE = "GSE2_LINUX_AUTO_TYPE"
# 禁用订阅业务列表
DISABLE_SUBSCRIPTION_SCOPE_LIST = "DISABLE_SUBSCRIPTION_SCOPE_LIST"
GSE2_GRAY_AP_MAP = "GSE2_GRAY_AP_MAP"
Expand Down
5 changes: 3 additions & 2 deletions script_tools/agent_tools/agent2/setup_agent.sh
Original file line number Diff line number Diff line change
Expand Up @@ -864,13 +864,14 @@ DEBUG_LOG_FILE=${TMP_DIR}/nm.${0##*/}.${TASK_ID}.debug
exec &> >(tee "$DEBUG_LOG_FILE")

log check_env - "Args are: $*"

# removed remove_crontab、setup_startup_scripts -> 由 gsectl 判断是否添加 / 移除

for step in check_env \
download_pkg \
remove_crontab \
remove_agent \
remove_proxy_if_exists \
setup_agent \
setup_startup_scripts \
check_deploy_result; do
$step
done
2 changes: 0 additions & 2 deletions script_tools/agent_tools/agent2/setup_proxy.sh
Original file line number Diff line number Diff line change
Expand Up @@ -807,11 +807,9 @@ log check_env - "$@"
#pre_view
for step in check_env \
download_pkg \
remove_crontab \
remove_proxy \
remove_agent_if_exists \
setup_proxy \
setup_startup_scripts \
check_deploy_result; do
$step
done
Loading

0 comments on commit c9782a0

Please sign in to comment.