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

################################ 公用变量 ##################################
export autoDirectory=`pwd`
export ucswitchDirectory="${autoDirectory}/ucswitch"

setupConfig="config.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..."
case $1 in
7)
	setupConfig="${autoDirectory}/conf/config7.ini"
;; 
8)
	setupConfig="${autoDirectory}/conf/config8.ini"
;;
75)
	setupConfig="${autoDirectory}/conf/config75.ini"
;;
*)
	echo "./setup.sh [7|8|75]"
	exit 1
;;
esac
echo "config file $setupConfig"
# 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}
iniGetFieldValue ${setupConfig} ${coreMod} "ucswitchName"
if [ $? -ne 0 ];then
	exit 1
fi
export ucswitchName=${iniTmpFieldValue}
iniGetFieldValue ${setupConfig} ${coreMod} "ucswitchIp"
if [ $? -ne 0 ];then
	exit 1
fi
export ucswitchIp=${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}
iniGetFieldValue ${setupConfig} ${tserverMod} "freeswitch.ip"
if [ $? -ne 0 ];then
        exit 1
fi
export jarFsIp=${iniTmpFieldValue}
iniGetFieldValue ${setupConfig} ${tserverMod} "jingle.cc.fs.consumer"
if [ $? -ne 0 ];then
        exit 1
fi
export jarFsConsumer=${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 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}/$1/config/sounds.tar.gz"
export fsConfUrl="${dependsUrl}/$1/config/conf.tar.gz"
export fsScriptsUrl="${dependsUrl}/$1/config/scripts.tar.gz"
export fsSbinUrl="${dependsUrl}/$1/config/sbin.tar.gz"
export tServerUrl="${dependsUrl}/$1/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
################################ 安装结束 ##################################
echo "[`date`] [DEBUG] setup.sh finished... "