#!/bin/bash
# 环境
export PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin

# 显示
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/zjconfig.log
    else
        echo -e "[${RED} fail ${NC}]\t$(date "+%F %H:%M:%S") $message" | tee -a /tmp/zjconfig.log
    fi
}
#
func_inst_packages(){
    # 更新YUM源
    if [ $(cat /etc/yum.repos.d/CentOS-Base.repo | grep -c 'mirrors.aliyun.com') -eq 0 ];then
        mv /etc/yum.repos.d/CentOS-Base.repo{,bak}
        curl -s -o /etc/yum.repos.d/CentOS-Base.repo https://mirrors.aliyun.com/repo/Centos-7.repo
        curl -s -o /etc/yum.repos.d/epel.repo https://mirrors.aliyun.com/repo/epel-7.repo   
    fi
   
    # 安装基础工具
    command -v jq &>/dev/null || yum -y install jq &>/dev/null
}
# 检查当前业务状态
func_current_business_status(){
    appid=$(/xyapp/system/miner.plugin-activation.ipk/tools/recruit_tools -p show | grep appid | awk '{print$2}')
    if [ $(echo ${appid} | grep -cE 'a562945659d73e5584b8cf54a23a0470|c011c3027d03ffa922bb79a12269de28') -eq 0 ];then
        show_result "The current business is not a SuperV,please switch first,Script exit." 1  
        exit 110
    fi
    status_code=$(cat /etc/xyapp/recruitResult.json | jq .status)
    if [ ${status_code} -ne 3 ];then
        show_result "The current state is not the running state.Script exit." 1 
        exit  110 
    fi
}

