Skip to content

Commit 65a9215

Browse files
committed
feat: 卸载agent时清理agent安装及运行过程中产生的目录(closed #2490)
1 parent 45ba302 commit 65a9215

File tree

16 files changed

+316
-8
lines changed

16 files changed

+316
-8
lines changed

apps/backend/agent/manager.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -297,3 +297,11 @@ def install_other_agent(cls, extra_agent_version: str, node_type: str = NodeType
297297
act.component.inputs.extra_agent_version = Var(type=Var.PLAIN, value=extra_agent_version)
298298
act.component.inputs.node_type = Var(type=Var.PLAIN, value=node_type)
299299
return act
300+
301+
@classmethod
302+
def stop_plugins(cls):
303+
"""停止插件"""
304+
act = AgentServiceActivity(
305+
component_code=components.StopPluginsComponent.code, name=components.StopPluginsComponent.name
306+
)
307+
return act

apps/backend/components/collections/agent_new/components.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
from .render_and_push_gse_config import RenderAndPushGseConfigService
3737
from .restart import RestartService
3838
from .run_upgrade_command import RunUpgradeCommandService
39+
from .stop_plugins import StopPluginsService
3940
from .unbind_host_agent import UnBindHostAgentService
4041
from .update_install_info import UpdateInstallInfoService
4142
from .update_process_status import UpdateProcessStatusService
@@ -221,3 +222,9 @@ class InstallOtherAgentComponent(Component):
221222
name = _("安装额外Agent")
222223
code = "install_other_agent"
223224
bound_service = InstallOtherAgentService
225+
226+
227+
class StopPluginsComponent(Component):
228+
name = _("停止插件")
229+
code = "stop_plugins"
230+
bound_service = StopPluginsService
Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
# -*- coding: utf-8 -*-
2+
"""
3+
TencentBlueKing is pleased to support the open source community by making 蓝鲸智云-节点管理(BlueKing-BK-NODEMAN) available.
4+
Copyright (C) 2017-2022 THL A29 Limited, a Tencent company. All rights reserved.
5+
Licensed under the MIT License (the "License"); you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at https://opensource.org/licenses/MIT
7+
Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
8+
an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
9+
specific language governing permissions and limitations under the License.
10+
"""
11+
12+
from typing import List
13+
14+
from django.conf import settings
15+
16+
from apps.core.concurrent.retry import RetryHandler
17+
from apps.node_man import constants, models
18+
from apps.utils.batch_request import request_multi_thread
19+
from common.api import NodeApi
20+
from common.api.exception import DataAPIException
21+
22+
from ..base import CommonData
23+
from ..subsubscription import SubSubscriptionBaseService
24+
from .base import AgentBaseService
25+
26+
27+
class StopPluginsService(SubSubscriptionBaseService, AgentBaseService):
28+
@staticmethod
29+
@RetryHandler(interval=1, retry_times=1, exception_types=[DataAPIException])
30+
def call_create_subscription_api(params):
31+
return NodeApi.create_subscription(params)
32+
33+
@classmethod
34+
def create_subscriptions(cls, common_data: CommonData) -> List[int]:
35+
36+
running_processes = models.ProcessStatus.objects.filter(
37+
status=constants.ProcStateType.RUNNING,
38+
bk_host_id__in=common_data.bk_host_ids,
39+
proc_type=constants.ProcType.PLUGIN,
40+
is_latest=True,
41+
)
42+
43+
host_ids_processes = {}
44+
for process in running_processes:
45+
if process.bk_host_id not in host_ids_processes:
46+
host_ids_processes[process.bk_host_id] = []
47+
host_ids_processes[process.bk_host_id].append(process)
48+
49+
params_list = []
50+
for host_id, processes in host_ids_processes.items():
51+
for process in processes:
52+
params_list.append(
53+
{
54+
"params": {
55+
"run_immediately": True,
56+
"category": models.Subscription.CategoryType.ONCE,
57+
"bk_username": settings.SYSTEM_USE_API_ACCOUNT,
58+
"is_main": True,
59+
"plugin_name": process.name,
60+
"scope": {
61+
"node_type": models.Subscription.NodeType.INSTANCE,
62+
"object_type": models.Subscription.ObjectType.HOST,
63+
"nodes": [{"bk_host_id": host_id}],
64+
},
65+
"steps": [
66+
{
67+
"id": process.name,
68+
"type": "PLUGIN",
69+
"config": {
70+
"job_type": constants.JobType.MAIN_STOP_PLUGIN,
71+
"plugin_name": process.name,
72+
"plugin_version": process.version or "latest",
73+
"config_templates": [
74+
{
75+
"name": "{}.conf".format(process.name),
76+
"version": process.version or "latest",
77+
"is_main": True,
78+
}
79+
],
80+
},
81+
"params": {"context": {}},
82+
}
83+
],
84+
}
85+
}
86+
)
87+
subscription_ids = request_multi_thread(
88+
cls.call_create_subscription_api, params_list, get_data=lambda x: [x["subscription_id"]]
89+
)
90+
return subscription_ids

apps/backend/subscription/steps/agent.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -497,6 +497,7 @@ class UninstallAgent(AgentAction):
497497
def _generate_activities(self, agent_manager: AgentManager):
498498
activities = [
499499
agent_manager.query_password(),
500+
agent_manager.stop_plugins(),
500501
agent_manager.uninstall_agent(),
501502
agent_manager.get_agent_status(expect_status=constants.ProcStateType.UNKNOWN),
502503
agent_manager.update_process_status(status=constants.ProcStateType.NOT_INSTALLED),
@@ -515,6 +516,7 @@ class UninstallProxy(AgentAction):
515516

516517
def _generate_activities(self, agent_manager: AgentManager):
517518
activities = [
519+
agent_manager.stop_plugins(),
518520
agent_manager.uninstall_proxy(),
519521
agent_manager.get_agent_status(expect_status=constants.ProcStateType.UNKNOWN, name=_("查询Proxy状态")),
520522
agent_manager.update_process_status(status=constants.ProcStateType.NOT_INSTALLED),

apps/node_man/handlers/job.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -493,6 +493,7 @@ def update_host(accept_list: list, ip_filter_list: list, is_manual: bool = False
493493
"bk_host_id": host["bk_host_id"],
494494
"bk_biz_id": host["bk_biz_id"],
495495
"bk_cloud_id": host["bk_cloud_id"],
496+
"bk_agent_id": host["bk_agent_id"],
496497
"inner_ip": host["inner_ip"],
497498
"inner_ipv6": host["inner_ipv6"],
498499
"outer_ip": host["outer_ip"],
@@ -569,6 +570,7 @@ def update_host(accept_list: list, ip_filter_list: list, is_manual: bool = False
569570
"bk_host_id": origin_host["bk_host_id"],
570571
"bk_biz_id": origin_host["bk_biz_id"],
571572
"bk_cloud_id": origin_host["bk_cloud_id"],
573+
"bk_agent_id": origin_host["bk_agent_id"] or "",
572574
"inner_ip": origin_host["inner_ip"],
573575
"outer_ip": origin_host["outer_ip"],
574576
"inner_ipv6": origin_host["inner_ipv6"],

script_tools/agent_tools/agent2/setup_agent.sh

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -329,6 +329,16 @@ remove_crontab () {
329329
fi
330330
}
331331

332+
remove_directory () {
333+
for dir in "$@"; do
334+
if [ -d "$dir" ]; then
335+
log remove_directory - "trying to remove directory [${dir}]"
336+
rm -rf "$dir"
337+
log remove_directory - "directory [${dir}] removed"
338+
fi
339+
done
340+
}
341+
332342
setup_startup_scripts () {
333343
if [ $IS_SUPER == false ]; then
334344
return
@@ -490,7 +500,8 @@ remove_agent () {
490500

491501
if [[ "$REMOVE" == "TRUE" ]]; then
492502
unregister_agent_id
493-
clean_up_agent_directory
503+
remove_directory "$GSE_HOME" "$GSE_AGENT_RUN_DIR" "$GSE_AGENT_DATA_DIR" "$GSE_AGENT_LOG_DIR"
504+
494505
log remove_agent DONE "agent removed"
495506
exit 0
496507
fi
@@ -920,6 +931,7 @@ done
920931
PKG_NAME=${NAME}-${VERSION}.tgz
921932
COMPLETE_DOWNLOAD_URL="${DOWNLOAD_URL}/agent/linux/${CPU_ARCH}"
922933
GSE_AGENT_CONFIG_PATH="${AGENT_SETUP_PATH}/etc/${GSE_AGENT_CONFIG}"
934+
GSE_HOME=$(dirname ${AGENT_SETUP_PATH})
923935

924936
LOG_FILE="$TMP_DIR"/nm.${0##*/}.$TASK_ID
925937
DEBUG_LOG_FILE=${TMP_DIR}/nm.${0##*/}.${TASK_ID}.debug

script_tools/agent_tools/agent2/setup_agent.zsh

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -329,6 +329,16 @@ remove_crontab () {
329329
fi
330330
}
331331

332+
remove_directory () {
333+
for dir in "$@"; do
334+
if [ -d "$dir" ]; then
335+
log remove_directory - "trying to remove directory [${dir}]"
336+
rm -rf "$dir"
337+
log remove_directory - "directory [${dir}] removed"
338+
fi
339+
done
340+
}
341+
332342
get_daemon_file () {
333343
DAEMON_FILE_PATH="/Library/LaunchDaemons/"
334344
DAEMON_FILE_NAME="com.tencent.$(echo ${AGENT_SETUP_PATH%*/} | tr '/' '.' | awk -F '.' '{print $(NF-1)"."$NF}').Daemon.plist"
@@ -501,7 +511,7 @@ remove_agent () {
501511

502512
if [[ "$REMOVE" == "TRUE" ]]; then
503513
unregister_agent_id
504-
clean_up_agent_directory
514+
remove_directory "$GSE_HOME" "$GSE_AGENT_RUN_DIR" "$GSE_AGENT_DATA_DIR" "$GSE_AGENT_LOG_DIR"
505515
log remove_agent DONE "agent removed"
506516
exit 0
507517
fi
@@ -859,6 +869,7 @@ check_env () {
859869
CLOUD_ID=0
860870
TMP_DIR=/tmp
861871
AGENT_SETUP_PATH="/usr/local/gse/${NODE_TYPE}"
872+
862873
CURR_PID=$$
863874
OVERIDE=false
864875
REMOVE=false
@@ -930,6 +941,7 @@ done
930941
PKG_NAME=${NAME}-${VERSION}.tgz
931942
COMPLETE_DOWNLOAD_URL="${DOWNLOAD_URL}/agent/darwin/${CPU_ARCH}"
932943
GSE_AGENT_CONFIG_PATH="${AGENT_SETUP_PATH}/etc/${GSE_AGENT_CONFIG}"
944+
GSE_HOME=$(dirname ${AGENT_SETUP_PATH})
933945

934946
LOG_FILE="$TMP_DIR"/nm.${0##*/}.$TASK_ID
935947
DEBUG_LOG_FILE=${TMP_DIR}/nm.${0##*/}.${TASK_ID}.debug

script_tools/agent_tools/agent2/setup_proxy.sh

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -327,6 +327,16 @@ remove_crontab () {
327327
fi
328328
}
329329

330+
remove_directory () {
331+
for dir in "$@"; do
332+
if [ -d "$dir" ]; then
333+
log remove_directory - "trying to remove directory [${dir}]"
334+
rm -rf "$dir"
335+
log remove_directory - "directory [${dir}] removed"
336+
fi
337+
done
338+
}
339+
330340
setup_startup_scripts () {
331341
if [ $IS_SUPER == false ]; then
332342
return
@@ -528,7 +538,7 @@ remove_proxy () {
528538

529539
if [[ "$REMOVE" == "TRUE" ]]; then
530540
unregister_agent_id SKIP
531-
clean_up_proxy_directory
541+
remove_directory "$GSE_HOME" "$GSE_AGENT_RUN_DIR" "$GSE_AGENT_DATA_DIR" "$GSE_AGENT_LOG_DIR"
532542
log remove_proxy DONE "proxy removed"
533543
exit 0
534544
else
@@ -910,6 +920,7 @@ DEBUG_LOG_FILE=${TMP_DIR}/nm.${0##*/}.${TASK_ID}.debug
910920
PKG_NAME=${NAME}-${VERSION}.tgz
911921
COMPLETE_DOWNLOAD_URL="${DOWNLOAD_URL}/agent/linux/${CPU_ARCH}/"
912922
GSE_AGENT_CONFIG_PATH="${AGENT_SETUP_PATH}/etc/${GSE_AGENT_CONFIG}"
923+
GSE_HOME=$(dirname ${AGENT_SETUP_PATH})
913924

914925
# redirect STDOUT & STDERR to DEBUG
915926
exec &> >(tee "$DEBUG_LOG_FILE")

script_tools/gsectl/agent/darwin/gsectl

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -411,6 +411,7 @@ start_by_rclocal () {
411411
}
412412

413413
stop_by_rclocal () {
414+
remove_startup
414415
stop_by_binary
415416
return
416417
}
@@ -563,6 +564,12 @@ EOF
563564
launchctl load $DAEMON_FILE_NAME
564565
}
565566

567+
remove_startup () {
568+
get_daemon_file
569+
launchctl unload $DAEMON_FILE_NAME
570+
rm -f $DAEMON_FILE_PATH$DAEMON_FILE_NAME
571+
}
572+
566573
add_config_to_systemd () {
567574

568575
local module="agent"

script_tools/gsectl/agent/linux/gsectl

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -410,6 +410,7 @@ start_by_rclocal () {
410410
}
411411

412412
stop_by_rclocal () {
413+
remove_startup
413414
stop_by_binary
414415
return
415416
}
@@ -556,6 +557,15 @@ add_startup_to_boot () {
556557
echo "[ -f ${WORK_HOME}/bin/gsectl ] && ${WORK_HOME}/bin/gsectl start ${module} 1>>/var/log/${INSTALL_ENV}_${node_type}.log 2>&1" >>$rcfile
557558
}
558559

560+
remove_startup () {
561+
local module=agent
562+
563+
check_rc_file
564+
local rcfile=$RC_LOCAL_FILE
565+
566+
sed -i "\|${WORK_HOME}/bin/gsectl start ${module}|d" $RC_LOCAL_FILE
567+
}
568+
559569
add_config_to_systemd () {
560570

561571
local module="agent"

0 commit comments

Comments
 (0)