Skip to content

Commit 9919cdc

Browse files
committed
feat: 卸载agent时清理agent安装及运行过程中产生的目录及安装脚本非root相关逻辑优化(closed #2490)
1 parent cf1bb2b commit 9919cdc

File tree

13 files changed

+453
-136
lines changed

13 files changed

+453
-136
lines changed

script_tools/agent_tools/agent2/setup_agent.sh

Lines changed: 21 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ get_cpu_arch () {
4343
fi
4444
}
4545

46-
get_cpu_arch "uname -p" || get_cpu_arch "uname -m" || arch || fail get_cpu_arch "Failed to get CPU arch, please contact the developer."
46+
get_cpu_arch "uname -p" || get_cpu_arch "uname -m" || get_cpu_arch "arch" || fail get_cpu_arch "Failed to get CPU arch, please contact the developer."
4747

4848

4949
get_os_info () {
@@ -81,6 +81,9 @@ get_os_type () {
8181
elif [[ "${OS_INFO,,}" =~ "hat" ]]; then
8282
OS_TYPE="redhat"
8383
RC_LOCAL_FILE="/etc/rc.d/rc.local"
84+
else
85+
OS_TYPE="other"
86+
RC_LOCAL_FILE="/etc/rc.d/rc.local"
8487
fi
8588
}
8689

@@ -313,19 +316,17 @@ check_heathz_by_gse () {
313316
}
314317

315318
remove_crontab () {
316-
if [ $IS_SUPER == false ]; then
317-
return
318-
fi
319-
320319
local tmpcron
321320
tmpcron=$(mktemp "$TMP_DIR"/cron.XXXXXXX)
322321

323322
crontab -l | grep -v "bin/gsectl" >"$tmpcron"
324323
crontab "$tmpcron" && rm -f "$tmpcron"
325324

326-
# 下面这段代码是为了确保修改的crontab能立即生效
327-
if pgrep -x crond &>/dev/null; then
328-
pkill -HUP -x crond
325+
# 下面这段代码是为了确保修改的crontab立即生效
326+
if [ $IS_SUPER == true ]; then
327+
if pgrep -x crond &>/dev/null; then
328+
pkill -HUP -x crond
329+
fi
329330
fi
330331
}
331332

@@ -470,6 +471,16 @@ stop_agent () {
470471
done
471472
}
472473

474+
remove_directory () {
475+
for dir in "$@"; do
476+
if [ -d "$dir" ]; then
477+
log remove_directory - "trying to remove directory [${dir}]"
478+
rm -rf "$dir"
479+
log remove_directory - "directory [${dir}] removed"
480+
fi
481+
done
482+
}
483+
473484
clean_up_agent_directory () {
474485
for dir in "${AGENT_CLEAN_UP_DIRS[@]}"; do
475486
rm -rf "${AGENT_SETUP_PATH}"/"${dir}"
@@ -490,7 +501,8 @@ remove_agent () {
490501

491502
if [[ "$REMOVE" == "TRUE" ]]; then
492503
unregister_agent_id
493-
clean_up_agent_directory
504+
remove_directory "$AGENT_SETUP_PATH" "$GSE_AGENT_RUN_DIR" "$GSE_AGENT_DATA_DIR" "$GSE_AGENT_LOG_DIR"
505+
494506
log remove_agent DONE "agent removed"
495507
exit 0
496508
fi

script_tools/agent_tools/agent2/setup_agent.zsh

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -36,14 +36,14 @@ get_cpu_arch () {
3636
elif [[ "${CPU_ARCH}" =~ "x86" || "${CPU_ARCH}" =~ ^i[3456]86 ]]; then
3737
CPU_ARCH="x86"
3838
return 0
39-
elif [[ "${CPU_ARCH}" =~ "aarch" ]]; then
39+
elif [[ "${CPU_ARCH}" =~ "aarch" || "${CPU_ARCH}" =~ "arm64" ]]; then
4040
return 0
4141
else
4242
return 1
4343
fi
4444
}
4545

46-
get_cpu_arch "uname -m" || fail get_cpu_arch "Failed to get CPU arch, please contact the developer."
46+
get_cpu_arch "uname -m" || get_cpu_arch "arch" || fail get_cpu_arch "Failed to get CPU arch, please contact the developer."
4747

4848

4949
get_os_info () {
@@ -313,19 +313,17 @@ check_heathz_by_gse () {
313313
}
314314

315315
remove_crontab () {
316-
if [ $IS_SUPER == false ]; then
317-
return
318-
fi
319-
320316
local tmpcron
321317
tmpcron=$(mktemp "$TMP_DIR"/cron.XXXXXXX)
322318

323319
crontab -l | grep -v "bin/gsectl" >"$tmpcron"
324320
crontab "$tmpcron" && rm -f "$tmpcron"
325321

326-
# 下面这段代码是为了确保修改的crontab能立即生效
327-
if pgrep -x crond &>/dev/null; then
328-
pkill -HUP -x crond
322+
# 下面这段代码是为了确保修改的crontab立即生效
323+
if [ $IS_SUPER == true ]; then
324+
if pgrep -x crond &>/dev/null; then
325+
pkill -HUP -x crond
326+
fi
329327
fi
330328
}
331329

@@ -482,6 +480,16 @@ stop_agent () {
482480
done
483481
}
484482

483+
remove_directory () {
484+
for dir in "$@"; do
485+
if [ -d "$dir" ]; then
486+
log remove_directory - "trying to remove directory [${dir}]"
487+
rm -rf "$dir"
488+
log remove_directory - "directory [${dir}] removed"
489+
fi
490+
done
491+
}
492+
485493
clean_up_agent_directory () {
486494
for dir in "${AGENT_CLEAN_UP_DIRS[@]}"; do
487495
rm -rf "${AGENT_SETUP_PATH}"/"${dir}"
@@ -501,7 +509,7 @@ remove_agent () {
501509

502510
if [[ "$REMOVE" == "TRUE" ]]; then
503511
unregister_agent_id
504-
clean_up_agent_directory
512+
remove_directory "$AGENT_SETUP_PATH" "$GSE_AGENT_RUN_DIR" "$GSE_AGENT_DATA_DIR" "$GSE_AGENT_LOG_DIR"
505513
log remove_agent DONE "agent removed"
506514
exit 0
507515
fi

script_tools/agent_tools/agent2/setup_proxy.sh

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,9 @@ get_os_type () {
7777
elif [[ "${OS_INFO,,}" =~ "hat" ]]; then
7878
OS_TYPE="redhat"
7979
RC_LOCAL_FILE="/etc/rc.d/rc.local"
80+
else
81+
OS_TYPE="other"
82+
RC_LOCAL_FILE="/etc/rc.d/rc.local"
8083
fi
8184
}
8285

@@ -311,19 +314,17 @@ report_mkdir () {
311314
}
312315

313316
remove_crontab () {
314-
if [ $IS_SUPER == false ]; then
315-
return
316-
fi
317-
318317
local tmpcron
319318
tmpcron=$(mktemp "$TMP_DIR"/cron.XXXXXXX)
320319

321320
crontab -l | grep -v "bin/gsectl" >"$tmpcron"
322321
crontab "$tmpcron" && rm -f "$tmpcron"
323322

324-
# 下面这段代码是为了确保修改的crontab能立即生效
325-
if pgrep -x crond &>/dev/null; then
326-
pkill -HUP -x crond
323+
# 下面这段代码是为了确保修改的crontab立即生效
324+
if [ $IS_SUPER == true ]; then
325+
if pgrep -x crond &>/dev/null; then
326+
pkill -HUP -x crond
327+
fi
327328
fi
328329
}
329330

@@ -514,6 +515,16 @@ stop_proxy () {
514515
done
515516
}
516517

518+
remove_directory () {
519+
for dir in "$@"; do
520+
if [ -d "$dir" ]; then
521+
log remove_directory - "trying to remove directory [${dir}]"
522+
rm -rf "$dir"
523+
log remove_directory - "directory [${dir}] removed"
524+
fi
525+
done
526+
}
527+
517528
clean_up_proxy_directory () {
518529
for dir in "${PROXY_CLEAN_UP_DIRS[@]}"; do
519530
rm -rf "${AGENT_SETUP_PATH}"/"${dir}"
@@ -528,7 +539,7 @@ remove_proxy () {
528539

529540
if [[ "$REMOVE" == "TRUE" ]]; then
530541
unregister_agent_id SKIP
531-
clean_up_proxy_directory
542+
remove_directory "$AGENT_SETUP_PATH" "$GSE_AGENT_RUN_DIR" "$GSE_AGENT_DATA_DIR" "$GSE_AGENT_LOG_DIR"
532543
log remove_proxy DONE "proxy removed"
533544
exit 0
534545
else

script_tools/gsectl/agent/aix/gsectl

Lines changed: 32 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -177,7 +177,8 @@ log () {
177177

178178
local opt=
179179

180-
if [ "${1:0:1}" == "-" ]; then
180+
first_char=$(expr substr "$1" 1 1)
181+
if [ "$first_char" = "-" ]; then
181182
opt=$1
182183
shift 1
183184
else
@@ -201,6 +202,9 @@ watch_by_binary () {
201202
# 如果文件存在,则读取文件中记录的次数
202203
if [ -f $LAST_RUN_FILE ]; then
203204
run_count=$(cat $LAST_RUN_FILE)
205+
if [ -z "$run_count" ]; then
206+
run_count=0
207+
fi
204208
else
205209
run_count=0
206210
fi
@@ -223,16 +227,16 @@ watch_by_binary () {
223227
# 检查上一次脚本是否存在
224228
if [ -f $VAR_RUN_DIR/gsectl_check_agent_status.pid ]; then
225229
pid=`cat $VAR_RUN_DIR/gsectl_check_agent_status.pid`
226-
if [ -d "/proc/$pid" ]; then
227-
log "`date +'%F %T.%N'` Last Script: $0 Detection status: PID:$pid is until running , no longer checking the status of the module: ${module}"
230+
if ps -p $pid >/dev/null 2>&1; then
231+
log "`date +'%F %T'` Last Script: $0 Detection status: PID:$pid is until running , no longer checking the status of the module: ${module}"
228232
return
229233
else
230234
# 如果超过阈值,则发出告警
231235
if [ $run_count -ge $THRESHOLD ]; then
232-
log "`date +'%F %T.%N'` Script: $0 Detection status: Failed to start the process, exceeded $run_count cycles, no longer checking the status of the module: ${module}"
236+
log "`date +'%F %T'` Script: $0 Detection status: Failed to start the process, exceeded $run_count cycles, no longer checking the status of the module: ${module}"
233237
return
234238
else
235-
log "`date +'%F %T.%N'` The previous script: $0 watch has ended, starting a new detection"
239+
log "`date +'%F %T'` The previous script: $0 watch has ended, starting a new detection"
236240
fi
237241
fi
238242
fi
@@ -249,13 +253,13 @@ watch_by_binary () {
249253
stop_by_binary
250254
start_by_binary
251255
if [ $? -ne 0 ];then
252-
log "`date +'%F %T.%N'` Process failed to start, increment counter"
256+
log "`date +'%F %T'` Process failed to start, increment counter"
253257
run_count=$((run_count + 1))
254258
echo $run_count > $LAST_RUN_FILE
255259
fi
256260
else
257261
if [ $run_count -ne 0 ];then
258-
log "`date +'%F %T.%N'` The previous script: $0 Detection ${module} status is Running , then reset the count"
262+
log "`date +'%F %T'` The previous script: $0 Detection ${module} status is Running , then reset the count"
259263
echo 0 > $LAST_RUN_FILE
260264
fi
261265
fi
@@ -346,21 +350,6 @@ healthz_by_rclocal () {
346350
return
347351
}
348352

349-
check_rc_file () {
350-
RC_LOCAL_FILE="/etc/rc.local"
351-
if [ -f "$RC_LOCAL_FILE" ]; then
352-
return 0
353-
elif [ -f "/etc/rc.d/rc.local" ]; then
354-
RC_LOCAL_FILE="/etc/rc.d/rc.local"
355-
elif [ -f "/etc/init.d/rc.local" ]; then
356-
RC_LOCAL_FILE="/etc/init.d/rc.local"
357-
elif [ -f "/etc/init.d/boot.local" ]; then
358-
RC_LOCAL_FILE="/etc/init.d/boot.local"
359-
else
360-
RC_LOCAL_FILE="`ls -l "/etc/rc.local" | awk '{print $NF}'`"
361-
fi
362-
}
363-
364353
add_startup_to_boot () {
365354

366355
# 非root用户无法操作rclocal
@@ -374,15 +363,18 @@ add_startup_to_boot () {
374363
# 添加启动项到 rc.local
375364
echo "Check startup items, and if not existing, add the [${module}] startup item to rc.local"
376365

377-
check_rc_file
378-
local rcfile=$RC_LOCAL_FILE
366+
local rcfile=/etc/rc.local
379367

380-
chmod +x $rcfile
368+
if [ -f $rcfile ];then
369+
# 先删后加,避免重复
370+
grep -v "${WORK_HOME}/bin/gsectl" "$rcfile" > "${rcfile}.tmp"
371+
# 用临时文件覆盖原文件
372+
mv "${rcfile}.tmp" "$rcfile"
373+
else
374+
touch "$rcfile" && chmod +x "$rcfile"
375+
fi
381376

382-
# 先删后加,避免重复
383-
rm ${rcfile}.bak && cp ${rcfile} ${rcfile}.bak
384-
sed "\|${WORK_HOME}/bin/gsectl start ${module}|d" $rcfile > ${rcfile}.tmp
385-
rm $rcfile && mv ${rcfile}.tmp ${rcfile}
377+
chmod +x $rcfile
386378

387379
echo "[ -f ${WORK_HOME}/bin/gsectl ] && ${WORK_HOME}/bin/gsectl start ${module} 1>>/var/log/${INSTALL_ENV}_${node_type}.log 2>&1" >>$rcfile
388380
}
@@ -411,14 +403,12 @@ remove_crontab () {
411403
local tmpcron
412404
local datatemp=$(date +%s)
413405

414-
crontab -l | grep -v "$AGENT_SETUP_PATH/bin/gsectl" > /tmp/cron.$datatemp
406+
crontab -l | grep -v "$WORK_HOME/bin/gsectl" > /tmp/cron.$datatemp
415407
crontab /tmp/cron.$datatemp && rm -f /tmp/cron.$datatemp
416408

417409
# 下面这段代码是为了确保修改的crontab能立即生效
418410
if [ $IS_SUPER == true ]; then
419411
ps -eo pid,comm | grep cron |awk '{print$1}' | xargs kill -9
420-
else
421-
crontab -l | crontab -
422412
fi
423413
}
424414

@@ -428,7 +418,8 @@ get_process_runtime () {
428418

429419
sleep 3
430420

431-
for i in {1..20}
421+
local i=1
422+
while [ $i -le 20 ];
432423
do
433424
set -A tmp_gse_agent_master_pids $(ps -eo ppid,pid,args | awk '$1 == 1 && $3 ~ /gse_agent/ {print $2}' | xargs)
434425

@@ -453,6 +444,7 @@ get_process_runtime () {
453444
fi
454445
fi
455446
done
447+
i=`expr $i + 1`
456448
done
457449
return $p_status
458450
}
@@ -493,7 +485,8 @@ __status (){
493485
local action=$1
494486

495487
# 最多等待20s来判断是否真正启动成功
496-
for i in {0..20}; do
488+
local i=0
489+
while [ $i -le 20 ]; do
497490
if [ "$action" == "stop" ];then
498491
if [ $(ps -eo pid,comm,args | grep gse_agent |egrep "${WORK_HOME}" |wc -l) -eq 0 ];then
499492
echo "gse_agent $action $action success"
@@ -516,16 +509,18 @@ __status (){
516509
return 3
517510
fi
518511
elif [ "$action" == "reload" ];then
519-
for i in {0..5}; do
512+
local wait_reload=0
513+
while [ $wait_reload -le 5 ]; do
520514
get_process_runtime
521515
if [ $? -eq 0 ];then
522516
break
523517
elif [ $? -ne 0 ];then
524518
sleep 2
525-
elif [ $i -eq 5 ];then
519+
elif [ $wait_reload -eq 5 ];then
526520
echo "gse_agent $action failed"
527521
return 3
528522
fi
523+
wait_reload=`expr $wait_reload + 1`
529524
done
530525
fi
531526

@@ -538,6 +533,7 @@ __status (){
538533
sleep 2
539534
fi
540535
fi
536+
i=`expr $i + 1`
541537
done
542538
}
543539

0 commit comments

Comments
 (0)