#复制/etc/xyapp分区到根分区
func_cp_xyapp(){
    if lsblk | grep -q "/etc/xyapp" ; then
        cd /etc/
        [ -f /root/xyapp.tar ] && rm -f /root/xyapp.tar
        tar -cf /root/xyapp.tar ./xyapp/*
        while true;do
            umount /etc/xyapp
            if ! lsblk | grep -q "/etc/xyapp" ; then
                cd /etc/
                tar -xf /root/xyapp.tar -C ./
            fi
            if ! lsblk | grep -q "/etc/xyapp" ; then
                break
            fi
            sleep 2
        done
        mount -a
    fi
}


# 基础配置
func_base_configuration(){
 
    # 生成定时任务配置文件
    [ ! -e /var/spool/cron/root ] && (touch /var/spool/cron/root && chmod 600 /var/spool/cron/root)
    chattr -i /var/spool/cron/root

    # 配置秘钥
    [ ! -d /root/.ssh ] && (mkdir -p /root/.ssh && chmod 700 /root/.ssh)
    [ ! -e /root/.ssh/authorized_keys ] && (touch /root/.ssh/authorized_keys && chmod 600 /root/.ssh/authorized_keys)
    if [ $(grep -c 'root@wxzyz001' /root/.ssh/authorized_keys) -eq 0 ];then
    cat>/root/.ssh/authorized_keys<<EOF
ssh-rsa [payload removed: 544 characters] liuyunpeng.0103@bytedance.com
ssh-rsa [payload removed: 372 characters] tianxiang@cdn_jumpserver
ssh-rsa [payload removed: 544 characters] xuebowen@C02Z24JHLVCF
ssh-rsa [payload removed: 372 characters] root@wxzyz001
ssh-rsa [payload removed: 372 characters] liujing.linuxfan@n198-064-044
EOF
        show_result "SSH keys configure." $?
    else
        show_result "SSH keys has been configured." 0
    fi
    [ -f /etc/shadow.xybak ] || cp /etc/shadow /etc/shadow.xybak
    # 配置DNS
    if [ $(grep -c '119.29.29.29' /etc/resolv.conf) -eq 0 ];then
        \cp -raf /etc/resolv.conf{,.bak}
cat>/etc/resolv.conf<<EOE
options timeout:1 attempts:1
nameserver 119.29.29.29
nameserver 223.6.6.6
nameserver 223.5.5.5
EOE
        show_result "DNS configure." $?
    else
        show_result "DNS has been configured." 0
    fi

    # 字节网卡硬件配置文件
    if [ ! -e /etc/zjfx.config ];then
        cat >/usr/local/setup_file_zjfx.sh<<'EOF'
#!/bin/bash
export PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
sn=$(cat /etc/xyapp/.efuse_sn)
if [ $(echo $sn|grep CTWX|wc -l) -ne 0 ];then
    sys_part=$(lsblk | grep "/etc/hosts" |awk '{print $1}'|sed 's/├─/ /')
    if [ $(echo "$sys_part"|grep nvme|wc -l) -ne 0 ];then
        sys_disk=$(echo "$sys_part" | sed "s/p[0-9]$//")
    else
        sys_disk=$(echo "$sys_part" | sed "s/[0-9]$//")
    fi 
else
    sys_disk=$(lsblk -l | egrep -B 1 "/$|boot|swap" | egrep -v "lvm|luks|--" | awk '{print $1}' | sed -e '/sd/s/[0-9]//' -e '/nvme/s/p[0-9]//' -e '/vd/s/[0-9]//' | sort|uniq)
fi
data_disk=$(lsblk | grep -E "^s|^n|^v" | grep -v sr0 | awk '{print"/dev/"$1}' | grep -v ${sys_disk} | sort | tr '\n' ',' | sed s/,$//)
echo "DISKLIST=${data_disk}" > /tmp/zjfx.config

if [ -f /tmp/multidialstatus.json ];then
    practicnic=$(cat /tmp/multidialstatus.json | jq -r .multidial[].practicnic)
else
    practicnic=""
fi
#
if [ -z "${practicnic}" ];then
    zjnic=$(route -n | grep '^0.0.0.0' | awk '{print$NF}')
    echo "NICS=${zjnic}" >> /tmp/zjfx.config 
else
    zjnic=$(echo ${practicnic} | sed 's/ /,/g')
    echo "NICS=${zjnic}" >> /tmp/zjfx.config
fi

if [ ! -e /etc/zjfx.config ];then
    \cp -raf /tmp/zjfx.config  /etc/zjfx.config
else
   diff -q /tmp/zjfx.config /etc/zjfx.config
   if [ $? -ne 0 ];then
      \cp -raf /tmp/zjfx.config /etc/zjfx.config
   fi
fi
EOF
        if [ $(grep -c 'setup_file_zjfx' /var/spool/cron/root) -eq 0 ];then
            echo '*/5 * * * * /bin/bash /usr/local/setup_file_zjfx.sh &>/dev/null' >> /var/spool/cron/root 
        fi
        bash /usr/local/setup_file_zjfx.sh  
        show_result "Add new configuration zjfx file." $?       
    else
        show_result "The zjfx config has been generated." 0   
    fi


    # 下载业务镜像
    if [ ! -e /root/cdnn80.tar.gpg ];then
        cd /root
        #curl -sL "https://lf9-edgec-sign.byteics.net/edgec-initscript/icsinit/image/cdnn80.tar.gpg?x-expires=1816483657&x-signature=x6AfEnFNoSZKQE4lolZANIdy96s%3D" --output cdnn80.tar.gpg
        curl -s  http://tw06d0006.onething.net/lzp/cdnn80.tar.gpg -o /root/cdnn80.tar.gpg
    elif [ $(stat -c%s "/root/cdnn80.tar.gpg") -lt  770818229 ];then
        curl -s http://tw06d0006.onething.net/lzp/cdnn80.tar.gpg -o /root/cdnn80.tar.gpg
    fi
    if [ -f /root/cdnn80.tar.gpg ];then
        md5=$(md5sum /root/cdnn80.tar.gpg | awk '{print$1}')
        if [ "${md5}" != "7d1cb792018a0b71da7d85af0283fa39" ];then
            rm -f /root/cdnn80.tar.gpg &> /dev/null 
            show_result "busniess image file cdnn80.tar.gpg md5 value error." 1
            exit 1
        else
            show_result "The business image has been downloaded." 0
        fi
    else
        show_result "busniess image file cdnn80.tar.gpg download failed." 1
        exit 1
    fi
    #监控挂载xyapp相关目录
    if [ ! -f /usr/bin/mountxyapp.sh ];then
    cat > /usr/bin/mountxyapp.sh <<'EOD'
#!/bin/bash
if ! lsblk | grep -q "/etc/xyapp" ;then 
    mount -a
    echo "$(date "+%F %H:%M:%S") mount /etc/xyapp" >> /tmp/mountxyapp.log
fi
if ! lsblk | grep "/xyapp";then
    mount -a
    echo "$(date "+%F %H:%M:%S") mount /xyapp" >> /tmp/mountxyapp.log
fi
EOD
    show_result "The mount xyapp config has been generated." 0   
    fi
    if [ $(grep -c 'mountxyapp.sh' /var/spool/cron/root) -eq 0 ];then
        echo "*/1 * * * * /bin/bash /usr/bin/mountxyapp.sh &> /dev/null" >> /var/spool/cron/root
        /bin/bash /usr/bin/mountxyapp.sh &> /dev/null
        show_result "Add new configuration mount xyapp directory." $?
    fi
}

