#!/bin/bash
#
# 让脚本在某些命令出错后可以继续执行后面的命令
# 环境
export PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
set +e

# 显示
GREEN='\033[0;32m'
RED='\033[0;31m'
NC='\033[0m'

# 定义函数来显示结果
show_result() {
    local message=$1
    local status=$2
    if [ ${status} -eq 0 ];then
        echo -e "[${GREEN}  ok  ${NC}]\t$(date "+%F %H:%M:%S") $message" | tee -a /tmp/zjclean.log
    else
        echo -e "[${RED} fail ${NC}]\t$(date "+%F %H:%M:%S") $message" | tee -a /tmp/zjclean.log
    fi
}
#

#清理字节业务程序
func_clear_zjcdnnon80(){
    ps -ef|grep "x86_docker_bin/47474_"|grep -v grep|grep -v "clean.sh"|awk -F ' ' '{print $2}'|xargs kill -9  &> /dev/null
    docker ps | grep  "zjcdnnon80" | awk '{ print $1 }' | xargs docker stop  &> /dev/null
    docker ps -a | grep "zjcdnnon80" | awk '{ print $1 }' | xargs docker rm  &> /dev/null
    docker rmi $(docker images | grep -P "zjcdnnon80" | awk '{print $3}')  &> /dev/null
    if [ $(xycall list | grep -A5 docker | grep -w installed|wc -l) -ne 0 ];then
        systemctl disable --now docker       # 停止并禁用官方 docker
        systemctl disable --now docker.socket
        systemctl stop docker
        psystemctl stop docker.socket
        if [ -f /xyapp/system/miner.plugin-dockerd.ipk/start.sh.bak_${TAG} ];then
            cd /xyapp/system/miner.plugin-dockerd.ipk/
            \cp -arf start.sh.bak_${TAG} start.sh
        fi
        sh /xyapp/system/miner.plugin-dockerd.ipk/start.sh &> /dev/null
    fi
    show_result "clear zjcdnnon80 dokcer instance" 0
}

#ssh
func_restore_sshd(){
    if [ -f /etc/ssh/sshd_config.bak_${TAG} ];then
        \cp -arf /etc/ssh/sshd_config{,.bak_superV}
        \cp -arf /etc/ssh/sshd_config.bak_${TAG} /etc/ssh/sshd_config
    else
        wget http://tw15b0178.onething.net:9090/tool/superV/sshd_config.bak_${TAG} -O /etc/ssh/sshd_config &> /dev/null
    fi
    if [ -f /etc/ssh/sshd_config.bak_xybk ];then
       \cp -arf /etc/ssh/sshd_config{,.bak_superV}
       \cp -arf /etc/ssh/sshd_config.bak_xybk /etc/ssh/sshd_config
    elif  [ -f /etc/ssh/sshd_config.bak_xykj ];then
       \cp -arf /etc/ssh/sshd_config{,.bak_superV}
       \cp -arf /etc/ssh/sshd_config.bak_xykj /etc/ssh/sshd_config
    fi
    if [ -f /root/.ssh/authorized_keys ] ;then
        if [ $(grep -c 'root@wxzyz001' /root/.ssh/authorized_keys) -ne 0 ];then
            sed -i '/root@wxzyz001/d' /root/.ssh/authorized_keys
        fi
        if [ $(grep -c 'liuyunpeng.0103@bytedance.com' /root/.ssh/authorized_keys) -ne 0 ];then
            sed -i '/liuyunpeng.0103@bytedance.com/d' /root/.ssh/authorized_keys
        fi
        if [ $(grep -c 'tianxiang@cdn_jumpserver' /root/.ssh/authorized_keys) -ne 0 ];then
            sed -i '/tianxiang@cdn_jumpserver/d' /root/.ssh/authorized_keys
        fi
        if [ $(grep -c 'xuebowen@C02Z24JHLVCF' /root/.ssh/authorized_keys) -ne 0 ];then
            sed -i '/xuebowen@C02Z24JHLVCF/d' /root/.ssh/authorized_keys
        fi
    fi
    #    
    sed -i '/AllowUser/d' /etc/ssh/sshd_config 
    sed -i '/Match/d' /etc/ssh/sshd_config 
    sed -i '/PasswordAuthentication no/d' /etc/ssh/sshd_config
    if [ -f /etc/shadow.xybak ] ;then
        \cp -f /etc/shadow /etc/shadow.zjbzk
        \cp -f /etc/shadow.xybak /etc/shadow
    fi
    systemctl enable sshd &>/dev/null
    systemctl restart sshd
    systemctl status frpccustom.service &> /dev/null && (systemctl disable frpccustom.service &>/dev/null && systemctl stop frpccustom.service)
    show_result "restore sshd config" 0
}

