setup.sh 6.29 KB
# 文件名:	setup.sh
# 作者 	: 	石晓旭
# 功能  :	对freeswitch服务器进行自动部署。
# 流程 	:	1、获取配置
#		2、安装nfs 客户端。录音存储使用。
#		3、安装tserver
#		4、安装ucswitch
#		5、优化ucswitch
#		6、修改主机名
#		7、修改hosts
#/bin/bash

################################ 公用变量 ##################################
_SERVER_FAMILY=7  # 7 8 75
_SERVER_HOST=`/sbin/ifconfig | grep 'inet addr' | grep -v '127.0.0.1' | awk -F ' ' {'print $2'} | awk -F ':' {'print $2'}`

read  -p "Please select the server family: [$_SERVER_FAMILY] " SERVER_FAMILY
if ! echo $SERVER_FAMILY | egrep -q '^[0-9]+$' ; then
        echo "Selecting default: $_SERVER_FAMILY"
        SERVER_FAMILY=$_SERVER_FAMILY
fi

read  -p "Please select the server host: [$_SERVER_HOST] " SERVER_HOST
echo $SERVER_HOST
if ! echo $SERVER_HOST | egrep -q "." ; then
        echo "Selecting default: $_SERVER_HOST"
        SERVER_HOST=$_SERVER_HOST
fi

export autoDirectory=`pwd`
export ucswitchDirectory="${autoDirectory}/ucswitch"

setupConfig="${autoDirectory}/conf/config${SERVER_FAMILY}.ini"
nfsMod="ucswitchNFS"
recordMod="ucswitchRecord"
dependsMod="ucswitchDepends"
gitMod="ucswitchGit"
coreMod="ucswitchCore"
tserverMod="TServer"

################################ 公用接口 ##################################
iniTmpFieldValue="nil"

