deployer-script.xml 9.01 KB
<?xml version="1.0" encoding="UTF-8"?>
<project basedir="." default="help" name="Deployer">
<!-- @author m.veroni
     @version 1.1
     @release 02/05/2013 -->


	<target name="help">
		<echo message="Help" />
		
		<echo message=" " />
		<echo message="uploadAndDeploy     Esegue la copia remota del file e il deploy sui nodi specificati" />
		<echo message="copySftp            Esegue la copia di un file su un server remoto" />
		<echo message="deploy              Esegue il deploy sui nodi specificati" />
		<echo message="refresh             Esegue un refresh di Eclipse" />
				
		<echo message=" " />
		<echo message="Lanciare i target senza parametri per scoprire quelli obbligatori" />
	</target>


	<target name="remote.exec">
		<echo message="Esegue comando ssh: ${command}" />
		<if>
			<not>
				<isset property="public.key"/>
			</not>
			<then>
				<sshexec password="${remote.password}" username="${remote.username}" host="${remote.host}"  
						 verbose="${verbose}" 
						 knownhosts="${project.dist}/ssh/known_hosts." trust="true" 
						 command="${command}">
				</sshexec>	
			</then>
			<else>
				<echo message="Cancello il contenuto della cartella" />
				<sshexec keyfile="${public.key}" username="${remote.username}" host="${remote.host}"  
						 verbose="${verbose}" 
						 knownhosts="${project.dist}/ssh/known_hosts." trust="true"  
						 command="${command}">
				</sshexec>					
			</else>	
		</if>
		
	</target>
	
	
	<target name="remote.copy">
		<echo message="Copia del file in ${remote.file} in corso con username/password" />
		<if>
			<not>
				<isset property="public.key"/>
			</not>
			<then>
				<!--
				Attenzione, Load up puttygen and “Export” the key in ‘openssh’ format. Try using this key instead.
				-->
				<scp password="${remote.password}" sftp="true"
					 verbose="${verbose}" 
					 knownhosts="${project.dist}/ssh/known_hosts." trust="true"  
					 file="${source}"
					 todir="${target}">
				</scp>
			</then>
			<else>
				<scp keyfile="${public.key}" 
					 verbose="${verbose}" 
					 knownhosts="${project.dist}/ssh/known_hosts." trust="true"  
					 file="${source}"
					 todir="${target}">
				</scp>			
			</else>	
		</if>
		
	</target>
	
	<!-- Include -->
	<target name="sub-include-AntContrib">
		<echo>Include ANT-contrib JAR</echo>

		<!-- Include le librerie ant-contrib-0.6.jar 
						     http://ant-contrib.sourceforge.net/ -->
		<taskdef resource="net/sf/antcontrib/antcontrib.properties">
			<classpath>
				<pathelement location="${basedir}/lib/ant/ant-contrib-0.6.jar"/>
			</classpath>
		</taskdef>

	</target>


	
	<target name="copySftp" if="remote.host" description="Routine di trasferimento file via SFTP">
			
		<antcall target="sub-checkMandatory">
			<param name="param.list" value="local.path,local.file,remote.host.list,remote.path,remote.username"/>	
			<!-- 
			<param name="param.list" value="local.path,local.file,remote.host.list,remote.path,remote.username,remote.password"/>
			-->
		</antcall>
		
		
		
		
		<property name="to.upload" value="${local.path}/${local.file}" />
		<echo message="Caricamento del file ${to.upload} sul server remoto ${remote.host}" />
	

		<!-- Setup accesso -->
		<property name="remote.file" value="${remote.path}/${local.file}" />
		<property name="remote.file.tmp" value="${remote.path}/${local.file}.tmp" />
		<property name="tofile" value="${remote.username}@${remote.host}:${remote.file.tmp}" />

		
		
		<!-- Copia del file --> 
		<property name="verbose" value="false" />
		<antcall target="remote.exec">
			<param name="command" value="rm -f ${remote.path}/*.war*" />
		</antcall>
		<antcall target="remote.copy">
			<param name="source" value="${to.upload}" />
			<param name="target" value="${tofile}" />
		</antcall>
		<antcall target="remote.exec">
			<param name="command" value="mv ${remote.file.tmp} ${remote.file}" />
		</antcall>
		
		<echo message="File ${to.upload} copiato correttamente" />
		<echo message="Upload completato" />		
	</target>
	
	
	<!-- Check dei parametri obbligatori -->
	<target name="sub-checkMandatory" if="param.list">			
		
		<for param="element" list="${param.list}" trim="true">
			<sequential>
				<if>
					<not>
						<isset property="@{element}"/>
					</not>						
					<then>
						<echo message="[ERROR] Parametro obbligatorio non valorizzato: '@{element}'" />
						<property name="do.abort" value="true" />
					</then>
				</if>			
			</sequential>
		</for>			
		<fail if="do.abort"  message="Parametri obbligatori non valorizzati" />		
	</target>
	
		
	<target name="uploadAndDeploy" depends="sub-include-AntContrib">
		
		<antcall target="sub-checkMandatory">
			<param name="param.list" value="local.path,warfile.name,remote.host.list,remote.path"/>			
		</antcall>
				
		
		<!-- Richiesta di conferma -->
		<input message="Vuoi eseguire l'upload del file ${warfile.name} sui nodi: ${remote.host.list}?" 
				   validargs="S,n" 
				   addproperty="input.response"
				   defaultvalue="S"/>
		<condition property="do.abort">
			<not>
				<equals arg1="S" arg2="${input.response}" />
			</not>
		</condition>
		<fail if="do.abort">Operazione annullata dall'utente</fail>




		<!-- Richiesta user e password per accesso SSH 
			 Questa fase può essere omessa se sono passate come param 'remote.username' e 'remote.password'
		-->
		<echo message="Verifica credenziali accesso cluster" />		
		<if>
			<not>
				<isset property="remote.username"/>
			</not>
			<then>
				<input message="Utente:" addproperty="remote.username" />
				<condition property="do.abort">
					<equals arg1="" arg2="${remote.username}" />
				</condition>
				<fail if="do.abort">Utente nullo, operazione annullata</fail>
			</then>
		</if>
		<!-- La password la verifico solo se non uso la chiave pubblica -->
		<if>
			<not>
				<isset property="public.key"/>
			</not>
			<then>
				
				<if>
					<not>
						<isset property="remote.password"/>
					</not>
					<then>
						<input message="Password:" addproperty="remote.password" />
						<condition property="do.abort">
							<equals arg1="" arg2="${remote.password}" />
						</condition>
						<fail if="do.abort">Password nulla, operazione annullata</fail>
					</then>
				</if>
			</then>
		</if>
		




		<!-- Ciclo sulla lista dei nodi per eseguire l'upload del file sui server remoti -->
		<for param="element" list="${remote.host.list}">
			<sequential>
				<property name="remote.host" value="@{element}" />				
				<if>
					<isset property="remote.host" />
					<then>
						<antcall target="copySftp">
							<param name="local.file" value="${warfile.name}" />
							<!-- Gli altri parametri sono ereditati -->
						</antcall>
					</then>
				</if>			
			</sequential>
		</for>


		


		<echo message="Deployment application" />
		<for param="element" list="${remote.host.list}">
			<sequential>
				<echo message="Host '${remote.host}'" />
				<echo message="War '${remote.path}/${warfile.name}'" />
				<antcall target="deploy">
					<!-- Gli altri parametri sono ereditati -->
				</antcall>
			</sequential>
		</for>
		
		
		
	</target>

	
	<!-- Operazione di deploy (locale) -->
	<target name="deploy">

		<antcall target="sub-checkMandatory">
			<param name="param.list" value="remote.host,remote.path,warfile.name,context"/>			
		</antcall>
		
		
		
		<!-- Richiesta di conferma -->
		<input message="Vuoi eseguire il deploy della nuova versione su tutti i nodi?" 
				   validargs="S,n" 
				   addproperty="input.response2"
				   defaultvalue="S"/>
		<condition property="do.abort">
			<not>
				<equals arg1="S" arg2="${input.response2}" />
			</not>
		</condition>
		<fail if="do.abort">Operazione annullata dall'utente</fail>



		<!-- Deploy -->
		<echo>Deploy della nuova versione sui nodi</echo>
		<echo>* context = ${context}</echo>
		<echo>* war (locale) = ${warfile.name}</echo>
		
		<echo message="Deploy automatico" />
		<antcall target="remote.exec">
			<param name="command">cd ${remote.path} &amp;&amp; tomcat-deploy -c -f</param>
		</antcall>
		
		<echo>Deploy completato su tutti i nodi</echo>
	</target>


	<!-- Esegue un comando 'refresh' di Eclipse -->
	<target name="refresh" depends="sub-include-AntContrib" description="REFRESH di Eclipse" >

		<trycatch reference="exception_ref">
			<try>
				<echo>Eclipse refresh in progress...</echo>
				<!-- Refresh del progetto 
					  Per funzionare deve essere impostato il seguente flag in "External tool configuration"
					  JRE >	Run in the same JRE as the workspace  
					-->
				<eclipse.refreshLocal resource="${project.name}" depth="infinite"/>
				<echo>Eclipse refreshed!</echo>

			</try>
			<catch>
				<echo>Per funzionare deve essere impostato il seguente flag in "External tool configuration"
						  JRE >	Run in the same JRE as the workspace</echo>
			</catch>
			<finally>
				<!-- <echo>Finally</echo> -->
			</finally>
		</trycatch>



	</target>



</project>