#!/bin/bash
os_release_file="/etc/os-release"  
OS="" 
HOSTNAME=`hostname`
COUNT=`ip a | grep eth1 | wc -l`
#设置ip存在的文件
IP_FILE="/opt/ip.list"

LOG_FILE="/var/log/set_ip.log"
PUBLC_INTERFACE=`ip route |grep 'default via' | awk '{print $5}'`
DEFAULT_ROUTE=`ip route |grep 'default via' | awk '{print $3}'`

DEFAULT_DNS="8.8.8.8"

setLog(){

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

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

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

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

	

}


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
}

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}'`
        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
#######################
SetIpByUbuntu(){
    myIP=`getInterfaceIP $PUBLC_INTERFACE`

    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} ..."

    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

}
#SetIpByDebian(){
#}
CallBack(){
    setLog "IP setting completed"

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

	curl -X POST -u ows:iIfxa1uACIXYz  -d "hostname=`hostname`&uuid=`ls /var/lib/cloud/instances`&timestamp=`date -R`&ip=${COUNT}" https://registry.ows-docker.cloud/lecs
    
    rm $IP_FILE
}


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" == "ubuntu" ]]; then

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


