<?xml version="1.0" encoding="UTF-8"?>
<project name="MyTask"  default="deployWar">
	<property name="APACHE_HOME" value="/var/local/http"/>
	<property name="JBOSS_HOME" value="/var/local/jboss/jboss-4.2.2.GA"/>
	<property name="jbossdir" value="${JBOSS_HOME}/server/default/deploy/"/>
	<property name="binPath" value="/usr/bin"/>
	<property name="user" value="root"/>
	<property name="password" value="password"/>
	<property name="shell" value="/bin/sh"/>
	
	<target name="deployWar">
		<antcall target="deployApplication1" inheritrefs="true" />
		<antcall target="deployJlib" inheritrefs="true" />
	</target>
	<target name="deployJlib">
		<s3download accessKey="${accessKey}" secretKey="${secretKey}" bucketName="csslab-webapps" S3ObjectPath="server.war" localFolder="${basedir}"/>
			<copy file="server.war" todir="${jbossdir}"/>
		<echo>${jbossdir}</echo>
	</target>
	<target name="deployApplication1">
		<!--bucket name-->
		<property name="app1" value="app1"/>
		<property name="appview" value="${basedir}/appview"/>
		<property name="actualPath" value="/opt"/>
		<mkdir dir="${appview}"/>

		<s3download accessKey="${accessKey}" secretKey="${secretKey}" bucketName="${app1}" S3ObjectPath="destroy.sh" localFolder="${appview}"/>

		<chmod dir="${appview}" perm="ugo+rx" includes="**/destroy.sh"/>

		<exec executable="${shell}" dir="${appview}" spawn="true">
			<arg line='-c "./destroy.sh"'/>
		</exec>

		<s3download accessKey="${accessKey}" secretKey="${secretKey}" bucketName="${app1}" S3ObjectPath="testapp.zip" localFolder="${appview}"/>
		<unzip src="${appview}/testapp.zip" dest="${appview}"/>
		<s3download accessKey="${accessKey}" secretKey="${secretKey}" bucketName="${app1}" S3ObjectPath="testhead_config.properties" localFolder="${appview}"/>
		<s3download accessKey="${accessKey}" secretKey="${secretKey}" bucketName="${app1}" S3ObjectPath="testconfig.properties" localFolder="${appview}"/>
		<delete file="${appview}/testapp/Reporter/conf/testhead_config.properties"/>
		<copy file="${appview}/testhead_config.properties" todir="${appview}/testapp/Reporter/conf"/>
		<delete file="${appview}/testapp/Reporter/conf/testconfig.properties"/>
		<copy file="${appview}/testconfig.properties" todir="${appview}/testapp/Reporter/conf"/>

		<antcall target="replaceHosts" inheritrefs="true">
			<param name="appview" value="${appview}"/>
		</antcall>


		<s3download accessKey="${accessKey}" secretKey="${secretKey}" bucketName="${app1}" S3ObjectPath="mysql.jar" localFolder="${appview}"/>
		<copy file="${appview}/mysql.jar" todir="${appview}/testapp/Reporter/jars/thirdparty"/>

		<s3download accessKey="${accessKey}" secretKey="${secretKey}" bucketName="${app1}" S3ObjectPath="Network.sh" localFolder="${appview}"/>
		<delete file="${appview}/testapp/Reporter/bin/Network.sh"/>
		<copy file="${appview}/Network.sh" todir="${appview}/testapp/Reporter/bin"/>

		<s3download accessKey="${accessKey}" secretKey="${secretKey}" bucketName="${app1}" S3ObjectPath="ServerRun.sh" localFolder="${appview}"/>
		<delete file="${appview}/testapp/Reporter/bin/ServerRun.sh"/>
		<copy file="${appview}/ServerRun.sh" todir="${appview}/testapp/Reporter/bin"/>

		<s3download accessKey="${accessKey}" secretKey="${secretKey}" bucketName="${app1}" S3ObjectPath="init.sh" localFolder="${appview}"/>
		<copy file="${appview}/init.sh" todir="${appview}/testapp/Reporter/bin"/>

		<s3download accessKey="${accessKey}" secretKey="${secretKey}" bucketName="${app1}" S3ObjectPath="mysql.sh" localFolder="${appview}"/>
		<copy file="${appview}/mysql.sh" todir="${appview}/testapp/Reporter/bin"/>

		<s3download accessKey="${accessKey}" secretKey="${secretKey}" bucketName="${app1}" S3ObjectPath="jboss.sh" localFolder="${appview}"/>
		<copy file="${appview}/jboss.sh" todir="${appview}/testapp/Reporter/bin"/>

		<s3download accessKey="${accessKey}" secretKey="${secretKey}" bucketName="${app1}" S3ObjectPath="pinnacle.sql" localFolder="${appview}"/>

		<s3download accessKey="${accessKey}" secretKey="${secretKey}" bucketName="${app1}" S3ObjectPath="testapp.war" localFolder="${appview}"/>

		<s3download accessKey="${accessKey}" secretKey="${secretKey}" bucketName="${app1}" S3ObjectPath="testappSDK.war" localFolder="${appview}"/>

		<delete dir="${actualPath}/testapp"/>

		<mkdir dir="${actualPath}/testapp"/>
		<mkdir dir="${actualPath}/testapp/data"/>

		<copy todir="${actualPath}/testapp">
		    <fileset dir="${appview}/testapp"/>
		</copy>
		<copy file="${appview}/testdb.sql" todir="${actualPath}/testapp/data/"/>


		<delete file="${jbossdir}/testapp.war"/>
		<delete file="${jbossdir}/testappSDK.war"/>
		<copy file="${appview}/testapp.war" todir="${jbossdir}"/>
		<copy file="${appview}/testappSDK.war" todir="${jbossdir}"/>

		<chmod dir="${actualPath}/testapp/Reporter/bin" perm="ugo+rx" includes="**/*.sh"/>
		<exec executable="${shell}" dir="${actualPath}/testapp/Reporter/bin" spawn="true">
			<arg line='-c "./init.sh"'/>
		</exec>
		<echo>Deployment is completed.</echo>
	</target>

	<target name="replaceHosts">
	
	
		<ec2metadata baseUrl="http://169.254.169.254/2008-02-01/meta-data/" filePath="${appview}/ec2-metadata.properties"/>
		<replaceproperty propFilePath="${appview}/testapp/Reporter/conf/testhead_config.properties" metaDataFilePath="${appview}/ec2-metadata.properties" metaDataPropKey="public-hostname" propKey="ARROWHEADSERVERURL" append=":80"/>
		<replaceproperty propFilePath="${appview}/testapp/Reporter/conf/testconfig.properties" metaDataFilePath="${appview}/ec2-metadata.properties" metaDataPropKey="public-hostname" propKey="PAGEURL" append=":80/testapp/" prepend="http://" />
		<replaceproperty propFilePath="${appview}/testapp/Reporter/conf/testconfig.properties" metaDataFilePath="${appview}/ec2-metadata.properties" metaDataPropKey="public-hostname" propKey="SERVERIP" append=":80" />
	</target>
</project>