Skip to content

Commit

Permalink
fix: 卸载agent时停止插件并清理agent安装及运行过程中产生的目录(closed TencentBlueKing#2490)
Browse files Browse the repository at this point in the history
  • Loading branch information
chalice-1831 committed Nov 20, 2024
1 parent f14f01b commit f12edb0
Show file tree
Hide file tree
Showing 15 changed files with 305 additions and 12 deletions.
8 changes: 8 additions & 0 deletions apps/backend/agent/manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
7 changes: 7 additions & 0 deletions apps/backend/components/collections/agent_new/components.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
86 changes: 86 additions & 0 deletions apps/backend/components/collections/agent_new/stop_plugins.py
Original file line number Diff line number Diff line change
@@ -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
2 changes: 2 additions & 0 deletions apps/backend/subscription/steps/agent.py
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand All @@ -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),
Expand Down
13 changes: 12 additions & 1 deletion script_tools/agent_tools/agent2/setup_agent.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
12 changes: 11 additions & 1 deletion script_tools/agent_tools/agent2/setup_agent.zsh
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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
Expand Down
12 changes: 11 additions & 1 deletion script_tools/agent_tools/agent2/setup_proxy.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
7 changes: 7 additions & 0 deletions script_tools/gsectl/agent/darwin/gsectl
Original file line number Diff line number Diff line change
Expand Up @@ -396,6 +396,7 @@ start_by_rclocal () {
}

stop_by_rclocal () {
remove_startup
stop_by_binary
return
}
Expand Down Expand Up @@ -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"
Expand Down
10 changes: 10 additions & 0 deletions script_tools/gsectl/agent/linux/gsectl
Original file line number Diff line number Diff line change
Expand Up @@ -395,6 +395,7 @@ start_by_rclocal () {
}

stop_by_rclocal () {
remove_startup
stop_by_binary
return
}
Expand Down Expand Up @@ -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"
Expand Down
14 changes: 13 additions & 1 deletion script_tools/gsectl/proxy/linux/gsectl
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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}"
Expand Down
33 changes: 32 additions & 1 deletion script_tools/setup_agent.ksh
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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

# 区分下载版本
Expand Down
Loading

0 comments on commit f12edb0

Please sign in to comment.