#clear lv
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* &>/dev/null
        lvremove -yf /dev/mapper/XYBM* &>/dev/null
        for i in $vg_group
        do
            vgremove -yf $i &> /dev/null
        done

        vgs=$(vgs | grep -E "XRVD|XYBM" | grep -v grep)
        if [ -z "${vgs}" ]; then
            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
    #
    if lsblk | grep -q "^zram" ;then 
        /xyapp/system/miner.plugin-perfoptimizer.ipk/script/zramstop
    fi
    dmsetup remove mpatha &> /dev/null
    dmsetup remove_all
    if [ $(lvs|wc -l) -ne 0 ];then
        show_result "lvm clean failed." 1
        exit 1
    elif [ $(vgs|wc -l) -ne 0 ];then
        show_result "lvm clean failed." 1
        exit 1
    elif [ $(vgs|wc -l) -ne 0 ];then
        show_result "lvm clean failed." 1
        exit 1
    else
        show_result "lvm clean success." 0
    fi
}


# 拉黑禁用插件
func_blacklistadd_plugin(){
    
    # 判断是否拉黑
    func_determine_blacklisted(){
        plugin_name=$1
        if [ $(xycall blacklistshow | grep -c ${plugin_name}) -eq 0 ];then
            if [ -e /xyapp/system/miner.${plugin_name}.ipk/stop.sh ];then
                cd /xyapp/system/miner.${plugin_name}.ipk/
                [ -e stop.sh ] && ./stop.sh &>/dev/null
                [ -e start.sh ] && mv start.sh{,.bak_xykj}
            fi
            xycall blacklistadd ${plugin_name}
            show_result "blacklistadd  ${plugin_name}." $?    
        else
            show_result "The ${plugin_name} has been blacklisted." 0       
        fi
    }

    plugin_arr=("plugin-systemrestart" "plugin-minionk8s" "plugin-secore" "plugin-dockerd")   
    for element in "${plugin_arr[@]}"; do
        func_determine_blacklisted ${element}
    done 
  
    # 拉黑插件
    if [ $(xycall blacklistshow | grep -c plugin-deploy) -eq 0 ];then
        xycall blacklistadd plugin-deploy
        if [ -d /xyapp/system/miner.plugin-deploy.ipk ];then
            cd /xyapp/system/miner.plugin-deploy.ipk
            [ -e stop.sh ] && ./stop.sh &>/dev/null
            [ -e start.sh ] && mv start.sh{,.bak_xykj}
            sleep 30
        fi
        if [ $(netstat -tulnp| grep deploy | grep -wc ':8900') -eq 0 ];then
            show_result "blacklistadd  plugin-deploy." $?
        fi
    else
            show_result "The plugin-deploy has been blacklisted." 0
    fi
    #
    if [ $(netstat -tulnp| grep deploy | grep -wc ':8900') -eq 0 ];then
        show_result "blacklistadd  plugin-deploy." $?
    else
        show_result "plugin-deploy not stop" 1
    fi
    # 
    # 禁止自更新,防止误操作
    if [ $(grep -c 'update.peiluyou.com' /xyapp/xyipk/ipk_opkg.conf) -ne 0 ];then
        \cp -raf /xyapp/xyipk/ipk_opkg.conf{,.bak_xykj}
        sed -i s/'update.peiluyou.com'/'127.0.0.1'/g /xyapp/xyipk/ipk_opkg.conf
        show_result "The ipk_opkg configuration file update." $?
    else
        show_result "The ipk_opkg configuration file has been updated." 0
    fi
}

