设为首页 收藏本站
查看: 1370|回复: 0

[经验分享] 转 Apache Ant 实现自动化部署

[复制链接]

尚未签到

发表于 2017-7-10 07:00:15 | 显示全部楼层 |阅读模式
  Apache Ant 实现自动化部署   






  Apache Ant 实现自动化部署







http://www.netkiller.cn/journal/java.ant.html





Mr. Neo Chen (陈景峯), netkiller, BG7NYT




  





  版权声明
  转载请与作者联系,转载时请务必标明文章原始出处和作者信息及本声明。


DSC0000.png


文档出处:
http://netkiller.github.io
http://netkiller.sourceforge.net


DSC0001.jpg

  微信扫描二维码进入 Netkiller 微信订阅号
  群:128659835 请注明“读者”



  2015-12-10






这篇文章帮你解决下列问题:  源码获取,源码编译,处理配置文件,应用部署,远程备份,部署回撤,启动,服务器状态,停止






我的系列文档  编程语言

Netkiller Architect 手札Netkiller Developer 手札Netkiller PHP 手札Netkiller Python 手札Netkiller Testing 手札Netkiller Cryptography 手札
Netkiller Perl 手札Netkiller Docbook 手札Netkiller Project 手札Netkiller Java 手札Netkiller DevOps 手札
  操作系统

Netkiller Linux 手札Netkiller Debian 手札Netkiller CentOS 手札Netkiller FreeBSD 手札Netkiller Shell 手札Netkiller Security 手札
Netkiller Web 手札Netkiller Monitoring 手札Netkiller Storage 手札Netkiller Mail 手札Netkiller Multimedia 手札
  数据库

Netkiller Database 手札Netkiller PostgreSQL 手札Netkiller MySQL 手札Netkiller NoSQL 手札Netkiller LDAP 手札
  网络设备及其他

Netkiller Network 手札Netkiller Cisco IOS 手札Netkiller H3C 手札Netkiller Amateur Radio 手札
  您可以使用iBook阅读当前文档





目录

  • 1. 背景
  • 2. 我们需要什么样的流程
  • 3. 怎样实现自动部署

    • 3.1. 操作系统
    • 3.2. 程序部署
    • 3.3. 自动部署程序


  • 4. Apache Ant 实现自动化部署

    • 4.1. 运行环境
    • 4.2. 部署机


  • 5. 延伸阅读



1. 背景
  在你的企业中是怎样完成从开发,测试到运维的?
  很多企业的升级是这样做的,写完代码后编译打包,放到FTP上,同时发送一个升级邮件。然后让运维按照升级文档,一步一步操作。
  这样的流程有很多问题



  •   开发者通常是在Windows系统上完成开发与编译,而服务器通常是Linux操作系统,操作系统的差异可能导致编译后的程序运行不了。

  •   安全角度,源码可以审查,但编译文件无法审查,打包过程可能被植入恶意代码

  •   经常出现生产环境与本地开发环境不一致,运行有差异

  •   浪费人力,理论上代码写完,就跟开发人员一点关系都没有了,但实际上每次升级过程开发与测试都需要在场

  稍先进一点做法是使用Subversion/Git,开发将代码放到版本库中,运维直接使用 svn up / git pull 升级,这样做法也有很多问题存在



  • 首次升级非常慢,svn 还好些,svn只取最后一次提交的版本;git 将所有的版本克隆到本地。
  • 如果修改了本地文件,更新会产生冲突
  • 配置文件无法个性化配置



2. 我们需要什么样的流程
  我们需要什么样的流程或者什么样的流程才是最理想流程?




我认为:

  •   开发人员不要做与开发无关的事情,代码写完就与开发没有半点关系了。通知测试人员,代码已经完成。

  •   测试人员自己部署测试环境,不依赖开发人员,测试完成,通知运维人员可能升级了

  •   运维人员不接受任何部门提供的打包或补丁程序,代码只能在配置管理服务器上完成编译打包以及部署。

  •   升级应该由自动化工具完成,而不是人工操作。

  开发,测试,运维各司其职,这就是DevOps。



3. 怎样实现自动部署
  实现自动化部署有很多方法,很多年前笔者就开始研究总结,下面是一些经验分享。



3.1. 操作系统
  开发,测试,生产三个环境的配置如果出自同一个模板会减少很多由于环境差异带来的困扰。




过程 1. 操作系统部署

  •   无人值守安装
      通过无人值守脚本安装操作系统,减少人为安装造成的差异

  •   运行环境
      统一配置运行环境,开发库以及版本统一

  •   应用服务器统一
      应用服务器版本,安装标准,配置文件都需要统一,减少差异




3.2. 程序部署
  实现应用程序自动部署,首先你要清楚自动部署所需要的流程,部署一个流程通常是这样的:




