-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathnexusSync.groovy
191 lines (140 loc) · 4.57 KB
/
nexusSync.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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
@Grab('org.codehaus.groovy.modules.http-builder:http-builder:0.7.1')
import groovyx.net.http.RESTClient
import static groovyx.net.http.ContentType.*
def printUsage(){
println """Usage:
groovy nexusSync.groovy [type] [sourceUrl] [toUrl] [TargetNexusID] [TargetNexusPass]
type: maven or npm
sourceUrl: sync source. must end with '/'
toUrl: sync target. must end with '/'
TargetNexusID: traget Nexus ID Default admin. Optional
TargetNexusPass: traget Nexus ID Default admin. Optional
Example:
groovy nexusSync.groovy maven http://localhost:8081/nexus/maven-public/ http://my-private-nexus.com/nexus/private_repository/ admin admin123
"""
}
if (args.length != 3){
printUsage();
return;
}
def type = args[0];
if ('maven' != type && 'npm' != type){
printUsage();
return;
}
def isValidUrl(url) {
return url.startsWith('http') && url.endsWith('/');
}
def sourceFullUrl = args[1];
def targetFullUrl = args[2];
def nexusID = args [3];
def nexusPass = args [4];
if (nexusID) {
println "Traget Nexus id ${nexusID}"
} else {
println "Traget Nexus id admin"
def nexusID = admin
}
if (nexusPass) {
println "Traget Nexus Pass ${nexusPass}"
} else {
println "Traget Nexus Pass admin123"
def nexusPass = admin123
}
if (sourceFullUrl == targetFullUrl || ! isValidUrl(sourceFullUrl) || ! isValidUrl(targetFullUrl)){
println "Invalid url"
printUsage();
return;
}
def sourceRepository = sourceFullUrl.split('/')[-1]
def targetRpository = targetFullUrl.split('/')[-1]
def leftUrl = sourceFullUrl[0..(sourceFullUrl.lastIndexOf(sourceRepository)-1)]
def rightUrl = targetFullUrl[0..(targetFullUrl.lastIndexOf(targetRpository)-1)]
println "Source Url: ${leftUrl} Repository: ${sourceRepository}"
println "Target Url: ${rightUrl} Repository: ${targetRpository}"
def repositories = [
[from:sourceRepository, to:targetRpository, type: type],
]
def fetch = { url, repository->
def context = url.substring(url.lastIndexOf('/'))
def restClient = new RESTClient(url)
def continuationToken = null ;
def items = []
def path = "${context}/service/rest/v1/assets".replace('//','/').replace('//','/').replace('//','/')
while(true) {
def query = ['repository':repository]
if (continuationToken!=null) {
query['continuationToken'] = continuationToken;
}
println query
def response = restClient.get(
path: path ,
contentType: JSON,
query:query
)
items.addAll(response.data.items)
continuationToken = response.data['continuationToken']
if (continuationToken == null) {
break;
}
}
return items;
}
def convert = {
return [
'path':it.path,
'downloadUrl':it.downloadUrl,
'checksum': it.checksum.sha1
]}
def fails = []
repositories.each { repository ->
def lefts = fetch(leftUrl, repository.from).collect(convert)
def rights = fetch(rightUrl, repository.to) .collect(convert)
def remains = []
lefts.findAll { leftItem ->
def matched = rights.find { rightItem ->
return leftItem.path == rightItem.path && leftItem.checksum == rightItem.checksum
}
if (matched == null){
remains += leftItem;
}
}
remains.each {
println "Mismtached: ${it}"
def fileName = "${it.downloadUrl.substring(it.downloadUrl.lastIndexOf('/')+1)}";
def downloadCmd = "curl ${it.downloadUrl} --output ${it.downloadUrl.substring(it.downloadUrl.lastIndexOf('/')+1)}"
if (repository.type == 'maven'){
println downloadCmd
println downloadCmd.execute().text
if (new File(fileName).exists()==false || new File(fileName).length() ==0){
println "!! Fail to download ${fileName}"
fails += fileName
return;
}
def leftPath = "/repository/${repository.from}/"
def subpath = it.downloadUrl.substring(it.downloadUrl.indexOf(leftPath)+leftPath.length());
def rightPath = "/repository/${repository.to}/"
def uploadUrl = "${rightUrl}/${rightPath[1..-1]}${subpath}"
def uploadCmd = "curl -v -u ${nexusID}:${nexusPass} --upload-file ${fileName} ${uploadUrl}"
println uploadCmd
println uploadCmd.execute().text
}
else if (repository.type == 'npm' && fileName.endsWith("gz")){
println downloadCmd
println downloadCmd.execute().text
if (new File(fileName).exists()==false || new File(fileName).length() ==0){
println "!! Fail to download ${fileName}"
fails += fileName
return;
}
def uploadUrl = "${rightUrl}/repository/${repository.to}/"
def uploadCmd = "npm --registry ${uploadUrl} publish ${fileName}"
println uploadCmd
println uploadCmd.execute().text
}
}
}
println "-----------------------------------"
println "Failed List"
println "-----------------------------------"
fails.each { println it}