# 更新插件
func_update_plugin(){
    plugin_name=$1
    req_ver=$2
    package_name=$3
    #min_ver=$(echo -e "$(xycall info ${plugin_name} | jq -r .local_version) \n${req_ver}" | sort -V | head -n 1)
    if [ $(xycall info ${plugin_name} | grep "local_version" |wc -l) -eq 0 ];then
        show_result "The ${plugin_name} uninstall." 0
        return 0
    fi
    cur_ver=$(xycall info ${plugin_name} | jq -r .local_version)
    if [[ $(printf "%s\n" "${req_ver}" "${cur_ver}" | sort -V | tail -n 1) == "${cur_ver}" ]];then
        show_result "The ${plugin_name} has been updated." 0
    else
        xycall installurl "http://tw15b0178.onething.net:9090/tool/superV/${package_name}"
        sleep 60
        cur_ver=$(xycall info ${plugin_name} | jq -r .local_version)
        if [[ $(printf "%s\n" "${req_ver}" "${cur_ver}" | sort -V | tail -n 1) == "${cur_ver}" ]];then
            show_result "${plugin_name} update." 0
            cd /xyapp/system/miner.${plugin_name}.ipk
            ./stop.sh &>/dev/null && ./start.sh &>/dev/null
        else
            show_result "${plugin_name} update." 1
        fi
    fi 
}
#
#升级插件
func_upgrade_plugin(){
    ## 更新配置文件
    if [ -d /xyapp/system/miner.plugin-activation.ipk ];then
        # 激活插件plugin-activation
        ## 更新插件
        func_update_plugin "plugin-activation" '2.25.1' "plugin-activation_2.25.1_xykj.ipk"
        if [ $(grep -c 58080 /xyapp/system/miner.plugin-activation.ipk/conf/activation.conf) -eq 0 ];then 
            \cp -raf /xyapp/system/miner.plugin-activation.ipk/conf/activation.conf{,.bak_xykj}
            curl -s -o /xyapp/system/miner.plugin-activation.ipk/conf/activation.conf http://tw15b0178.onething.net:9090/tool/superV/activation.conf
            cd /xyapp/system/miner.plugin-activation.ipk
            ./stop.sh &>/dev/null && ./start.sh &>/dev/null
            show_result "The plugin-activation configuration file update." $?
        else
            show_result "The plugin-activation configuration file has been updated." 0
        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) -eq 0 ];then
                \cp -raf  /xyapp/system/miner.plugin-activation.ipk/tools/recruit_tools{,.bak_xykj}
                sed -i 's/8080/58080/g' "/xyapp/system/miner.plugin-activation.ipk/tools/recruit_tools"
                show_result "The recruit_tools update." $?
            else
                show_result "The recruit_tools has been updated." 0
            fi
        fi
        xycall blacklistadd plugin-activation
    fi
    # 代理插件plugin-httpproxy
    func_update_plugin "plugin-httpproxy" '1.1.52' "plugin-httpproxy_1.1.52_${TAG}.ipk"
    
    # 压测插件plugin-syspress
    func_update_plugin "plugin-syspress" '0.6.6' "plugin-syspress_0.6.6_${TAG}.ipk"
     
    # frp插件plugin-frpc.ipk   
    func_update_plugin "plugin-frpc" '1.3.2' "plugin-frpc_1.3.2_${TAG}.ipk"

    # 流量统计插件
    func_update_plugin "plugin-tupleprobe" '1.0.16' "plugin-tupleprobe_1.0.16_${TAG}.ipk"

    #更新agent  
    #func_update_plugin "plugin-agent" '100.3.95' "plugin-agent_100.3.95_${TAG}.ipk"
    func_update_plugin "plugin-agent" '100.3.121' "plugin-agent_100.3.121_${TAG}.ipk"
   
    #禁用cdtrace
    if [ $(netstat -tunlp | grep 7021|grep cdntrace|wc -l) -gt 0 ];then
        chattr -i /var/spool/cron/root
        sed -i '/cdntrace/s/^/#/' /var/spool/cron/root
        mv /usr/local/cdntrace/cdntrace{,.bak}
        ps aux | grep cdntrace | grep -v grep |awk '{print "kill -9 "$2}'|sh
    fi
}

