-
Notifications
You must be signed in to change notification settings - Fork 134
/
PackageBuildOutputs.groovy
1341 lines (1172 loc) · 54.7 KB
/
PackageBuildOutputs.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
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
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
@groovy.transform.BaseScript com.ibm.dbb.groovy.ScriptLoader baseScript
import java.io.File
import com.ibm.dbb.build.CopyToHFS
import com.ibm.dbb.build.BuildProperties
import com.ibm.dbb.build.DBBConstants
import com.ibm.dbb.build.DBBConstants.CopyMode
import com.ibm.dbb.build.report.BuildReport
import com.ibm.dbb.build.report.records.*
import groovy.transform.*
import groovy.cli.commons.*
import groovy.io.FileType
import java.nio.file.*
import static java.nio.file.StandardCopyOption.*
import com.ibm.jzos.ZFile;
/************************************************************************************
* This script creates a simplified package with the outputs generated from a DBB build
* Optionally, it publishes it to an Artifact repository.
*
* usage: see help command or README
*
* Version 0 - 2019
* called PublishLoadModule.groovy and located in Build/PublishLoadModules
*
* Version 1 - 2021
* Re-Design to run as a post-build script and make publishing optional
*
* Version 2 - 2022-02
* - Externalize the Map of LLQ to CopyMode
* - Add capablity to add additional files from build workspace
* - Verbose logging will print tar contents
*
* Version 3 - 2022-08
* - Ability to pass multiple build reports to build a cumulative package
* - Add an optional option to add the deploy type extension to the member
*
* Version 4 - 2022-12
* - Generalized the options to publish package to artifact repository
* - Script is supporting both JFrog Artifactory and SonarType Nexus
* - Added additional CLI option to pass artifact repository parameters
*
* Version 5 - 2023-07
* - Added support for DeployableArtifact, to manage the correct stacking
* of duplicates artifacts
*
* Version 6 - 2024-03
* - Added support to write IBM Wazi Deploy Application Manifest file
*
* Version 7 - 2024-04
* - Added support to SBOM files
*
* Version 8 - 2024-07
* - Reworked error management and fixed few glitches
*
* Version 9 - 2024-10
* - Added the following
* a) Fields and code to generate concert manifest linking to
* concertBuildManifestGenerator.groovy
* b) Refactoring to generate SBOM details like SerialNumber from this script
* to be passed to sbomGenerator.groovy and concertBuildManifestGenerator.groovy
*
* Version 10 - 2024-11
* - Enhanced the packaging with the Application Descriptor file
* and baseline packages which allow full description of interfaces
*
************************************************************************************/
// start create & publish package
@Field Properties props = null
def scriptDir = new File(getClass().protectionDomain.codeSource.location.path).parent
@Field def wdManifestGeneratorUtilities = loadScript(new File("${scriptDir}/utilities/WaziDeployManifestGenerator.groovy"))
@Field def applicationDescriptorUtils
@Field def applicationDescriptor
@Field def sbomUtilities
@Field def concertManifestGeneratorUtilities
@Field def rc = 0
@Field def sbomSerialNumber
@Field def sbomFileName
@Field def concertBuild
@Field String includeSubfolder = "include"
@Field String libSubfolder = "lib"
@Field String binSubfolder = "bin"
@Field def tempLoadDir
def startTime = new Date()
props = parseInput(args)
props.startTime = startTime.format("yyyyMMdd.HHmmss.SSS")
println("** PackageBuildOutputs start at $props.startTime")
println("** Properties at startup:")
props.sort().each { k,v->
if ( k == "artifactRepository.password" )
println " $k -> xxxxxx "
else
println " $k -> $v"
}
if (rc != 0) {
println("*! [ERROR] One or several properties were missing in the configuration. Review the console output.")
System.exit(rc)
}
// Enable file tagging
BuildProperties.setProperty("dbb.file.tagging", "true") // Enable dbb file tagging
// Map of last level dataset qualifier to DBB CopyToHFS CopyMode.
def copyModeMap = parseCopyModeMap(props.copyModeMap)
// Hashmap of BuildOutput to Record
Map<DeployableArtifact, Map> buildOutputsMap = new HashMap<DeployableArtifact, Map>()
// Field to store default tarFileLabel (buildInfo.label) when cli argument tarFileName is not passed.
@Field def String tarFileLabel = "Default"
@Field def String tarFileName = ""
// Field to store build number, set to default when its not decipherable from build report
def String buildNumber = "UNKNOWN"
// Object to store scm information for Wazi Deploy Application Manifest file
HashMap<String,String> scmInfo = new HashMap<String, String>()
// Package the public Include Files and service submodules
// if Path to Application Descriptor file is specified
if (props.publishInterfaces && props.publishInterfaces.toBoolean()) {
File applicationDescriptorFile = new File("${props.applicationFolderPath}/applicationDescriptor.yml")
if (applicationDescriptorFile.exists()) {
applicationDescriptorUtils = loadScript(new File("utilities/applicationDescriptorUtils.groovy"))
applicationDescriptor = applicationDescriptorUtils.readApplicationDescriptor(applicationDescriptorFile)
} else {
println("*! [ERROR] No Application Descriptor file '${props.applicationFolderPath}/applicationDescriptor.yml' found. Exiting.")
System.exit(1)
}
}
// iterate over all build reports to obtain build output
props.buildReportOrder.each { buildReportFile ->
Map<DeployableArtifact, Map> temporaryBuildOutputsMap = new HashMap<DeployableArtifact, Map>()
println("** Read build report data from '${buildReportFile}'.")
def jsonOutputFile = new File(buildReportFile)
if (!jsonOutputFile.exists()){
println("*! [ERROR] Build Report '$buildReportFile' not found.")
rc = 1
} else {
def buildReport= BuildReport.parse(new FileInputStream(jsonOutputFile))
// Read buildInfo to obtain build information
def buildInfo = buildReport.getRecords().findAll{
try {
it.getType()==DefaultRecordFactory.TYPE_BUILD_RESULT
} catch (Exception e){}
}
if (buildInfo.size() != 0) {
tarFileLabel = buildInfo[0].label
buildNumber = buildInfo[0].label
}
// retrieve the buildResultPropertiesRecord
def buildResultPropertiesRecord = buildReport.getRecords().find {
try {
it.getType()==DefaultRecordFactory.TYPE_PROPERTIES && it.getId()=="DBB.BuildResultProperties"
} catch (Exception e){}
}
// finds all the build outputs with a deployType
def buildRecords = buildReport.getRecords().findAll{
try {
(it.getType()==DefaultRecordFactory.TYPE_EXECUTE || it.getType()==DefaultRecordFactory.TYPE_COPY_TO_PDS) &&
!it.getOutputs().isEmpty()
} catch (Exception e){}
}
// finds all the build outputs with a deployType
// Today the USS_RECORD type is built using an AnyTypeRecord record
// An Idea is currently opened to have an official USS_RECORD: https://ideas.ibm.com/ideas/DBB-I-43
def ussBuildRecords = buildReport.getRecords().findAll{
try {
it.getType()=="USS_RECORD" && !it.getAttribute("outputs").isEmpty()
} catch (Exception e){}
}
// find all deletions using the DELETE_RECORD of zAppBuild
def deletionRecords = buildReport.getRecords().findAll {
try {
// Obtain delete records, which got added by zAppBuild
it.getType() == "DELETE_RECORD"
} catch (Exception e) {
println e
}
}
if (props.deployTypeFilter) {
println("** Filter Output Records on following deployTypes: ${props.deployTypeFilter}...")
buildRecords.each {
// filtered executes
def filteredOutputs = it.getOutputs().findAll { o ->
o.deployType != null && (props.deployTypeFilter).split(',').contains(o.deployType)
}
// Manipulating the scope of build outputs
it.getOutputs().clear()
it.getOutputs().addAll(filteredOutputs)
}
ussBuildRecords.each {
ArrayList<ArrayList> outputs = []
it.getAttribute("outputs").split(';').collectEntries { entry ->
outputs += entry.replaceAll('\\[|\\]', '').split(',')
}
ArrayList<String> filteredOutputs = []
outputs.each { output ->
rootDir = output[0].trim()
file = output[1].trim()
deployType = output[2].trim()
if (!(props.deployTypeFilter).split(',').contains(deployType)) {
filteredOutputs += output.toString()
}
}
def filteredOutputsStrings = String.join(";", filteredOutputs)
it.setAttribute("outputs", filteredOutputsStrings)
}
} else {
// Remove outputs without deployType + ZUNIT-TESTCASEs
println("** Remove output records without deployType or with deployType=ZUNIT-TESTCASE")
buildRecords.each {
def unwantedOutputs = it.getOutputs().findAll{ o ->
o.deployType == null || o.deployType == 'ZUNIT-TESTCASE'
}
it.getOutputs().removeAll(unwantedOutputs)
}
}
buildRecords += ussBuildRecords // append USS records
def datasetMembersCount = 0
def zFSFilesCount = 0
def deletionCount = 0
// adding files and executes with outputs to Hashmap to remove redundant data
buildRecords.each{ buildRecord ->
if (buildRecord.getType()=="USS_RECORD") {
if (!buildRecord.getAttribute("outputs").isEmpty()) {
ArrayList<ArrayList> outputs = []
buildRecord.getAttribute("outputs").split(';').collectEntries { entry ->
outputs += entry.replaceAll('\\[|\\]', '').split(',')
}
zFSFilesCount += outputs.size()
outputs.each{ output ->
rootDir = output[0].trim()
file = output[1].trim()
deployType = output[2].trim()
def dependencySetRecord = buildReport.getRecords().find {
it.getType()==DefaultRecordFactory.TYPE_DEPENDENCY_SET && it.getFile().equals(file)
}
temporaryBuildOutputsMap.put(new DeployableArtifact(file, deployType, "zFSFile"), [
container: rootDir,
owningApplication: props.application,
record: buildRecord,
propertiesRecord: buildResultPropertiesRecord,
dependencySetRecord: dependencySetRecord
])
}
}
} else {
if (buildRecord.getOutputs().size() != 0) {
buildRecord.getOutputs().each { output ->
def (dataset, member) = getDatasetName(output.dataset)
def fileUsage
if (applicationDescriptor && output.deployType.equals("OBJ")) {
fileUsage = applicationDescriptorUtils.getFileUsageByType(applicationDescriptor, "Program", member)
}
// If the artifact is not an Object Deck or has no usage or its usage is not main
if ((output.deployType.equals("OBJ") && fileUsage && (fileUsage.equals("internal submodule") || fileUsage.equals("service submodule"))) || !output.deployType.equals("OBJ")) {
datasetMembersCount++
String file = buildRecord.getFile()
def dependencySetRecord = buildReport.getRecords().find {
it.getType()==DefaultRecordFactory.TYPE_DEPENDENCY_SET && it.getFile().equals(file)
}
temporaryBuildOutputsMap.put(new DeployableArtifact(member, output.deployType, "DatasetMember"), [
container: dataset,
owningApplication: props.application,
record: buildRecord,
propertiesRecord: buildResultPropertiesRecord,
dependencySetRecord: dependencySetRecord
])
} else {
if (props.verbose) println("*! Build output ${output.dataset} with deployType '${output.deployType}' has been excluded from packaging.")
}
}
}
}
}
deletionRecords.each { deleteRecord ->
deletionCount += deleteRecord.getAttributeAsList("deletedBuildOutputs").size()
deleteRecord.getAttributeAsList("deletedBuildOutputs").each{ deletedFile ->
String cleansedDeletedFile = ((String) deletedFile).replace('"', '');
def (dataset, member) = getDatasetName(cleansedDeletedFile)
// search for an existing deployableArtifacts record
ArrayList<DeployableArtifact> filteredDeployableArtifacts = new ArrayList()
temporaryBuildOutputsMap.each { DeployableArtifact deployableArtifact, Map info ->
if (deployableArtifact.file == deleteRecord.getAttribute("file")) {
filteredDeployableArtifacts.add(deployableArtifact, info)
}
}
if (filteredDeployableArtifacts){
filteredDeployableArtifacts.each {deployableArtifact, info ->
String container = info.get("container")
if (container == dataset && member == deployableArtifact.file) {
deployType = deployableArtifact.deployType
// remove any existing change
temporaryBuildOutputsMap.remove(deployableArtifact)
// add deletion
temporaryBuildOutputsMap.put(new DeployableArtifact(member, deployType, "DatasetMemberDelete"), [
container: dataset,
owningApplication: props.application,
record: buildRecord,
propertiesRecord: buildResultPropertiesRecord,
dependencySetRecord: dependencySetRecord
])
}
}
} else {
deployType = dataset.replaceAll(/.*\.([^.]*)/, "\$1") // DELETE_RECORD does not contain deployType attribute. Use LLQ
temporaryBuildOutputsMap.put(new DeployableArtifact(member, deployType, "DatasetMemberDelete"), [
container: dataset,
owningApplication: props.application,
record: deleteRecord,
propertiesRecord: buildResultPropertiesRecord
])
}
}
}
// Print summary of BuildReport
if ( datasetMembersCount + zFSFilesCount == 0 ) {
println("** No items to package in '$buildReportFile'.")
} else {
println("** ${temporaryBuildOutputsMap.size()} Build outputs detected in '$buildReportFile':")
temporaryBuildOutputsMap.each { deployableArtifact, info ->
String container = info.get("container")
String owningApplication = info.get("owningApplication")
Record record = info.get("record")
PropertiesRecord propertiesRecord = info.get("propertiesRecord")
DependencySetRecord dependencySetRecord = info.get("dependencySetRecord")
println("\t'${deployableArtifact.file}' from '${container}' with Deploy Type '${deployableArtifact.deployType}'")
}
}
// Log detected deleted files
if (deletionCount != 0) {
println("** Deleted files detected in '$buildReportFile':")
deletionRecords.each { it.getAttributeAsList("deletedBuildOutputs").each { println(" ${it}")}}
}
buildOutputsMap.putAll(temporaryBuildOutputsMap)
// generate scmInfo for Wazi Deploy Application Manifest file
if ((props.generateWaziDeployAppManifest && props.generateWaziDeployAppManifest.toBoolean()) ||
(props.generateConcertBuildManifest && props.generateConcertBuildManifest.toBoolean() )) {
if (props.buildReportOrder.size() == 1) {
scmInfo.put("type", "git")
gitUrl = retrieveBuildResultProperty (buildResultPropertiesRecord, "giturl")
if (gitUrl) scmInfo.put("uri", gitUrl)
gitHash = retrieveBuildResultProperty (buildResultPropertiesRecord, "githash")
if (gitHash) scmInfo.put("shortCommit", gitHash)
scmInfo.put("branch", props.branch)
} else {
scmInfo.put("shortCommit", "multipleBuildReports")
scmInfo.put("uri", "multipleBuildReports")
}
}
}
}
if (rc == 0) {
if (buildOutputsMap.size() == 0) {
println("** There are no build outputs found in all provided build reports. Exiting.")
rc = 0
} else {
// Initialize Wazi Deploy Manifest Generator
if (props.generateWaziDeployAppManifest && props.generateWaziDeployAppManifest.toBoolean()) {
wdManifestGeneratorUtilities.initWaziDeployManifestGenerator(props)// Wazi Deploy Application Manifest
wdManifestGeneratorUtilities.setScmInfo(scmInfo)
if (props.externalDependenciesEvidences) {
File externalDependenciesEvidenceFile = new File("${props.externalDependenciesEvidences}")
if (externalDependenciesEvidenceFile.exists()){
wdManifestGeneratorUtilities.setExternalDependencies(externalDependenciesEvidenceFile)
} else {
println("** External build dependencies file not found (${props.externalDependenciesEvidences}). Exiting.")
rc=4
}
}
}
// Initialize SBOM
if (props.generateSBOM && props.generateSBOM.toBoolean()) {
sbomUtilities = loadScript(new File("${scriptDir}/utilities/sbomGenerator.groovy"))
sbomSerialNumber = "url:uuid:" + UUID.randomUUID().toString()
sbomFileName = "${buildNumber}_sbom.json"
sbomUtilities.initializeSBOM(props.sbomAuthor, sbomSerialNumber)
}
// Initialize Concert Build Manifest Generator
if (props.generateConcertBuildManifest && props.generateConcertBuildManifest.toBoolean()) {
// Concert Build Manifest
concertManifestGeneratorUtilities = loadScript(new File("${scriptDir}/utilities/concertBuildManifestGenerator.groovy"))
concertManifestGeneratorUtilities.initConcertBuildManifestGenerator()
concertBuild = concertManifestGeneratorUtilities.addBuild(props.application, props.versionName, buildNumber)
concertManifestGeneratorUtilities.addRepositoryToBuild(concertBuild, scmInfo.uri, scmInfo.branch, scmInfo.shortCommit)
}
// Local variables
tarFileName = (props.tarFileName) ? props.tarFileName : "${tarFileLabel}.tar"
def tarFile = "$props.workDir/${tarFileName}"
//Create a temporary directory on zFS to copy the load modules from data sets to
tempLoadDir = new File("$props.workDir/tempPackageDir")
!tempLoadDir.exists() ?: tempLoadDir.deleteDir()
tempLoadDir.mkdirs()
// A baseline Package has been specified, we then extract it in the $tempLoadDir folder
if (props.baselinePackageFilePath) {
File baselinePackageFile = new File(props.baselinePackageFilePath)
if (baselinePackageFile.exists()) {
println("** Extract the baseline package from '${props.baselinePackageFilePath}'")
def processCmd = [
"sh",
"-c",
"tar -xUXf ${props.baselinePackageFilePath}"
]
def processRC = runProcess(processCmd, tempLoadDir)
rc = Math.max(rc, processRC)
if (rc == 0) {
println("** Baseline Package '${props.baselinePackageFilePath}' successfully extracted.")
// Read the existing Wazi Deploy Manifest if any
File wdManifestFile = new File("$tempLoadDir/wazideploy_manifest.yml")
if (wdManifestFile.exists()) {
if (props.generateWaziDeployAppManifest && props.generateWaziDeployAppManifest.toBoolean()) {
// Read the manifest file if it exists
wdManifestGeneratorUtilities.readWaziDeployManifestFile(wdManifestFile, props)
wdManifestGeneratorUtilities.setScmInfo(scmInfo)
} else {
wdManifestFile.delete()
}
} else {
if (props.generateWaziDeployAppManifest && props.generateWaziDeployAppManifest.toBoolean()) {
// Otherwise initialize an empty manifest
wdManifestGeneratorUtilities.initWaziDeployManifestGenerator(props)
wdManifestGeneratorUtilities.setScmInfo(scmInfo)
}
}
// Search in all subfolders of the archive except the folders
// that contains includes "$includeSubfolder" and binaries "$binSubfolder"
// Copy the artifacts found to comply with the right structure
// All the artifact that don't comply will end up in the binSubfolder
tempLoadDir.eachDir() { subfolder ->
if (!subfolder.getName().equals(includeSubfolder) && !subfolder.getName().equals(libSubfolder)) {
subfolder.eachFileRecurse(FileType.FILES) { file ->
String fileName = file.getName()
def fileNameParts = fileName.split("\\.")
if (fileNameParts.size() > 1) {
fileName = fileNameParts.first()
String fileDeployType = fileNameParts.last()
if (props.fullPackage && props.fullPackage.toBoolean()) {
String expectedFilePath = "$tempLoadDir/$binSubfolder/$fileDeployType/$fileName"
try {
Path destinationPath = Paths.get("$tempLoadDir/$binSubfolder/$fileDeployType/${fileName}.${fileDeployType}")
Path destinationDirPath = destinationPath.getParent()
destinationDirPath.toFile().mkdirs()
Path sourcePath = file.toPath()
copyFiles(sourcePath.toString(), destinationPath.toString())
println("\tCopy file '${sourcePath}' to '${destinationPath}'")
if (props.generateWaziDeployAppManifest && props.generateWaziDeployAppManifest.toBoolean()) {
// Update Path for the moved file in Wazi Deploy Manifest
rc = rc + wdManifestGeneratorUtilities.updateArtifactPathToManifest(fileName, fileDeployType, "$binSubfolder/$fileDeployType/${fileName}.${fileDeployType}")
}
} catch (IOException e) {
println("!* [ERROR] Error when moving file '${sourcePath}' to '${destinationPath}' during baseline package extraction.")
rc = 1
}
} else {
file.delete()
if (props.generateWaziDeployAppManifest && props.generateWaziDeployAppManifest.toBoolean()) {
wdManifestGeneratorUtilities.removeArtifactFromManifest(fileName, fileDeployType)
}
}
}
}
subfolder.deleteDir()
}
if (subfolder.getName().equals("tmp")) {
subfolder.deleteDir()
}
}
} else {
println("*! [ERROR] Error when extracting baseline package '${created}' with rc=$rc.")
rc = 1
}
} else {
println("*! [ERROR] The Baseline Package '${props.baselinePackageFilePath}' was not found.")
rc = 1
}
}
if (rc == 0) {
println("** Total number of build outputs to package: ${buildOutputsMap.size()}")
def publicInterfacesDeployTypes
def privateInterfacesDeployTypes
def processedArtifacts = 0 // used as a checksum that all files got categorized
if (props.publicInterfacesDeployTypes) {
publicInterfacesDeployTypes = props.publicInterfacesDeployTypes.split(",") // split comma separated list into list
} else {
println("*! [WARNING] Property 'publicInterfacesDeployTypes' not defined, using default types 'OBJ'.")
publicInterfacesDeployTypes = ["OBJ"]
}
if (props.privateInterfacesDeployTypes) {
privateInterfacesDeployTypes = props.privateInterfacesDeployTypes.split(",") // split comma separated list into list
} else {
println("*! [WARNING] Property 'privateInterfacesDeployTypes' not defined, using default types 'OBJ,BMSCOPY'.")
privateInterfacesDeployTypes = "OBJ,BMSCOPY".split(",")
}
def deployableOutputs = buildOutputsMap.findAll { deployableArtifact, info ->
!((publicInterfacesDeployTypes && publicInterfacesDeployTypes.contains(deployableArtifact.deployType)) || (privateInterfacesDeployTypes && privateInterfacesDeployTypes.contains(deployableArtifact.deployType)))
}
if (deployableOutputs && !deployableOutputs.isEmpty()) {
println("** Copy ${deployableOutputs.size()} deployable artifacts to temporary package directory '$tempLoadDir/$binSubfolder'")
copyArtifactsToUSS(deployableOutputs, binSubfolder, copyModeMap)
processedArtifacts += deployableOutputs.size()
}
if (props.publishInterfaces && props.publishInterfaces.toBoolean()) {
def publicInterfaces
if (publicInterfacesDeployTypes) {
// build outputs that are mapped to a public deployType and are flagged as 'service submodule' in the application descriptor
publicInterfaces = buildOutputsMap.findAll { deployableArtifact, info ->
if (deployableArtifact.deployType.equals("OBJ")) {
fileUsage = applicationDescriptorUtils.getFileUsageByType(applicationDescriptor, "Program", deployableArtifact.file)
publicInterfacesDeployTypes.contains(deployableArtifact.deployType) && fileUsage && fileUsage.equals("service submodule")
} else {
publicInterfacesDeployTypes.contains(deployableArtifact.deployType)
}
}
if (publicInterfaces && !publicInterfaces.isEmpty()) {
println("** Copy ${publicInterfaces.size()} public interfaces to temporary package directory '$tempLoadDir/$includeSubfolder'")
copyArtifactsToUSS(publicInterfaces, includeSubfolder, copyModeMap)
processedArtifacts += publicInterfaces.size()
}
}
def privateInterfaces
if (privateInterfacesDeployTypes) {
// build outputs that are mapped to a public deployType and are flagged as 'service submodule' in the application descriptor
privateInterfaces = buildOutputsMap.findAll { deployableArtifact, info ->
if (deployableArtifact.deployType.equals("OBJ")) {
fileUsage = applicationDescriptorUtils.getFileUsageByType(applicationDescriptor, "Program", deployableArtifact.file)
privateInterfacesDeployTypes.contains(deployableArtifact.deployType) && fileUsage && fileUsage.equals("internal submodule")
} else {
privateInterfacesDeployTypes.contains(deployableArtifact.deployType)
}
}
println("** Copy ${privateInterfaces.size()} private interfaces to temporary package directory '$tempLoadDir/$libSubfolder'")
copyArtifactsToUSS(privateInterfaces, libSubfolder, copyModeMap)
processedArtifacts += privateInterfaces.size()
}
if ((publicInterfaces && !publicInterfaces.isEmpty()) ||
(privateInterfaces && !privateInterfaces.isEmpty())) {
// Checks if all binary interfaces (submodules) are in the archive or not
println("** Validate if all interfaces known in Application Descriptor are packaged.")
checkBinaryInterfaces(tempLoadDir, applicationDescriptor)
}
ArrayList<String> publicIncludeFiles = applicationDescriptorUtils.getFilesByTypeAndUsage(applicationDescriptor, "Include File", "public")
ArrayList<String> sharedIncludeFiles = applicationDescriptorUtils.getFilesByTypeAndUsage(applicationDescriptor, "Include File", "shared")
ArrayList<String> allIncludeFiles = new ArrayList<String>()
if (publicIncludeFiles && !publicIncludeFiles.isEmpty()) {
allIncludeFiles.addAll(publicIncludeFiles)
}
if (sharedIncludeFiles && !sharedIncludeFiles.isEmpty()) {
allIncludeFiles.addAll(sharedIncludeFiles)
}
if (!allIncludeFiles.isEmpty()) {
println("** Copy ${allIncludeFiles.size()} public/shared Include Files from Application Folder to temporary package directory '$tempLoadDir'")
allIncludeFiles.forEach() { includeFile ->
Path includeFilePath = Paths.get("${props.applicationFolderPath}/${includeFile}")
Path targetIncludeFilePath = Paths.get("${tempLoadDir.getPath()}/${includeSubfolder}/src/${includeFilePath.getFileName()}")
try {
//Create target parent folder if it doesn't exist
def targetIncludeFilesFolder = targetIncludeFilePath.getParent().toFile()
if (!targetIncludeFilesFolder.exists()) {
targetIncludeFilesFolder.mkdirs()
}
println("\tCopy '${includeFilePath}' file to '${targetIncludeFilePath}'")
copyFiles(includeFilePath.toString(), targetIncludeFilePath.toString())
} catch (IOException exception) {
println "!* [ERROR] Copy failed: an error occurred when copying '${includeFilePath}' to '${targetIncludeFilePath}'"
rc = Math.max(rc, 1)
}
}
}
}
if (processedArtifacts != buildOutputsMap.size()) {
println("*! [WARNING] The number of copied artifacts ($processedArtifacts) doesn't match the number of identified build outputs (${buildOutputsMap.size()}). Some files might have an incorrect 'usage' in the Application Descriptor.")
}
}
if (props.generateSBOM && props.generateSBOM.toBoolean() && rc == 0) {
sbomUtilities.writeSBOM("$tempLoadDir/$sbomFileName", props.fileEncoding)
}
if (wdManifestGeneratorUtilities && props.generateWaziDeployAppManifest && props.generateWaziDeployAppManifest.toBoolean() && rc == 0) {
if (props.publish && props.publish.toBoolean()) {
HashMap<String,String> packageInfo = new HashMap<String, String>()
packageInfo.put("type", "artifactRepository")
packageInfo.put("name", props.versionName)
packageUrl = computeAbsoluteRepositoryUrl()
if (packageUrl) packageInfo.put("uri", packageUrl)
wdManifestGeneratorUtilities.setPackageInfo(packageInfo)
}
// print application manifest
// wazideploy_manifest.yml is the default name of the manifest file
wdManifestGeneratorUtilities.writeApplicationManifest(new File("$tempLoadDir/wazideploy_manifest.yml"), props.fileEncoding, props.verbose)
}
if (rc == 0) {
// log buildReportOrder file and add build reports to tar file
File buildReportOrder = new File("$tempLoadDir/buildReportOrder.txt")
ArrayList<String> buildReportOrderLines = new ArrayList<String>()
if (buildReportOrder.exists()) {
String line
buildReportOrder.withReader(props.fileEncoding) { reader ->
while ((line = reader.readLine()) != null && !line.equals("")) {
buildReportOrderLines.add(line)
}
}
}
println("** Generate package build report order file to '$buildReportOrder'")
props.buildReportOrder.each { buildReportFile ->
Path buildReportFilePath = Paths.get(buildReportFile)
// Always prefix the buildreport with sequence number
int nextIndex = buildReportOrderLines.size() + 1
Path copiedBuildReportFilePath = Paths.get(tempLoadDir.getPath() + "/" + "$nextIndex".padLeft(3, "0") + "_" + buildReportFilePath.getFileName().toString())
copyFiles(buildReportFilePath.toString(), copiedBuildReportFilePath.toString())
buildReportOrderLines.add("${copiedBuildReportFilePath.getFileName().toString()}\n")
}
buildReportOrder.withWriter(props.fileEncoding) { writer ->
buildReportOrderLines.each { line ->
if (!line.isEmpty()) {
File buildReportFilePath = new File(line)
String buildReportFileName = buildReportFilePath.getName()
writer.write("$buildReportFileName\n")
}
}
}
Path packagingPropertiesFilePath = Paths.get(props.packagingPropertiesFile)
Path copiedPackagingPropertiesFilePath = Paths.get(tempLoadDir.getPath() + "/" + packagingPropertiesFilePath.getFileName().toString())
if (props.verbose) println("** Copy packaging properties config file to '$copiedPackagingPropertiesFilePath'")
copyFiles(packagingPropertiesFilePath.toString(), copiedPackagingPropertiesFilePath.toString())
if (props.owner) {
def processCmd = [
"sh",
"-c",
"chown -R ${props.owner} $tempLoadDir/*"
]
def processRC = runProcess(processCmd, tempLoadDir)
rc = Math.max(rc, processRC)
if (rc == 0) {
println("** Ownership of files in '$tempLoadDir' successfully changed to '${props.owner}'.")
} else {
println("*! [ERROR] Error when changing ownership to '${props.owner}' with rc=$rc.")
}
}
if (rc == 0) {
println("** Create tar file at ${tarFile}")
// Note: https://www.ibm.com/docs/en/zos/2.4.0?topic=scd-tar-manipulate-tar-archive-files-copy-back-up-file
// To save all attributes to be restored on z/OS and non-z/OS systems : tar -UX
def processCmd = [
"sh",
"-c",
"tar cUXf $tarFile *"
]
def processRC = runProcess(processCmd, tempLoadDir)
rc = Math.max(rc, processRC)
if (rc == 0) {
println("** Package '${tarFile}' successfully created.")
} else {
println("*! [ERROR] Error when creating Package '${tarFile}' with rc=$rc.")
}
}
}
//Package additional outputs to tar file.
if (props.includeLogs && rc == 0) {
(props.includeLogs).split(",").each { logPattern ->
println("** Add files with file pattern '$logPattern' from '${props.workDir}' to '${tarFile}'")
processCmd = [
"sh",
"-c",
"tar rUXf $tarFile $logPattern"
]
processRC = runProcess(processCmd, new File(props.workDir))
rc = Math.max(rc, processRC)
if (rc != 0) {
println("*! [ERROR] Error when appending '$logPattern' files to Package '${tarFile}' with rc=$rc.")
}
}
}
if (props.verbose && props.verbose.toBoolean() && rc == 0) {
println ("** List package contents.")
processCmd = [
"sh",
"-c",
"tar tvf $tarFile"
]
processRC = runProcess(processCmd, new File(props.workDir))
rc = Math.max(rc, processRC)
if (rc != 0) {
println("*! [ERROR] Error when listing contents of Package '${tarFile}' with rc=$rc.")
}
}
//Set up the artifact repository information to publish the tar file
if (props.publish && props.publish.toBoolean() && rc == 0){
// Configuring artifact repositoryHelper parms
def url = computeAbsoluteRepositoryUrl()
def apiKey = props.'artifactRepository.user'
def user = props.'artifactRepository.user'
def password = props.'artifactRepository.password'
def httpClientVersion = props.'artifactRepository.httpClientVersion'
def repo = props.get('artifactRepository.repo') as String
//Call the artifactRepositoryHelpers to publish the tar file
File artifactRepoHelpersFile = new File("$scriptDir/ArtifactRepositoryHelpers.groovy")
Class artifactRepositoryHelpersClass = new GroovyClassLoader(getClass().getClassLoader()).parseClass(artifactRepoHelpersFile)
GroovyObject artifactRepositoryHelpers = (GroovyObject) artifactRepositoryHelpersClass.newInstance()
println ("** Upload package to Artifact Repository '$url'.")
artifactRepositoryHelpers.upload(url, tarFile as String, user, password, props.verbose.toBoolean(), httpClientVersion)
// generate PackageInfo for Concert Manifest file
if (props.generateConcertBuildManifest && props.generateConcertBuildManifest.toBoolean()) {
concertManifestGeneratorUtilities.addLibraryInfoTobuild(concertBuild, tarFileName, url)
}
}
if (concertManifestGeneratorUtilities && props.generateConcertBuildManifest && props.generateConcertBuildManifest.toBoolean() && rc == 0) {
// concert_build_manifest.yaml is the default name of the manifest file
if (props.generateSBOM && props.generateSBOM.toBoolean() && rc == 0) {
concertManifestGeneratorUtilities.addSBOMInfoToBuild(concertBuild, sbomFileName, sbomSerialNumber)
}
concertManifestGeneratorUtilities.writeBuildManifest(new File("$tempLoadDir/concert_build_manifest.yaml"), props.fileEncoding, props.verbose)
println("** Add concert build config yaml to tar file at ${tarFile}")
// Note: https://www.ibm.com/docs/en/zos/2.4.0?topic=scd-tar-manipulate-tar-archive-files-copy-back-up-file
// To save all attributes to be restored on z/OS and non-z/OS systems : tar -UX
def processCmd = [
"sh",
"-c",
"tar rUXf $tarFile concert_build_manifest.yaml"
]
def processRC = runProcess(processCmd, tempLoadDir)
rc = Math.max(rc, processRC)
if (rc == 0) {
println("** Package '${tarFile}' successfully appended with concert manifest yaml.")
} else {
println("*! [ERROR] Error appending '${tarFile}' with concert manifest yaml rc=$rc.")
}
}
}
}
if (rc > 0) {
println ("*! [ERROR] Packaging failed with rc=$rc. Review the console output.")
} else {
println ("** Packaging completed successfully.")
}
System.exit(rc)
def copyArtifactsToUSS(Map<DeployableArtifact, Map> buildOutputsMap, String tarSubfolder, HashMap<String, String> copyModeMap) {
buildOutputsMap.each { deployableArtifact, info ->
String container = info.get("container")
String owningApplication = info.get("owningApplication")
Record record = info.get("record")
PropertiesRecord propertiesRecord = info.get("propertiesRecord")
DependencySetRecord dependencySetRecord = info.get("dependencySetRecord")
def relativeFilePath = ""
if (deployableArtifact.artifactType.equals("zFSFile")) {
relativeFilePath = "$binSubfolder/uss"
} else {
if (deployableArtifact.deployType.equals("OBJ")) {
relativeFilePath = "$tarSubfolder/bin"
} else {
relativeFilePath = "$tarSubfolder/${deployableArtifact.deployType.toLowerCase()}"
}
}
// define file name in USS
def fileName = deployableArtifact.file
// add deployType to file name
if (props.addExtension && props.addExtension.toBoolean()) {
fileName = fileName + '.' + deployableArtifact.deployType
}
def file = new File("$tempLoadDir/$relativeFilePath/$fileName")
def (directory, relativeFileName) = extractDirectoryAndFile(file.toPath().toString())
new File(directory).mkdirs()
if (deployableArtifact.artifactType.equals("zFSFile")) {
def originalFile = new File(container + "/" + deployableArtifact.file)
println "\tCopy '${originalFile.toPath()}' to '${file.toPath()}'"
try {
copyFiles(originalFile.getAbsoluteName(), file.getAbsoluteName())
} catch (IOException exception) {
println "!* [ERROR] Copy failed: an error occurred when copying '${originalFile.toPath()}' to '${file.toPath()}'"
rc = Math.max(rc, 1)
}
} else if (deployableArtifact.artifactType.equals("DatasetMember")) {
// set copyMode based on last level qualifier
currentCopyMode = copyModeMap[container.replaceAll(/.*\.([^.]*)/, "\$1")]
if (currentCopyMode != null) {
if (ZFile.exists("//'$container(${deployableArtifact.file})'")) {
// Copy outputs to HFS
CopyToHFS copy = new CopyToHFS()
copy.setCopyMode(DBBConstants.CopyMode.valueOf(currentCopyMode))
copy.setDataset(container)
println "\tCopy '$container(${deployableArtifact.file})' to '$tempLoadDir/$relativeFilePath/$fileName' with DBB Copymode '$currentCopyMode'"
copy.dataset(container).member(deployableArtifact.file).file(file).execute()
// Tagging binary files
if (currentCopyMode == CopyMode.BINARY || currentCopyMode == CopyMode.LOAD) {
StringBuffer stdout = new StringBuffer()
StringBuffer stderr = new StringBuffer()
Process process = "chtag -b $file".execute()
process.waitForProcessOutput(stdout, stderr)
if (stderr){
println ("*! stderr : $stderr")
println ("*! stdout : $stdout")
}
}
// Append record to Wazi Deploy Application Manifest
if (wdManifestGeneratorUtilities && props.generateWaziDeployAppManifest && props.generateWaziDeployAppManifest.toBoolean()) {
wdManifestGeneratorUtilities.appendArtifactToManifest(deployableArtifact, "$relativeFilePath/$fileName", record, dependencySetRecord, propertiesRecord)
}
} else {
println "*! [ERROR] Copy failed: The file '$container(${deployableArtifact.file})' doesn't exist."
rc = Math.max(rc, 1)
}
} else {
println "*! [ERROR] Copy failed: The file '$container(${deployableArtifact.file})' could not be copied due to missing mapping."
rc = Math.max(rc, 1)
}
} else if (deployableArtifact.artifactType.equals("DatasetMemberDelete")) {
// generate delete instruction for Wazi Deploy
if (wdManifestGeneratorUtilities && props.generateWaziDeployAppManifest && props.generateWaziDeployAppManifest.toBoolean()) {
wdManifestGeneratorUtilities.appendArtifactDeletionToManifest(deployableArtifact, "$relativeFilePath/$fileName", record, propertiesRecord)
}
}
if (props.generateSBOM && props.generateSBOM.toBoolean() && rc == 0) {
sbomUtilities.addEntryToSBOM(deployableArtifact, info)
}
}
}
/**
* parse data set name and member name
* @param fullname e.g. BLD.LOAD(PGM1)
* @return e.g. (BLD.LOAD, PGM1)
*/
def getDatasetName(String fullname){
def ds,member;
def elements = fullname.split("[\\(\\)]");
ds = elements[0];
member = elements.size()>1? elements[1] : "";
return [ds, member];
}
/**
* Extract the directory and the file
* from the fullname of a zFS file
* For instance: /var/test/file.txt --> [/var/test, file.txt]
*/
def extractDirectoryAndFile(String fullname) {
Path filePath = Paths.get(fullname);
String file = filePath.getFileName().toString();
String directory = filePath.getParent();
return [directory, file];
}
/**
* read cliArgs
*/
def parseInput(String[] cliArgs){
def cli = new CliBuilder(usage: "PackageBuildOutputs.groovy [options]", stopAtNonOption:false)
// required packaging options
cli.w(longOpt:'workDir', args:1, argName:'dir', 'Absolute path to the DBB build output directory')
cli.properties(longOpt:'packagingPropertiesFile', args:1, argName:'packagingPropertiesFile', 'Path of a property file containing application specific packaging details.')
// optional packaging options
cli.d(longOpt:'deployTypes', args:1, argName:'deployTypes','Comma-seperated list of deployTypes to filter on the scope of the tar file. (Optional)')
cli.t(longOpt:'tarFileName', args:1, argName:'filename', 'Name of the package tar file. (Optional unless using --buildReportOrder or --buildReportOrderFile)')
cli.il(longOpt:'includeLogs', args:1, argName:'includeLogs', 'Comma-separated list of files/patterns from the USS build workspace. (Optional)')
cli.ae(longOpt:'addExtension', 'Flag to add the deploy type extension to the member in the package tar file. (Optional)')
// Wazi Deploy Application Manifest generation
cli.wd(longOpt:'generateWaziDeployAppManifest', 'Flag indicating to generate and add the Wazi Deploy Application Manifest file.')
cli.ed(longOpt:'externalDependenciesEvidences', args:1, argName:'externalDependenciesEvidences', 'File documenting the external dependencies that were provided to the build phase.')
// Concert Build Manifest generation
cli.ic(longOpt:'generateConcertBuildManifest', 'Flag indicating to generate and add the IBM Concert Build Manifest file.')
cli.b(longOpt:'branch', args:1, argName:'branch', 'The git branch processed by the pipeline')
cli.a(longOpt:'application', args:1, argName:'application', 'The name of the application')
cli.af(longOpt:'applicationFolderPath', args:1, argName:'applicationFolderPath', 'Path to the Application\'s Git repository folder')
// Baseline package
cli.bp(longOpt:'baselinePackage', args:1, argName:'baselinePackageFilePath', 'Path to a baseline Package. (Optional)')
// Artifact repository options ::
cli.p(longOpt:'publish', 'Flag to indicate package upload to the provided Artifact Repository server. (Optional)')
cli.v(longOpt:'versionName', args:1, argName:'versionName', 'Name of the version/package on the Artifact repository server. (Optional)')
// Artifact repository info
cli.au(longOpt:'artifactRepositoryUrl', args:1, argName:'url', 'URL to the Artifact repository server. (Optional)')
cli.ar(longOpt:'artifactRepositoryName', args:1, argName:'repoName', 'Artifact repository name to store the build. (Optional)')
cli.ad(longOpt:'artifactRepositoryDirectory', args:1, argName:'repoDirectory', 'Directory path in the repository to store the build . (Optional)')
cli.aU(longOpt:'artifactRepositoryUser', args:1, argName:'user', 'User to connect to the Artifact repository server. (Optional)')
cli.aP(longOpt:'artifactRepositoryPassword', args:1, argName:'password', 'Password to connect to the Artifact repository server. (Optional)')
cli.ah(longOpt:'artifactRepositoryHttpClientProtocolVersion', args:1, argName: 'httpClientProtocolVersion', 'HttpClient.Version setting to override the HTTP protocol version. (Optional)')
cli.aprop(longOpt:'artifactRepositoryPropertyFile', args:1, argName:'propertyFile', 'Path of a property file containing application specific artifact repository details. (Optional) ** (Deprecated)')
// Tracing
cli.verb(longOpt:'verbose', 'Flag to provide more log output. (Optional)')
// multiple build reports
cli.boFile(longOpt:'buildReportOrderFile', args:1, argName:'buildReportOrderFile', 'A file that lists build reports in order of processing')
cli.bO(longOpt:'buildReportOrder', args:1, argName:'buildReportOrder', 'List of build reports in order of processing ')
// SBOM generation
cli.s(longOpt:'sbom', argName:'sbom', 'Flag to control the generation of SBOM')
cli.sa(longOpt:'sbomAuthor', args:1, argName:'sbomAuthor', 'Author of the SBOM, in form "Name <email>"')
// Owner of the artifacts
cli.o(longOpt:'owner', args:1, argName:'owner', 'Owner of the packaged artifacts')
cli.h(longOpt:'help', 'Prints this message')
def opts = cli.parse(cliArgs)
if (opts.h) { // if help option used, print usage and exit
cli.usage()
System.exit(0)
}
def props = new Properties()