Skip to content

Commit 39a164e

Browse files
committed
feat: 卸载agent时停止插件并清理agent安装及运行过程中产生的目录(closed #2490)
1 parent 847a5d7 commit 39a164e

File tree

15 files changed

+314
-12
lines changed

15 files changed

+314
-12
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: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
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 collections import defaultdict
13+
from typing import List
14+
15+
from django.conf import settings
16+
17+
from apps.core.concurrent.retry import RetryHandler
18+
from apps.node_man import constants, models
19+
from apps.utils.batch_request import request_multi_thread
20+
from common.api import NodeApi
21+
from common.api.exception import DataAPIException
22+
23+
from ..base import CommonData
24+
from ..subsubscription import SubSubscriptionBaseService
25+
from .base import AgentBaseService
26+
27+
28+
class StopPluginsService(SubSubscriptionBaseService, AgentBaseService):
29+
@staticmethod
30+
@RetryHandler(interval=1, retry_times=1, exception_types=[DataAPIException])
31+
def call_create_subscription_api(params):
32+
return NodeApi.create_subscription(params)
33+
34+
@classmethod
35+
def create_subscriptions(cls, common_data: CommonData) -> List[int]:
36+
37+
host_ids_group_by_os = defaultdict(list)
38+
for host in common_data.host_id_obj_map.values():
39+
host_ids_group_by_os[host.os_type.lower()].append(host.bk_host_id)
40+
41+
installed_running_plugin_names = models.ProcessStatus.objects.filter(
42+
status=constants.ProcStateType.RUNNING,
43+
bk_host_id__in=common_data.bk_host_ids,
44+
proc_type=constants.ProcType.PLUGIN,
45+
).values_list("name", flat=True)
46+
47+
plugin_name__os_type_set = set(
48+
models.Packages.objects.filter(
49+
project__in=installed_running_plugin_names, os__in=host_ids_group_by_os.keys()
50+
).values_list("project", "os")
51+
)
52+
params_list = []
53+
for (plugin_name, os_type) in plugin_name__os_type_set:
54+
params_list.append(
55+
{
56+
"params": {
57+
"run_immediately": True,
58+
"category": models.Subscription.CategoryType.ONCE,
59+
"bk_username": settings.SYSTEM_USE_API_ACCOUNT,
60+
"scope": {
61+
"node_type": models.Subscription.NodeType.INSTANCE,
62+
"object_type": models.Subscription.ObjectType.HOST,
63+
"nodes": [{"bk_host_id": bk_host_id} for bk_host_id in host_ids_group_by_os[os_type]],
64+
},
65+
"steps": [
66+
{
67+
"id": plugin_name,
68+
"type": "PLUGIN",
69+
"config": {
70+
"job_type": constants.JobType.MAIN_STOP_PLUGIN,
71+
"plugin_name": plugin_name,
72+
"plugin_version": "latest",
73+
"config_templates": [
74+
{"name": "{}.conf".format(plugin_name), "version": "latest", "is_main": True}
75+
],
76+
},
77+
"params": {"context": {}},
78+
}
79+
],
80+
}
81+
}
82+
)
83+
subscription_ids = request_multi_thread(
84+
cls.call_create_subscription_api, params_list, get_data=lambda x: [x["subscription_id"]]
85+
)
86+
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),

script_tools/agent_tools/agent2/setup_agent.sh

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

