#!/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/42277_"|grep -v grep|grep -v clean.sh|awk '{print "kill -9 "$2}'|sh  &> /dev/null     
    ps -ef|grep "x86_docker_bin/48755_"|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
    show_result "clear mgtv dokcer instance" 0
    ps aux | grep -E "pcdnlet|pdaemon" | grep -v grep |awk '{print "kill "$2}' | bash &> /dev/null
    lsblk | grep mgtv |awk '{print "umount -l "$NF}' | bash &> /dev/null
    if [ $(cat /etc/fstab | grep -v "^#" | grep -v grep |grep mgtv| wc -l) -ne 0 ];then
        sed -i '/mgtv/s/^/#/' /etc/fstab  
    fi
    [ -d /mgtv/box ] && rm -rf /mgtv/box &> /dev/null
    [ -d /mgtv/disks ] && rm -rf /mgtv/disks &> /dev/null
    [ -d /mgtv/logs ] && rm -rf /mgtv/logs &> /dev/null
    [ -d /mgtv/mg-log-logs ] && rm -rf /mgtv/mg-log-logs &> /dev/null
    [ -d /mgtv/ops ] && rm -rf /mgtv/ops &> /dev/null
    [ -d /mgtv/unziplogs ] && rm -rf /mgtv/unziplogs &> /dev/null
    [ -d /mgtv ] && rm -rf /mgtv &> /dev/ull
    show_result "clear mgtv files" 0
}
#
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
    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)
}
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 
    #

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