From a829137a0e32e34ad979613cb20c8f494d4ba26a Mon Sep 17 00:00:00 2001 From: chalice-1831 <844589474@qq.com> Date: Wed, 13 Nov 2024 16:30:04 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E5=8D=B8=E8=BD=BDagent=E6=97=B6?= =?UTF-8?q?=E6=B8=85=E7=90=86agent=E5=AE=89=E8=A3=85=E5=8F=8A=E8=BF=90?= =?UTF-8?q?=E8=A1=8C=E8=BF=87=E7=A8=8B=E4=B8=AD=E4=BA=A7=E7=94=9F=E7=9A=84?= =?UTF-8?q?=E7=9B=AE=E5=BD=95(closed=20#2490)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../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 | 28 +++++++++++++++- script_tools/setup_agent.sh | 26 +++++++++++++-- script_tools/setup_agent.zsh | 32 ++++++++++++++++--- script_tools/setup_proxy.sh | 23 +++++++++++++ script_tools/setup_solaris_agent.sh | 12 +++++++ 11 files changed, 177 insertions(+), 12 deletions(-) diff --git a/script_tools/agent_tools/agent2/setup_agent.sh b/script_tools/agent_tools/agent2/setup_agent.sh index 88c0e9de03..820de9811f 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 468c19bd97..1ab381312f 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 8ed7b13c88..05089c3330 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 86bf7909b7..cd64ada294 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 8d9ca1b60d..fba87096b7 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 609d851e54..eb64b0a7c7 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 7c99e064b1..349882607d 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 diff --git a/script_tools/setup_agent.sh b/script_tools/setup_agent.sh index 6479d2d238..f43dfaeebe 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 diff --git a/script_tools/setup_agent.zsh b/script_tools/setup_agent.zsh index 25e65f8b54..00468a2da1 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 diff --git a/script_tools/setup_proxy.sh b/script_tools/setup_proxy.sh index 1a3ec146eb..bdc223dd93 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 diff --git a/script_tools/setup_solaris_agent.sh b/script_tools/setup_solaris_agent.sh index 641a68bb54..33422cf46f 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