# 放行所需端口
func_setup_iptables(){
  
    # 放行脚本 
    if [ ! -e /usr/local/setup_iptables.sh ];then
        cat >/usr/local/setup_iptables.sh<<'EOK'
#!/bin/bash

export PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin

if [ $(iptables -nvL  | grep -c NAT_TYPE_DETECTION) -eq 0 ];then
    iptables -I INPUT 1 -p udp -m multiport --dports 50000:53000 -m comment --comment "NAT_TYPE_DETECTION" -j ACCEPT
fi

if [ $(iptables -nvL  | grep -c reverse_connection_detection) -eq 0 ];then
    iptables -I INPUT 1 -p tcp -m multiport --dports 50000:53000 -m comment --comment "reverse_connection_detection" -j ACCEPT
fi 

if [ $(iptables -nvL  | grep -c WEB_CONSOLE) -eq 0 ];then
    iptables -I INPUT 1 -p tcp -m multiport --dports 58080 -m comment --comment "WEB_CONSOLE" -j ACCEPT
fi
EOK
        chmod +x /usr/local/setup_iptables.sh
        if [ $(grep -c 'setup_iptables' /var/spool/cron/root) -eq 0 ];then
            echo '*/5 * * * * /bin/bash /usr/local/setup_iptables.sh &>/dev/null' >> /var/spool/cron/root
        fi
        bash /usr/local/setup_iptables.sh
        show_result "Iptables configuration." $?                    
    else
        show_result "Iptables has been configured." 0
    fi
}