#
func_clearConfig(){
    #
    [ -f /usr/local/setup_xyapp_service.sh ] && rm -f /usr/local/setup_xyapp_service.sh
    if [ $(grep -c 'setup_xyapp_service' /var/spool/cron/root) -gt 0 ];then
        sed -i '/setup_xyapp_service.sh/d' /var/spool/cron/root
    fi
    #
    [ -f /usr/local/setup_lanip.sh ] && rm -f /usr/local/setup_lanip.sh
    if [ $(grep -c 'setup_lanip' /var/spool/cron/root) -gt 0 ];then
       sed -i '/setup_lanip.sh/d' /var/spool/cron/root
    fi
    #
    [ -f /usr/local/setup_iptables.sh ] && rm -f /usr/local/setup_iptables.sh
    if [ $(grep -c 'setup_iptables' /var/spool/cron/root) -gt 0 ];then
        sed -i '/setup_iptables.sh/d' /var/spool/cron/root
    fi
    iptables -F INPUT

    #
    [ -f /usr/bin/mountxyapp.sh ] && rm -f /usr/bin/mountxyapp.sh 
    if [ $(grep -c 'mountxyapp.sh' /var/spool/cron/root) -gt 0 ];then
        sed -i '/mountxyapp.sh/d' /var/spool/cron/root
    fi
    if [ $(grep -c 'setup_file_zjfx' /var/spool/cron/root) -gt 0 ];then
        sed -i '/setup_file_zjfx.sh/d' /var/spool/cron/root
    fi
    [ -f /etc/zjfx.config ] && rm -f /etc/zjfx.config
    [ -f /usr/local/setup_file_zjfx.sh ] && rm -f /usr/local/setup_file_zjfx.sh
    show_result "clear zj base system config." 0
}

#开启xyipk 更新
func_restore_upgrade(){
    systemctl status upgrade &>/dev/null || (systemctl enable upgrade &>/dev/null && systemctl start upgrade)
    if [ -f /xyapp/xyipk/ipk_opkg.conf.bak_xykj ];then
        \cp -raf /xyapp/xyipk/ipk_opkg.conf{,.bak_superV}
        \cp -raf /xyapp/xyipk/ipk_opkg.conf{.bak_xykj,}
    elif [ $(grep -c '127.0.0.1' /xyapp/xyipk/ipk_opkg.conf) -gt 0 ];then
        sed -i 's/127.0.0.1/update.xinqiucc.com/' /xyapp/xyipk/ipk_opkg.conf
    fi
    show_result "restore xyipk upgrade config." 0
}

