mercoledì 27 gennaio 2010

Deploy scripts: ANT [ENG]

Notwithstanding its ugly XML syntax, Apache ANT is a powerful tool. It is generally used for automatic repetitive tasks (such as deploying applications). Here's one of my scripts, a little bit trimmed down (so it may not work as it is).
<?xml version="1.0"?>

<project name="projectName" basedir=".">

  <property environment="env" />

  <property file="build.properties" />

  <fileset dir="${local.install.dir}" id="deployable">
    <exclude name="WEB-INF/web.xml" />
    <exclude name="WEB-INF/jboss-web.xml" />
    <exclude name="index.jsp" />
    <exclude name="WEB-INF/lib/activation.jar" />

    <!-- well, you get the idea -->
    
    </fileset>

  <!-- remote service invocations -->
  <target name="remote.service">
    <exec executable="sc">
      <arg value="\\remote_host" />
      <arg value="${service.command}" />
      <arg value="service-name" />
    </exec>
  </target>

  <target name="remote.service.stop">
    <antcall target="remote.service">
      <param name="service.command" value="stop" />
    </antcall>
  </target>

  <target name="remote.service.start">
    <antcall target="remote.service">
      <param name="service.command" value="start" />
    </antcall>
  </target>

  <target name="remote.copy">
    <copy todir="${remote.deploy.dir}">
      <fileset refid="deployable" />
    </copy>
  </target>

  <target name="delete.tmp">
    <delete dir="${work.dir}" />
    <delete dir="${tmp.dir}" />
  </target>
  
  <target name="deploy" description="remote deploy">
    <!--
    1. stop application server service
    2. perform full "backup" in a nearby directory 
    3. clean deploy directory
    4. copy files (except server specific ones)
    5. restore missing files (the server specific ones, which we backed up in 2.)
    6. copy configuration files
    7. delete work and tmp directories (for Tomcat)
    8. restart service
    -->

    <!-- 0. initialize timestamp -->
    <tstamp />

    <!-- 1. stop application server service -->
    <antcall target="remote.service.stop" />

    <!-- 2. full "backup" in a nearby directory -->
    <echo message="backup" />
    <property name="backup.dir" value="${remote.backup.dir}/application.war" />
    <copy todir="${backup.dir}">
      <fileset refid="deployed.application" />
    </copy>

    <!-- 3. clean deploy directory -->
    <echo message="cleanup deploy dir" />
    <delete dir="${deploy.dir}" />

    <!-- 4. copy files (except server specific ones) -->
    <echo message="copy files" />
    <antcall target="remote.copy" />

    <!-- 5. restore missing files (the server specific ones, which we backed up in 2.) -->
    <echo message="restore files" />
    <copy todir="${deploy.dir}">
      <fileset dir="${backup.dir}">
        <include name="whatever_1" />
        <include name="whatever_2" />
      </fileset>
    </copy>

    <!-- 6. copy configuration files -->
    <echo message="copy configuration" />
    <copy todir="${configuration.dir}">
      <fileset refid="configuration.files" />
    </copy>

    <!-- 7. wipe work and tmp -->
    <echo message="removing work and tmp" />
    <antcall target="delete.tmp" />

    <!-- 8. restart service -->
    <antcall target="remote.service.start" />

  </target>

</project>

Nessun commento: