From f12edb0bbb31a085c4e4b528b8441374b61dd5ad Mon Sep 17 00:00:00 2001 From: chalice-1831 <844589474@qq.com> Date: Wed, 20 Nov 2024 14:28:50 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E5=8D=B8=E8=BD=BDagent=E6=97=B6?= =?UTF-8?q?=E5=81=9C=E6=AD=A2=E6=8F=92=E4=BB=B6=E5=B9=B6=E6=B8=85=E7=90=86?= =?UTF-8?q?agent=E5=AE=89=E8=A3=85=E5=8F=8A=E8=BF=90=E8=A1=8C=E8=BF=87?= =?UTF-8?q?=E7=A8=8B=E4=B8=AD=E4=BA=A7=E7=94=9F=E7=9A=84=E7=9B=AE=E5=BD=95?= =?UTF-8?q?(closed=20#2490)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- apps/backend/agent/manager.py | 8 ++ .../collections/agent_new/components.py | 7 ++ .../collections/agent_new/stop_plugins.py | 86 +++++++++++++++++++ apps/backend/subscription/steps/agent.py | 2 + .../agent_tools/agent2/setup_agent.sh | 13 ++- .../agent_tools/agent2/setup_agent.zsh | 12 ++- .../agent_tools/agent2/setup_proxy.sh | 12 ++- script_tools/gsectl/agent/darwin/gsectl | 7 ++ script_tools/gsectl/agent/linux/gsectl | 10 +++ script_tools/gsectl/proxy/linux/gsectl | 14 ++- script_tools/setup_agent.ksh | 33 ++++++- script_tools/setup_agent.sh | 31 ++++++- script_tools/setup_agent.zsh | 37 ++++++-- script_tools/setup_proxy.sh | 28 ++++++ script_tools/setup_solaris_agent.sh | 17 ++++ 15 files changed, 305 insertions(+), 12 deletions(-) create mode 100644 apps/backend/components/collections/agent_new/stop_plugins.py diff --git a/apps/backend/agent/manager.py b/apps/backend/agent/manager.py index 1fa8393d3..d13f462af 100644 --- a/apps/backend/agent/manager.py +++ b/apps/backend/agent/manager.py @@ -297,3 +297,11 @@ def install_other_agent(cls, extra_agent_version: str, node_type: str = NodeType act.component.inputs.extra_agent_version = Var(type=Var.PLAIN, value=extra_agent_version) act.component.inputs.node_type = Var(type=Var.PLAIN, value=node_type) return act + + @classmethod + def stop_plugins(cls): + """停止插件""" + act = AgentServiceActivity( + component_code=components.StopPluginsComponent.code, name=components.StopPluginsComponent.name + ) + return act diff --git a/apps/backend/components/collections/agent_new/components.py b/apps/backend/components/collections/agent_new/components.py index 9fb74ef43..b66ce83a8 100644 --- a/apps/backend/components/collections/agent_new/components.py +++ b/apps/backend/components/collections/agent_new/components.py @@ -36,6 +36,7 @@ from .render_and_push_gse_config import RenderAndPushGseConfigService from .restart import RestartService from .run_upgrade_command import RunUpgradeCommandService +from .stop_plugins import StopPluginsService from .unbind_host_agent import UnBindHostAgentService from .update_install_info import UpdateInstallInfoService from .update_process_status import UpdateProcessStatusService @@ -221,3 +222,9 @@ class InstallOtherAgentComponent(Component): name = _("安装额外Agent") code = "install_other_agent" bound_service = InstallOtherAgentService + + +class StopPluginsComponent(Component): + name = _("停止插件") + code = "stop_plugins" + bound_service = StopPluginsService diff --git a/apps/backend/components/collections/agent_new/stop_plugins.py b/apps/backend/components/collections/agent_new/stop_plugins.py new file mode 100644 index 000000000..39d5d0a74 --- /dev/null +++ b/apps/backend/components/collections/agent_new/stop_plugins.py @@ -0,0 +1,86 @@ +# -*- coding: utf-8 -*- +""" +TencentBlueKing is pleased to support the open source community by making 蓝鲸智云-节点管理(BlueKing-BK-NODEMAN) available. +Copyright (C) 2017-2022 THL A29 Limited, a Tencent company. All rights reserved. +Licensed under the MIT License (the "License"); you may not use this file except in compliance with the License. +You may obtain a copy of the License at https://opensource.org/licenses/MIT +Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on +an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the +specific language governing permissions and limitations under the License. +""" + +from collections import defaultdict +from typing import List + +from django.conf import settings + +from apps.core.concurrent.retry import RetryHandler +from apps.node_man import constants, models +from apps.utils.batch_request import request_multi_thread +from common.api import NodeApi +from common.api.exception import DataAPIException + +from ..base import CommonData +from ..subsubscription import SubSubscriptionBaseService +from .base import AgentBaseService + + +class StopPluginsService(SubSubscriptionBaseService, AgentBaseService): + @staticmethod + @RetryHandler(interval=1, retry_times=1, exception_types=[DataAPIException]) + def call_create_subscription_api(params): + return NodeApi.create_subscription(params) + + @classmethod + def create_subscriptions(cls, common_data: CommonData) -> List[int]: + + host_ids_group_by_os = defaultdict(list) + for host in common_data.host_id_obj_map.values(): + host_ids_group_by_os[host.os_type.lower()].append(host.bk_host_id) + + installed_running_plugin_names = models.ProcessStatus.objects.filter( + status=constants.ProcStateType.RUNNING, + bk_host_id__in=common_data.bk_host_ids, + proc_type=constants.ProcType.PLUGIN, + ).values_list("name", flat=True) + + plugin_name__os_type_set = set( + models.Packages.objects.filter( + project__in=installed_running_plugin_names, os__in=host_ids_group_by_os.keys() + ).values_list("project", "os") + ) + params_list = [] + for (plugin_name, os_type) in plugin_name__os_type_set: + params_list.append( + { + "params": { + "run_immediately": True, + "category": models.Subscription.CategoryType.ONCE, + "bk_username": settings.SYSTEM_USE_API_ACCOUNT, + "scope": { + "node_type": models.Subscription.NodeType.INSTANCE, + "object_type": models.Subscription.ObjectType.HOST, + "nodes": [{"bk_host_id": bk_host_id} for bk_host_id in host_ids_group_by_os[os_type]], + }, + "steps": [ + { + "id": plugin_name, + "type": "PLUGIN", + "config": { + "job_type": constants.JobType.MAIN_STOP_PLUGIN, + "plugin_name": plugin_name, + "plugin_version": "latest", + "config_templates": [ + {"name": "{}.conf".format(plugin_name), "version": "latest", "is_main": True} + ], + }, + "params": {"context": {}}, + } + ], + } + } + ) + subscription_ids = request_multi_thread( + cls.call_create_subscription_api, params_list, get_data=lambda x: [x["subscription_id"]] + ) + return subscription_ids diff --git a/apps/backend/subscription/steps/agent.py b/apps/backend/subscription/steps/agent.py index ec6bb466b..a801781cd 100644 --- a/apps/backend/subscription/steps/agent.py +++ b/apps/backend/subscription/steps/agent.py @@ -497,6 +497,7 @@ class UninstallAgent(AgentAction): def _generate_activities(self, agent_manager: AgentManager): activities = [ agent_manager.query_password(), + agent_manager.stop_plugins(), agent_manager.uninstall_agent(), agent_manager.get_agent_status(expect_status=constants.ProcStateType.UNKNOWN), agent_manager.update_process_status(status=constants.ProcStateType.NOT_INSTALLED), @@ -515,6 +516,7 @@ class UninstallProxy(AgentAction): def _generate_activities(self, agent_manager: AgentManager): activities = [ + agent_manager.stop_plugins(), agent_manager.uninstall_proxy(), agent_manager.get_agent_status(expect_status=constants.ProcStateType.UNKNOWN, name=_("查询Proxy状态")), agent_manager.update_process_status(status=constants.ProcStateType.NOT_INSTALLED), diff --git a/script_tools/agent_tools/agent2/setup_agent.sh b/script_tools/agent_tools/agent2/setup_agent.sh index 88c0e9de0..820de9811 100755 --- a/script_tools/agent_tools/agent2/setup_agent.sh +++ b/script_tools/agent_tools/agent2/setup_agent.sh @@ -326,6 +326,16 @@ remove_crontab () { fi } +remove_directory () { + for dir in "$@"; do + if [ -d "$dir" ]; then + log remove_directory - "trying to remove directory [${dir}]" + rm -rf "$dir" + log remove_directory - "directory [${dir}] removed" + fi + done +} + setup_startup_scripts () { check_rc_file local rcfile=$RC_LOCAL_FILE @@ -480,7 +490,8 @@ remove_agent () { if [[ "$REMOVE" == "TRUE" ]]; then unregister_agent_id - clean_up_agent_directory + remove_directory "$AGENT_SETUP_PATH" "$GSE_AGENT_RUN_DIR" "$GSE_AGENT_DATA_DIR" "$GSE_AGENT_LOG_DIR" + log remove_agent DONE "agent removed" exit 0 fi diff --git a/script_tools/agent_tools/agent2/setup_agent.zsh b/script_tools/agent_tools/agent2/setup_agent.zsh index 468c19bd9..1ab381312 100644 --- a/script_tools/agent_tools/agent2/setup_agent.zsh +++ b/script_tools/agent_tools/agent2/setup_agent.zsh @@ -326,6 +326,16 @@ remove_crontab () { fi } +remove_directory () { + for dir in "$@"; do + if [ -d "$dir" ]; then + log remove_directory - "trying to remove directory [${dir}]" + rm -rf "$dir" + log remove_directory - "directory [${dir}] removed" + fi + done +} + get_daemon_file () { DAEMON_FILE_PATH="/Library/LaunchDaemons/" DAEMON_FILE_NAME="com.tencent.$(echo ${AGENT_SETUP_PATH%*/} | tr '/' '.' | awk -F '.' '{print $(NF-1)"."$NF}').Daemon.plist" @@ -492,7 +502,7 @@ remove_agent () { if [[ "$REMOVE" == "TRUE" ]]; then unregister_agent_id - clean_up_agent_directory + remove_directory "$AGENT_SETUP_PATH" "$GSE_AGENT_RUN_DIR" "$GSE_AGENT_DATA_DIR" "$GSE_AGENT_LOG_DIR" log remove_agent DONE "agent removed" exit 0 fi diff --git a/script_tools/agent_tools/agent2/setup_proxy.sh b/script_tools/agent_tools/agent2/setup_proxy.sh index 8ed7b13c8..05089c333 100755 --- a/script_tools/agent_tools/agent2/setup_proxy.sh +++ b/script_tools/agent_tools/agent2/setup_proxy.sh @@ -323,6 +323,16 @@ remove_crontab () { fi } +remove_directory () { + for dir in "$@"; do + if [ -d "$dir" ]; then + log remove_directory - "trying to remove directory [${dir}]" + rm -rf "$dir" + log remove_directory - "directory [${dir}] removed" + fi + done +} + setup_startup_scripts () { check_rc_file local rcfile=$RC_LOCAL_FILE @@ -520,7 +530,7 @@ remove_proxy () { if [[ "$REMOVE" == "TRUE" ]]; then unregister_agent_id SKIP - clean_up_proxy_directory + remove_directory "$AGENT_SETUP_PATH" "$GSE_AGENT_RUN_DIR" "$GSE_AGENT_DATA_DIR" "$GSE_AGENT_LOG_DIR" log remove_proxy DONE "proxy removed" exit 0 else diff --git a/script_tools/gsectl/agent/darwin/gsectl b/script_tools/gsectl/agent/darwin/gsectl index 86bf7909b..cd64ada29 100755 --- a/script_tools/gsectl/agent/darwin/gsectl +++ b/script_tools/gsectl/agent/darwin/gsectl @@ -396,6 +396,7 @@ start_by_rclocal () { } stop_by_rclocal () { + remove_startup stop_by_binary return } @@ -536,6 +537,12 @@ EOF launchctl load $DAEMON_FILE_NAME } +remove_startup () { + get_daemon_file + launchctl unload $DAEMON_FILE_NAME + rm -f $DAEMON_FILE_PATH$DAEMON_FILE_NAME +} + add_config_to_systemd () { local module="agent" diff --git a/script_tools/gsectl/agent/linux/gsectl b/script_tools/gsectl/agent/linux/gsectl index 8d9ca1b60..fba87096b 100755 --- a/script_tools/gsectl/agent/linux/gsectl +++ b/script_tools/gsectl/agent/linux/gsectl @@ -395,6 +395,7 @@ start_by_rclocal () { } stop_by_rclocal () { + remove_startup stop_by_binary return } @@ -527,6 +528,15 @@ add_startup_to_boot () { echo "[ -f ${WORK_HOME}/bin/gsectl ] && ${WORK_HOME}/bin/gsectl start ${module} 1>>/var/log/${INSTALL_ENV}_${node_type}.log 2>&1" >>$rcfile } +remove_startup () { + local module=agent + + check_rc_file + local rcfile=$RC_LOCAL_FILE + + sed -i "\|${WORK_HOME}/bin/gsectl start ${module}|d" $RC_LOCAL_FILE +} + add_config_to_systemd () { local module="agent" diff --git a/script_tools/gsectl/proxy/linux/gsectl b/script_tools/gsectl/proxy/linux/gsectl index 609d851e5..eb64b0a7c 100755 --- a/script_tools/gsectl/proxy/linux/gsectl +++ b/script_tools/gsectl/proxy/linux/gsectl @@ -679,15 +679,18 @@ start_by_rclocal () { else start_by_binary "${module}" fi + + add_startup_to_boot "${module}" done - add_startup_to_boot "${module}" return } stop_by_rclocal () { for module in $modules do + remove_startup "${module}" + stop_by_binary "${module}" if [ $? -ne 0 ];then echo "have $? module start failed" @@ -853,6 +856,15 @@ add_startup_to_boot () { echo "[ -f ${WORK_HOME}/bin/gsectl ] && ${WORK_HOME}/bin/gsectl start ${module} 1>>/var/log/${INSTALL_ENV}_${node_type}.log 2>&1" >>$rcfile } +remove_startup() { + local module=$1 + + check_rc_file + local rcfile=$RC_LOCAL_FILE + + sed -i "\|${WORK_HOME}/bin/gsectl start ${module}|d" $rcfile +} + add_config_to_systemd () { local module="${1}" diff --git a/script_tools/setup_agent.ksh b/script_tools/setup_agent.ksh index 7c99e064b..5158d5ed7 100644 --- a/script_tools/setup_agent.ksh +++ b/script_tools/setup_agent.ksh @@ -324,12 +324,33 @@ setup_startup_scripts () { tmp_rcfile=$(grep -v "${AGENT_SETUP_PATH}/bin/gsectl") echo "$tmp_rcfile" >$rcfile else - touch "$rcfile" && chmod 755 "$rcfile" + touch "$rcfile" && chmod 755 "$rcfile" fi echo "[ -f $AGENT_SETUP_PATH/bin/gsectl ] && $AGENT_SETUP_PATH/bin/gsectl start >/var/log/gse_start.log 2>&1" >>$rcfile } +remove_startup () { + local rcfile=/etc/rc.local + + if [ -f $rcfile ];then + tmp_rcfile=$(grep -v "${AGENT_SETUP_PATH}/bin/gsectl") + echo "$tmp_rcfile" >$rcfile + else + touch "$rcfile" && chmod 755 "$rcfile" + fi +} + +remove_directory () { + for dir in "$@"; do + if [ -d "$dir" ]; then + log remove_directory - "trying to remove directory [${dir}]" + rm -rf "$dir" + log remove_directory - "directory [${dir}] removed" + fi + done +} + start_agent () { local i p @@ -417,6 +438,11 @@ remove_agent () { rm -rf "${AGENT_SETUP_PATH}" if [[ "$REMOVE" == "TRUE" ]]; then + remove_directory ${GSE_AGENT_RUN_DIR} ${GSE_AGENT_DATA_DIR} ${GSE_AGENT_LOG_DIR} + + remove_startup + log remove_agent - "rc.local startup script removed" + log remove_agent DONE "agent removed" exit 0 else @@ -503,6 +529,11 @@ setup_agent () { } download_pkg () { + if [[ "${REMOVE}" == "TRUE" ]]; then + log download_pkg - "remove agent, no need to download package" + return 0 + fi + local f http_status # 区分下载版本 diff --git a/script_tools/setup_agent.sh b/script_tools/setup_agent.sh index 6479d2d23..dda87b4ff 100644 --- a/script_tools/setup_agent.sh +++ b/script_tools/setup_agent.sh @@ -109,7 +109,7 @@ cleanup () { # 打印错误行数信息 report_err () { awk -v LN="$1" -v L="ERROR" -v D="$(date +%F\ %T)" \ - 'NR>LN-3 && NR>>":""), $0 }' $0 + 'NR>LN-3 && NR>>":""), $0 }' $0 } validate_setup_path () { @@ -401,7 +401,7 @@ remove_crontab () { # 下面这段代码是为了确保修改的crontab能立即生效 if pgrep -x crond &>/dev/null; then - pkill -HUP -x crond + pkill -HUP -x crond fi } @@ -420,6 +420,23 @@ setup_startup_scripts () { echo "[ -f $AGENT_SETUP_PATH/bin/gsectl ] && $AGENT_SETUP_PATH/bin/gsectl start >/var/log/gse_start.log 2>&1" >>$rcfile } +remove_startup () { + check_rc_file + local rcfile=$RC_LOCAL_FILE + + sed -i "\|${AGENT_SETUP_PATH}/bin/gsectl|d" $rcfile +} + +remove_directory () { + for dir in "$@"; do + if [ -d "$dir" ]; then + log remove_directory - "trying to remove directory [${dir}]" + rm -rf "$dir" + log remove_directory - "directory [${dir}] removed" + fi + done +} + start_agent () { local i p @@ -517,6 +534,11 @@ remove_agent () { rm -rf "${AGENT_SETUP_PATH}" if [[ "$REMOVE" == "TRUE" ]]; then + remove_directory ${GSE_AGENT_RUN_DIR} ${GSE_AGENT_DATA_DIR} ${GSE_AGENT_LOG_DIR} + + remove_startup + log remove_agent - "startup script removed" + log remove_agent DONE "agent removed" exit 0 fi @@ -586,6 +608,11 @@ setup_agent () { } download_pkg () { + if [[ "${REMOVE}" == "TRUE" ]]; then + log download_pkg - "remove agent, no need to download package" + return 0 + fi + local f http_status path local tmp_stdout tmp_stderr curl_pid diff --git a/script_tools/setup_agent.zsh b/script_tools/setup_agent.zsh index 25e65f8b5..5be4f19f0 100644 --- a/script_tools/setup_agent.zsh +++ b/script_tools/setup_agent.zsh @@ -25,7 +25,7 @@ report_step_status () { [ -z "$CALLBACK_URL" ] && return 0 # echo "$@" | read date _time log_level step status message - echo "$@" | read date _time log_level step + echo "$@" | read date _time log_level step tmp_time=$(date +%Y%m%d_%H%M%S) tmp_date=$(date +%s) @@ -79,7 +79,7 @@ PKG_NAME=gse_client-mac-${CPU_ARCH}.tgz get_daemon_file () { - daemon_fill_path="/Library/LaunchDaemons/" + DAEMON_FILE_PATH="/Library/LaunchDaemons/" setup_path=$(echo ${AGENT_SETUP_PATH%*/} | tr '\/' '.') DAEMON_FILE_NAME="com.tencent.gse_${NODE_TYPE}${setup_path}.Daemon.plist" } @@ -379,15 +379,14 @@ remove_crontab () { # 下面这段代码是为了确保修改的crontab能立即生效 if pgrep -x crond &>/dev/null; then - pkill -HUP -x crond + pkill -HUP -x crond fi } setup_startup_scripts () { get_daemon_file - local damonfile=$DAEMON_FILE_NAME - cat >$damonfile << EOF + bash -c "cat >$DAEMON_FILE_PATH$DAEMON_FILE_NAME" << EOF @@ -411,6 +410,24 @@ setup_startup_scripts () { EOF + launchctl load $DAEMON_FILE_NAME +} + +remove_startup () { + get_daemon_file + + launchctl unload $DAEMON_FILE_NAME + rm -f $DAEMON_FILE_PATH$DAEMON_FILE_NAME +} + +remove_directory () { + for dir in "$@"; do + if [ -d "$dir" ]; then + log remove_directory - "trying to remove directory [${dir}]" + rm -rf "$dir" + log remove_directory - "directory [${dir}] removed" + fi + done } start_agent () { @@ -506,6 +523,11 @@ remove_agent () { rm -rf "${AGENT_SETUP_PATH}" if [[ "$REMOVE" == "TRUE" ]]; then + remove_directory ${GSE_AGENT_RUN_DIR} ${GSE_AGENT_DATA_DIR} ${GSE_AGENT_LOG_DIR} + + remove_startup + log remove_agent - "startup script removed" + log remove_agent DONE "agent removed" exit 0 fi @@ -575,6 +597,11 @@ setup_agent () { } download_pkg () { + if [[ "${REMOVE}" == "TRUE" ]]; then + log download_pkg - "remove agent, no need to download package" + return 0 + fi + local f http_status local tmp_stdout tmp_stderr curl_pid diff --git a/script_tools/setup_proxy.sh b/script_tools/setup_proxy.sh index 1a3ec146e..fc72ed9e4 100755 --- a/script_tools/setup_proxy.sh +++ b/script_tools/setup_proxy.sh @@ -396,6 +396,24 @@ setup_startup_scripts () { echo "[ -f $AGENT_SETUP_PATH/bin/gsectl ] && $AGENT_SETUP_PATH/bin/gsectl start >/var/log/gse_start.log 2>&1" >>$rcfile } + +remove_startup () { + check_rc_file + local rcfile=$RC_LOCAL_FILE + + sed -i "\|${AGENT_SETUP_PATH}/bin/gsectl|d" $rcfile +} + +remove_directory () { + for dir in "$@"; do + if [ -d "$dir" ]; then + log remove_directory - "trying to remove directory [${dir}]" + rm -rf "$dir" + log remove_directory - "directory [${dir}] removed" + fi + done +} + start_proxy () { local i p @@ -461,6 +479,11 @@ remove_proxy () { rm -rf "${AGENT_SETUP_PATH}" if [[ "$REMOVE" = "TRUE" ]]; then + remove_directory ${GSE_AGENT_RUN_DIR} ${GSE_AGENT_DATA_DIR} ${GSE_AGENT_LOG_DIR} + + remove_startup + log remove_proxy - "startup script removed" + log remove_proxy DONE "proxy removed" exit 0 else @@ -555,6 +578,11 @@ setup_py36 () { } download_pkg () { + if [[ "${REMOVE}" == "TRUE" ]]; then + log download_pkg - "remove agent, no need to download package" + return 0 + fi + local f http_status path log download_pkg START "download gse agent package from $DOWNLOAD_URL/$PKG_NAME)." diff --git a/script_tools/setup_solaris_agent.sh b/script_tools/setup_solaris_agent.sh index 641a68bb5..6649ad879 100644 --- a/script_tools/setup_solaris_agent.sh +++ b/script_tools/setup_solaris_agent.sh @@ -347,6 +347,16 @@ remove_crontab () { fi } +remove_directory () { + for dir in "$@"; do + if [ -d "$dir" ]; then + log remove_directory - "trying to remove directory [${dir}]" + rm -rf "$dir" + log remove_directory - "directory [${dir}] removed" + fi + done +} + setup_startup_scripts () { check_rc_file local os_type=$OS_TYPE @@ -446,6 +456,8 @@ remove_agent () { rm -rf "${AGENT_SETUP_PATH}" if [[ "$REMOVE" == "TRUE" ]]; then + remove_directory ${GSE_AGENT_RUN_DIR} ${GSE_AGENT_DATA_DIR} ${GSE_AGENT_LOG_DIR} + log remove_agent DONE "agent removed" exit 0 fi @@ -515,6 +527,11 @@ setup_agent () { } download_pkg () { + if [[ "${REMOVE}" == "TRUE" ]]; then + log download_pkg - "remove agent, no need to download package" + return 0 + fi + local f http_status path local tmp_stdout tmp_stderr curl_pid