-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDeploy.groovy
169 lines (146 loc) · 4.63 KB
/
Deploy.groovy
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
/*
* Simple SSH deploy script exmaple.
*
*/
includeTargets << grailsScript("Clean")
includeTargets << grailsScript("Init")
includeTargets << grailsScript("War")
@GrabResolver(name='jcraft', root='http://jsch.sourceforge.net/maven2')
@Grab(group='com.jcraft', module='jsch', version='0.1.44')
import com.jcraft.jsch.*
import grails.util.Environment
import org.codehaus.groovy.grails.commons.ConfigurationHolder
/**
* Wrap SSH commands into closure.
*/
Session.metaClass.doRemote = { Closure closure ->
connect()
try {
closure.delegate = delegate
closure.call()
} finally {
disconnect()
}
}
/**
* Exec SSH command, throw exception on bad exit status.
* Command output is suppressed.
* @param session SSH session
* @param cmd command to exec
* @return command output string
*/
Session.metaClass.exec = { cmd ->
Channel channel = openChannel("exec")
channel.command = cmd
channel.inputStream = null
channel.errStream = System.err
InputStream inp = channel.inputStream
channel.connect()
int exitStatus = -1
StringBuilder output = new StringBuilder()
try {
while (true) {
output << inp
if (channel.closed) {
exitStatus = channel.exitStatus
break
}
try {
sleep(1000)
} catch (Exception ee) {
}
}
} finally {
channel.disconnect()
}
if (exitStatus != 0) {
println output
throw new RuntimeException("Command [${cmd}] returned exit-status ${exitStatus}")
}
output.toString()
}
Session.metaClass.scp = { sourceFile, dst ->
ChannelSftp channel = (ChannelSftp) openChannel("sftp")
channel.connect()
println "${sourceFile.path} => ${dst}"
try {
channel.put(new FileInputStream(sourceFile), dst, new SftpProgressMonitor() {
private int max = 1
private int points = 0
private int current = 0
void init(int op, String src, String dest, long max) {
this.max = max
this.current = 0
}
boolean count(long count) {
current += count
int newPoints = (current * 20 / max) as int
if (newPoints > points) {
print '.'
}
points = newPoints
true
}
void end() {
println ''
}
})
} finally {
channel.disconnect()
}
}
Session.metaClass.isTomcatRunning = { tomcatHome ->
return exec("ps -ef|grep java|grep ${tomcatHome}").contains('catalina')
}
target(main: "Deploy Grails application as WAR file.") {
JSch jsch = new JSch()
Properties config = new Properties()
config.put("StrictHostKeyChecking", "no")
config.put("HashKnownHosts", "yes")
jsch.config = config
def user = 'root'
depends(compile)
depends(createConfig)
def host = ConfigurationHolder.config?.deploy.host
def username = ConfigurationHolder.config?.deploy.username
if (!host || !username) return
def tomcatHome = '/opt/tomcat/latest'
def webAppPrefix = 'ROOT'
String password = new String(System.console().readPassword("Enter password for ${username}@${host}: "))
Session session = jsch.getSession(user, host, 22)
session.setPassword(password)
session.doRemote {
if (isTomcatRunning(tomcatHome)) {
println "Tomcat running. Stopping..."
exec "bash --login -i -c '${tomcatHome}/bin/catalina.sh stop 100 -force'"
// wait for being stopped.
int seconds = 0
while (seconds < 100) {
sleep(1000)
if (!isTomcatRunning(tomcatHome)) {
break
}
seconds++
}
if (isTomcatRunning(tomcatHome)) {
println "Unable to stop Tomcat at ${tomcatHome}."
return
}
println "Tomcat (${tomcatHome}) stopped."
}
// Deploy WAR
depends(war)
// Clear old ROOT application.
exec "rm -f ${tomcatHome}/webapps/${webAppPrefix}.*"
exec "rm -rf ${tomcatHome}/webapps/${webAppPrefix}"
println "Cleaned old ${webAppPrefix} application."
File warFile = grailsSettings.projectWarFile
if (!warFile) {
println "WAR ${warFile} not found."
}
scp warFile, "${tomcatHome}/webapps/${webAppPrefix}.war"
exec "bash --login -i -c '${tomcatHome}/bin/catalina.sh start'"
println "Deploy complete."
}
}
setDefaultTarget(main)