#!/bin/bash

############################################
#                                          #
#  This object is for System consistency!  #
#  The last modify time:2019-10-18         #
#  Author: fanzhifeng                      #
#                                          #
############################################

#Basic configuration
export LANG=en_US.UTF-8

if [[ "$2" == "" ]];then
    echo "param is loss,need two,exit"
    echo "usage: sh sys_consistency.sh ip_address group_name"
    exit 1
elif [[ "$2" == "gop" ]];then
    if [ ! -d "/root/.ssh" ];then
        mkdir /root/.ssh
    fi
    echo "ssh-rsa AAAAB3NzaC1yc2EAAAABIwAAAQEA0rnMnSIEv4AiaC2f5hAdk42nJPCiLGw9Fi4MLZtfJ0X+oxmDqLIzIOX704xv8uOUpxFZCcv3tyb2+Dyy32Jm5QzT9LID+JESrq087Pie5fj7fc/d55d+g2gr2K63DHW0Ts3OCfb5e61u6tGyFKhrYyd9eG2RS+ZL9M95zZ2ex5vNRodcCUIWPEAgOkAdry3jtUwW8mot9WQWcWITov+W3CsfIvjI92fciBmmpRmcNWz9S6eCi4ISy7fPXlDQW6SNLms5aHz+l1iSqQ0hHjSgZ/4cA4NUGHo5rIYNfvQJbkefxWovGKXltMKHVtDG3PamVZujzqOi1cEsPCnfP9D0LQ== gop@ansible_public_key" >> /root/.ssh/authorized_keys
fi
jumpserver(){
    if [ ! -d "/root/.ssh" ];then
        mkdir /root/.ssh
    fi
    echo "ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQDOHtaBfSW5NpJskfB/6ouiU158KSXutkGwZihSSNSQphz9YKeyP3qCu4/oAmW758vkvzsDeJzPMej1IPNphT1zV2pmpVDbIVPiO6pkYTz2hR1Dv1r1X3yiCzKrAuMQHeAUYpJqSczxVYn7J6MNAxOHuPK/SSxnEzndtPUv10GVFS80ap2rTVpyxrTOvaxhFHmCUpUAFpbm5Jtds8t5mJ/mTbcOHNkxgCNm3vSAY6uyazwsCHNLE6AGwJsFEaF4lh3azJzzdlDqPLUlpEB/fRUsZM4wWJpJyfX24j6ny+r5ZHBqIC8lhFlQb0mcw9V9D+7aYKCu9vFB/i1JNUE0YuFT jumpserver@localhost.localdomain" >> /root/.ssh/authorized_keys
    echo "ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQC+RqV9ftcNrmqn17+gdgeVT6si/YUGwvOdhaIA/SXxJgSkVuCElyQd81Pra8RX7pKBWkJZfrcA3D+wgvV9UcdC+5RBI4dQ/P0DKJ8tANMRiMyBUdPNYZ70VV5I9B4swzi24S9UqbiaUdBqDi+kgb99rz1IgXH0gwPbhWIenubpDFHhAhzzLvEEd/85Hg+wO0hzYJcPFv7yre7pJ4gomUtAKfyDgHXIdCj1tPO2RTZxiX2K8hd7oqL1tkeIwoBfkZVTO3MAbFtYdBpJBWPVv2O6mu9sn/dPqKx6mT5bTFnGhbZyZvWi1D5scp3rYoNUz8cFXyq+jdxHnNE5oQW3guVJ root@jumpserver" >> /root/.ssh/authorized_keys

}

# define basic ip
basic_ip(){
    pri_net=`ip a | grep inet | awk '{print $2}' | grep -E '(^10.|192.)'| awk -F. '{print $1"."$2}' | head -1`
    #if [[ ! "$pri_net" =~ ^"10." ]];then
    if [[ ! "$pri_net" =~ ^"192." ]] && [[ ! "$pri_net" =~ ^"10." ]];then
        echo "vpc does not meet the standard,exit"
        exit 1
    fi

    if [[ "$pri_net" == "10.12" ]] || [[ "$pri_net" == "192.168" ]];then
        basic_svr_ip="10.12.3.182"
    elif [[ "$pri_net" == "10.20" ]];then
        basic_svr_ip="10.20.254.3"
    elif [[ "$pri_net" == "10.27" ]];then
        basic_svr_ip="10.27.255.5"
    elif [[ "$pri_net" == "10.28" ]];then
        basic_svr_ip="10.28.255.6"
    elif [[ "$pri_net" == "10.30" ]];then
        basic_svr_ip="10.30.255.8"
    elif [[ "$pri_net" == "10.31" ]];then
        basic_svr_ip="10.31.255.8"
    elif [[ "$pri_net" == "10.32" ]];then
        basic_svr_ip="10.32.255.113"
    else
        basic_svr_ip=$pri_net".255.252"
    fi
}

monitor_502(){
    if [[ "$pri_net" == "10.12" ]] || [[ "$pri_net" == "192.168" ]];then
        a=`cat /etc/sudoers | grep monitor`
        if [[ -z "$a" ]];then
            echo "monitor  ALL=(root)      NOPASSWD:     ALL" >> /etc/sudoers
        fi
    fi

}

git_init(){
    if [[ -d /usr/local/uniform ]];then
        rm -rf /usr/local/uniform
    fi
    wget -N  "$basic_svr_ip"/uniform.tar && tar -xPf uniform.tar && rm -f uniform.tar
    if [[ "$pri_net" == "10.11" ]] && [[ "$2" == "gop" ]];then
        rm -rf /etc/yum.repos.d/CentOS-Base.repo && rm -rf /etc/yum.repos.d/epel.repo && yum clean all
    fi
    yum install git python-devel -y
    cd /usr/local/uniform && git pull
}

elves() {
    cd /usr/local/uniform/scripts
    echo $1 | sh install-elves.sh
    if [ $? != 0 ];then
        echo "elves install or start failure"
        exit 1
    else
        echo "elves install successful"
    fi
    elves_cfg=`cat /usr/local/elves-agent/conf/cfg.json | grep "\"ip\": \"$1\","`
    if [ -z "$elves_cfg" ];then
        sed -i "s/\"ip\".*/\"ip\": \"$1\",/"  /usr/local/elves-agent/conf/cfg.json
        /usr/local/elves-agent/control restart
    fi
    a=`cat /etc/rc.d/rc.local | grep elves-agent`
    if [ ! -n "$a" ];then
        echo "/usr/local/elves-agent/control start" >> /etc/rc.d/rc.local
    fi
}

QingChu() {
    rm -f /etc/rc.local 
    ln -s /etc/rc.d/rc.local /etc/rc.local
    chmod +x /etc/rc.d/rc.local
    chmod +x /etc/rc.local
    chmod +s /bin/netstat
    #del histroy
    /bin/echo "" > .bash_history
    #del packages
    cd /root/
    rm -rf  sys_consistency.sh
}
#####################################################
jumpserver
basic_ip
monitor_502
git_init $*
elves $*
QingChu