329+
remove_directory () {
330+
for dir in "$@"; do
331+
if [ -d "$dir" ]; then
332+
log remove_directory - "trying to remove directory [${dir}]"
333+
rm -rf "$dir"
334+
log remove_directory - "directory [${dir}] removed"
335+
fi
336+
done
337+
}
338+
329339
setup_startup_scripts () {
330340
check_rc_file
331341
local rcfile=$RC_LOCAL_FILE
@@ -480,7 +490,8 @@ remove_agent () {
480490

481491
if [[ "$REMOVE" == "TRUE" ]]; then
482492
unregister_agent_id
483-
clean_up_agent_directory
493+
remove_directory "$GSE_HOME" "$GSE_AGENT_RUN_DIR" "$GSE_AGENT_DATA_DIR" "$GSE_AGENT_LOG_DIR"
494+
484495
log remove_agent DONE "agent removed"
485496
exit 0
486497
fi
@@ -903,6 +914,7 @@ done
903914
PKG_NAME=${NAME}-${VERSION}.tgz
904915
COMPLETE_DOWNLOAD_URL="${DOWNLOAD_URL}/agent/linux/${CPU_ARCH}"
905916
GSE_AGENT_CONFIG_PATH="${AGENT_SETUP_PATH}/etc/${GSE_AGENT_CONFIG}"
917+
GSE_HOME=$(dirname ${AGENT_SETUP_PATH})
906918

907919
LOG_FILE="$TMP_DIR"/nm.${0##*/}.$TASK_ID
908920
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
@@ -326,6 +326,16 @@ remove_crontab () {
326326
fi
327327
}
328328

329+
remove_directory () {
330+
for dir in "$@"; do
331+
if [ -d "$dir" ]; then
332+
log remove_directory - "trying to remove directory [${dir}]"
333+
rm -rf "$dir"
334+
log remove_directory - "directory [${dir}] removed"
335+
fi
336+
done
337+
}
338+
329339
get_daemon_file () {
330340
DAEMON_FILE_PATH="/Library/LaunchDaemons/"
331341
DAEMON_FILE_NAME="com.tencent.$(echo ${AGENT_SETUP_PATH%*/} | tr '/' '.' | awk -F '.' '{print $(NF-1)"."$NF}').Daemon.plist"
@@ -492,7 +502,7 @@ remove_agent () {
492502

493503
if [[ "$REMOVE" == "TRUE" ]]; then
494504
unregister_agent_id
495-
clean_up_agent_directory
505+
remove_directory "$GSE_HOME" "$GSE_AGENT_RUN_DIR" "$GSE_AGENT_DATA_DIR" "$GSE_AGENT_LOG_DIR"
496506
log remove_agent DONE "agent removed"
497507
exit 0
498508
fi
@@ -850,6 +860,7 @@ check_env () {
850860
CLOUD_ID=0
851861
TMP_DIR=/tmp
852862
AGENT_SETUP_PATH="/usr/local/gse/${NODE_TYPE}"
863+
853864
CURR_PID=$$
854865
OVERIDE=false
855866
REMOVE=false
@@ -914,6 +925,7 @@ done
914925
PKG_NAME=${NAME}-${VERSION}.tgz
915926
COMPLETE_DOWNLOAD_URL="${DOWNLOAD_URL}/agent/darwin/${CPU_ARCH}"
916927
GSE_AGENT_CONFIG_PATH="${AGENT_SETUP_PATH}/etc/${GSE_AGENT_CONFIG}"
928+
GSE_HOME=$(dirname ${AGENT_SETUP_PATH})
917929

918930
LOG_FILE="$TMP_DIR"/nm.${0##*/}.$TASK_ID
919931
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
@@ -323,6 +323,16 @@ remove_crontab () {
323323
fi
324324
}
325325

326+
remove_directory () {
327+
for dir in "$@"; do
328+
if [ -d "$dir" ]; then
329+
log remove_directory - "trying to remove directory [${dir}]"
330+
rm -rf "$dir"
331+
log remove_directory - "directory [${dir}] removed"
332+
fi
333+
done
334+
}
335+
326336
setup_startup_scripts () {
327337
check_rc_file
328338
local rcfile=$RC_LOCAL_FILE
@@ -520,7 +530,7 @@ remove_proxy () {
520530

521531
if [[ "$REMOVE" == "TRUE" ]]; then
522532
unregister_agent_id SKIP
523-
clean_up_proxy_directory
533+
remove_directory "$GSE_HOME" "$GSE_AGENT_RUN_DIR" "$GSE_AGENT_DATA_DIR" "$GSE_AGENT_LOG_DIR"
524534
log remove_proxy DONE "proxy removed"
525535
exit 0
526536
else
@@ -896,6 +906,7 @@ DEBUG_LOG_FILE=${TMP_DIR}/nm.${0##*/}.${TASK_ID}.debug
896906
PKG_NAME=${NAME}-${VERSION}.tgz
897907
COMPLETE_DOWNLOAD_URL="${DOWNLOAD_URL}/agent/linux/${CPU_ARCH}/"
898908
GSE_AGENT_CONFIG_PATH="${AGENT_SETUP_PATH}/etc/${GSE_AGENT_CONFIG}"
909+
GSE_HOME=$(dirname ${AGENT_SETUP_PATH})
899910

900911
# redirect STDOUT & STDERR to DEBUG
901912
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
@@ -396,6 +396,7 @@ start_by_rclocal () {
396396
}
397397

398398
stop_by_rclocal () {
399+
remove_startup
399400
stop_by_binary
400401
return
401402
}
@@ -536,6 +537,12 @@ EOF
536537
launchctl load $DAEMON_FILE_NAME
537538
}
538539

540+
remove_startup () {
541+
get_daemon_file
542+
launchctl unload $DAEMON_FILE_NAME
543+
rm -f $DAEMON_FILE_PATH$DAEMON_FILE_NAME
544+
}
545+
539546
add_config_to_systemd () {
540547

541548
local module="agent"

script_tools/gsectl/agent/linux/gsectl

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

397397
stop_by_rclocal () {
398+
remove_startup
398399
stop_by_binary
399400
return
400401
}
@@ -527,6 +528,15 @@ add_startup_to_boot () {
527528
echo "[ -f ${WORK_HOME}/bin/gsectl ] && ${WORK_HOME}/bin/gsectl start ${module} 1>>/var/log/${INSTALL_ENV}_${node_type}.log 2>&1" >>$rcfile
528529
}
529530

531+
remove_startup () {
532+
local module=agent
533+
534+
check_rc_file
535+
local rcfile=$RC_LOCAL_FILE
536+
537+
sed -i "\|${WORK_HOME}/bin/gsectl start ${module}|d" $RC_LOCAL_FILE
538+
}
539+
530540
add_config_to_systemd () {
531541

532542
local module="agent"

script_tools/gsectl/proxy/linux/gsectl

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -679,15 +679,18 @@ start_by_rclocal () {
679679
else
680680
start_by_binary "${module}"
681681
fi
682+
683+
add_startup_to_boot "${module}"
682684
done
683685

684-
add_startup_to_boot "${module}"
685686
return
686687
}
687688

688689
stop_by_rclocal () {
689690
for module in $modules
690691
do
692+
remove_startup "${module}"
693+
691694
stop_by_binary "${module}"
692695
if [ $? -ne 0 ];then
693696
echo "have $? module start failed"
@@ -853,6 +856,15 @@ add_startup_to_boot () {
853856
echo "[ -f ${WORK_HOME}/bin/gsectl ] && ${WORK_HOME}/bin/gsectl start ${module} 1>>/var/log/${INSTALL_ENV}_${node_type}.log 2>&1" >>$rcfile
854857
}
855858

859+
remove_startup() {
860+
local module=$1
861+
862+
check_rc_file
863+
local rcfile=$RC_LOCAL_FILE
864+
865+
sed -i "\|${WORK_HOME}/bin/gsectl start ${module}|d" $rcfile
866+
}
867+
856868
add_config_to_systemd () {
857869

858870
local module="${1}"

0 commit comments

Comments
 (0)