diff --git a/tools/afterinstall.sh b/tools/afterinstall.sh index afbce06..e91b461 100644 --- a/tools/afterinstall.sh +++ b/tools/afterinstall.sh @@ -2,6 +2,52 @@ 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 + echo 'return config file '$1/${i} + cp -f $2/$i $1/$i + fi + done +} + + # Find Java if [[ -x "$WORK_DIR/jdk/bin/java" ]]; then JAVA_EXE="$WORK_DIR/jdk/bin/java" @@ -119,46 +165,3 @@ EOF fi fi -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 - echo 'return config file '$1/${i} - cp -f $2/$i $1/$i - fi - done -} diff --git a/tools/afterremove.sh b/tools/afterremove.sh index 8ac5395..db73c86 100644 --- a/tools/afterremove.sh +++ b/tools/afterremove.sh @@ -1,4 +1,14 @@ #!/bin/sh + +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 +} + if [ 0 -eq $1 ];then echo 'start remove nz-talon module from disk...' @@ -33,11 +43,4 @@ if [ 0 -eq $1 ];then echo 'uninstall success!' fi -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 -} + diff --git a/tools/beforeinstall.sh b/tools/beforeinstall.sh index 84da859..c1cccfa 100644 --- a/tools/beforeinstall.sh +++ b/tools/beforeinstall.sh @@ -4,6 +4,17 @@ TALON_PATH=/opt/nezha/nz-talon PROMTAIL_PATH=/opt/nezha/promtail TELEGRAF_PATH=/opt/nezha/telegraf + +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 +} + + if [ 2 -eq $1 ];then if [ ! -d ${TALON_PATH}/config ];then @@ -52,12 +63,3 @@ if [ 2 -eq $1 ];then fi fi - -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 -}