针对接口api版本更新,自动下载、打包、备份、上传、重启全量更新
第一步:点击选择需要部署的城市,会自动生成部署的项目信息,确认IP和项目路径
第二步:点击构建

jobs应用场景

公司代码库利用mvn run build打包后传送接口文件夹到目标服务器,因为公司项目问题遗留都部署一个文件夹,只能通过这种方式去部署。

application.properties   fdfs_client.conf  log4j.properties这三个文件在打包后需要删除,保留原先部署服务器的文件

首先打完包后把这三个文件删除,备份部署服务器的源文件到/data/backup目录下,传送新版本文件夹到目标服务器,再从/data/backup下把上面3个文件在传回到部署服务器中

 

 

 上面引入变量后直接就进入shell下用ansible进行文件更新

#!/bin/bash

echo -e "\033[34m#####判断是否正确输入数据#####\033[0m"
if [ -z ${source_file} ] && [ -z ${destination_file} ];then
echo -e "\033[31m未输入参数,请重新输入\033[0m"
exit 1
else
echo -e "\033[34m输入内容正确\033[0m"


echo -e "\033[34m######判断IP是否输入正确#####\033[0m"
cat /etc/ansible/hosts | grep -v ^# | awk  '{print $1}' | grep ${destination_ip}
if [ $? -eq 0 ]
   then
       echo -e "\033[34m此IP在hosts列表,可以执行soft安装\033[0m"
   else
       echo -e "\033[31m确定IP,先添加到hosts文件\033[0m"
       exit 1
fi
echo -e "\033[34m#######打印输入#####\033[0m"
echo -e "\033[34m源路径:${source_file}\033[0m"
echo -e "\033[34m目标路径:${destination_file}\033[0m"
echo -e "\033[34m目标IP:${destination_ip}\033[0m"


if [ $GIT_PREVIOUS_SUCCESSFUL_COMMIT == $GIT_COMMIT ] 
then
echo -e "版本相同无需打包"
else
echo -e "\033[34m#####mvn打包#####\033[0m"
echo -e "\033[34m开始打包\033[0m"
echo $localpacket
mvn  clean  -Dfile=$localpacket  package
result=$?
[ ${result} -eq 0 ] && echo -e "\033[34mmaven打包成功\033[0m" || {
echo -e "\033[31mmaven打包失败\033[0m"
exit ${result}
}
echo -e "\033[34m#####删除文件#####\033[0m"
\rm target/l-1.0-SNAPSHOT/WEB-INF/classes/application.properties
 [ $? -eq 0 ] && echo -e "\033[34m application.properties删除成功\033[0m" || { 
            echo -e "\033[31mapplication.properties删除失败\033[0m"
            exit 1
        }
\rm target/l-1.0-SNAPSHOT/WEB-INF/classes/fdfs_client.conf
 [ $? -eq 0 ] && echo -e "\033[34m fdfs_client删除成功\033[0m" || { 
            echo -e "\033[31mfdfs_client删除失败\033[0m"
            exit 1
        }
\rm target/l-1.0-SNAPSHOT/WEB-INF/classes/generatorConfig.xml
 [ $? -eq 0 ] && echo -e "\033[34m generatorConfig删除成功\033[0m" || { 
            echo -e "\033[31mgeneratorConfig删除失败\033[0m"
            exit 1
        }
\rm target/l-1.0-SNAPSHOT/WEB-INF/classes/log4j.properties
 [ $? -eq 0 ] && echo -e "\033[34m log4j.properties删除成功\033[0m" || { 
            echo -e "\033[31mlog4j.properties删除失败\033[0m"
            exit 1
        }
fi

#####利用ansible进行文件拷贝


echo -e "\033[34m#####目标文件备份#####\033[0m"
echo ${destination_file} > /tmp/name
backup_name=`cat /tmp/name | awk -F "/" '{print $5}'`
echo -e "\033[34m备份目录:/data/backup/$backup_name\033[0m"

echo -e "\033[34m#判断备份目录是否存在,不存在重新创建\033[0m"
ansible ${destination_ip} -m file -a 'path=/data/backup/{{ backup_name }}  state=directory'  -e  backup_name=${backup_name}
echo -e "\033[34m/data/backup/$backup_name备份目录创建完成\033[0m"


echo -e "\033[34m###开始备份###\033[0m"
date=`date "+%Y-%m-%d-%H-%M-%S"`
back_file=${destination_file##*/}
ansible "$destination_ip" -m command -a '\mv  "{{ destination_file }}" "/data/backup/{{ backup_name }}/{{ back_file }}".bak"{{ date }}"' -e back_file=${back_file} -e backup_name=${backup_name} -e destination_file=${destination_file} -e date=$date
 [ $? -eq 0 ] && echo -e "\033[34m ${destination_file}备份完成\033[0m" || { 
            echo -e "\033[31m${destination_file}备份失败\033[0m"
            exit 1
        }
        
              
echo -e "\033[34m###开始同步###  \033[0m"      
/usr/bin/ansible-playbook /etc/ansible/transfer-api.yml  -e source_file=$WORKSPACE/${source_file} -e destination_ip=${destination_ip} -e destination_file=${destination_file}
result=$?
[ ${result} -eq 0 ] && echo -e "\033[34m${destination_file}同步完成!\033[0m" || {
exit $result
echo -e "\033[31m${destination_file}同步失败!\033[0m"
}




application_properties="/data/backup/${backup_name}/${back_file}.bak$date/classes/application.properties"
fdfs_client_conf="/data/backup/${backup_name}/${back_file}.bak$date/classes/fdfs_client.conf"
log4j_properties="/data/backup/${backup_name}/${back_file}.bak$date/classes/log4j.properties"

ansible "$destination_ip" -m command -a '\cp "{{ application_properties }}" "{{ destination_file }}"' -e application_properties=${application_properties} -e destination_file=${destination_file}/classes/
ansible "$destination_ip" -m command -a '\cp "{{ fdfs_client_conf }}" "{{ destination_file }}"' -e fdfs_client_conf=${fdfs_client_conf} -e destination_file=${destination_file}/classes/
ansible "$destination_ip" -m command -a '\cp "{{ log4j_properties }}" "{{ destination_file }}"' -e log4j_properties=${log4j_properties} -e destination_file=${destination_file}/classes/

echo -e "\033[34m###重启tomcat###  \033[0m"  
tomcat_restart=${backup_name}
ansible "$destination_ip" -m shell -a 'nohup /usr/bin/bash /usr/local/web/{{ tomcat_restart }}/bin/shutdown.sh & ' -e tomcat_restart=${tomcat_restart}
sleep 5
ansible "$destination_ip" -m shell -a 'nohup /usr/bin/bash /usr/local/web/{{ tomcat_restart }}/bin/startup.sh &' -e tomcat_restart=${tomcat_restart}
 [ $? -eq 0 ] && echo -e "\033[34m tomcat重启完成\033[0m" || { 
           echo -e "\033[31mtomcat重启失败\033[0m"
           exit 1
        }


fi        

[root@srv ansible]# vim transfer-api.yml 

- hosts: 1.1.1.1
  remote_user: root
  tasks:
    - name: transfer sourcefile to destinationfile
      synchronize:
        src: "{{ source_file }}"
        dest: "{{ destination_file }}"
        mode: pull
      delegate_to: "{{ destination_ip }}"

Logo

权威|前沿|技术|干货|国内首个API全生命周期开发者社区

更多推荐