过程 2. 自动部署步骤

  •   初始化
      建立工作环境,例如目录,检查所需环境

  •   获取
      从版本库指定分支中获取代码并保存到本地

  •   编译
      编译可执行代码

  •   配置
      处理配置文件

  •   备份
      备份应用程序

  •   停止
      服务服务

  •   部署
      部署应用程序到目的主机,如果已存在需要覆盖原来的程序

  •   启动
      启动服务




3.3. 自动部署程序
  自动部署程序完成上面的部署,还需要做下面一些事情。




日志功能

  • 记录什么时间点做过部署
  • 部署了那些文件



4. Apache Ant 实现自动化部署



4.1. 运行环境
  准备一个全新的的服务器,最小化安装CentOS 7操作系统,然后运行下面脚本初始化


curl -s https://raw.githubusercontent.com/oscm/shell/master/os/centos7.sh | bash
curl -s https://raw.githubusercontent.com/oscm/shell/master/os/selinux.sh | bash
curl -s https://raw.githubusercontent.com/oscm/shell/master/os/iptables/iptables.sh | bash
curl -shttps://raw.githubusercontent.com/oscm/shell/master/os/ntpd/ntp.sh |bash
curl -shttps://raw.githubusercontent.com/oscm/shell/master/os/ssh/sshd_config.sh| bash
curl -shttps://raw.githubusercontent.com/oscm/shell/master/os/user/www.sh |bash
curl -shttps://raw.githubusercontent.com/oscm/shell/master/lang/gcc/gcc.sh| bash

  安装 server-jre 与 apache-tomcat


curl -shttps://raw.githubusercontent.com/oscm/shell/master/lang/java/server-jre-8u40.sh| bash
curl -shttps://raw.githubusercontent.com/oscm/shell/master/web/tomcat/apache-tomcat-8.0.26.sh| bash
curl -shttps://raw.githubusercontent.com/oscm/shell/master/web/tomcat/systemctl.sh| bash

  请使用systemctl 启动与停止 Tomcat


systemctl start tomcat
systemctl stop tomcat

  Infrastructure Management Shell https://github.com/oscm/shell



4.2. 部署机
  安装Ant


curl -shttps://raw.githubusercontent.com/oscm/shell/master/lang/java/ant.sh | bash

  下载build.xml文件 https://github.com/oscm/build/tree/master/Ant


wget https://raw.githubusercontent.com/oscm/build/master/Ant/build.xml

  打开 build.xml 文件