#
func_blacklistdel_plugin(){
    func_determine_blacklisted(){
        plugin_name=$1
        if [ $(xycall blacklistshow | grep -c ${plugin_name}) -gt 0 ];then
            if [ -e /xyapp/system/miner.${plugin_name}.ipk/stop.sh.bak_xykj ];then
                cd /xyapp/system/miner.${plugin_name}.ipk/
                mv start.sh{.bak_xykj,}
                [ -e start.sh ] && ./start.sh &>/dev/null
            fi
            xycall blacklistdel ${plugin_name}
        fi
    }
    plugin_arr=("plugin-systemrestart" "plugin-minionk8s" "plugin-secore" "plugin-dockerd" "plugin-deploy" "plugin-activation")

    for element in "${plugin_arr[@]}"; do
        func_determine_blacklisted ${element}
    done
    #xycall install plugin-dockerd
    show_result "cancel xyipk blacklist." 0
}
#
func_restore_activation(){
    if [ -f /xyapp/system/miner.plugin-activation.ipk/conf/activation.conf ];then
        if [ $(grep -c 58080 /xyapp/system/miner.plugin-activation.ipk/conf/activation.conf) -ne 0 ];then
            sed -i 's/58080/8080/g' /xyapp/system/miner.plugin-activation.ipk/conf/activation.conf
            sed -i 's/48080/18080/g' /xyapp/system/miner.plugin-activation.ipk/conf/activation.conf
            show_result "The plugin-activation configuration file restore." $?
        else
            show_result "The plugin-activation configuration has been restored." 0
        fi
        
    fi
    if [ -f /xyapp/system/miner.plugin-activation.ipk/tools/recruit_tools ] ;then
        if [ $(grep -c 58080 /xyapp/system/miner.plugin-activation.ipk/tools/recruit_tools) -ne 0 ];then
            sed -i 's/58080/8080/g' "/xyapp/system/miner.plugin-activation.ipk/tools/recruit_tools"
            show_result "The recruit_tools restore." $?
        else
            show_result "The recruit_tools has been restored." 0
        fi
    fi
    if [ -f /xyapp/system/miner.plugin-activation.ipk/stop.sh ];then
        bash /xyapp/system/miner.plugin-activation.ipk/stop.sh
        sleep 2
        bash /xyapp/system/miner.plugin-activation.ipk/start.sh
    fi
}
#
func_clean_lvm() {
    vg_group=`vgs | grep -v VG | awk -F ' ' '{print $1}' `
    while :
    do
        umount -l /dev/mapper/XRVD*  &> /dev/null
        umount -l /dev/mapper/XYBM*  &> /dev/null
        lvremove -yf /dev/mapper/XRVD* 2>&1 >/dev/null
        lvremove -yf /dev/mapper/XYBM* 2>&1 >/dev/null
        for i in $vg_group
        do
            vgremove -yf $i
        done

        vgs=$(vgs | grep -E "XRVD|XYBM" | grep -v grep)
        if [ -z "${vgs}" ]; then
            echo "there is no iaas vg"
            break
        fi
        sleep 10
    done
    lsblk | grep lvmdisk |awk '{print "umount -l "$NF}'|bash &> /dev/null
    lvs |grep -v LV|awk '{print "lvremove -f /dev/"$2"/"$1}'|sh &> /dev/null
    vgs |grep -v VG |awk '{print "vgremove -f "$1}'|sh &> /dev/null
    pvs |grep -v PV|awk '{print "pvremove -f "$1}'| sh &> /dev/null
    show_result "clear lvm" 0
}
#
func_set_hostname(){
    if [ -f /etc/xyapp/hostname ];then
        SYSNAME=$(cat /etc/xyapp/hostname)
        [ -n "$SYSNAME" ] && hostnamectl set-hostname $SYSNAME
    else
         hostnamectl set-hostname x86
    fi
    show_result "set hostname" 0
}
#
main(){
    if [ -f /etc/xyapp/.sysname ];then
        TAG=$(cat /etc/xyapp/.sysname)
    else
        show_result "file /etc/xyapp/.sysname not exist" 1
        TAG=xybk
    fi
    #
    if [ "${TAG}" == "xr" ];then
        TAG=xykj
    elif [ "${TAG}" == "XRVD" ];then
        TAG=xykj
    elif [ "${TAG}" != "xybk" ];then
        TAG=xykj
    fi
    #
    func_clear_zjcdnnon80
    func_blacklistdel_plugin
    func_restore_upgrade
    func_restore_activation
    #func_clean_lvm
    func_set_hostname
    func_clearConfig
    func_restore_sshd
}
#
main
