-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathnexus-exporter.sh
executable file
·618 lines (588 loc) · 18.5 KB
/
nexus-exporter.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
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
#!/bin/bash
# Please note, you will have to have a valid settings.xml file (in ~/.m2)
# and a valid .npmrc (in your home folder) for this script to actually publish stuff to DevOps
#
# Additionally your maven blob folder must start with maven-
# and your npm blob folders must start with npm-
#
# If you have proxies to other repositories, you can postfix them with -proxy and enable the
# skip_proxies setting to ignore them.
# Hint: if your blobs don't follow this format, just create a folder somewhere and symlink
# the blob folders to match the expected formats. Otherwise just edit away.
# Nexus Blobs directory
root_dir="/opt/sonatype/sonatype-work/nexus3/blobs"
# Where to store temporary work
export_dir="/tmp/nexus-backup"
# Virtual Maven Repository
repo_dir="${export_dir}/repository"
# Edit this to match the correct Artifacts Feed
# Check the Artifacts Feeds Connect To page for more information
organization_id="organization";
repo_id="repo";
project_id="project";
repo_url="https://pkgs.dev.azure.com/${organization_id}/${project_id}/_packaging/${repo_id}/maven/v1"
npm_url="https://pkgs.dev.azure.com/${organization_id}/${project_id}/_packaging/${repo_id}/npm/registry/"
# If you are using publishConfig with a repository on your npm packages put here the original repo
nexus_npm_url="https://someurl/nexus/repository/npm-telco-release";
# Set to 1 to deploy to Artifacts
deploy_artifacts=0;
# set to 1 to generate a SQL file with existing packages
generate_sql=0;
# set to 1 to generate a CSV file with existing packages
generate_csv=1;
# set to 1 to generate a virtual repository which can be used to deploy javadoc and sources
generate_virtual_repo=1;
# set to 1 to skip any repository that ends with proxy
skip_proxies=1;
# set to 1 to skip deploying maven javadoc files
skip_deploy_javadoc=1;
# set to 1 to skip deploying maven sources files
skip_deploy_sources=1;
# For better results, enable generate virtual repo and disable deploying javadoc and source files.
# Some tests
is_alpine=0;
is_debian=0;
is_rhel=0;
# test if alpine
if [[ -f /etc/alpine_release ]]; then
is_alpine=1;
fi
# test if debian/ubuntu
if [[ ( -f /etc/debian_version ) || ( -f /etc/lsb_release ) ]]; then
is_debian=1;
fi
# test if rhel/fedora/centos
if [[ ( -f /etc/redhat-release ) || ( -f /etc/fedora-release ) ]]; then
is_rhel=1;
fi
print_ok() {
echo -e "[ \e[32mOK\e[0m ]";
}
print_warn() {
echo -e "[\e[33mWARN\e[0m]";
}
print_ignr() {
echo -e "[\e[33mIGNR\e[0m]";
}
print_skip() {
echo -e "[\e[33mSKIP\e[0m]";
}
print_fail() {
echo -e "[\e[31mFAIL\e[0m]";
}
ensure_pkgs() {
echo -ne "\tChecking for Maven ... ";
mvn --version &> /dev/null
if [[ $? == 0 ]]; then
print_ok;
else
print_fail;
if [[ $is_alpine == 1 ]]; then
echo -e "\tInstalling maven (# apk add maven) ... "
apk add maven &> /dev/null;
if [[ $? == 0 ]]; then
print_ok;
else
print_fail;
exit 1;
fi
elif [[ $is_debian == 1 ]]; then
echo -e "\tInstalling maven (# apt install maven) ... "
apt install maven &> /dev/null;
if [[ $? == 0 ]]; then
print_ok;
else
print_fail;
exit 1;
fi
elif [[ $is_rhel == 1 ]]; then
echo -e "\tInstalling maven (# yum install apache-maven) ... "
yum install apache-maven &> /dev/null;
if [[ $? == 0 ]]; then
print_ok;
else
print_fail;
exit 1;
fi
else
exit 1;
fi
fi
echo -ne "\tChecking for NPM ... ";
npm --version &> /dev/null
if [[ $? == 0 ]]; then
print_ok;
else
print_fail;
if [[ $is_alpine == 1 ]]; then
echo -e "\tInstalling npm (# apk add npm) ... "
apk add npm &> /dev/null;
if [[ $? == 0 ]]; then
print_ok;
else
print_fail;
exit 1;
fi
elif [[ $is_debian == 1 ]]; then
echo -e "\tInstalling maven (# apt install npm) ... "
apt install npm &> /dev/null;
if [[ $? == 0 ]]; then
print_ok;
else
print_fail;
exit 1;
fi
elif [[ $is_rhel == 1 ]]; then
echo -e "\tInstalling maven (# yum install nodejs) ... "
yum install nodejs &> /dev/null;
if [[ $? == 0 ]]; then
print_ok;
else
print_fail;
exit 1;
fi
else
exit 1;
fi
fi
}
# ensure m2 folder
ensure_m2() {
echo -ne "\tChecking for local m2 folder ... "
if [[ -d ~/.m2 ]]; then
print_ok;
else
print_fail;
echo -ne "\tCreating m2 folder (# mkdir ~/.m2) ... "
mkdir ~/.m2 &> /dev/null
if [[ $? == 0 ]]; then
print_ok;
else
print_fail;
exit 1;
fi
fi
}
ensure_settings() {
echo -ne "\tChecking for settings.xml in ~/.m2 ... ";
if [[ -f ~/.m2/settings.xml ]]; then
print_ok;
else
if [[ $deploy_artifacts == 0 ]]; then
print_warn;
echo -e "\tPlease create a valid settings.xml file in the ~/.m2 folder!";
else
print_fail;
echo -e "\tPlease create a valid settings.xml file in the ~/.m2 folder!";
exit 1;
fi
fi
}
ensure_npmrc() {
echo -ne "\tChecking for .npmrc in ~/ ... ";
if [[ -f ~/.npmrc ]]; then
print_ok;
else
if [[ $deploy_artifacts == 0 ]]; then
print_warn;
echo -e "\tPlease create a valid .npmrc file in the ~/ folder!";
else
print_fail;
echo -e "\tPlease create a valid .npmrc file in the ~/ folder!";
exit 1;
fi
fi
}
print_settings() {
echo "Nexus exporter settings..."
echo
echo -e "\tNexus blob directory: ${root_dir}"
echo -e "\tExport directory: ${export_dir}"
echo -e "\tVirtual maven repository directory: ${repo_dir}";
echo
echo -e "\tArtifacts Feed identifier: ${repo_id}"
echo -e "\tArtifacts Maven URL: ${repo_url}";
echo -e "\tArtifacts NPM URL: ${npm_url}";
echo -e "\tNexus NPM URL: ${nexus_npm_url}";
echo
echo -e "\tDeploy to Artifacts: ${deploy_artifacts}";
echo -e "\tGenerate SQL: ${generate_sql}";
echo -e "\tGenerate CSV: ${generate_csv}";
echo
echo -e "\tSkip proxy repositories: ${skip_proxies}";
echo -e "\tSkip deploying maven javadoc packages: ${skip_deploy_javadoc}";
echo -e "\tSkip deploying maven sources packages: ${skip_deploy_sources}";
echo
echo "Validating environment..."
echo
}
print_settings;
ensure_pkgs;
ensure_m2;
ensure_settings;
ensure_npmrc;
echo
mkdir -p ${repo_dir};
generate_vrepo() {
if [[ ${generate_virtual_repo} == 0 ]]; then
return;
fi
echo "Generating virtual maven repository...";
for dir in ${root_dir}/*; do
dirname=$(basename $dir);
if [[ ( $skip_proxies == 1 ) && ( ${dirname##*-} == "proxy" ) ]]; then
continue;
fi
if [[ ${dirname:0:5} != "maven" ]]; then
continue;
fi
backup_dir="${repo_dir}/${dirname}"
mkdir -p ${backup_dir};
echo -e "\tProcessing \"${dirname}\"...";
new_dir="${dir}/content";
for vol in ${root_dir}/${dirname}/content/vol-*; do
volname=$(basename $vol);
if [[ ( "$volname" == "tmp" ) || ( "$volname" == "vol-*" ) ]]; then
continue;
fi
echo -e "\t\tProcessing volume \"${volname}\"...";
for chapter in ${vol}/chap-*; do
chaptername=$(basename $chapter);
if [[ "$chaptername" == "chap-*" ]]; then
continue;
fi
# process properties files
for file in ${chapter}/*.properties; do
fname=$(basename $file);
fname=${fname%%.*};
deleted=0;
sha="";
size=0;
creation_time=0;
repo_name="";
content_type="";
blob_name="";
group_id="";
artifact_id="";
version="";
qualifier="";
packaging="";
type="";
is_metadata=0;
# process atribute files
while IFS= read -r line; do
if [[ $line == deleted\=* ]]; then
deleted=${line##deleted\=}
if [[ $deleted == true ]]; then
deleted=1;
else
deleted=0;
fi
fi
if [[ $line == size* ]]; then
size=${line##size\=}
fi
if [[ $line == sha* ]]; then
sha=${line#sha1\=}
fi
if [[ $line == creationTime* ]]; then
creation_time=${line##creationTime\=}
fi
if [[ $line == \@BlobStore\.content\-type* ]]; then
content_type=${line##\@BlobStore\.content\-type\=}
fi
if [[ $line == \@BlobStore\.blob\-name* ]]; then
blob_name=${line##\@BlobStore\.blob\-name\=};
# some extra handling to figure out stuff
last_segment=${blob_name##*/};
if [[ ( $last_segment == "maven-metadata.xml" ) || ( $last_segment == "archetype-catalog.xml" ) ]]; then
is_metadata=1;
fi
#now replace the last segment within the line so that we can extract the version
verstr=${blob_name/\/${last_segment}/};
version=${verstr##*/};
artstr=${verstr/\/${version}/};
if [[ ${version} == \- ]]; then
aux=${last_segment##*-};
version=${aux%\.*};
fi
# handle now artifactId
artifact_id=${artstr##*/};
# handle groupId
groupstr=${artstr/\/${artifact_id}/};
if [[ ${groupstr} != ${artifact_id} ]]; then
group_id=${groupstr//\//\.};
fi
# handle qualifier
aux=${last_segment/${artifact_id}-${version}/};
type=${aux##*.};
packaging=${aux#*.};
if [[ ${aux:0:1} == "-" ]]; then
aux=${aux##*-};
aux=${aux%%.*};
qualifier=$aux;
fi
fi
if [[ $line == \@Bucket\.repo\-name* ]]; then
repo_name=${line##\@Bucket\.repo\-name\=}
fi
done < "$file";
if [[ ( $deleted == 1 ) || ( ${type} == md5 ) || ( ${type} == sha1 ) || ( ${is_metadata} == 1 ) ]]; then
continue;
fi
# generate virtual repo
bin_file=${file/.properties/.bytes};
skip_deploy=0;
artifact_type="maven";
vdir="${backup_dir}/${group_id}/${artifact_id}/${version}";
mkdir -p ${vdir}
vfile="${artifact_id}-${version}";
echo -ne "\t\t\tLinking ${artifact_type} blob \"${bin_file}\" to virtual artifact (${group_id}/${artifact_id}-${version}";
if [[ ${#qualifier} > 0 ]]; then
echo -ne "-${qualifier}";
vfile="${vfile}-${qualifier}";
fi
echo -ne ".${packaging}) ... ";
vfile="${vfile}.${packaging}";
ln -sf ${bin_file} "${vdir}/${vfile}";
if [[ $? == 0 ]]; then
print_ok;
else
print_fail;
fi
done
done
done
done
}
process_blobs() {
echo "Processing blobs..."
for dir in ${root_dir}/*; do
dirname=$(basename $dir);
if [[ ( $skip_proxies == 1 ) && ( ${dirname##*-} == "proxy" ) ]]; then
continue;
fi
vrepo_dir="${repo_dir}/${dirname}"
backup_dir="${export_dir}/${dirname}"
export_file="${export_dir}/${dirname}.sql"
csv_file="${export_dir}/${dirname}.csv"
if [[ $generate_csv == 1 ]]; then
echo "sha1,size,deleted,creationTime,blobName,groupId,artifactId,version,type,qualifier,contentType,repoName,store,volume,chapter" > ${csv_file};
fi
mkdir -p ${backup_dir};
rm ${export_file} &> /dev/null;
echo -e "\tProcessing \"${dirname}\"...";
new_dir="${dir}/content";
for vol in ${root_dir}/${dirname}/content/vol-*; do
volname=$(basename $vol);
if [[ ( "$volname" == "tmp" ) || ( "$volname" == "vol-*" ) ]]; then
continue;
fi
echo -e "\t\tProcessing volume \"${volname}\"...";
for chapter in ${vol}/chap-*; do
chaptername=$(basename $chapter);
if [[ "$chaptername" == "chap-*" ]]; then
continue;
fi
chapter_dir=${backup_dir}/${volname}/${chaptername};
mkdir -p ${chapter_dir};
# process properties files
for file in ${chapter}/*.properties; do
fname=$(basename $file);
fname=${fname%%.*};
deleted=0;
sha="";
size=0;
creation_time=0;
repo_name="";
content_type="";
blob_name="";
group_id="";
artifact_id="";
version="";
qualifier="";
packaging="";
type="";
is_metadata=0;
# process attribute files
while IFS= read -r line; do
if [[ $line == deleted\=* ]]; then
deleted=${line##deleted\=}
if [[ $deleted == true ]]; then
deleted=1;
else
deleted=0;
fi
fi
if [[ $line == size* ]]; then
size=${line##size\=}
fi
if [[ $line == sha* ]]; then
sha=${line#sha1\=}
fi
if [[ $line == creationTime* ]]; then
creation_time=${line##creationTime\=}
fi
if [[ $line == \@BlobStore\.content\-type* ]]; then
content_type=${line##\@BlobStore\.content\-type\=}
fi
if [[ $line == \@BlobStore\.blob\-name* ]]; then
blob_name=${line##\@BlobStore\.blob\-name\=};
# some extra handling to figure out stuff
last_segment=${blob_name##*/};
if [[ ( $last_segment == "maven-metadata.xml" ) || ( $last_segment == "archetype-catalog.xml" ) ]]; then
is_metadata=1;
fi
#now replace the last segment within the line so that we can extract the version
verstr=${blob_name/\/${last_segment}/};
version=${verstr##*/};
artstr=${verstr/\/${version}/};
if [[ ${version} == \- ]]; then
aux=${last_segment##*-};
version=${aux%\.*};
fi
# handle now artifactId
artifact_id=${artstr##*/};
# handle groupId
groupstr=${artstr/\/${artifact_id}/};
if [[ ${groupstr} != ${artifact_id} ]]; then
group_id=${groupstr//\//\.};
fi
# handle qualifier
aux=${last_segment/${artifact_id}-${version}/};
type=${aux##*.};
packaging=${aux#*.};
if [[ ${aux:0:1} == "-" ]]; then
aux=${aux##*-};
aux=${aux%%.*};
qualifier=$aux;
fi
if [[ ( $qualifier == javadoc ) && ( ${type} != sha1 ) && ( ${type} != md5 ) ]]; then
packaging="javadoc"
elif [[ ( $qualifier == sources ) && ( ${type} != sha1 ) && ( ${type} != md5 ) ]]; then
packaging="java-source"
fi
fi
if [[ $line == \@Bucket\.repo\-name* ]]; then
repo_name=${line##\@Bucket\.repo\-name\=}
fi
done < "$file";
# ignore deleted files
if [[ ( $generate_sql == 1) && ( $deleted == 0 ) && ( $type != $artifact_id ) ]]; then
# echo to file
echo "INSERT INTO ARTIFACTS (sha1,size,deleted,creationTime,blobName,groupId,artifactId,version,type,qualifier,contentType,repoName,store,volume,chapter) VALUES ('${sha}',${size},${deleted},${creation_time},'${blob_name}','${group_id}','${artifact_id}','${version}','${type}','${qualifier}','${content_type}','${repo_name}','${dirname}','${volname}','${chaptername}');" >> ${export_file};
fi
if [[ ( $generate_csv == 1) && ( $deleted == 0 ) && ( $type != $artifact_id ) ]]; then
# echo to file
echo '"${sha}",${size},${deleted},${creation_time},"${blob_name}","${group_id}","${artifact_id}","${version}","${type}","${qualifier}","${content_type}","${repo_name}","${dirname}","${volname}","${chaptername}"' >> ${csv_file};
fi
if [[ ( $deploy_artifacts == 1) && ( $deleted == 0 ) && ( $is_metadata == 0 ) && ( $type != md5 ) && ( $type != sha1 ) && ( $type != $artifact_id ) ]]; then
bin_file=${file/.properties/.bytes};
skip_deploy=0;
# Upload to devops
artifact_type="maven";
if [[ ${dirname:0:3} == npm ]]; then
artifact_type="npm";
fi
vdir="${vrepo_dir}/${group_id}/${artifact_id}/${version}";
vfile="${artifact_id}-${version}";
echo -ne "\t\t\tDeploying ${artifact_type} blob \"${bin_file}\" to Artifacts (${group_id}/${artifact_id}-${version}";
if [[ ${#qualifier} > 0 ]]; then
echo -ne "-${qualifier}";
fi
if [[ $qualifier == "javadoc" ]]; then
echo -ne ".jar ";
elif [[ $qualifier == "java-source" ]]; then
echo -ne ".jar ";
else
echo -ne ".${packaging} ";
fi
has_javadoc=0;
has_sources=0;
fjavadoc=""
fsources="";
if [[ ( $type == jar ) && ( $qualifier != "java-source" ) && ( ${qualifier} != "javadoc" ) ]]; then
if [[ -f "${vdir}/${vfile}-javadoc.jar" ]]; then
fjavadoc="${vdir}/${vfile}-javadoc.jar";
has_javadoc=1;
echo -ne "[javadoc]";
fi
if [[ -f "${vdir}/${vfile}-sources.jar" ]]; then
has_sources=1;
fsources="${vdir}/${vfile}-sources.jar";
echo -ne "[sources]";
fi
fi
echo -ne ") ... ";
# check if we should skip deployment
if [[ ( $skip_deploy_karafassembly == 1 ) && ( ${is_assembly} == 1 ) ]]; then
skip_deploy=1;
elif [[ ( $skip_deploy_javadoc == 1 ) && ( ${qualifier} == "javadoc" ) ]]; then
skip_deploy=1;
elif [[ ( $skip_deploy_sources == 1 ) && ( ${qualifier} == "sources" ) ]]; then
skip_deploy=1;
fi
if [[ ( $skip_deploy == 0 ) && ( ${dirname:0:5} == maven ) && ( ${dirname##*-} != proxy ) ]]; then
# do not generate poms to avoid fails on actual pom upload
cmd="mvn -nsu -B -ntp deploy:deploy-file -DgeneratePom=false -DgroupId=${group_id} -DartifactId=${artifact_id} -Dversion=${version} -Dpackaging=${packaging} -Dfile=${bin_file} -DrepositoryId=${repo_id} -Durl=${repo_url}";
if [[ ${#qualifier} > 0 ]]; then
cmd="${cmd} -Dclassifier=${qualifier}";
fi
# also we will only try to upload sources and javadoc for jar files
if [[ ( $type == jar ) && ( $qualifier != "java-source" ) && ( ${qualifier} != "javadoc" ) ]]; then
if [[ $has_sources == 1 ]]; then
cmd="${cmd} -Dsources=${fsources}";
fi
if [[ $has_javadoc == 1 ]]; then
cmd="${cmd} -Djavadoc=${fjavadoc}";
fi
fi
cmd="${cmd} &> /dev/null";
eval ${cmd};
if [[ $? == 0 ]]; then
print_ok;
else
print_fail;
fi
elif [[ ( $skip_deploy == 0 ) && ( ${dirname:0:3} == npm ) && ( ${dirname##*-} != proxy ) ]]; then
tgz_file=$(basename ${file/.properties/.tgz});
# we need to change the package json BEFORE publishing
tar xf ${bin_file};
# this will extract to /tmp (which is where we should be running this)
# fix non lowercase package names
pname=$(sed -rn 's/.*"name":.*"([^"]+)",/\1/p' "package/package.json" | tr '\r' '\n');
sed -i 's|.*"name":.*"'${pname}'",| "name": "'${pname,,}'",|g' "package/package.json";
# fix non valid semver versions
sed -ri 's/.*"version":.*"([0-9]+)\.([0-9]+)\.([0-9]+)\.([0-9]+)",/ "version": "\1.\2.\3-\4",/g' "package/package.json";
# now edit the package/package.json file
sed -i "s|${nexus_npm_url}|${npm_url}|g" "package/package.json"
# now create a new tar file
tar czf ${tgz_file} package;
# and publish
npm publish ${tgz_file} &> /dev/null;
if [[ $? == 0 ]]; then
print_ok;
else
print_fail;
fi
# clean up package folder
rm -rf package &> /dev/null;
# clean up tgz file
rm -f ${tgz_file};
elif [[ $skip_deploy == 1 ]]; then
print_skip;
else
print_ignr;
fi
fi
done
done
done
done
}
# generate a virtual repo
generate_vrepo;
# then process blobs
process_blobs;
echo
echo "Script finished.";