forked from apache/tapestry-5
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ssh.gradle
124 lines (102 loc) · 2.7 KB
/
ssh.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
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
project.ext.Scp = Scp.class
project.ext.SshExec = SshExec.class
project.ext.SshTask = SshTask.class
configurations {
sshAntTask
}
dependencies {
sshAntTask "org.apache.ant:ant-jsch:1.8.2"
}
tasks.withType(SshTask) {
sshAntClasspath = configurations.sshAntTask
}
class SshTask extends DefaultTask {
@InputFiles
FileCollection sshAntClasspath
@Input
String host
@Input
String userName
@Input
String password
boolean verbose = false
private boolean antInited = false
protected initAnt() {
if (!antInited) {
ant.taskdef(name: 'scp',
classname: 'org.apache.tools.ant.taskdefs.optional.ssh.Scp',
classpath: sshAntClasspath.asPath,
loaderref: 'ssh')
ant.taskdef(name: 'sshexec',
classname: 'org.apache.tools.ant.taskdefs.optional.ssh.SSHExec',
classpath: sshAntClasspath.asPath,
loaderref: 'ssh')
antInited = true
}
}
protected withInfoLogging(Closure action) {
def oldLogLevel = logging.level
logging.level = [LogLevel.INFO, oldLogLevel].min()
try {
action()
} finally {
if (oldLogLevel) {
logging.level = oldLogLevel
}
}
}
def scpFile(source, destination) {
initAnt()
withInfoLogging {
ant.scp(localFile: project.files(source).singleFile, remoteToFile: "${userName}@${host}:${destination}", keyfile : "${System.properties['user.home']}/.ssh/id_dsa", verbose: verbose)
}
}
def scpDir(source, destination) {
initAnt()
withInfoLogging {
ant.sshexec(host: host, username: userName, password: password, command: "mkdir -p ${destination}")
ant.scp(remoteTodir: "${userName}@${host}:${destination}", keyfile : "${System.properties['user.home']}/.ssh/id_dsa", verbose: verbose) {
project.files(source).addToAntBuilder(ant, "fileSet", FileCollection.AntType.FileSet)
}
}
}
def ssh(Object... commandLine) {
initAnt()
withInfoLogging {
ant.sshexec(host: host, username: userName, password: password, command: commandLine.join(' '))
}
}
}
class Scp extends SshTask {
@InputFiles @SkipWhenEmpty source
@Input destination
boolean isDir = false
@TaskAction
void doActions() {
if (isDir) {
scpDir(source, destination)
return
}
project.files(source).each { doFile(it) }
}
void doFile(File file) {
if (file.directory) {
file.eachFile { doFile(it) }
} else {
scpFile(file, destination)
}
}
}
class SshExec extends SshTask {
@Input
List<String[]> commandLines = []
void commandLine(String... commandLine) {
commandLines << commandLine
}
@TaskAction
void doActions() {
commandLines.each { commandLine ->
ssh(* commandLine)
}
}
}