赞
踩
先简单描述一下环境,Jenkins部署在私有云,从git拉取代码并编译后会调用脚本将jar包部署到阿里云的应用服务器。
阿里云应用服务器无公网IP,通过跳板机登录。就产生了连续调用脚本,穿透跳板机部署到阿里云内网的需求。
服务器信息: 主机名称 | 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 |
拓扑图如下:
主要实现功能:
[root@bobo365 prod]# more backend-earphone-8888.sh
- #!/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 ."
主要实现功能:
- [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
- #!/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 &
转载于:https://blog.51cto.com/bobo365/2129809
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。