先簡單描述一下環境,Jenkins部署在私有云,從git拉取代碼並編譯後會調用腳本將jar包部署到阿里雲的應用服務器。
阿里雲應用服務器無公網IP,經過跳板機登陸。就產生了連續調用腳本,穿透跳板機部署到阿里雲內網的需求。java
服務器信息: 主機名稱 |
IP |
---|---|
git server | 10.150.27.51 |
jenkins server | 10.150.27.52 |
jump server | 1.1.1.1/192.168.2.20 |
app server1 | 192.168.2.21 |
app server1 | 192.168.2.22 |
拓撲圖以下:
node
主要實現功能:git
[root@bobo365 prod]# more backend-earphone-8888.sh shell
#!/bin/bash . /etc/init.d/functions node="1.1.1.1" current="/home/bobo365/scripts/prod" dest_user='remote' dest_passwd='xxxxxxx' package_name="earphone-1.08888.jar" source_dir="${JENKINS_HOME}/jobs/${PROMOTED_JOB_NAME}/builds/${PROMOTED_NUMBER}/archive" source_file="${PROMOTED_JOB_NAME}/builds/${PROMOTED_NUMBER}/archive/$package_name" dest_temp="/data/deploy/pccode/earphone" back_dir="/data/backup" shell_dir="/data/deploy/script" shell="earphone.sh" back_prog="backend-earphone" back_time=`date +"%Y-%m-%d-%s"` NOW=`date +"%Y/%m/%d %T"` echo "Clean the dir." sshpass -p $dest_passwd ssh $dest_user@$node -p 22 -o StrictHostKeychecking=no "rm -rf '$dest_temp'/*" if [ $? = 0 ] then echo $node $NOW clean the dir success | tee -a $current/logs/backend-earphone.log else echo $node $NOW clean the dir failed | tee -a $current/logs/backend-earphone.log echo >> $current/backend-earphone.log exit 1 fi echo "Scp the file." sshpass -p $dest_passwd scp -P22 -o StrictHostKeyChecking=no $source_dir/$package_name $dest_user@$node:$dest_temp/$package_name if [ $? = 0 ] then echo $node $NOW scp code success | tee -a $current/logs/backend-earphone.log else echo $node $NOW scp code failed | tee -a $current/logs/backend-earphone.log echo >> $current/backend-earphone.log exit 1 fi echo "Deploy the file." sshpass -p $dest_passwd ssh $dest_user@$node -p 22 -o StrictHostKeychecking=no "/bin/bash $shell_dir/$shell" if [ $? = 0 ] then echo $node $NOW deploy code success | tee -a $current/logs/backend-earphone.log else echo $node $NOW deploy code failed | tee -a $current/logs/backend-earphone.log echo >> $current/backend-earphone.log exit 1 fi NOW=`date +"%Y/%m/%d %T"` echo -e "$NOW: $prog complete of deploy ."
主要實現功能:bash
[promote] 192.168.2.21 192.168.2.22
[root@JUMP script]# more earphone.sh 服務器
#!/bin/bash current="/data/deploy/script" source_dir="/data/deploy/pccode/earphone" package_name="earphone-1.08888.jar" ansible promote -m copy -a "src=$source_dir/$package_name dest=/root" sleep 10 ansible promote -a "/bin/bash /root/restart-headset.sh"
主要實現功能:網絡
[root@app ~]# more restart-headset.sh app
#!/bin/bash package_name="earphone-1.08888.jar" PID=`ps -ef|grep 8888|grep -v grep|awk -F " " '{print $2}'` kill -9 $PID nohup /usr/local/jdk1.8.0_60/bin/java -jar $package_name &