Gradle 이용한 간단한 원격 배포 셈플링
그룹별 병렬? 배포 - WAS Instance 가 늘더라도 배포 시간 절약
- WAS 유입 차단 -> 5초대기 -> WAS 중지
 - 소스 배포 -> 소스 압축 풀기 및 설정
 - WAS 기동
 - 필수 서비스 체크(CURL)
 - WAS 유입 활성화
 
빌드 파일과 별도 분리 해도 되고...(분리된 파일을 Jenkins에서 실행)
clean build remoteDeploy
..
..
apply plugin: 'org.hidetake.ssh'
..
..
buildscript {
    repositories {
        jcenter()
    }
    dependencies {
       classpath 'org.hidetake:gradle-ssh-plugin:2.4.2'
    }
}
task remoteDeploy << {
	def startTime = System.currentTimeMillis()
	println "project: [$project]"
	def PROJECT_NM 		= project.name
	def DEPLOY_SERVERS 	= []
	
	if( "$targetServer" == "dev" ){
		DEPLOY_SERVERS = [
			group1: [
			],
		]
	} else if( "$targetServer" == "stage" ){
		DEPLOY_SERVERS = [
			group1: [
			],
		]
	} else if( "$targetServer" == "real" ){
		DEPLOY_SERVERS = [
			group1: [
		            [host: "dkwas01", 	user: "tomcat", wasHome: "/usr/local/tomcat/domains/test11", 	
wasDomain: "test11",	webRoot: "/data/webroot/app-test", isWait: true,],
		            [host: "dkwas02", 	user: "tomcat", wasHome: "/usr/local/tomcat/domains/test21", 	
wasDomain: "test21",	webRoot: "/data/webroot/app-test", isWait: true,],
			],
			group2: [
		            [host: "dkwas03", 	user: "tomcat", wasHome: "/usr/local/tomcat/domains/test31", 	
wasDomain: "test31",	webRoot: "/data/webroot/app-test", isWait: true,],
		            [host: "dkwas04", 	user: "tomcat", wasHome: "/usr/local/tomcat/domains/test41", 	
wasDomain: "test41",	webRoot: "/data/webroot/app-test", isWait: true,],
			],
		]
	}
	
	DEPLOY_SERVERS.eachWithIndex { gItem, gIdx --->
		logInfo(DEPLOY_SERVERS, gItem, gIdx, null, null, "START")
	
		gItem.value.eachWithIndex() { hItem, hIdx ->
			def remoteUser 		= hItem.user
			def remoteHost 		= hItem.host
			def isWait 			= hItem.isWait
			def catalinaBase 	= hItem.wasHome
			def wasDomain 		= hItem.wasDomain
			def webRoot 		= hItem.webRoot
			def warPath 		= webRoot + "/../" + PROJECT_NM
			
			def remoteServer = [host: remoteHost, user: remoteUser, identity: file("${System.properties['user.home']}/.ssh/id_rsa")]
			ssh.run {
				session(remoteServer) {
					logInfo(DEPLOY_SERVERS, gItem, gIdx, hItem, hIdx, "HTTPD(JK_MOD) BLOCK -> WAS SHUTDOWN")
					// 고객유입 차단 -> WAS 중지
					execute "cd $catalinaBase; ./shutdown.sh " + (isWait?'wait update 5':'')
					
					logInfo(DEPLOY_SERVERS, gItem, gIdx, hItem, hIdx, "WAR REMOTE COPY")
					put from: war.archivePath.path, into: warPath
					
					execute "rm -Rf " + webRoot + "/*"
					execute "cd " + webRoot + "; jar xf " + warPath
					execute "chmod -Rf 755 " + webRoot + "/*"
					
					logInfo(DEPLOY_SERVERS, gItem, gIdx, hItem, hIdx, "WAR START (HTTPD(JK_MOD) BLOCKING)")
					// WAS 기동 (고객 유입은 차단상태)
					execute "cd $catalinaBase; ./start.sh"
				}
			}
		}
		
		gItem.value.eachWithIndex() { hItem, hIdx ->
			def remoteUser 		= hItem.user
			def remoteHost 		= hItem.host
			def catalinaBase 	= hItem.wasHome
			def isWait 			= hItem.isWait
			def wasDomain 		= hItem.wasDomain
			
			def remoteServer = [host: remoteHost, user: remoteUser, identity: file("${System.properties['user.home']}/.ssh/id_rsa")]
			ssh.run {
				session(remoteServer) {
					logInfo(DEPLOY_SERVERS, gItem, gIdx, hItem, hIdx, "CHECK SERVICE")
					while (true) {
						sleep(2000)
						
						def resultStr = execute "cd $catalinaBase; ./checkService.sh"
						if (resultStr.contains(wasDomain) && resultStr.contains("SERVICE CHECK ERROR") == false) {
							break
						}
						print "."
					}
					
					logInfo(DEPLOY_SERVERS, gItem, gIdx, hItem, hIdx, "HTTPD(JK_MOD) FLOW")
					// 고객 유입
					execute "cd $catalinaBase; ./httpd.sh start"
				}
			}
		}
		logInfo(DEPLOY_SERVERS, gItem, gIdx, null, null, "END")
	}
	
	def totalExecTime = (System.currentTimeMillis() - startTime) / 1000
	println "==================== TOTAL EXEC TIME: " + (totalExecTime) + "s ====================" 
}
static logInfo(deployServers, gItem, gIdx, hItem, hIdx, msg) {
	println("==================== [GROUP $gItem.key " + (gIdx + 1) + "/" + deployServers.size() + (hItem?" " + (hIdx + 1) + "/" + gItem.value.size() + " $hItem.host:$hItem.wasDomain] ":" ") + msg + " ====================" )
}