Skip to content

Commit

Permalink
sprintfix: 修复多 Agent 场景下 crontab 越界移除的问题 (fixed TencentBlueKing#1698)
Browse files Browse the repository at this point in the history
  • Loading branch information
ZhuoZhuoCrayon committed Jul 28, 2023
1 parent e7b4e99 commit a22bd3a
Show file tree
Hide file tree
Showing 5 changed files with 85 additions and 95 deletions.
85 changes: 39 additions & 46 deletions script_tools/gsectl/agent/linux/gsectl
Original file line number Diff line number Diff line change
Expand Up @@ -502,31 +502,42 @@ get_os_type () {
fi
}

add_startup_to_boot (){
check_rc_file () {
get_os_type
if [ -f $RC_LOCAL_FILE ]; then
return 0
elif [ -f "/etc/rc.d/rc.local" ]; then
RC_LOCAL_FILE="/etc/rc.d/rc.local"
elif [ -f "/etc/init.d/rc.local" ]; then
RC_LOCAL_FILE="/etc/init.d/rc.local"
elif [ -f "/etc/init.d/boot.local" ]; then
RC_LOCAL_FILE="/etc/init.d/boot.local"
else
RC_LOCAL_FILE="/etc/rc.local"
fi
}

add_startup_to_boot () {

local module=agent

# 添加启动项到 rc.local
echo "Check startup items, and if not existing, add the startup item to rc.local"
get_os_type
if [ -f $RC_LOCAL_FILE ];then
if [ `egrep "local/${INSTALL_ENV}/${node_type}" $RC_LOCAL_FILE |egrep ${module} |wc -l` -eq 0 ];then
echo "[ -f /usr/local/${INSTALL_ENV}/${node_type}/bin/gsectl ] && /usr/local/${INSTALL_ENV}/${node_type}/bin/gsectl start ${module} 1>>/var/log/${INSTALL_ENV}_${node_type}.log 2>&1" >> $RC_LOCAL_FILE
fi
else
if [ -f /etc/rc.d/rc.local ];then
if [ `egrep "local/${INSTALL_ENV}/${node_type}" /etc/rc.d/rc.local |egrep ${module} |wc -l` -eq 0 ];then
echo "[ -f /usr/local/${INSTALL_ENV}/${node_type}/bin/gsectl ] && /usr/local/${INSTALL_ENV}/${node_type}/bin/gsectl start ${module} 1>>/var/log/${INSTALL_ENV}_${node_type}.log 2>&1" >> $RC_LOCAL_FILE
fi
elif [ -f /etc/init.d/rc.local ];then
if [ `egrep "local/${INSTALL_ENV}/${node_type}" /etc/init.d/rc.local |egrep ${module} |wc -l` -eq 0 ];then
echo "[ -f /usr/local/${INSTALL_ENV}/${node_type}/bin/gsectl ] && /usr/local/${INSTALL_ENV}/${node_type}/bin/gsectl start ${module} 1>>/var/log/${INSTALL_ENV}_${node_type}.log 2>&1" >> $RC_LOCAL_FILE
fi
elif [ -f /etc/init.d/boot.local ];then
if [ `egrep "local/${INSTALL_ENV}/${node_type}" /etc/init.d/boot.local |egrep ${module} |wc -l` -eq 0 ];then
echo "[ -f /usr/local/${INSTALL_ENV}/${node_type}/bin/gsectl ] && /usr/local/${INSTALL_ENV}/${node_type}/bin/gsectl start ${module} 1>>/var/log/${INSTALL_ENV}_${node_type}.log 2>&1" >> $RC_LOCAL_FILE
fi
fi
echo "Check startup items, and if not existing, add the [${module}] startup item to rc.local"

check_rc_file
local rcfile=$RC_LOCAL_FILE

if [ $OS_TYPE == "ubuntu" ]; then
sed -i "\|\#\!/bin/bash|d" $rcfile
sed -i "1i \#\!/bin/bash" $rcfile
fi

chmod +x $rcfile

# 先删后加,避免重复
sed -i "\|${WORK_HOME}/bin/gsectl start ${module}|d" $rcfile

echo "[ -f ${WORK_HOME}/bin/gsectl ] && ${WORK_HOME}/bin/gsectl start ${module} 1>>/var/log/${INSTALL_ENV}_${node_type}.log 2>&1" >>$rcfile
}

add_config_to_systemd () {
Expand All @@ -541,11 +552,11 @@ After=network-online.target
[Service]
LimitNOFILE=512000
LimitCORE=infinity
WorkingDirectory=/usr/local/${INSTALL_ENV}/${node_type}/bin
PIDFile=/usr/local/${INSTALL_ENV}/${node_type}/bin/run/${module}.pid
ExecStart=/usr/local/${INSTALL_ENV}/${node_type}/bin/gse_agent -f /usr/local/${INSTALL_ENV}/${node_type}/etc/gse_agent.conf
ExecReload=/usr/local/${INSTALL_ENV}/${node_type}/bin/gse_agent --reload
ExecStop=/usr/local/${INSTALL_ENV}/${node_type}/bin/gse_agent --quit
WorkingDirectory=${WORK_HOME}/bin
PIDFile=${WORK_HOME}/bin/run/${module}.pid
ExecStart=${WORK_HOME}/bin/gse_agent -f /usr/local/${INSTALL_ENV}/${node_type}/etc/gse_agent.conf
ExecReload=${WORK_HOME}/bin/gse_agent --reload
ExecStop=${WORK_HOME}/bin/gse_agent --quit
Type=forking
KillMode=process
User=root
Expand Down Expand Up @@ -573,26 +584,8 @@ EOF
fi

# 删除rc.local里的启动项
get_os_type
if [ -f $RC_LOCAL_FILE ];then
if [ `egrep "local/${INSTALL_ENV}/${node_type}" $RC_LOCAL_FILE |wc -l` -ne 0 ];then
sed -i "/local\/${INSTALL_ENV}\/${node_type}/d" $RC_LOCAL_FILE
fi
else
if [ -f /etc/rc.d/rc.local ];then
if [ `egrep "local/${INSTALL_ENV}/${node_type}" /etc/rc.d/rc.local |wc -l` -ne 0 ];then
sed -i "/local\/${INSTALL_ENV}\/${node_type}/d" /etc/rc.d/rc.local
fi
elif [ -f /etc/init.d/rc.local ];then
if [ `egrep "local/${INSTALL_ENV}/${node_type}" /etc/init.d/rc.local |wc -l` -ne 0 ];then
sed -i "/local\/${INSTALL_ENV}\/${node_type}/d" /etc/init.d/rc.local
fi
elif [ -f /etc/init.d/boot.local ];then
if [ `egrep "local/${INSTALL_ENV}/${node_type}" /etc/init.d/boot.local |wc -l` -ne 0 ];then
sed -i "/local\/${INSTALL_ENV}\/${node_type}/d" /etc/init.d/boot.local
fi
fi
fi
check_rc_file
sed -i "\|${WORK_HOME}/bin/gsectl start ${module}|d" $RC_LOCAL_FILE

# 删除crontab里的watch条目
remove_crontab
Expand Down
86 changes: 40 additions & 46 deletions script_tools/gsectl/proxy/linux/gsectl
Original file line number Diff line number Diff line change
Expand Up @@ -828,31 +828,43 @@ get_os_type () {
fi
}

add_startup_to_boot (){
check_rc_file () {
get_os_type
if [ -f $RC_LOCAL_FILE ]; then
return 0
elif [ -f "/etc/rc.d/rc.local" ]; then
RC_LOCAL_FILE="/etc/rc.d/rc.local"
elif [ -f "/etc/init.d/rc.local" ]; then
RC_LOCAL_FILE="/etc/init.d/rc.local"
elif [ -f "/etc/init.d/boot.local" ]; then
RC_LOCAL_FILE="/etc/init.d/boot.local"
else
RC_LOCAL_FILE="/etc/rc.local"
fi
}


add_startup_to_boot () {

local module=$1

# 添加启动项到 rc.local
echo "Check startup items, and if not existing, add the startup item to rc.local"
get_os_type
if [ -f $RC_LOCAL_FILE ];then
if [ `egrep "local/${INSTALL_ENV}/${node_type}" $RC_LOCAL_FILE |egrep ${module} |wc -l` -eq 0 ];then
echo "[ -f /usr/local/${INSTALL_ENV}/${node_type}/bin/gsectl ] && /usr/local/${INSTALL_ENV}/${node_type}/bin/gsectl start ${module} 1>>/var/log/${INSTALL_ENV}_${node_type}.log 2>&1" >> $RC_LOCAL_FILE
fi
else
if [ -f /etc/rc.d/rc.local ];then
if [ `egrep "local/${INSTALL_ENV}/${node_type}" /etc/rc.d/rc.local |egrep ${module} |wc -l` -eq 0 ];then
echo "[ -f /usr/local/${INSTALL_ENV}/${node_type}/bin/gsectl ] && /usr/local/${INSTALL_ENV}/${node_type}/bin/gsectl start ${module} 1>>/var/log/${INSTALL_ENV}_${node_type}.log 2>&1" >> $RC_LOCAL_FILE
fi
elif [ -f /etc/init.d/rc.local ];then
if [ `egrep "local/${INSTALL_ENV}/${node_type}" /etc/init.d/rc.local |egrep ${module} |wc -l` -eq 0 ];then
echo "[ -f /usr/local/${INSTALL_ENV}/${node_type}/bin/gsectl ] && /usr/local/${INSTALL_ENV}/${node_type}/bin/gsectl start ${module} 1>>/var/log/${INSTALL_ENV}_${node_type}.log 2>&1" >> $RC_LOCAL_FILE
fi
elif [ -f /etc/init.d/boot.local ];then
if [ `egrep "local/${INSTALL_ENV}/${node_type}" /etc/init.d/boot.local |egrep ${module} |wc -l` -eq 0 ];then
echo "[ -f /usr/local/${INSTALL_ENV}/${node_type}/bin/gsectl ] && /usr/local/${INSTALL_ENV}/${node_type}/bin/gsectl start ${module} 1>>/var/log/${INSTALL_ENV}_${node_type}.log 2>&1" >> $RC_LOCAL_FILE
fi
fi
echo "Check startup items, and if not existing, add the [${module}] startup item to rc.local"

check_rc_file
local rcfile=$RC_LOCAL_FILE

if [ $OS_TYPE == "ubuntu" ]; then
sed -i "\|\#\!/bin/bash|d" $rcfile
sed -i "1i \#\!/bin/bash" $rcfile
fi

chmod +x $rcfile

# 先删后加,避免重复
sed -i "\|${WORK_HOME}/bin/gsectl start ${module}|d" $rcfile

echo "[ -f ${WORK_HOME}/bin/gsectl ] && ${WORK_HOME}/bin/gsectl start ${module} 1>>/var/log/${INSTALL_ENV}_${node_type}.log 2>&1" >>$rcfile
}

add_config_to_systemd () {
Expand All @@ -875,11 +887,11 @@ After=network-online.target
[Service]
LimitNOFILE=512000
LimitCORE=infinity
WorkingDirectory=/usr/local/${INSTALL_ENV}/${node_type}/bin
PIDFile=/usr/local/${INSTALL_ENV}/${node_type}/bin/run/${module}.pid
ExecStart=/usr/local/${INSTALL_ENV}/${node_type}/bin/gse_${module} -f /usr/local/${INSTALL_ENV}/${node_type}/etc/gse_${config}.conf
ExecReload=/usr/local/${INSTALL_ENV}/${node_type}/bin/gse_${module} --reload
ExecStop=/usr/local/${INSTALL_ENV}/${node_type}/bin/gse_${module} --quit
WorkingDirectory=${WORK_HOME}/bin
PIDFile=${WORK_HOME}/bin/run/${module}.pid
ExecStart=${WORK_HOME}/bin/gse_${module} -f ${WORK_HOME}/etc/gse_${config}.conf
ExecReload=${WORK_HOME}/bin/gse_${module} --reload
ExecStop=${WORK_HOME}/bin/gse_${module} --quit
Type=forking
KillMode=process
User=root
Expand Down Expand Up @@ -909,26 +921,8 @@ EOF
[ -f /tmp/${INSTALL_ENV}_${node_type}_${module}.service ] && rm /tmp/${INSTALL_ENV}_${node_type}_${module}.service

# 删除rc.local里的启动项
get_os_type
if [ -f $RC_LOCAL_FILE ];then
if [ `egrep "local/${INSTALL_ENV}/${node_type}" $RC_LOCAL_FILE |wc -l` -ne 0 ];then
sed -i "/local\/${INSTALL_ENV}\/${node_type}/d" $RC_LOCAL_FILE
fi
else
if [ -f /etc/rc.d/rc.local ];then
if [ `egrep "local/${INSTALL_ENV}/${node_type}" /etc/rc.d/rc.local |wc -l` -ne 0 ];then
sed -i "/local\/${INSTALL_ENV}\/${node_type}/d" /etc/rc.d/rc.local
fi
elif [ -f /etc/init.d/rc.local ];then
if [ `egrep "local/${INSTALL_ENV}/${node_type}" /etc/init.d/rc.local |wc -l` -ne 0 ];then
sed -i "/local\/${INSTALL_ENV}\/${node_type}/d" /etc/init.d/rc.local
fi
elif [ -f /etc/init.d/boot.local ];then
if [ `egrep "local/${INSTALL_ENV}/${node_type}" /etc/init.d/boot.local |wc -l` -ne 0 ];then
sed -i "/local\/${INSTALL_ENV}\/${node_type}/d" /etc/init.d/boot.local
fi
fi
fi
check_rc_file
sed -i "\|${WORK_HOME}/bin/gsectl start ${module}|d" $RC_LOCAL_FILE

# 删除crontab里的定时任务
remove_crontab
Expand Down
3 changes: 2 additions & 1 deletion script_tools/setup_agent.sh
Original file line number Diff line number Diff line change
Expand Up @@ -395,7 +395,8 @@ remove_crontab () {
local tmpcron
tmpcron=$(mktemp "$TMP_DIR"/cron.XXXXXXX)

crontab -l | grep -v "bin/gsectl" >"$tmpcron"
# 仅删除关联到安装目录的 crontab,避免多 Agent 互相影响
crontab -l | grep -v "${AGENT_SETUP_PATH}" >"$tmpcron"
crontab "$tmpcron" && rm -f "$tmpcron"

# 下面这段代码是为了确保修改的crontab能立即生效
Expand Down
3 changes: 2 additions & 1 deletion script_tools/setup_agent.zsh
Original file line number Diff line number Diff line change
Expand Up @@ -373,7 +373,8 @@ remove_crontab () {
local tmpcron
tmpcron=$(mktemp "$TMP_DIR"/cron.XXXXXXX)

crontab -l | grep -v "bin/gsectl" >"$tmpcron"
# 仅删除关联到安装目录的 crontab,避免多 Agent 互相影响
crontab -l | grep -v "${AGENT_SETUP_PATH}" >"$tmpcron"
crontab "$tmpcron" && rm -f "$tmpcron"

# 下面这段代码是为了确保修改的crontab能立即生效
Expand Down
3 changes: 2 additions & 1 deletion script_tools/setup_proxy.sh
Original file line number Diff line number Diff line change
Expand Up @@ -352,7 +352,8 @@ remove_crontab () {
local tmpcron
tmpcron=$(mktemp "$TMP_DIR"/cron.XXXXXXX)

crontab -l | grep -v "bin/gsectl" >"$tmpcron"
# 仅删除关联到安装目录的 crontab,避免多 Agent 互相影响
crontab -l | grep -v "${AGENT_SETUP_PATH}" >"$tmpcron"
crontab "$tmpcron" && rm -f "$tmpcron"

# 下面这段代码是为了确保修改的crontab能立即生效
Expand Down

0 comments on commit a22bd3a

Please sign in to comment.