# 配置frp
func_setup_frpc(){
    # 获取后端分配端口
    sn=$(cat /etc/xyapp/.efuse_sn)
    [ -z "$sn" ] && (show_result "sn is empty" 1;exit 1)
    #appid=$(/xyapp/system/miner.plugin-activation.ipk/tools/recruit_tools -p show | grep appid | awk '{print$NF}')
    if [ "$TAG" == xykj -o "$TAG" == XRVD ];then
        business_id=0
        appid="a562945659d73e5584b8cf54a23a0470"
    else
        business_id=1054
        appid=""
    fi
    #
    ssh_port=22
    if [ $(echo $sn | grep CTWX | wc -l) -ne 0 ];then
        if [ $(ss -tunlp | grep ":2222"|wc -l) -ne 0 ];then
            ssh_port=2222
        elif [ $(ss -tunlp | grep ":10022"|wc -l) -ne 0 ];then
            ssh_port=10022
        else
            ssh_port=$(ss -tunlp | grep sshd |awk '{print $5}'|awk -F":" '{print $2}'|head -1)
        fi
    fi
    frpc_port=0
    json_data="{\"sn\": \"${sn}\", \"appid\":\"${appid}\",\"business_id\":${business_id}}"
    frpc_port=$(curl -sL -X POST -H "Content-Type: application/json" -d "$json_data" --location 'https://miner-ex-service-sgp.onething.net/v2/api/port/generate_port' | jq .data.port)
    #frpc_port=$1
    if [ "${frpc_port}" == null ]; then
        show_result "Frp port is null" 1
        exit 1
    elif [ "${frpc_port}" == 0 -o "${frpc_port}" == "" ];then
        show_result "Frp port is null" 1
        exit 1
    elif [ -n "${frpc_port}" ];then 
        show_result "Frp port: ${frpc_port}" 0 
        [ ! -e /etc/frp ] && mkdir -p /etc/frp
        cat >/etc/frp/frpczj.ini<<EOG
[common]
server_addr = frpcustomer.xinqiucc.com
server_port = 30976
#server_addr = frpcbd.onething.net
#server_port = 7900
admin_addr = "127.0.0.1"
admin_port = 7401
admin_user = "admin"
admin_password = "admin"

[ssh-${frpc_port}]
type        = tcp
local_ip    = 127.0.0.1
local_port  = ${ssh_port}
remote_port = ${frpc_port}
EOG
    else
        show_result "Frp port is empty" 1
    fi
    #
    if [ ! -e /lib/systemd/system/frpccustom.service ];then 
        [ ! -e /usr/bin/frpc ] && ln -s /xyapp/system/miner.plugin-frpc.ipk/bin/frpc /usr/bin/frpc
        cat >/lib/systemd/system/frpccustom.service<<EOH
[Unit]
Description=Frp Client Service
After=network.target

[Service]
Type=simple
User=root
Restart=on-failure
RestartSec=5s
ExecStart=/usr/bin/frpc -c /etc/frp/frpczj.ini
ExecReload=/usr/bin/frpc reload -c /etc/frp/frpczj.ini

[Install]
WantedBy=multi-user.target
EOH
        systemctl daemon-reload
        systemctl start frpccustom.service 
        show_result "Frp install" $?
        systemctl enable frpccustom.service &>/dev/null
    else
        systemctl restart frpccustom.service
        show_result "Frp has restart" $?
    fi                                    
}

# 配置内网IP
func_setup_lanip(){

    if [ ! -e /usr/local/setup_lanip.sh ];then
        cat > /usr/local/setup_lanip.sh << 'EOI'
#!/bin/bash
export PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
if [ $(ip addr|grep inet | grep tun | grep -c '10\.249\.1\.200') -eq 0 ];then
    ip tuntap add dev tun0 mode tun
    ip link set dev tun0 up
    /sbin/ifconfig tun0 10.249.1.200/24
fi
EOI
        chmod +x /usr/local/setup_lanip.sh
        if [ $(grep -c 'setup_lanip' /var/spool/cron/root) -eq 0 ];then
            echo '*/5 * * * * /bin/bash /usr/local/setup_lanip.sh &>/dev/null' >> /var/spool/cron/root
        fi
        bash /usr/local/setup_lanip.sh
        show_result "Internal IP configuration." $?
    else
        show_result "The internal ip has been configured." 0
    fi
}

