#!/bin/bash

_install() {
    local dns_server_conf=""
    local dns_servers=`/usr/bin/python $BASEDIR/utils/GetApolloConf.py regioninfo $REGION.dns`
    for host in $dns_servers;
    do
      local dns_server_conf=$dns_server_conf"nameserver $host\n"
    done
    if [[ -z $dns_server_conf ]];then
        PRINT_ERR "[ERROR] DNS Server Not Found In http://uconf.ucloudadmin.com"
        exit 1
    fi
    echo -e $dns_server_conf > /etc/resolv.conf
	local DNS1=`cat /etc/resolv.conf |grep -E "172|192|10"|head -1|awk '{print $2}'`
    if [[ -n $DNS1 ]];then
    	ping -c 2 -w 5 $DNS1 >/dev/null 2>&1
    	if [[ $? = 0 ]];then
    		sed -i '/softlib.service.ucloud.cn/d' /etc/hosts
    		sed -i '/yum.service.ucloud.cn/d' /etc/hosts
    	fi
    else
        PRINT_ERR "[ERROR] DNS Server Not Found In /etc/resolv.conf."
        exit 1
    fi
}

_check() {
	_check="PASS"

	if [ ! -s "/etc/resolv.conf" ];then
		_check="FAIL"
		PRINT_ERR "[ERROR] File /etc/resolv.conf Is Empty."
	fi
    local dns_servers=`/usr/bin/python $BASEDIR/utils/GetApolloConf.py regioninfo $REGION.dns`
    for host in $dns_servers;
    do
        grep -q $host /etc/resolv.conf
        if [[ $? -ne 0 ]];then
		    _check="FAIL"
		    PRINT_ERR "[ERROR] nameserver $host not in File /etc/resolv.conf"
        fi
    done
}
