-
Notifications
You must be signed in to change notification settings - Fork 0
/
runCmd.gradle
60 lines (56 loc) · 1.75 KB
/
runCmd.gradle
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
ext.runCmd = {String cmd, File baseDir ->
def sout = new StringBuffer()
def serr = new StringBuffer()
println "\nRunning:: ${cmd}\nin ${baseDir}\nOn localhost\n"
def outProc = Runtime.runtime.exec(cmd, [] as String[], baseDir)
def running = true
def bufferPrinter = {buffer ->
def lastIndex = 0
while(running) {
def length = buffer.length()
if (length > lastIndex) {
print buffer.subSequence(lastIndex, length)
lastIndex = length
}
Thread.sleep(100)
}
}
Thread.start bufferPrinter.curry(sout)
Thread.start bufferPrinter.curry(serr)
outProc.consumeProcessOutput(sout, serr)
try {
outProc.waitFor()
}
finally {
running = false
}
if (outProc.exitValue()) {
println "Error code: ${outProc.exitValue()}"
println serr
System.exit(1)
}
}
ext.runSshCmd = {cmd, host, args = [] ->
String keyFile = args.keyFile?:ant.properties."build.userkey"
String buildUser = args.buildUser?:ant.properties."build.user"
String passphrase = args.passphrase?:ant.properties."build.userPassphrase"
Boolean failOnError = args.failOnError?: true
def log = new File(ant.properties.remoteOutputFile)
log.append("\nRunning:: ${cmd}\nOn host ${host} As ${buildUser}\n")
ant.sshexec(
username: buildUser,
host:host,
command: cmd,
failonerror:failOnError,
keyfile:keyFile,
passphrase:passphrase,
append: true,
trust:true,
outputproperty: "outputBuffer",
verbose: 'true'
)
if (ant.properties.outputBuffer){
log.append("Output:\n${ant.properties.outputBuffer}")
}
log.append("Done\n")
}