# 管理services
func_management_services(){
    
    # 开启docker服务
    if [ -f /usr/lib/systemd/system/docker.service ];then
        if grep -q "ExecStartPre=/usr/local/by-watcher/watcher"  /usr/lib/systemd/system/docker.service ;then
            sed -i '/ExecStartPre=\/usr\/local\/by-watcher\/watcher/d' /usr/lib/systemd/system/docker.service
	    systemctl daemon-reload
        fi
    fi
    if systemctl status docker &>/dev/null ;then 
        show_result "The docker service is already running." 0 
    else
        systemctl stop docker.service
        systemctl stop docker.socket
        systemctl start docker  
        show_result "systemctl start docker" $?
    fi
    systemctl enable docker &> /dev/null
    # 关闭xyipk更新服务
    systemctl status upgrade &>/dev/null && ((systemctl disable upgrade &>/dev/null && systemctl stop upgrade) && show_result "systemctl stop upgrade" $?) || show_result "The xyipk upgrade service has been stopped." 0
    

    # 开启sshd服务
    if [ $(grep 'sftp-server' /etc/ssh/sshd_config | grep -c '^\s*#') -ne 0 ];then
        cp -raf /etc/ssh/sshd_config /etc/ssh/sshd_config.bak_${TAG}
        sed -i '/sftp-server/s/^#//' /etc/ssh/sshd_config
        mkdir -p /usr/lib/openssh
        curl -s -o /usr/lib/openssh/sftp-server http://tw15b0178.onething.net:9090/tool/superV/sftp-server
        chmod 755 /usr/lib/openssh/sftp-server
        systemctl enable sshd &>/dev/null
        systemctl restart sshd
        show_result "systemctl restart sshd." $?
    else
        show_result "The sshd service has been restart." 0
    fi
    if [ $(cat /etc/ssh/sshd_config | grep -c "#Port 22") -ne 0 ];then
        sed -i 's/#Port 22/Port 22/' /etc/ssh/sshd_config
    elif [ $(cat /etc/ssh/sshd_config | grep -c "Port 22") -eq 0 ];then
        echo "Port 22" >>  /etc/ssh/sshd_config
    fi
    if [ $(cat /etc/ssh/sshd_config | grep -vE "^#" | grep -c "Port 10022") -eq 0 ];then
        sed -i '/Port 22/aPort 10022' /etc/ssh/sshd_config
        systemctl reload sshd
    fi
    # 
    # 设置xyapp.service
    if [ ! -e /usr/local/setup_xyapp_service.sh ];then
        cat >/usr/local/setup_xyapp_service.sh<<'EOY'
#!/bin/bash
export PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
systemctl status  xyapp &>/dev/null
if [ $? -ne 0 ];then
    systemctl restart  xyapp
fi 
EOY
        if [ $(grep -c 'setup_xyapp_service' /var/spool/cron/root) -eq 0 ];then
            echo '*/5 * * * * /bin/bash /usr/local/setup_xyapp_service.sh &>/dev/null' >> /var/spool/cron/root
        fi
        bash /usr/local/setup_xyapp_service.sh
        show_result "The xyapp service liveness check configuration." $?
    else
        show_result "The xyapp service has been configured." 0
    fi
}

post_delv_info(){
    if [ ! -f /usr/local/zjhj_delivery_info.py ];then
        curl -s http://tw15b0178.onething.net:9090/tool/superV/zjhj_delivery_info.py -o /usr/local/zjhj_delivery_info.py &> /dev/null
    fi
    if [ $(ps aux | grep zjhj_delivery_info | grep -v grep | wc -l) -lt 1 ];then
        nohup python /usr/local/zjhj_delivery_info.py &> /dev/null &
        show_result "post zjhj delivery info." 0
    fi
}

# 主函数 
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
    #
    func_inst_packages 
    if [ "${TAG}" == "xr" ];then
        TAG=xykj
        #func_current_business_status
    elif [ "${TAG}" == "XRVD" ];then
        TAG=xykj
    elif [ "${TAG}" != "xybk" ];then
        TAG=xykj
    fi
    #
    func_cp_xyapp
    func_base_configuration
    func_blacklistadd_plugin
    func_upgrade_plugin
    func_setup_iptables
    func_setup_lanip
    func_management_services
    func_setup_frpc "$@"
    func_clean_lvm
}


# 脚本入口
main "$@"
#
if echo $0 | grep -q xykj2superV.sh ;then
    exit 0
fi
while true;
do
    post_delv_info
    sleep 120
done