function iniGetFieldValue()  
{
	if [ ! -f $1 ] || [ $# -ne 3 ];then  
		echo "[`date`] [ERROR] iniGetFieldValue Param." 
        	return 1 
    	fi
	nRet=1	
	bFindBlockBegin=0
	bFindBlockEnd=0
#	cat $1 | while read line
	while read line
	do
		#判断块开始
		if [ "[$2]" == "${line}" ];then
			bFindBlockBegin=1
			continue
		fi
		if [ ${bFindBlockBegin} -eq 1 ];then
			#判断块结束,块结束后退出。
			if [[ "${line}" =~ "[" ]] && [[ "${line}" =~ "]" ]];then
				bFindBlockEnd=1
				break
			fi
			#块内获取值
			if [ ${bFindBlockBegin} -eq 1 ] && [ ${bFindBlockEnd} -eq 0 ];then
				field=$(echo $line | awk -F= '{gsub(" |\t","",$1); print $1}')  
            			value=$(echo ${line#*=})
            			#value=$(echo $line | awk -F= '{gsub(" |\t","",$2); print $2}')
                               	if [ "${field}" == "$3" ];then
                                       iniTmpFieldValue=${value}
                                       return 0 
                               	fi
			fi
		fi
	done <<< "$(cat $1)"
#	done 
	echo "[`date`] [ERROR] iniGetFieldValue $1 $2 $3 not exist."
	return 1
}   
################################ 配置变量 ##################################
echo "[`date`] [DEBUG] setup.sh start..."
export ucswitchName=${SERVER_HOST}
export ucswitchIp=${SERVER_HOST}
export jarFsIp=${SERVER_HOST}
export jarFsConsumer="freeswitch='${SERVER_HOST}'"
export hostName="CC${SERVER_FAMILY}-PROD-FS${SERVER_HOST##*.}"

# NFS # 
iniGetFieldValue ${setupConfig} ${nfsMod} "nfsServer"
if [ $? -ne 0 ];then
	exit 1
fi
export nfsServer=${iniTmpFieldValue}
# depends #
iniGetFieldValue ${setupConfig} ${dependsMod} "DependUrl"
if [ $? -ne 0 ];then
	exit 1
fi
export dependsUrl=${iniTmpFieldValue}
# git #
iniGetFieldValue ${setupConfig} ${gitMod} "gitUser"
if [ $? -ne 0 ];then
	exit 1
fi
export gitUser=${iniTmpFieldValue}
iniGetFieldValue ${setupConfig} ${gitMod} "gitPassword"
if [ $? -ne 0 ];then
	exit 1
fi
export gitPassword=${iniTmpFieldValue}
iniGetFieldValue ${setupConfig} ${gitMod} "gitUrl"
if [ $? -ne 0 ];then
	exit 1
fi
export gitUrl=${iniTmpFieldValue}
iniGetFieldValue ${setupConfig} ${gitMod} "gitBranch"
if [ $? -ne 0 ];then
	exit 1
fi
export gitBranch=${iniTmpFieldValue}
#tserver
iniGetFieldValue ${setupConfig} ${tserverMod} "jarPath"
if [ $? -ne 0 ];then
        exit 1
fi
export jarPath=${iniTmpFieldValue}
iniGetFieldValue ${setupConfig} ${tserverMod} "jarName"
if [ $? -ne 0 ];then
        exit 1
fi
export jarName=${iniTmpFieldValue}


echo "[`date`] [DEBUG] setup.sh nfsServer $nfsServer"
echo "[`date`] [DEBUG] setup.sh jarPath $jarPath"
echo "[`date`] [DEBUG] setup.sh jarName $jarName"
echo "[`date`] [DEBUG] setup.sh jarFsIp $jarFsIp"
echo "[`date`] [DEBUG] setup.sh jarFsConsumer $jarFsConsumer"

echo "[`date`] [DEBUG] setup.sh ucswitchName $ucswitchName"
echo "[`date`] [DEBUG] setup.sh ucswitchIp $ucswitchIp"

echo "[`date`] [DEBUG] setup.sh hostName  $hostName"
echo "[`date`] [DEBUG] setup.sh dependsUrl $dependsUrl"


echo "[`date`] [DEBUG] setup.sh gitUrl $gitUrl"
echo "[`date`] [DEBUG] setup.sh gitBranch $gitBranch"
echo "[`date`] [DEBUG] setup.sh gitUser $gitUser"
echo "[`date`] [DEBUG] setup.sh gitPassword $gitPassword"

export fsSoundsUrl="${dependsUrl}/${SERVER_FAMILY}/config/sounds.tar.gz"
export fsConfUrl="${dependsUrl}/${SERVER_FAMILY}/config/conf.tar.gz"
export fsScriptsUrl="${dependsUrl}/${SERVER_FAMILY}/config/scripts.tar.gz"
export fsSbinUrl="${dependsUrl}/${SERVER_FAMILY}/config/sbin.tar.gz"
export tServerUrl="${dependsUrl}/${SERVER_FAMILY}/tserver/cti.tar.gz"
################################ 安装流程 ##################################
yum install -y wget
${autoDirectory}/nfsSetup.sh
if [ $? -eq 0 ];then
	echo "[`date`] [DEBUG] setup.sh nfsClient install and config success"
else
	echo "[`date`] [ERROR] setup.sh Failed to install nfsClient or config"
	exit 1
fi

${autoDirectory}/ucswitchSetup.sh
if [ $? -eq 0 ];then
	echo "[`date`] [DEBUG] setup.sh ucswitch install and config success"
else
	echo "[`date`] [ERROR] setup.sh Failed to install ucswitch or config"
	exit 1
fi

/usr/bin/crontab -u root "${autoDirectory}/conf/cron.conf"
if [ $? -eq 0 ];then
        echo "[`date`] [DEBUG] ucswitchSetup.sh config crontab  success" >> ${ucswitchSetupLogFile}
else
        echo "[`date`] [ERROR] ucswitchSetup.sh Failed to config crontab" >> ${ucswitchSetupLogFile}
        exit 1
fi
${autoDirectory}/tserverSetup.sh
if [ $? -eq 0 ];then
        echo "[`date`] [DEBUG] setup.sh tserverSetup success "
else
        echo "[`date`] [ERROR] setup.sh Failed to tserverSetup "
        exit 1
fi
#修改主机名
sed -i "s%HOSTNAME=.*%HOSTNAME=$hostName%g" /etc/sysconfig/network
if [ $? -eq 0 ];then
        echo "[`date`] [DEBUG] setup.sh set hostname success "
else
        echo "[`date`] [ERROR] setup.sh Failed to set hostname "
        exit 1
fi
echo "127.0.0.1   $hostName" > /etc/hosts
echo "::1         $hostName" >> /etc/hosts
/sbin/reboot
################################ 安装结束 ##################################
echo "[`date`] [DEBUG] setup.sh finished... "