使用expect脚本语言写一键发布服务(代码发布、所有服务重启)

互联网服务有很多台服务,但是在上线的时候需要将这些服务版本都更新与个个都重启,下面的脚本语言,就是一键发布服务~

1.在/home/weihu/deploy/ 目录下建下publish 、publish下建下bak、bin、cfg、rel文件夹(其中bak为备份、bin为自己写的脚本语言目录、cfg为生产环境的配置文件、rel每次版本发布)

2.安装expect脚本 查看是否已经安装 whereis expect

3.工程版本中,生产配置文件往往是很少变的,但是自己打的版本可能是在开发环境可能和生产环境配置不同。这样你整理下生产环境中不能改变的配置文件放在配置文件中

具体的脚本代码

start_all_XX_service.sh

#!/bin/bash
/home/weihu/bin/startWeblogic_9011.sh &&
/home/weihu/bin/startWeblogic_9012.sh &&
/home/weihu/bin/startWeblogic_9013.sh &&
/home/weihu/bin/startWeblogic_9014.sh && 
/home/weihu/bin/startWeblogic_9015.sh

stop_all_XX_service.sh

#!/bin/bash

/home/weihu/bin/stopWeblogic_9011.sh &&
/home/weihu/bin/stopWeblogic_9012.sh &&
/home/weihu/bin/stopWeblogic_9013.sh &&
/home/weihu/bin/stopWeblogic_9014.sh &&
/home/weihu/bin/stopWeblogic_9015.sh

 

一键发布脚本

rdr.sh

#!/bin/bash
/home/weihu/bin/startWeblogic_9011.sh &&
/home/weihu/bin/startWeblogic_9012.sh &&
/home/weihu/bin/startWeblogic_9013.sh &&
/home/weihu/bin/startWeblogic_9014.sh && 
/home/weihu/bin/startWeblogic_9015.sh
[jizhiping@ececp6 bin]$ more stop_all_openapi_service.sh
#!/bin/bash
/home/weihu/bin/stopWeblogic_9011.sh &&
/home/weihu/bin/stopWeblogic_9012.sh &&
/home/weihu/bin/stopWeblogic_9013.sh &&
/home/weihu/bin/stopWeblogic_9014.sh &&
/home/weihu/bin/stopWeblogic_9015.sh
[jizhiping@ececp6 bin]$ more rdr.sh
#!/bin/sh
echo "--------------------begin delete the version and  unzip the basic of openapi.zip --------------------"
mv /home/weihu/deploy/publish/rel/openapi  /home/weihu/deploy/publish/bak/openapi_`date +%Y%m%d%H%M%S`
unzip /home/weihu/deploy/publish/rel/openapi.zip -d /home/weihu/deploy/publish/rel/
echo "--------------------end delete the version and unzip the basic of openapi.zip --------------------"
sleep 1

for i in $(seq 5); do
let port=9010+$i
echo ""
echo "--------------------start deploy openapi to 10.173.23.54:$port--------------------"
expect <<!
spawn ssh -l jizhiping 10.173.23.54 "rm -rf /home/weihu/deploy/deploy$port/openapi"
set timeout 300
expect "*password:"
send "jizhiping_2015\r"
expect eof

 

spawn scp -rpq /home/weihu/deploy/publish/rel/openapi [email protected]:/home/weihu/deploy/deploy$port
set timeout 300
expect "*password:"
send "jizhiping_2015\r"
expect eof


spawn scp -rpq /home/weihu/deploy/publish/cfg/config.properties [email protected]:/home/weihu/deploy/deploy$port/openapi/WEB-INF/classes/
set timeout 300
expect "*password:"
send "jizhiping_2015\r"
expect eof

spawn scp -rpq /home/weihu/deploy/publish/cfg/datasource.properties [email protected]:/home/weihu/deploy/deploy$port/openapi/WEB-INF/classes/
set timeout 300
expect "*password:"
send "jizhiping_2015\r"
expect eof

spawn scp -rpq /home/weihu/deploy/publish/cfg/jtcfg.properties [email protected]:/home/weihu/deploy/deploy$port/openapi/WEB-INF/classes/
set timeout 300
expect "*password:"
send "jizhiping_2015\r"
expect eof

spawn scp -rpq /home/weihu/deploy/publish/cfg/syscfg.properties [email protected]:/home/weihu/deploy/deploy$port/openapi/WEB-INF/classes/
set timeout 300
expect "*password:"
send "jizhiping_2015\r"
expect eof


spawn scp -rpq /home/weihu/deploy/publish/cfg/applicationContext-dubbo.xml [email protected]:/home/weihu/deploy/deploy$port/openapi/WEB-INF/classes/app
lication/base/
set timeout 300
expect "*password:"
send "jizhiping_2015\r"
expect eof

!
done;

sleep 3

echo ""
echo "--------------------start unzip the basic of openapi.zip to 55--------------------"

for i in $(seq 5); do
let port=9010+$i
echo ""
echo "--------------------start deploy openapi to 10.173.23.55:$port--------------------"
expect <<!
spawn ssh -l jizhiping 10.173.23.55 "rm -rf /home/weihu/deploy/deploy$port/openapi"
set timeout 300
expect "*password:"
send "jizhiping_2015\r"
expect eof

 

spawn scp -rpq /home/weihu/deploy/publish/rel/openapi [email protected]:/home/weihu/deploy/deploy$port
set timeout 300
expect "*password:"
send "jizhiping_2015\r"
expect eof


spawn scp -rpq /home/weihu/deploy/publish/cfg/config.properties [email protected]:/home/weihu/deploy/deploy$port/openapi/WEB-INF/classes/
set timeout 300
expect "*password:"
send "jizhiping_2015\r"
expect eof

spawn scp -rpq /home/weihu/deploy/publish/cfg/datasource.properties [email protected]:/home/weihu/deploy/deploy$port/openapi/WEB-INF/classes/
set timeout 300
expect "*password:"
send "jizhiping_2015\r"
expect eof

spawn scp -rpq /home/weihu/deploy/publish/cfg/jtcfg.properties [email protected]:/home/weihu/deploy/deploy$port/openapi/WEB-INF/classes/
set timeout 300
expect "*password:"
send "jizhiping_2015\r"
expect eof

spawn scp -rpq /home/weihu/deploy/publish/cfg/syscfg.properties [email protected]:/home/weihu/deploy/deploy$port/openapi/WEB-INF/classes/
set timeout 300
expect "*password:"
send "jizhiping_2015\r"
expect eof


spawn scp -rpq /home/weihu/deploy/publish/cfg/applicationContext-dubbo.xml [email protected]:/home/weihu/deploy/deploy$port/openapi/WEB-INF/classes/app
lication/base/
set timeout 300
expect "*password:"
send "jizhiping_2015\r"
expect eof

!
done;

sleep 3

echo ""
echo "--------------------begin restart openapi server from 10.173.23.54--------------------"
expect <<!
spawn ssh -l jizhiping 10.173.23.54 "/home/weihu/deploy/publish/bin/restart_all_openapi_service.sh"
set timeout 300
expect "*password:"
send "jizhiping_2015\r"
expect eof
!

sleep 3

echo ""
echo "--------------------begin restart openapi server from 10.173.23.55--------------------"
expect <<!
spawn ssh -l jizhiping 10.173.23.55 "/home/weihu/deploy/publish/bin/restart_all_openapi_service.sh"
set timeout 300
expect "*password:"
send "jizhiping_2015\r"
expect eof
!

echo "--------------------deploy and restart for 54 and 55 is successful--------------------"

 

你可能感兴趣的:(expect)