<p>..
..
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"
)
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)"
)
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 +
" ===================="
)
}
</p>