-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnexus_copy_maven_repositories.sh
342 lines (285 loc) · 14.3 KB
/
nexus_copy_maven_repositories.sh
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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
#!/bin/bash
#
#
# Copy repository artifacts from Nexus 3 to another Nexus 3 instance
# Run this on the source Nexus 3 machine and make sure the repository already exists on the target
# Prerequisites: curl, jq, wget
#
# "If it looks stupid but it works, it ain't stupid."
#
#####################################################################################################
#
#
# Beware: the api from nexus 3.12 to 3.13 had changed from /rest/beta to /rest/v1
#
# Example
#
# sh nexus_copy_artifacts.sh -h localhost -P <port e.g. 8081> -u <source/local user> -p <source/local password> -H <target hostname> -U <target user> -W <target pass> -r <repository>
#
set +x -e
# Define opts
while getopts h:P:u:p:r:H:U:W:t: option
do
case "${option}"
in
h) HOST=${OPTARG};;
P) PORT=${OPTARG};;
u) USER=${OPTARG};;
p) PASSWORD=${OPTARG};;
r) REPO=${OPTARG};;
H) TARGET_HOST=${OPTARG};;
U) TARGET_HOST_USER=${OPTARG};;
W) TARGET_HOST_PASSWORD=${OPTARG};;
t) REPO_TYPE=${OPTARG};;
esac
done
function postMvnComponent {
#waaaah - y u no working!?
mvnGroupId=${1}
mvnArtifactId=${2}
mvnVersion=${3}
mvnPackaging=${4}
numberOfFiles=${5} #notinuse
listOfFiles="${6}"
echo "curl -X POST -u ${TARGET_HOST_USER}:\"${TARGET_HOST_PASSWORD}\" \"https://${TARGET_HOST}/service/rest/v1/components?repository=${REPO}\"
-H \"accept: application/json\" \
-H \"Content-Type: multipart/form-data\" \
-F \"maven2.groupId=${mvnGroupId}\" \
-F \"maven2.artifactId=${mvnArtifactId}\" \
-F \"maven2.version=${mvnVersion}\" \
-F \"maven2.generate-pom=false\" \
-F \"maven2.packaging=${mvnPackaging}\" \
${filepathWithArgs}"
curl -X POST -u ${TARGET_HOST_USER}:"${TARGET_HOST_PASSWORD}" "https://${TARGET_HOST}/service/rest/v1/components?repository=${REPO}" -H "accept: application/json" -H "Content-Type: multipart/form-data" -F "maven2.groupId=${mvnGroupId}" -F "maven2.artifactId=${mvnArtifactId}" -F "maven2.version=${mvnVersion}" -F "maven2.generate-pom=false" -F "maven2.packaging=${mvnPackaging}" "${filepathWithArgs}"
exitcode=$?
return ${exitcode}
#curl -X POST "https://t1p-nexus.t1p-cc.aws.route71.net/service/rest/v1/components?repository=android-releases" -H "accept: application/json" -H "Content-Type: multipart/form-data" -F "maven2.groupId=de.maxdome.app" -F "maven2.artifactId=app" -F "maven2.version=13.3.7" -F "maven2.generate-pom=true" -F "maven2.packaging=apk" -F "[email protected];type=application/vnd.android.package-archive" -F "maven2.asset1.extension=apk" -F "[email protected];type=application/vnd.android.package-archive" -F "maven2.asset2.extension=apk" -F "[email protected];type="
}
function mvnPublishFile {
echo "<settings><servers><server><id>nexus</id><username>${TARGET_HOST_USER}</username><password>${TARGET_HOST_PASSWORD}</password></server></servers></settings>" > ~/.m2/settings.xml
mvnGroupId="${1}"
mvnArtifactId="${2}"
mvnVersion="${3}"
mvnPackaging="${4}"
numberOfFiles="${5}" #notinuse
listOfFiles="${6}" #komma separated
classifiers="${7}" #komma separated
pomFile="${8}"
fileArgs=""
if [ -z ${classifiers} ]; then
for pathToFile in $(echo ${listOfFiles} |tr -d "," ); do
fileArgs="${fileArgs} -Dfile=${pathToFile}"
done
elif [ ${classifiers} = "null" ]; then
for pathToFile in $(echo ${listOfFiles} |tr -d "," ); do
fileArgs="${fileArgs} -Dfile=${pathToFile}"
done
else
fileArgs="-Dfiles=${listOfFiles} -Dclassifiers=${classifiers}"
fi
#maybe just one file
if [ -z ${pomFile} ]; then
echo "mvn deploy:deploy-file -Durl=https://${TARGET_HOST}/repository/${REPO} -DgeneratePom=false -DartifactId=${mvnArtifactId} -Dversion=${mvnVersion} -DgroupId=${mvnGroupId} -DartifactId=${mvnArtifactId} -DrepositoryId=nexus ${fileArgs}"
mvn deploy:deploy-file -Durl=https://${TARGET_HOST}/repository/${REPO} -DgeneratePom=false -DartifactId=${mvnArtifactId} -Dversion=${mvnVersion} -DgroupId=${mvnGroupId} -DartifactId=${mvnArtifactId} -DrepositoryId=nexus ${fileArgs}
exitcode=$?
# #maybe pom and one file
# elif [ ${classifiers} == "null" ]; then
# echo "mvn deploy:deploy-file -Durl=https://${TARGET_HOST}/repository/${REPO} -DgeneratePom=false -DartifactId=${mvnArtifactId} -Dversion=${mvnVersion} -DgroupId=${mvnGroupId} -DartifactId=${mvnArtifactId} -DrepositoryId=nexus -DpomFile=${pomFile} ${fileArgs}"
# mvn deploy:deploy-file -Durl=https://${TARGET_HOST}/repository/${REPO} -DgeneratePom=false -DartifactId=${mvnArtifactId} -Dversion=${mvnVersion} -DgroupId=${mvnGroupId} -DartifactId=${mvnArtifactId} -DrepositoryId=nexus -DpomFile=${pomFile} ${fileArgs}
else
echo "mvn deploy:deploy-file -Durl=https://${TARGET_HOST}/repository/${REPO} -DgeneratePom=false -DartifactId=${mvnArtifactId} -Dversion=${mvnVersion} -DgroupId=${mvnGroupId} -DartifactId=${mvnArtifactId} -DrepositoryId=nexus -DpomFile=${pomFile} ${fileArgs}"
mvn deploy:deploy-file -Durl=https://${TARGET_HOST}/repository/${REPO} -DgeneratePom=false -DartifactId=${mvnArtifactId} -Dversion=${mvnVersion} -DgroupId=${mvnGroupId} -DartifactId=${mvnArtifactId} -DrepositoryId=nexus -DpomFile=${pomFile} ${fileArgs}
exitcode=$?
fi
if [ ${exitcode} -ne 0 ]; then
echo "FAILED TO migrate: ${mvnArtifactId} ${mvnGroupId}-${mvnVersion}" >>./failed2bemigrated-${REPO}.txt
fi
return ${exitcode}
}
function fileIsChecksum {
filenameString=${1}
if [[ ${filenameString} =~ (\.md5|\.sha1)$ ]]; then
echo 1
elif [[ ! ${filenameString} =~ (\.md5|\.sha1)$ ]]; then
echo 0
#else
# echo "ERROR: unable to match for checksum-file"
fi
}
function downloadFile {
componentid=${1}
url=${2}
wget -4 -q --cut-dirs=2 -nH -x -P ./assets/${componentid} ${url}
}
function cleanup {
echo "INFO: cleanup function started"
files_to_be_deleted="temp_file.json this_component.json all_items_in_${REPO}.txt ./assets ~/.m2/settings.xml ./failed2bemigrated-${REPO}.txt"
echo "Migration failures for Repository ${REPO}"
cat ./failed2bemigrated-${REPO}.txt
for file_or_folder in $(echo ${files_to_be_deleted}); do
if [ -e ${file_or_folder} ]; then
echo "INFO: ${file_or_folder} exists... deleting4cleanup"
rm -rf ${file_or_folder}
else
echo "WARN: ${file_or_folder} is not existing. unable to cleanup"
fi
done
}
#initial api request to gather all component IDs
#echo "debug: Host https://${HOST}:${PORT}"
curl -s -X GET -u ${USER}:"${PASSWORD}" "http://${HOST}:${PORT}/service/rest/beta/components?repository=${REPO}" -H "accept: application/json" > temp_file.json
CONTINUATION_TOKEN=$(cat temp_file.json | jq -r '.continuationToken')
#echo "debug: INITIAL CONTINUATION_TOKEN: ${CONTINUATION_TOKEN}"
#NUMBER_OF_ITEMSs=$(cat temp_file.json | jq -r '.items[].id'| wc -l)
#echo "NUMBER_OF_ITEMSs: ${NUMBER_OF_ITEMSs}"
ITEMS=$(cat temp_file.json | jq -r '.items[].id')
echo "${ITEMS}" > all_items_in_${REPO}.txt
while [ ! -z ${CONTINUATION_TOKEN} -a ${CONTINUATION_TOKEN} != "null" ]; do
curl -s -X GET u ${USER}:"${PASSWORD}" "http://${HOST}:${PORT}/service/rest/beta/components?continuationToken=${CONTINUATION_TOKEN}&repository=${REPO}" -H "accept: application/json" > temp_file.json
CONTINUATION_TOKEN=$(cat temp_file.json | jq -r '.continuationToken')
# echo "debug: CONTINUATION_TOKEN: ${CONTINUATION_TOKEN}"
ITEMS=$(cat temp_file.json | jq -r '.items[].id')
echo "${ITEMS}" >> all_items_in_${REPO}.txt
done
echo "INFO: FINISHED gathering all component IDs for maven Repo ${REPO} in $(pwd)/all_items_in_${REPO}.txt"
#gather all assets for ID
for componentID in $(cat all_items_in_${REPO}.txt); do
# echo "debug: componentID ${componentID}"
curl -s -X GET -u ${USER}:"${PASSWORD}" "http://${HOST}:${PORT}/service/rest/beta/components/${componentID}" -H "accept: application/json" > this_component.json
#extract component metadata
searchstr=".id"
this_item_id=$(cat this_component.json | jq -r "${searchstr}")
# echo "debug: this_item_id: ${this_item_id}"
# echo "debug: componentID ${componentID}"
# echo "debug: versus ${this_item_id}"
searchstr=".group"
this_group=$(cat this_component.json | jq -r "${searchstr}")
# echo "debug: this_group: ${this_group}"
searchstr=".name"
this_name=$(cat this_component.json | jq -r "${searchstr}")
# echo "debug: this_name: ${this_name}"
searchstr=".version"
this_version=$(cat this_component.json | jq -r "${searchstr}")
# echo "debug: this_version: ${this_version}"
#extract downloadURLs
searchstr=".assets[].downloadUrl"
this_downloadurls=$(cat this_component.json | jq -r "${searchstr}")
# echo "debug: this_downloadurl: ${this_downloadurls}"
thisDownloadUrlCounted=$(echo ${this_downloadurls}|wc -l)
#extract path
searchstr=".assets[].path"
this_paths=$(cat this_component.json | jq -r "${searchstr}")
this_pathsCounted=$(echo ${this_paths}|wc -l)
mkdir -p ./assets/${componentID}
#echo "this_paths: ${this_paths}"
filepathWithArgs=""
counter=1
packaging=""
classifier=""
mvnFilelist="" #only for mvn
mvnClassifiers="" #only for mvn
mvnFiles="" #only for mvn - for classifiers
mvnPomfile="" #only for mvn
#Formulardaten zusammenbauen und dateien runterladen
for i in $(echo -e ${this_paths}); do
#echo "doing File: ${i}"
#detect wether md5sum-file or sha-file and leave out
if [[ ${i} =~ (\.md5$|\.sha1$) ]]; then
#echo "debug: md5sumfile detected: ${i}"
continue
fi
downloadFile ${componentID} "http://${HOST}:${PORT}/repository/${REPO}/${i}"
#detect wether pom or not (if so extract <packaging> tag)
if [[ ${i} =~ (\.pom$) ]]; then
packaging=$(xmllint --xpath "/*[name()='project']/*[name()='packaging']/text()" $(find ./assets/${componentID}/ -type f -name "*.pom"))
filepathWithArgs="${filepathWithArgs}-F \"maven2.asset${counter}=@./assets/${componentID}/${i}\" "
mvnPomfile="./assets/${componentID}/${i}"
((counter++))
continue
fi
# this_groupSlash=$(echo ${this_group} | sed -s/\./\//g)
# regexBefore="${this_groupSlash}/${this_name}/${this_version}/${this_name}-${this_version}-"
# regexAfter=""
#inbetween Classifier
#extract classifeier
classifier="$(echo -e ${i} | awk -F '/' '{print $NF}'|sed "s/${this_name}-${this_version}[-]*//" | sed "s/\....$//" )"
echo "classifier ${classifier}"
#this one is needed for curl-upload
#if [ ! -z ${classifier} ]; then
# filepathWithArgs="${filepathWithArgs}-F \"maven2.asset${counter}.classifier=${classifier}\" "
#fi
#this part is for mvn
#echo "mvnClassifiers: ${mvnClassifiers}"
if [ "${mvnClassifiers}" = "null" ]; then
echo "You should not come here. mvnClassifiers ${mvnClassifiers} matches for string 'null'"
echo "This means: either there is already a file or this Component has no classifiers"
echo "componentID: ${componentID}"
echo "Exiting..."
exit 1
elif [ -z "${mvnClassifiers}" -a -z "${classifier}" ]; then
#case: classifiers befor were not extracted before and now extraction brought zero length
mvnClassifiers="null"
elif [ "${mvnClassifiers}" = "null" -a -z "${classifier}" ]; then
#case: classifiers before brought zero and now no classifier was extracted
mvnClassifiers="null"
elif [ ! -z "${mvnClassifiers}" -a -z "${classifier}" ]; then
echo "You should not come here. mvnClassifiers ${mvnClassifiers} does not match for string 'null' but classifier ${classifier} has zero length"
echo "This means: either there is already a file or this Component has no classifiers"
echo "componentID: ${componentID}"
echo "Exiting..."
exit 1
elif [ -z "${mvnClassifiers}" -a ! -z "${classifier}" ]; then
mvnClassifiers="${classifier}"
if [ -z ${mvnFiles} ]; then
mvnFiles="./assets/${componentID}/${i}"
else
mvnFiles="${mvnFiles},./assets/${componentID}/${i}"
fi
else
mvnClassifiers="${mvnClassifiers},${classifier}"
mvnFiles="${mvnFiles},./assets/${componentID}/${i}"
fi
dateiendung="$(echo -e ${i} | rev |cut -d "/" -f 1|rev|sed "s/${this_name}-${this_version}-//"|rev|cut -f 1 -d "."|rev )"
echo "dateiendung: ${dateiendung}"
if [ ! -z ${dateiendung} ]; then
filepathWithArgs="${filepathWithArgs}-F \"maven2.asset${counter}.extension=${dateiendung}\" "
fi
#build Arguments for formula
filepathWithArgs="${filepathWithArgs}-F \"maven2.asset${counter}=@./assets/${componentID}/${i}\" "
mvnFilelist="${mvnFilelist},./assets/${componentID}/${i}"
((counter++))
done
#echo "debug: filepathWithArgs ${filepathWithArgs}"
###
# postMvnComponent via curl -> does not work
#if [ ! -z ${packaging} ]; then
# postMvnComponent ${this_group} ${this_name} ${this_version} ${packaging} unusedArg "${filepathWithArgs}"
#else
# postMvnComponent ${this_group} ${this_name} ${this_version} undef unusedArg "${filepathWithArgs}"
#fi
#mvnGroupId=${1}
#mvnArtifactId=${2}
#mvnVersion=${3}
#mvnPackaging=${4}
#numberOfFiles=${5} #notinuse
#listOfFiles=${6}
###
# mvnPublishFile via mvn publish
if [ ${mvnClassifiers} = "null" ]; then
mvnPublishFile ${this_group} ${this_name} ${this_version} ${packaging} null ${mvnFilelist} null ${mvnPomfile}
else
mvnPublishFile ${this_group} ${this_name} ${this_version} ${packaging} null ${mvnFiles} ${mvnClassifiers} ${mvnPomfile}
fi
#mvnGroupId="${1}"
#mvnArtifactId="${2}"
#mvnVersion="${3}"
#mvnPackaging="${4}"
#numberOfFiles="${5}" #notinuse
#listOfFiles="${6}" #komma separated
#classifiers="${7}" #komma separated
#pomFile="${8}"
echo "INFO: Component ${componentID} ${this_group} ${this_name}-${this_version} migrated"
rm -rf ./assets/${componentID}
done
cleanup()
exit 0