#!/bin/bash
init_shell_script=$1
ip_last=$2
project_name=$3
is_reload_config=$4
project_type=$5
set timeout 1200
ip=192.168.4.$ip_last
echo "参数为：
init_shell_script: ${init_shell_script}
ip : ${ip}
project_name : ${project_name}
is_reload_config : ${is_reload_config}
project_type : ${project_type}
"
#初始化脚本环境
if  $init_shell_script ;then
	expect -c "
		 set timeout 10
	   spawn ssh root@${ip}
	   expect \"]#\"
	   send \"rm -rf /home/qa-deploy-utils/qa_shell_script\r\"
		 expect \"]#\"
		 send \"cd /home\r\"
		 expect \"]#\"
	   send \"git clone http://shengcong.gan:12345678@gitabc.xyqb.com/QA/qa-deploy-utils.git\r\"
	   expect \"]#\"
	   send \"exit\r\"
	expect eof;"
fi

if [ -n "$project_name" ]; then
	expect -c "
		 set timeout 10
	   spawn ssh root@${ip}
		 expect \"]#\"
		 send \"cd /home/qa-deploy-utils/qa_shell_script\r\"
		 expect \"]#\"
		 send \"sh 1_mount_config_files.sh\r\"
		 set timeout -1
	   expect \"]#\"
		 send \"sh 2_get_code_by_name.sh $project_name $branch_name\r\"
		 set timeout -1
	   expect \"]#\"
		 send \"sh 3_link_config_file_by_name.sh $project_name\r\"
		 set timeout -1
	   expect \"]#\"
		 send \"sh 4_build_by_name.sh $project_name\r\"
		 set timeout -1
	   expect \"]#\"
		 send \"sh 5_restart_by_name.sh $project_name\r\"
		 set timeout -1
	   expect \"]#\"
	   send \"exit\r\"
	expect eof;"
else
	expect -c "
		 set timeout 10
	   spawn ssh root@${ip}
		 expect \"]#\"
		 send \"cd /home/qa-deploy-utils/qa_shell_script\r\"
		 expect \"]#\"
		 send \"sh 1_mount_config_files.sh\r\"
		 set timeout -1
	   expect \"]#\"
		 send \"sh 2_get_code_by_type.sh $project_type $branch_name\r\"
		 set timeout -1
	   expect \"]#\"
		 send \"sh 3_link_config_file_by_type.sh $project_type\r\"
		 set timeout -1
	   expect \"]#\"
		 send \"sh 4_build_by_type.sh $project_type\r\"
		 set timeout -1
	   expect \"]#\"
		 send \"sh 5_restart_by_type.sh $project_type\r\"
		 set timeout -1
	   expect \"]#\"
	   send \"exit\r\"
	expect eof;"
fi

