This repository has been archived on 2025-09-14. You can view files and clone it, but cannot push or open issues or pull requests.
Files
nezha-nz-talon/tools/afterinstall.sh
shizhendong 8e7b0c7940 fix: NEZ-2138 rpm 升级脚本中替换 application-prod.yml 配置文件中 promtail 服务名称
1. 修复使用 rpm 升级安装包时,会自动还原配置文件,导致 application-prod.yml 中依然是 promtail,没有更名为 nz-promtail 问题
2023-04-27 10:10:28 +08:00

206 lines
5.1 KiB
Bash

#!/bin/sh
WORK_DIR=/opt/nezha/nz-talon
service_exists(){
local n=$1
if [[ $(systemctl list-units --all -t service --full --no-legend "$n.service" | sed 's/^\s*//g' | cut -f1 -d' ') == $n.service ]]; then
return 0
else
return 1
fi
}
compareMD5(){
if [ ! -f $1 ] || [ ! -f $2 ];then
echo 1
return 1
fi
local f1MD5=`md5sum $1|awk '{print $1}'`
local f2MD5=`md5sum $2|awk '{print $1}'`
if [ ${f1MD5} = ${f2MD5} ];then
echo 0
return 0
else
echo 1
return 1
fi
}
# 还原配置文件
# $1 需要还原的文件目录 /opt/nezha/nz-talon/config /opt/nezha/nz-talon/promtail ...
# $2 备份文件目录 /tmp/nezha/nz-talon/config /tmp/nezha/nz-talon/promtail ...
restoreComponentConfig(){
if [ ! -d $1 ] || [ ! -d $2 ];then
echo "Directory does not exist"
return 1
fi
for i in $(ls $2);do
if [ 1 -eq `compareMD5 $1/$i $2/$i` ];then
if [ -f $1/${i} ];then
echo 'return config file '$1/${i}
cp -f $2/$i $1/$i
fi
fi
done
}
# Find Java
if [[ -x "$WORK_DIR/jdk/bin/java" ]]; then
JAVA_EXE="$WORK_DIR/jdk/bin/java"
elif [[ -n "$JAVA_HOME" ]] && [[ -x "$JAVA_HOME/bin/java" ]]; then
JAVA_EXE="$JAVA_HOME/bin/java"
elif type -p java > /dev/null 2>&1; then
JAVA_EXE=$(type -p java)
elif [[ -x "/usr/bin/java" ]]; then
JAVA_EXE="/usr/bin/java"
else
echo "Unable to find Java"
JAVA_EXE="java"
# exit 1
fi
# install
if [ 1 -eq $1 ];then
cat > /usr/lib/systemd/system/nz-talon.service <<EOF
[Unit]
Description=nz-talon
After=network.target
[Service]
WorkingDirectory=/opt/nezha/nz-talon
ExecStart=/opt/nezha/nz-talon/xjar ${JAVA_EXE} -Dnz-agent.dir=/opt/nezha/nz-talon -jar /opt/nezha/nz-talon/nz-talon.xjar
RestartSec=5s
Restart=always
[Install]
WantedBy=multi-user.target
EOF
cat > /usr/lib/systemd/system/nz-promtail.service <<"EOF"
[Unit]
Description=nz-talon
After=network.target
[Service]
WorkingDirectory=/opt/nezha/promtail
EnvironmentFile=/opt/nezha/promtail/config.conf
ExecStart=/opt/nezha/promtail/promtail $OPTION
RestartSec=5s
Restart=always
[Install]
WantedBy=multi-user.target
EOF
cat > /usr/lib/systemd/system/nz-telegraf.service <<"EOF"
[Unit]
Description=Telegraf
Documentation=https://github.com/influxdata/telegraf
After=network-online.target
Wants=network-online.target
[Service]
WorkingDirectory=/opt/nezha/telegraf
Type=notify
EnvironmentFile=/opt/nezha/telegraf/config.conf
ExecStart=/opt/nezha/telegraf/telegraf $OPTION
ExecReload=/bin/kill -HUP $MAINPID
Restart=on-failure
RestartForceExitStatus=SIGPIPE
KillMode=control-group
LimitMEMLOCK=8M:8M
[Install]
WantedBy=multi-user.target
EOF
systemctl daemon-reload
systemctl enable nz-talon && systemctl restart nz-talon
systemctl enable nz-promtail && systemctl restart nz-promtail
systemctl enable nz-telegraf && systemctl restart nz-telegraf
echo 'install nz-talon success !'
fi
# update
if [ 2 -eq $1 ];then
TALON_PATH=/opt/nezha/nz-talon
TMP_PATH=/tmp/nezha/nz-talon
PROMTAIL_PATH=/opt/nezha/promtail
TELEGRAF_PATH=/opt/nezha/telegraf
echo 'move backup config file...'
if [ -d $TMP_PATH ];then
# nz-talon config
restoreComponentConfig $TALON_PATH/config $TMP_PATH/config
# promtail.service 名称改为 nz-promtail.service
# 这里重新替换下 application-prod.yml 中的 systemctl start|stop promtail 命令
sed -i 's/systemctl start promtail/systemctl start nz-promtail/g' $TALON_PATH/config/application-prod.yml
sed -i 's/systemctl stop promtail/systemctl stop nz-promtail/g' $TALON_PATH/config/application-prod.yml
# promtail config
restoreComponentConfig $PROMTAIL_PATH $TMP_PATH/promtail
# telegraf config
if [ -d $TELEGRAF_PATH ];then
restoreComponentConfig $TELEGRAF_PATH $TMP_PATH/telegraf
restoreComponentConfig $TELEGRAF_PATH/telegraf.d $TMP_PATH/telegraf/telegraf.d
fi
fi
systemctl restart nz-talon
# nz-promtail.service
if service_exists nz-promtail; then
systemctl restart nz-promtail
else
cat > /usr/lib/systemd/system/nz-promtail.service <<"EOF"
[Unit]
Description=nz-talon
After=network.target
[Service]
WorkingDirectory=/opt/nezha/promtail
EnvironmentFile=/opt/nezha/promtail/config.conf
ExecStart=/opt/nezha/promtail/promtail $OPTION
RestartSec=5s
Restart=always
[Install]
WantedBy=multi-user.target
EOF
systemctl daemon-reload
systemctl enable nz-promtail && systemctl restart nz-promtail
fi
# nz-telegraf.service
if service_exists nz-telegraf; then
systemctl restart nz-telegraf
else
cat > /usr/lib/systemd/system/nz-telegraf.service <<"EOF"
[Unit]
Description=Telegraf
Documentation=https://github.com/influxdata/telegraf
After=network-online.target
Wants=network-online.target
[Service]
WorkingDirectory=/opt/nezha/telegraf
Type=notify
EnvironmentFile=/opt/nezha/telegraf/config.conf
ExecStart=/opt/nezha/telegraf/telegraf $OPTION
ExecReload=/bin/kill -HUP $MAINPID
Restart=on-failure
RestartForceExitStatus=SIGPIPE
KillMode=control-group
LimitMEMLOCK=8M:8M
[Install]
WantedBy=multi-user.target
EOF
systemctl daemon-reload
systemctl enable nz-telegraf && systemctl restart nz-telegraf
fi
fi