#!/bin/bash
os_release_file="/etc/os-release"  
OS="" 
HOSTNAME=`hostname`

COUNT=`ip a | grep eth1 | wc -l`

#设置ip存在的文件
IP_FILE="/opt/ip.list"

DEFAULT_DNS="8.8.8.8"

setLog(){
    logger -t init_shell "$1"
}

getInterfaceMac(){
    cat /sys/class/net/$1/address
}

getInterfaceMTU(){
    cat /sys/class/net/$nic/mtu
}

getInterfaceIP(){
    ip addr show $1 |grep inet |grep -v inet6 | awk '{print $2}' | head -n 1
}

CallBack(){
    setLog "IP setting completed"

    sleep 3
    COUNT=`ip a | grep $PUBLC_INTERFACE | wc -l`

    
    rm $IP_FILE
}

SetIpByCentOS(){
	#private_ip=`ifconfig eth0 | grep 172 | awk '{print $2}'`
	#echo $private_ip
	setLog "start set ip by centos ..."

    which nmcli 2>&1 > /dev/null 
    if [ $? -ne 0 ];then
        yum install -y NetworkManager

        if [ $? -ne 0 ];then
            setLog "Install package [NetworkManager] fail."
            exit 1
        fi

        systemctl start  NetworkManager
    fi

    for ip in `cat $IP_FILE | sed 's/,/ /g'`;
    do 
        nmcli con mod "System $PUBLC_INTERFACE" +ipv4.addresses $ip;
    done

    if [[ "$VERSION_ID" == "8" ]];then
        systemctl restart NetworkManager
    else
        systemctl restart network
    fi

    if [ $? -ne 0 ];then
        setLog "Restart Service NetworkManager fail"
        exit 1
    else
        CallBack
    fi


}

configNetworkConfig_ubuntu(){
    nic=$1
    ips=$2
    config_file=$3

    nic_mac=`getInterfaceMac $nic`
    nic_mtu=`getInterfaceMTU $nic`
    cat >>$config_file<<EOF
       $nic:
            match:
                macaddress: $nic_mac
            mtu: $nic_mtu
            set-name: $nic
EOF
    #not ip
    if [[ "$ips" == "" ]];then
        break;
    fi

    echo "            addresses:" >>$config_file

    for ip in `echo $ips | sed 's/,/ /g'`;
    do
        echo "            - ${ip}" >> $config_file
    done

    #config route
    if [[ "$PUBLC_INTERFACE" == "$nic" ]];then

        cat >>$config_file<<EOF
            routes:
            -   to: 0.0.0.0/0
                via: $DEFAULT_ROUTE

EOF
        ##config dns
        config_dns=`cat /run/systemd/resolve/resolv.conf  |grep nameserver   | awk '{print $2}' |sort -u`
        if [[ "$config_dns" == "" ]];then
            config_dns=$DEFAULT_DNS
        fi

        cat >>$config_file<<EOF
            nameservers:
                addresses:
EOF

        for dns in $config_dns;
        do
            echo "                - $dns" >>$config_file
        done

    fi

}


#######################
### Support 20.04 22.04 18.04
#######################
SetIpByUbuntu(){

    nic_config_file='/etc/netplan/50-cloud-init.yaml'

    if [ -f $nic_config_file ];then
        mv $nic_config_file ${nic_config_file}.org
    fi

    ##init default config
    setLog "start set ip by ${ID} ${VERSION_ID} ..."

    which netplan 2>&1 > /dev/null 
    if [ $? -ne 0 ];then
        apt install -y netplan

        if [ $? -ne 0 ];then
            setLog "Install package [netplan] fail."
            exit 1
        fi
    fi

    cat <<EOF > $nic_config_file
network:
    version: 2
    ethernets:

EOF


    for nic in $(ls /sys/class/net/ |grep -v 'lo');
    do
        config_ips=`getInterfaceIP $nic`

        if [[ $nic == "$PUBLC_INTERFACE" ]];then
            config_ips=`cat $IP_FILE`
        fi

        configNetworkConfig_ubuntu $nic ${config_ips} $nic_config_file

    done

    netplan apply
    if [ $? -eq 0 ];then
        CallBack
    fi

}

#######################
### Support 10 / 11 /12
#######################
SetIpByDebian(){

    config_file="/etc/network/interfaces.d/50-cloud-init"

    for ip in `cat $IP_FILE | sed 's/,/ /g'`;
    do
        if [[ "$ip" != "$PUBLC_INTERFACE_IP" ]]; then
            cat >>$config_file<<EOF
iface $PUBLC_INTERFACE inet static
address $ip
EOF
        fi
    done

    
    systemctl restart networking 

    if [ $? -ne 0 ];then
        setLog "Restart Service networking fail"
        exit 1
    else
        CallBack
    fi
}

###############Start

PUBLC_INTERFACE=`ip route |grep 'default via' | awk '{print $5}'`
DEFAULT_ROUTE=`ip route |grep 'default via' | awk '{print $3}'`

PUBLC_INTERFACE_IP=`getInterfaceIP $PUBLC_INTERFACE`


if [ ! -f $IP_FILE  ];then
    setLog "Cannot find ip list file [$IP_FILE]"
    exit 1
fi

if [ -f "$os_release_file" ]; then  
    source "$os_release_file"  
  
    if [[ "$ID" == "centos" ]]; then 
	    SetIpByCentOS
    elif [[ "$ID" == "debian" ]]; then
        SetIpByDebian
    elif [[ "$ID" == "ubuntu" ]]; then

        if [[ "$VERSION_ID" == "20.04" ]] || [[ "$VERSION_ID" == "22.04" ]] || [[ "$VERSION_ID" == "18.04" ]]; then
            SetIpByUbuntu
        else
            setLog "The OS does not support [$ID ,$VERSION_ID]" 
            exit 1
        fi
    else  
        setLog "The OS does not support [$ID ,$VERSION_ID]" 
        exit 1	
    fi  
else  
    setLog "Can't find system version "
    exit 1
fi


