#!/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/mgclean.log
    else
        echo -e "[${RED} fail ${NC}]\t$(date "+%F %H:%M:%S") $message" | tee -a /tmp/mgclean.log
    fi
}
#
#清理芒果业务程序
func_clear_mgtv(){
    ps -ef|grep "x86_docker_bin/44198_"|grep -v grep|grep -v clean.sh|awk '{print "kill -9 "$2}'|sh  &> /dev/null
    docker ps | grep  -E "imgo|mgtv" | awk '{ print $1 }' | xargs docker stop  &> /dev/null
    docker ps -a | grep -E "imgo|mgtv" | awk '{ print $1 }' | xargs docker rm  &> /dev/null
    docker rmi $(docker images | grep -PE "imgo|mgtv" | awk '{print $3}')  &> /dev/null
    ps aux | grep -E "pcdnlet|pdaemon" | grep -v grep |awk '{print "kill -9 "$2}' | sh &> /dev/null
    ps aux | grep -iE "Titan|cdn" | grep -v grep | awk '{print "kill -9 "$2}' | sh &> /dev/null
    [ -f /home/Titan/nginx/sbin/nginx ] && /home/Titan/nginx/sbin/nginx -s stop
    [ -d /var/log/cdn/agent ] && rm -rf /var/log/cdn/agent &> /dev/null
    [ -d /var/log/cdn/cagent ] && rm -rf /var/log/cdn/cagent &> /dev/null
    [ -d /var/log/cdn/hotspot ] && rm -rf /var/log/cdn/hotspot  &> /dev/null
    [ -d /var/log/cdn/kcptun-client ] && rm -rf /var/log/cdn/kcptun-client &> /dev/null
    [ -d /var/log/cdn/Titan/mgvod-ngx ] && rm -rf /var/log/cdn/Titan/mgvod-ngx &> /dev/null
    [ -d /var/log/cdn/Titan ] && rm -rf /var/log/cdn/Titan &> /dev/null
    [ -d /var/log/cdn ] && rm -rf /var/log/cdn &> /dev/null
    
    [ -d /home/Titan/agent ] && rm -rf /home/Titan/agent &> /dev/null
    [ -d /home/Titan/cagent ] && rm -rf /home/Titan/cagent  &> /dev/null
    [ -d /home/Titan/DeliNodeSvr ]  && rm -rf /home/Titan/DeliNodeSvr  &> /dev/null
    [ -d /home/Titan/hotspot ]  && rm -rf /home/Titan/hotspot  &> /dev/null
    [ -d /home/Titan/mg-log-agent ]  && rm -rf /home/Titan/mg-log-agent  &> /dev/null
    [ -d /home/Titan/nginx ]  && rm -rf /home/Titan/nginx  &> /dev/null
    [ -d /home/Titan ]  && rm -rf /home/Titan  &> /dev/null
    show_result "clear mgtv dokcer instance" 0
    #
    systemctl stop zabbix-agentd-cdn
    systemctl disable zabbix-agentd-cdn
    [ -d /usr/local/zabbix ] && rm -rf /usr/local/zabbix &> /dev/null
    userdel -r zabbix &> /dev/null
}
#
func_restore_sshd(){
    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
        if [ $(grep -c 'yunpeng2' /root/.ssh/authorized_keys) -ne 0 ];then
            sed -i '/yunpeng2/d' /root/.ssh/authorized_keys
        fi
        if [ $(grep -c 'hailong2@mgtv.com' /root/.ssh/authorized_keys) -ne 0 ];then
            sed -i '/hailong2@mgtv.com/d' /root/.ssh/authorized_keys
        fi
        if [ $(grep -c 'yaoliang@mgtv.com' /root/.ssh/authorized_keys) -ne 0 ];then
            sed -i '/yaoliang@mgtv.com/d' /root/.ssh/authorized_keys
        fi
    fi
    sed -i 's/PasswordAuthentication no/PasswordAuthentication yes/' /etc/ssh/sshd_config
    sed -i 's/PermitRootLogin no/PermitRootLogin yes/' /etc/ssh/sshd_config 
    sed -i '/sshd:ALL:deny/d' /etc/hosts.allow
    > /etc/motd
    systemctl enable sshd &>/dev/null
    systemctl restart sshd
    systemctl status frpccustom.service &> /dev/null && (systemctl disable frpccustom.service &>/dev/null && systemctl stop frpccustom.service)
}
func_clearConfig(){
    #
    [ -f /usr/local/unzip_mgtv_accesslog.sh ] && rm -f /usr/local/unzip_mgtv_accesslog.sh
    if [ $(grep -c 'unzip_mgtv_accesslog.sh' /var/spool/cron/root) -gt 0 ];then
        chattr -i /var/spool/cron/root
        sed -i '/unzip_mgtv_accesslog.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
        chattr -i /var/spool/cron/root
        sed -i '/setup_iptables.sh/d' /var/spool/cron/root
    fi 
    #
    [ -f /usr/local/scripts/update_motd.sh ] && rm -f /usr/local/scripts/update_motd.sh
    if [ $(grep -c "/usr/local/scripts/update_motd.sh" /var/spool/cron/root) -gt 0 ];then
        chattr -i /var/spool/cron/root
        sed -i '/update_motd.sh/d' /var/spool/cron/root
    fi
    #
    [ -f /usr/local/scripts/reporting/get_server_info.py ] && rm -f /usr/local/scripts/reporting/get_server_info.py
    if [ $(grep -c "get_server_info.py" /var/spool/cron/root) -gt 0 ];then
        chattr -i /var/spool/cron/root
        sed -i '/get_server_info.py/d' /var/spool/cron/root
    fi
    #
    [ -f /usr/local/scripts/renice_hotspot.sh ] && rm -f /usr/local/scripts/renice_hotspot.sh
    if [ $(grep -c "renice_hotspot.sh" /var/spool/cron/root) -gt 0 ];then
        chattr -i /var/spool/cron/root
        sed -i '/renice_hotspot.sh/d' /var/spool/cron/root
    fi
    #
    [ -f /usr/local/mgtv.logs.download.server/crc ] && rm -f /usr/local/mgtv.logs.download.server/crc
    if [ $(grep -c "mgtv.logs.download.server" /var/spool/cron/root) -gt 0 ];then 
        chattr -i /var/spool/cron/root
        sed -i '/mgtv.logs.download.server/d' /var/spool/cron/root
    fi
    #
    [ -f /usr/local/scripts/remove_nginx_log_non80.sh ] && rm -f /usr/local/scripts/remove_nginx_log_non80.sh
    if [ $(grep -c "remove_nginx_log_non80.sh" /var/spool/cron/root) -gt 0 ];then
        chattr -i /var/spool/cron/root
        sed -i '/remove_nginx_log_non80.sh/d' /var/spool/cron/root
    fi
    #
    [ -f /usr/local/scripts/access-log-report ] && rm -f /usr/local/scripts/access-log-report
    if [ $(grep -c "access-log-report" /var/spool/cron/root) -gt 0 ];then
        chattr -i /var/spool/cron/root
        sed -i '/access-log-report/d' /var/spool/cron/root
    fi
    chattr -i /var/spool/cron/root
    sed -i '/MAILTO/d' /var/spool/cron/root
    sed -i '/#Ansible/d' /var/spool/cron/root
}
#开启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-syspress")

    for element in "${plugin_arr[@]}"; do
        func_determine_blacklisted ${element}
    done
    #xycall install plugin-dockerd
    show_result "cancel xyipk blacklist." 0
}
#
func_clean_lvm() {
    vg_group=`vgs | grep -v VG | awk -F ' ' '{print $1}' `
    count=0
    while [ $count -lt 10 ];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 5
        count=$((count + 1))
    done
    lsblk | grep lvmdisk |awk '{print "umount -l "$NF}'|bash &> /dev/null
    lvs |grep -vw LV|awk '{print "lvremove -f /dev/"$2"/"$1}'|sh &> /dev/null
    vgs |grep -vw VG |awk '{print "vgremove -f "$1}'|sh &> /dev/null
    pvs |grep -vw PV|awk '{print "pvremove -f "$1}'| sh &> /dev/null
    show_result "clear lvm" 0
    if [ $(lvs|wc -l) -ne 0 ];then
        show_result "lvm clean failed." 1
    elif [ $(vgs|wc -l) -ne 0 ];then
        show_result "lvm clean failed." 1
    elif [ $(vgs|wc -l) -ne 0 ];then
        show_result "lvm clean failed." 1
    else
        show_result "lvm clean success." 0
    fi
}

func_umount_disk() {
    lsblk | grep -E "ssd|data" | awk '{print "fuser -km "$NF}' | sh &> /dev/null
    lsblk | grep -E "ssd|data" | awk '{print "umount "$NF}'| sh &> /dev/null
}
#


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
}
#
main(){
    func_clear_mgtv
    func_restore_sshd
    func_restore_upgrade
    func_blacklistdel_plugin
    func_clearConfig
    #func_clean_lvm
    func_set_hostname
}
main
