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 13, 2024
1 parent f14f01b commit a829137
Show file tree
Hide file tree
Showing 11 changed files with 177 additions and 12 deletions.
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
28 changes: 27 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
26 changes: 24 additions & 2 deletions script_tools/setup_agent.sh
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ cleanup () {
# 打印错误行数信息
report_err () {
awk -v LN="$1" -v L="ERROR" -v D="$(date +%F\ %T)" \
'NR>LN-3 && NR<LN+3 { printf "%s %s cmd-return-err %-5d%3s%s\n", D, L, NR, (NR==LN?">>>":""), $0 }' $0
'NR>LN-3 && NR<LN+3 { printf "%s %s cmd-return-err %-5d%3s%s\n", D, L, NR, (NR==LN?">>>":""), $0 }' $0
}

validate_setup_path () {
Expand Down Expand Up @@ -401,7 +401,7 @@ remove_crontab () {

# 下面这段代码是为了确保修改的crontab能立即生效
if pgrep -x crond &>/dev/null; then
pkill -HUP -x crond
pkill -HUP -x crond
fi
}

Expand All @@ -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

Expand Down Expand Up @@ -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
Expand Down
32 changes: 27 additions & 5 deletions script_tools/setup_agent.zsh
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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"
}
Expand Down Expand Up @@ -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
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
Expand All @@ -411,6 +410,24 @@ setup_startup_scripts () {
</dict>
</plist>
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 () {
Expand Down Expand Up @@ -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
Expand Down
23 changes: 23 additions & 0 deletions script_tools/setup_proxy.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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
Expand Down
12 changes: 12 additions & 0 deletions script_tools/setup_solaris_agent.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit a829137

Please sign in to comment.