-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPublishPublicInterfaces.groovy
278 lines (235 loc) · 8.14 KB
/
PublishPublicInterfaces.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
import com.ibm.dbb.metadata.*
import com.ibm.dbb.dependency.*
import com.ibm.dbb.build.*
import com.ibm.dbb.build.report.*
import com.ibm.dbb.build.report.records.*
import groovy.transform.*
import groovy.cli.commons.*
import java.nio.file.*;
// PublishPublicInterfaces.groovy
/**
* This script parses the BuildReport.json
*
* * usage: PublishPublicInterfaces.groovy [options]
*
* options:
* -w, --workDir <dir> Absolute path to the DBB build output directory
* -tHfs, --targetHfsDirectory Path to Shared Git repo
* -a, --application Application Name
* -v, --verbose Enable verbose tracing
* -h, --help Prints this message
*
*/
// configuration vars
@Field lockingFile = ".processLock"
@Field deployTypeName = "PublicCopy"
@Field subfolderName = "copybooks"
// start DeployBuildOutputs
@Field properties = parseInput(args)
def startTime = new Date()
properties.startTime = startTime.format("yyyyMMdd.hhmmss.mmm")
println("** PublicPublicCopy start at $properties.startTime")
if (properties.verbose) println("** Properties at startup:")
properties.each{k,v->
if (properties.verbose) println " $k -> $v"
}
// vars
Path lockFile = Paths.get(properties.targetHfsDirectory);
// read build report data
println("** Read build report data from $properties.workDir/BuildReport.json")
def jsonOutputFile = new File("${properties.workDir}/BuildReport.json")
if(!jsonOutputFile.exists()){
println("** Build report data at $properties.workDir/BuildReport.json not found")
System.exit(1)
}
def buildReport= BuildReport.parse(new FileInputStream(jsonOutputFile))
// parse build report to find the build result meta info
def buildResult = buildReport.getRecords().findAll{it.getType()==DefaultRecordFactory.TYPE_BUILD_RESULT}[0];
def dependencies = buildReport.getRecords().findAll{it.getType()==DefaultRecordFactory.TYPE_DEPENDENCY_SET};
// parse build report to find the build outputs to be deployed.
println("** Find and display Public Interfaces in the build report.")
// find all the public interfaces which got copied to the pds
def publicCopies= buildReport.getRecords().findAll{
it.getType()==DefaultRecordFactory.TYPE_COPY_TO_PDS &&
!it.getOutputs().findAll{ o ->
o.deployType == deployTypeName
}.isEmpty()
}
// only run logic, if we found Public Interfaces
if (publicCopies.size()!=0){
setLock(lockFile)
// e.q.: /var/jenkins/shared-repo/Shared/<Application>-<subfolder>
String targetDir = properties.targetHfsDirectory +"/"+properties.application+"-"+subfolderName
if (!isGitDir(properties.targetHfsDirectory)){
println("*! $targetDir is not a Git repo. Skipping publishing process.")
} else if(!isGitDetachedHEAD(targetDir)) {
def gitHash
publicCopies.each {
Path originalPath = Paths.get("${it.source}");
Path targetPath = Paths.get(targetDir)
Path targetFile = targetPath.resolve(originalPath.getFileName())
Files.createDirectories(targetPath)
println("* Copying " + originalPath.getFileName() + " to $targetDir")
Files.copy(originalPath, targetFile, StandardCopyOption.REPLACE_EXISTING);
// set file tag
currentTag = processCmd("chtag -p " + originalPath).split("\\s+")[1];
processCmd("chtag -t -c $currentTag " + targetFile.toString())
gitHash = getCurrentGitHash(originalPath.getParent().toString())
}
//git opertions - git status + add
processCmd("git -C $targetDir status")
processCmd("git -C $targetDir add .")
processCmd("git -C $targetDir status")
//git commit
ArrayList nextCmd=new ArrayList<String>()
nextCmd.add("git")
nextCmd.add("-C")
nextCmd.add(targetDir)
nextCmd.add("commit")
nextCmd.add("-m")
nextCmd.add("\"${properties.application}:${gitHash}\"")
processCmd(nextCmd)
//git push
processCmd("git -C $targetDir push")
// Placeholder: IEBCOPY Files to shared library, to publish files to a shared library
} else {
println("*!Git Branch in a detached state. Skipping publishing.")
}
//release lock
releaseLock(lockFile)
}
else {
println("*! No Public Copybooks found")
}
/*
* Methods
*/
/*
* setLock file. Creates a lock file in the shared repository structure to avoid collisions, while working on the Shared repository.
*/
def setLock(Path sharedDir){
println("** Setting lock file to avoid collisions." )
def lockAttempts = 0
while(lockAttempts < 10){
try{
Files.createFile(sharedDir.resolve(lockingFile))
return
} catch (FileAlreadyExistsException e){
println("*! $lockingFile exits. Waiting 5 seconds. Attempt : $lockAttempts")
lockAttempts++
sleep(5000) // sleep for 5 seconds
}
}
println("*! Could not obtain lock for publishing files. Exiting." )
System.exit(1)
}
/*
* releaseLock file.
*/
def releaseLock(Path sharedDir){
println("* Releasing lock file." )
Files.deleteIfExists(sharedDir.resolve(lockingFile))
}
/*
* Returns String containing current hash.
*/
def getCurrentGitHash(String gitDir) {
String cmd = "git -C $gitDir rev-parse HEAD"
StringBuffer gitHash = new StringBuffer()
StringBuffer gitError = new StringBuffer()
Process process = cmd.execute()
process.waitForProcessOutput(gitHash, gitError)
if (gitError) {
println("*! Error executing Git command: $cmd error: $gitError")
}
return gitHash.toString().trim()
}
/*
* Returns true if this is a detached HEAD
*
* @param String gitDir Local Git repository directory
*/
def isGitDetachedHEAD(String gitDir) {
String cmd = "git -C $gitDir status"
StringBuffer gitStatus = new StringBuffer()
StringBuffer gitError = new StringBuffer()
Process process = cmd.execute()
process.waitForProcessOutput(gitStatus, gitError)
if (gitError) {
println("*! Error executing Git command: $cmd error $gitError")
}
return gitStatus.toString().contains("HEAD detached at")
}
/*
* Tests if directory is in a local git repository
*
* @param String dir Directory to test
* @return boolean
*/
def isGitDir(String dir) {
String cmd = "git -C $dir rev-parse --is-inside-work-tree"
StringBuffer gitResponse = new StringBuffer()
StringBuffer gitError = new StringBuffer()
boolean isGit = false
Process process = cmd.execute()
process.waitForProcessOutput(gitResponse, gitError)
if (gitError) {
println("*? Warning executing isGitDir($dir). Git command: $cmd error: $gitError")
}
else if (gitResponse) {
isGit = gitResponse.toString().trim().toBoolean()
}
return isGit
}
/*
* Process any cmd. Exits with exit code:2, if failing.
*/
def processCmd(def cmd){
println("** Running cmd: $cmd")
StringBuffer cmdResponse = new StringBuffer()
StringBuffer cmdError = new StringBuffer()
Process process = cmd.execute()
process.waitForProcessOutput(cmdResponse, cmdError)
if (cmdError) {
println("*? Error executing cmd. command: $cmd error: $cmdError")
println("*? The shared repository might be in a inconsistent state. Lock file kept. Please release the conflict.")
println("*? Exiting with exit code:2 ")
System.exit(2)
}
else if (cmdResponse) {
if (properties.verbose) println("** Command response: " + cmdResponse.toString())
return cmdResponse.toString().trim()
}
}
/*
* Parses cmd inputs.
*/
def parseInput(String[] cliArgs){
def cli = new CliBuilder(usage: "RunCodeReview.groovy [options]")
cli.w(longOpt:'workDir', args:1, argName:'dir', 'Absolute path to the DBB directory containing the BuildReport.json')
cli.tHfs(longOpt:'targetHfsDirectory', args:1, 'Path to Shared Git repo')
cli.a(longOpt:'application', args:1,'Application Name')
cli.v(longOpt:'verbose', 'Activate script tracing.')
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 properties = new Properties()
// set command line arguments
if (opts.w) properties.workDir = opts.w
if (opts.tHfs) properties.targetHfsDirectory = opts.tHfs
if (opts.a) properties.application = opts.a
if (opts.v) properties.verbose = true
// validate required properties
try {
assert properties.workDir: "Missing property build work directory"
assert properties.targetHfsDirectory: "Missing property path to shared git repo"
assert properties.application: "Missing property application"
} catch (AssertionError e) {
cli.usage()
throw e
}
return properties
}