deployer.xml 14 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419
<?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>


	<!-- 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" />
		<if>
			<not>
				<isset property="public.key"/>
			</not>
			<then>
				<!--
				Attenzione, Load up puttygen and “Export” the key in ‘openssh’ format. Try using this key instead.
				-->
				<echo message="Copia del file in ${remote.file} in corso con username/password" />
				<scp password="${remote.password}" sftp="true"
					 verbose="${verbose}" 
					 knownhosts="${project.dist}/ssh/known_hosts." trust="true"  
					 file="${to.upload}"
					 todir="${tofile}">
				</scp>
				
				<echo message="Rinomino il file temporaneo" />
				<sshexec password="${remote.password}" username="${remote.username}" host="${remote.host}"  
						 verbose="${verbose}" 
						 knownhosts="${project.dist}/ssh/known_hosts." trust="true"  
						 command="mv ${remote.file.tmp} ${remote.file}">
				</sshexec>				
			</then>
			<else>
				<echo message="Copia del file in ${remote.file} in corso con private key" />
				<scp keyfile="${public.key}" 
					 verbose="${verbose}" 
					 knownhosts="${project.dist}/ssh/known_hosts." trust="true"  
					 file="${to.upload}"
					 todir="${tofile}">
				</scp>
				
				<echo message="Rinomino il file temporaneo" />
				<sshexec keyfile="${public.key}" username="${remote.username}" host="${remote.host}"  
						 verbose="${verbose}" 
						 knownhosts="${project.dist}/ssh/known_hosts." trust="true"  
						 command="mv ${remote.file.tmp} ${remote.file}">
				</sshexec>	
			</else>	
		</if>

		<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,tomcat.manager.list,tomcat.context.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>




		<!-- Se username e password per tomcat non sono impostati assegno 
		     come valore quello utilizzato per la connessione -->
		<condition property="tomcat.manager.username" value="${remote.username}">
			<not>
				<isset property="tomcat.manager.username"/>
			</not>
		</condition>
		<condition property="tomcat.manager.password" value="${remote.password}">
			<not>
				<isset property="tomcat.manager.password"/>
			</not>
		</condition>



		<echo message="Deployment application" />
		<for param="element" list="${tomcat.manager.list}">
			<sequential>
				<echo message="Manager url '@{element}'" />
				<echo message="War '${remote.path}/${warfile.name}'" />
				<antcall target="deploy">
					<param name="tomcat.war" value="${remote.path}/${warfile.name}"/>
					<param name="tomcat.manager.url" value="@{element}"/>
					<!-- Gli altri parametri sono ereditati -->
				</antcall>
			</sequential>
		</for>
		
		
		<!-- Deploy sul nodo 1 -->
		<if>
			<isset property="tomcat.manager.url.nodo1"/>
			<then>
				<antcall target="subDeployer">
					<param name="tomcat.war" value="${remote.path}/${warfile.name}"/>
					<param name="tomcat.manager.url" value="${tomcat.manager.url.nodo1}"/>					
				</antcall>
			</then>
		</if>
	</target>

	
	<!-- Operazione di deploy (locale) -->
	<target name="deploy" depends="sub-include-TomcatDeployer">

		<antcall target="sub-checkMandatory">
			<param name="param.list" value="tomcat.manager.url,tomcat.war,tomcat.context.path,tomcat.manager.username,tomcat.manager.password"/>			
		</antcall>
		
		
		<propertyregex property="nome.cluster" input="${tomcat.manager.url}" regexp="([a-z0-9\?]+(\.[a-z0-9]+)+)|(www(\.[a-z0-9]+){2,})" select="\0" /> 
		<!-- <property name="nome.cluster" value="${tomcat.manager.url}" /> -->
		
		
		<!-- Richiesta di conferma -->
		<input message="Vuoi eseguire il deploy della nuova versione sul nodo '${nome.cluster}'?" 
				   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>


		<!-- Tomcat manager URL 
		<echo>Deploy attraverso ${tomcat.manager.url}</echo>
		<fail message="URL non raggiungibile: ${tomcat.manager.url}">
			<condition >
				<not>
					<isreachable url="${tomcat.manager.url}"/>
				</not>
				<! - -
				Uses Java1.5+ networking APIs to probe for a (remote) system being reachable. Exactly what probe mechanisms are used is an implementation feature of the JVM. They may include ICMP "ping" packets, UDP or TCP connections to port 7 "echo service" or other means. On Java1.4 and earlier, being able to resolve the hostname is considered success. This means that if DNS is not working or a URL/hostname is bad, the test will fail, but otherwise succeed even if the remote host is actually absent.
				This condition turns unknown host exceptions into false conditions. This is because on a laptop, DNS is one of the first services when the network goes; you are implicitly offline.
				If a URL is supplied instead of a host, the hostname is extracted and used in the test - all other parts of the URL are discarded.
				The test may not work through firewalls, that is, something may be reachable using a protocol such as HTTP, while the lower level ICMP packets get dropped on the floor. Similarly, a host may detected as reachable with ICMP, but not reachable on other ports (i.e. port 80), because of firewalls.
			    - - >
			</condition>
		</fail>
		-->


		<!-- Eventuale undeploy -->
		<echo>Passo 1 - Undeploy della vecchia versione su ${nome.cluster}</echo>
		<echo>* context = ${tomcat.context.path}</echo>
		<trycatch reference="exception_ref">
			<try>
				<!-- ... some risky Ant work ... -->
				<antcall target="tomcat_undeploy">
					<param name="url" value="${tomcat.manager.url}" />
					<param name="username" value="${tomcat.manager.username}" />
					<param name="password" value="${tomcat.manager.password}" />
					<param name="path" value="${tomcat.context.path}" />
				</antcall>
				<echo>Tutto ok</echo>
			</try>
			<catch>
				<echo>Controllare messaggio FAIL</echo>
			</catch>
			<finally>
				<!-- <echo>Finally</echo> -->
			</finally>
		</trycatch>



		<!-- Deploy -->
		<echo>Passo 2 - Deploy della nuova versione su ${nome.cluster}</echo>
		<echo>* context = ${tomcat.context.path}</echo>
		<echo>* war (locale) = ${tomcat.war}</echo>
		<antcall target="tomcat_deploy_local">
			<param name="url" value="${tomcat.manager.url}" />
			<param name="username" value="${tomcat.manager.username}" />
			<param name="password" value="${tomcat.manager.password}" />
			<param name="path" value="${tomcat.context.path}"/>
			<param name="webapp.path" value="${tomcat.war}"/>
		</antcall>


		<echo>Deploy completato su ${nome.cluster}</echo>
	</target>


	<!-- Target estratti dal BUILD.XML del package deployer di Tomcat -->
	<target name="tomcat_deploy_local" description="Deploy web application">
		<deploy url="${url}" username="${username}" password="${password}" path="${path}" localWar="${webapp.path}" update="true" />
	</target>

	<target name="tomcat_deploy_remote" description="Deploy web application">
		<deploy url="${url}" username="${username}" password="${password}" path="${path}" war="${webapp.path}" update="true" />
	</target>

	<target name="tomcat_undeploy" description="Undeploy web application">
		<undeploy url="${url}" username="${username}" password="${password}" path="${path}"/>
	</target>

	<!-- Webapp lifecycle control -->
	<target name="tomcat_start" description="Start web application">
		<start url="${url}" username="${username}" password="${password}" path="${path}"/>
	</target>

	<target name="tomcat_reload" description="Reload web application">
		<reload url="${url}" username="${username}" password="${password}" path="${path}"/>
	</target>

	<target name="tomcat_stop" description="Stop web application">
		<stop url="${url}" username="${username}" password="${password}" path="${path}"/>
	</target>


	
	<!-- Carica le librerie del deployer di Tomcat -->
	<target name="sub-include-TomcatDeployer" depends="sub-include-AntContrib" >
		<echo>Include Tomcat Deployer JAR</echo>
		<!--
			<path id="deployer.classpath">
				<fileset dir="${basedir}/lib/ant/apache">
					<include name="*.jar" />
				</fileset>
			</path>
			-->
		<!-- Configure the custom Ant tasks for the Manager application -->
		<taskdef resource="org/apache/catalina/ant/catalina.tasks">
			<classpath>
				<path>
					<fileset dir="${basedir}/lib/ant/apache">
						<include name="*.jar" />
					</fileset>
				</path>
			</classpath>
		</taskdef>
	</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>