Cleanup ExecRemote code:
* Replace action extension with simple closure composition * Separate remote and local execution
This commit is contained in:
committed by
Pavel Punegov
parent
0623bb9474
commit
a3ad2101f5
@@ -22,74 +22,81 @@ import org.gradle.process.ExecResult
|
|||||||
import org.gradle.process.ExecSpec
|
import org.gradle.process.ExecSpec
|
||||||
import org.gradle.util.ConfigureUtil
|
import org.gradle.util.ConfigureUtil
|
||||||
|
|
||||||
// This class provides process.execRemote -- a drop-in replacement for process.exec .
|
import java.nio.file.Paths
|
||||||
// If we provide -Premote=user@host the binaries are executed on a remote host
|
import java.util.function.Function
|
||||||
// If we omit -Premote then the binary is executed locally as usual.
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* This class provides process.execRemote -- a drop-in replacement for process.exec.
|
||||||
|
* If we provide -Premote=user@host the binaries are executed on a remote host
|
||||||
|
* If we omit -Premote then the binary is executed locally as usual.
|
||||||
|
*/
|
||||||
class ExecRemote {
|
class ExecRemote {
|
||||||
|
|
||||||
private final Project project
|
private final Project project
|
||||||
private final String remote = null
|
private final Function<Closure<? super ExecSpec>, ExecResult> executor
|
||||||
private final String remoteDir = null
|
|
||||||
|
|
||||||
ExecRemote(Project project) {
|
ExecRemote(Project project) {
|
||||||
this.project = project
|
this.project = project
|
||||||
if (project.hasProperty('remote')) {
|
if (project.hasProperty('remote')) {
|
||||||
remote = project.ext.remote
|
def remote = project.property('remote') as String
|
||||||
remoteDir = uniqSessionName()
|
executor = new SSHExecutor(remote)
|
||||||
createRemoteDir()
|
} else {
|
||||||
}
|
executor = { project.exec(ConfigureUtil.configureUsing(it)) }
|
||||||
}
|
|
||||||
|
|
||||||
private String uniqSessionName() {
|
|
||||||
def date = new Date().format('yyyyMMddHHmmss')
|
|
||||||
return project.ext.remoteRoot + File.createTempFile(System.properties['user.name'], "_" + date)
|
|
||||||
}
|
|
||||||
|
|
||||||
private createRemoteDir() {
|
|
||||||
project.exec {
|
|
||||||
commandLine('ssh', remote, 'mkdir', '-p', remoteDir)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private upload(String fileName) {
|
ExecResult execRemote(Action<? super ExecSpec> action) {
|
||||||
project.exec {
|
project.exec(action)
|
||||||
commandLine('scp', fileName, "${remote}:${remoteDir}")
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private cleanup(String fileName) {
|
ExecResult execRemote(Closure<? super ExecSpec> closure) {
|
||||||
println "Remove ${remote}:${fileName}"
|
this.executor.apply(closure)
|
||||||
project.exec {
|
|
||||||
commandLine('ssh', remote, 'rm', fileName )
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public ExecResult execRemote(Action<? super ExecSpec> action) {
|
private class SSHExecutor implements Function<Closure<? super ExecSpec>, ExecResult> {
|
||||||
String execFile
|
private final String remote
|
||||||
Action<? super ExecSpec> extendedAction = new Action<ExecSpec>() {
|
private final String remoteDir
|
||||||
@Override
|
|
||||||
void execute(ExecSpec execSpec) {
|
|
||||||
action.execute(execSpec)
|
|
||||||
|
|
||||||
if (remote == null) return
|
SSHExecutor(String remote) {
|
||||||
|
this.remote = remote
|
||||||
|
this.remoteDir = uniqueSessionName()
|
||||||
|
createRemoteDir()
|
||||||
|
}
|
||||||
|
|
||||||
execSpec.with {
|
@Override
|
||||||
upload(getExecutable())
|
ExecResult apply(Closure<? super ExecSpec> closure) {
|
||||||
executable = "$remoteDir/${new File(executable).name}"
|
String execFile
|
||||||
execFile = executable
|
def execResult = project.exec closure >> {
|
||||||
if (project.hasProperty('remote')) {
|
upload(executable)
|
||||||
commandLine = ['/usr/bin/ssh', remote] + commandLine
|
execFile = executable = "$remoteDir/${new File(executable).name}"
|
||||||
}
|
commandLine = ['/usr/bin/ssh', remote] + commandLine
|
||||||
}
|
}
|
||||||
|
if (execFile != null) cleanup(execFile)
|
||||||
|
return execResult
|
||||||
|
}
|
||||||
|
|
||||||
|
private String uniqueSessionName() {
|
||||||
|
def date = new Date().format('yyyyMMddHHmmss')
|
||||||
|
Paths.get(project.ext.remoteRoot.toString(), "tmp",
|
||||||
|
System.properties['user.name'].toString() + "_" + date).toString()
|
||||||
|
}
|
||||||
|
|
||||||
|
private createRemoteDir() {
|
||||||
|
project.exec {
|
||||||
|
commandLine('ssh', remote, 'mkdir', '-p', remoteDir)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
def execResult = project.exec(extendedAction)
|
|
||||||
if (execFile != null) cleanup(execFile)
|
|
||||||
return execResult
|
|
||||||
}
|
|
||||||
|
|
||||||
public ExecResult execRemote(Closure closure) {
|
private upload(String fileName) {
|
||||||
return this.execRemote(ConfigureUtil.configureUsing(closure));
|
project.exec {
|
||||||
|
commandLine('scp', fileName, "${remote}:${remoteDir}")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private cleanup(String fileName) {
|
||||||
|
project.exec {
|
||||||
|
commandLine('ssh', remote, 'rm', fileName)
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user