<?xml version="1.0" encoding="UTF-8" ?>
<!--
Homepage: http://www.netkiller.cn
Author: neo <netkiller@msn.com>
Date: 2015-12-07
-->
<project name="admin.example.com" default="compile" basedir=".">
<property name="repository" value="git@58.96.11.18:example.com/admin.example.com.git" />
<property name="branch" value="master" />
<property name="remote" value="www@23.25.22.72" />
<property name="destination" value="/www/example.com/admin.example.com" />
<property name="project.dir" value="repository" />
<property name="project.src" value="${project.dir}/src" />
<property name="project.build" value="build" />
<property name="project.config" value="config" />
<property name="project.log" value="log" />
<property name="pkg" value="example-1.0.0.jar" />
<property name="backup.dir" value="backup" />
<property name="receive.timepoint" value="2015-12-04.17:46:35" />
<property name="build.sysclasspath" value="last" />
<property environment="env" />
<echo message="JAVA_HOME is set to = ${env.JAVA_HOME}" />
<echo message="CATALINA_HOME is set to = ${env.CATALINA_HOME}" />
<path id="classpath">
<fileset dir="${env.JAVA_HOME}/lib">
<include name="*.jar" />
</fileset>
<fileset dir="${env.CATALINA_HOME}/lib">
<include name="*.jar" />
</fileset>
<fileset dir="${project.dir}/WebRoot/WEB-INF/lib" includes="*.jar" />
</path>
<macrodef name="git">
<attribute name="command" />
<attribute name="dir" default="" />
<element name="args" optional="true" />
<sequential>
<!-- echo message="git @{command}" / -->
<exec executable="git" dir="@{dir}">
<arg value="@{command}" />
<args />
</exec>
</sequential>
</macrodef>
<macrodef name="rsync">
<attribute name="option" default="auzv" />
<attribute name="src" default="" />
<attribute name="dest" default="" />
<element name="args" optional="true" />
<sequential>
<!-- echo message="rsync @{option} ${src} ${dest}" / -->
<exec executable="rsync">
<arg value="@{option}" />
<args />
<arg value="@{src}" />
<arg value="@{dest}" />
</exec>
</sequential>
</macrodef>
<macrodef name="ssh">
<attribute name="host" />
<attribute name="command" />
<attribute name="keyfile" default="~/.ssh/id_rsa" />
<element name="args" optional="true" />
<sequential>
<exec executable="ssh">
<arg value="@{host}" />
<!-- arg value="-i @{keyfile}" / -->
<args />
<arg value="@{command}" />
</exec>
</sequential>
</macrodef>
<target name="dir.check">
<condition property="dir.exists">
<available file="${project.dir}" type="dir" />
</condition>
</target>
<target name="clone" depends="dir.check" unless="dir.exists">
<echo>clone</echo>
<git command="clone">
<args>
<arg value="${repository}" />
<arg value="${project.dir}" />
</args>
</git>
</target>
<target name="pull" depends="clone" if="dir.exists">
<echo>${project.dir} exists</echo>
<git command="pull" dir="${project.dir}" />
<git command="clean" dir="${project.dir}">
<args>
<arg value="-df" />
</args>
</git>
<git command="reset" dir="${project.dir}">
<args>
<arg value="HEAD" />
<arg value="--hard" />
</args>
</git>
</target>
<target name="branch" depends="pull" if="dir.exists">
<echo>${project.dir} exists</echo>
<git command="checkout" dir="${project.dir}">
<args>
<arg value="-f" />
<arg value="${branch}" />
</args>
</git>
</target>
<target name="init" depends="branch">
<mkdir dir="${project.build}" />
<mkdir dir="${project.log}" />
<copy todir="${project.build}">
<fileset dir="${project.dir}/WebRoot" includes="**/*" />
</copy>
<copy todir="${project.build}/WEB-INF/classes">
<fileset dir="${project.src}">
<include name="**/*.xml" />
<include name="**/*.properties" />
</fileset>
</copy>
</target>
<target name="compile" depends="init">
<javac srcdir="${project.src}" destdir="${project.build}/WEB-INF/classes">
<classpath refid="classpath" />
</javac>
</target>
<target name="config" depends="compile">
<copy todir="${project.build}" overwrite="true">
<fileset dir="${project.config}" includes="**/*" />
</copy>
</target>
<target name="deploy" depends="config">
<tstamp>
<format property="timepoint" pattern="yyyy-MM-dd.HH:mm:ss" locale="cn,CN" />
</tstamp>
<rsync option="-auzv" src="${project.build}/" dest="${remote}:${destination}">
<args>
<arg value="--exclude=.git" />
<arg value="--exclude=.svn" />
<arg value="--exclude=.gitignore" />
<arg value="--backup" />
<arg value="--backup-dir=~/${backup.dir}/${timepoint}" />
<arg value="--log-file=log/${ant.project.name}.${timepoint}.log" />
</args>
</rsync>
</target>
<target name="pkg" depends="compile">
<jar jarfile="${pkg}" basedir="${project.build}" />
</target>
<target name="backup" depends="">
<tstamp>
<format property="TIMEPOINT" pattern="yyyy-MM-dd.HH:mm:ss" locale="cn,CN" />
</tstamp>
<echo>the backup directory is ${TIMEPOINT}.</echo>
<mkdir dir="${backup.dir}/${TIMEPOINT}" />
<rsync option="-auzv" src="${remote}:${destination}" dest="${backup.dir}/${TIMEPOINT}">
</rsync>
</target>
<target name="receive" depends="">
<echo>the receive directory is ${receive.timepoint}.</echo>
<rsync option="-auzv" src="${backup.dir}/${receive.timepoint}" dest="${remote}:${destination}" />
</target>
<target name="fetch">
<ant target="pull" />
<ant target="branch" />
</target>
<target name="stop" depends="">
<!-- ssh host="${remote}" command="/srv/apache-tomcat/bin/catalina.sh stop -force" keyfile="~/.ssh/id_rsa" / -->
<ssh host="${remote}" command="/srv/apache-tomcat/bin/shutdown.sh" />
<ant target="status" />
</target>
<target name="start" depends="">
<ssh host="${remote}" command="/srv/apache-tomcat/bin/startup.sh" keyfile="~/.ssh/id_rsa" />
<ant target="status" />
</target>
<target name="status" depends="">
<ssh host="${remote}" command="ps ax | grep tomcat | grep -v grep" />
</target>
<target name="kill" depends="">
<ssh host="${remote}" command="pkill -9 -f tomcat" />
<ant target="status" />
</target>
<target name="run" depends="">
<java classname="test.ant.HelloWorld" classpath="${hello}" />
</target>
<target name="clean">
<delete dir="${project.build}" />
<delete file="${hello}" />
</target>
</project>


  修改下面几处定义


<property name="repository" value="版本库地址" />
<property name="branch" value="部署分支" />
<property name="remote" value="远程服务器" />
<property name="destination" value="远程目录" />

  开始部署代码


ant backup
ant stop
ant deploy
ant start



5. 延伸阅读
  如果你想学习制作部署工具,还可以看看笔者早期的作品https://github.com/oscm/deployment这个工具使用Bash开发,写这个工具仅仅半天时间,后面小改过几次,这个工具伴随笔者很多年。
  第一个版本因为很多缺陷存在,笔者使用Python重新开发 https://github.com/oscm/devops 这个工具更适合PHP项目部署

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-392196-1-1.html 上篇帖子: 深度分析Linux下双网卡绑定七种模式 多网卡的7种bond模式原理 下篇帖子: ELK 性能(1)
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表