forked from craftercms/craftercms
-
Notifications
You must be signed in to change notification settings - Fork 0
/
build.gradle
629 lines (562 loc) · 23.1 KB
/
build.gradle
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
import java.nio.file.Files
import java.nio.file.Paths
import java.nio.file.StandardWatchEventKinds
plugins {
id "de.undercouch.download" version "3.1.2"
}
def isWin=System.getProperty('os.name').toLowerCase().contains('windows')
def isOSX=System.getProperty('os.name').toLowerCase().contains('mac')
def isNix=(System.getProperty('os.name').toLowerCase().contains('nix') || System.getProperty('os.name').toLowerCase().contains('aix') || System.getProperty('os.name').toLowerCase().contains('nux'))
def commandLinePrefix = []
def startUpFile = "./startup.sh"
def shutDownFile ="./shutdown.sh"
if (isWin) {
startUpFile = "startup.bat"
shutDownFile = "shutdown.bat"
commandLinePrefix =["cmd","/c"]
}
def getModule() {
if (project.hasProperty("moduleName")) {
def moduleName=project.property("moduleName")
moduleName=moduleName.trim().toLowerCase()
return moduleName.trim().isEmpty()? "all" : moduleName
} else {
return "all"
}
}
def getEnv(){
if (project.hasProperty("env")) {
def enviromentName=project.property("env")
enviromentName=enviromentName.trim().toLowerCase()
return enviromentName.trim().isEmpty()? "all" : enviromentName
} else {
return "all"
}
}
def isAllEnvs() {
return getEnv().equalsIgnoreCase("all")
}
ext {
/** Unzip Properties **/
tomcatVersion=project.hasProperty("tomcat.version")? project.property("tomcat.version") : "8.5.6"
solrVersion=project.hasProperty("solr.version")? project.property("solr.version") : "6.4.1"
downloadDir=project.hasProperty("downloadDir")? project.property("downloadDir") : "./downloads/"
/** Environment Building properties **/
authEnv=project.hasProperty("authoring.root")? project.property("authoring.root") : "./crafter-auth-env/"
deliveryEnv=project.hasProperty("delivery.root")? project.property("delivery.root") : "./crafter-delivery-env/"
includeProfile=project.hasProperty("crafter.profile")? project.property("crafter.profile") : false
includeSocial=project.hasProperty("crafter.social")? project.property("crafter.social") : false
/** Social Need Profile **/
if (includeSocial) {
includeProfile=true
}
//Authoring
authTomcatPort=project.hasProperty("authoring.tomcat.http.port")? project.property("authoring.tomcat.http.port") : 8080
authTomcatShutdownPort=project.hasProperty("authoring.tomcat.shutdown.port")? project.property("authoring.tomcat.shutdown.port") : 8005
authTomcatAJPPort=project.hasProperty("authoring.tomcat.ajp.port")? project.property("authoring.tomcat.ajp.port") : 8009
authTomcatSSLPort=project.hasProperty("authoring.tomcat.https.port")? project.property("authoring.tomcat.https.port") : 8443
authTomcatDebug=project.hasProperty("authoring.tomcat.debug.port")? project.property("authoring.tomcat.debug.port") : 8000
authMongoDBPort=project.hasProperty("authoring.mongo.port")? project.property("authoring.mongo.port") : 27020
authSolrPort=project.hasProperty("authoring.solr.port")? project.property("authoring.solr.port") : 8694
authSolrDebugPort=project.hasProperty("authoring.solr.debug.port")? project.property("authoring.solr.debug.port") : 5005
authMariaDbPort=project.hasProperty("authoring.mariadb.port")? project.property("authoring.mariadb.port") : 33306
authDeployerPort=project.hasProperty("authoring.deployer.port")? project.property("authoring.deployer.port") : 9191
authDeployerDebugPort=project.hasProperty("authoring.deployer.debug.port")? project.property("authoring.deployer.debug.port") : 5000
authDeploymentDir=project.hasProperty("authoring.deployment.dir")? project.property("authoring.deployment.dir") : "data/repos/sites"
// Delivery
deliverySolrPort=project.hasProperty("delivery.solr.port")? project.property("delivery.solr.port") : 8695
deliverySolrDebugPort=project.hasProperty("delivery.solr.debug.port")? project.property("delivery.solr.debug.port") : 5006
deliveryDeployerPort=project.hasProperty("delivery.deployer.port")? project.property("delivery.deployer.port") : 9192
deliveryDeployerDebugPort=project.hasProperty("delivery.deployer.debug.port")? project.property("delivery.deployer.debug.port") : 5001
deliveryDeploymentDir=project.hasProperty("delivery.deployment.dir")? project.property("delivery.deployment.dir") : "data/repos/sites"
deliveryTomcatPort=project.hasProperty("delivery.tomcat.http.port")? project.property("delivery.tomcat.http.port") : 9080
deliveryTomcatShutdownPort=project.hasProperty("delivery.tomcat.shutdown.port")? project.property("delivery.tomcat.shutdown.port") : 9005
deliveryTomcatAJPPort=project.hasProperty("delivery.tomcat.ajp.port")? project.property("delivery.tomcat.ajp.port") : 9009
deliveryTomcatSSLPort=project.hasProperty("delivery.tomcat.https.port")? project.property("delivery.tomcat.https.port") : 9443
deliveryTomcatDebugPort=project.hasProperty("delivery.tomcat.debug.port")? project.property("delivery.tomcat.debug.port") : 9000
deliveryMongoDBPort=project.hasProperty("delivery.mongodb.port")? project.property("delivery.mongodb.port") : 28020
/** Git properties **/
gitURLTemplate=project.hasProperty("crafter.git.url")? project.property("crafter.git.url") : "https://github.com/craftercms/"
gitSourceBranch=project.hasProperty("crafter.git.branch")? project.property("crafter.git.branch") : "master"
gitRepo=project.hasProperty("crafter.git.remote")? project.property("crafter.git.remote") : "origin"
studioUIFromRepo=project.hasProperty("crafter.ui.repo")? project.property("crafter.ui.repo") : false
forceDeploy=project.hasProperty("forceDeploy")? project.property("forceDeploy") : false
keepBin=project.hasProperty("keepBin")? project.property("keepBin") : false
/** Clonable Artifacts **/
VALID_MODULES=["commons", "core", "search", "profile", "engine", "deployer", "studio", "social", "studio2-ui"]
/** Deployable Artifacts **/
VALID_DEPLOYABLE_MODULES=[
"search":"./src/search/crafter-search-server/target/crafter-search.war",
"profile":"./src/profile/server/target/crafter-profile.war",
"engine":"./src/engine/target/ROOT.war",
"deployer":"./src/deployer/target/crafter-deployer.jar",
"studio":"./src/studio/target/studio.war",
"social":"./src/social/server/target/crafter-social.war"]
}
apply from:"downloads.gradle"
apply from:"environments.gradle"
task("utils"){
description "builds the utils project and copies the utils jar file to the environment"
doLast {
buildModule(commandLinePrefix, "utils", "./")
copy {
from "./utils/target/craftercms-utils.jar"
into "${authEnv}/bin"
}
copy {
from "./utils/target/craftercms-utils.jar"
into "${deliveryEnv}/bin"
}
}
}
task("upgrade"){
description "Upgrades the installed tomcat version, solr scripts, etc. without deleting your data then builds and deploys"
doLast {
def authEnvFolder = new File("${authEnv}")
if (authEnvFolder.exists()) {
authEnvFolder.eachFile { dir ->
if (!dir.getName().equalsIgnoreCase("data")) {
if (!dir.isFile()) {
dir.deleteDir()
} else {
dir.delete()
}
}
}
}
forceDeploy=true
build.execute()
deploy.execute()
}
}
task("init"){
description "Clones Crafter CMS stack"
doFirst{
def module = getModule();
}
doLast {
if (module.equals("all")) {
VALID_MODULES.each {
cloneModule(commandLinePrefix, it).execute()
}
} else if (VALID_MODULES.contains(module)) {
cloneModule(commandLinePrefix, module).execute()
} else {
throw new InvalidUserDataException("${module} is not a valid module, please one of ${VALID_MODULES}")
}
}
}
task("update") {
description "Update the various modules of Crafter CMS"
doFirst{
def module = getModule();
}
doLast {
if (module.equals("all")) {
VALID_MODULES.each {
updateModule(commandLinePrefix, it).execute()
}
} else if (VALID_MODULES.contains(module)) {
updateModule(commandLinePrefix, module).execute()
} else {
throw new InvalidUserDataException("${module} is not a valid module, please use one of ${VALID_MODULES}")
}
}
}
task("selfupdate") {
description "Updates the Crafter CMS project"
doLast {
def arry = commandLinePrefix + ["git", "pull", gitRepo, gitSourceBranch]
executeProcess(arry,".")
}
}
task("start") {
description "Start Crafter CMS"
doLast {
def env = getEnv()
println "Running for the first time might take longer while fetching OS dependent requirements."
switch (env) {
case "all":
if(!file(authEnv).exists() || !file(deliveryEnv).exists()){
throw new InvalidUserDataException("Neither enviroment ('authoring'/'delivery') are deployed. Please run deploy before 'start'.")
}
startAuth(commandLinePrefix, startUpFile).execute()
startDelivery(commandLinePrefix, startUpFile).execute()
break
case "authoring":
if (!file(authEnv).exists()) {
throw new InvalidUserDataException("The 'authoring' enviroment is not deployed. Please run 'deploy -Penv=authoring' before 'start'.")
}
startAuth(commandLinePrefix, startUpFile).execute()
break
case "delivery":
if (!file(deliveryEnv).exists()) {
throw new InvalidUserDataException("The 'delivery' enviroment is not deployed. Please run 'deploy -Penv=delivery' before 'start'.")
}
startDelivery(commandLinePrefix, startUpFile).execute()
break
default:
throw new InvalidUserDataException("'${env}' is not a valid environment, please use 'authoring' or 'delivery'.")
}
}
}
task("stop") {
description "Stop Crafter CMS"
doLast {
def env = getEnv()
switch (env) {
case "all":
if (!file(authEnv).exists() || !file(deliveryEnv).exists()) {
throw new InvalidUserDataException("Neither enviroment ('authoring'/'delivery') are deployed. Please run deploy before 'stop'.")
}
stopAuth(commandLinePrefix,shutDownFile).execute()
stopDelivery(commandLinePrefix, shutDownFile).execute()
case "authoring":
if (!file(authEnv).exists()) {
throw new InvalidUserDataException("The 'authoring' enviroment is not deployed. Please run 'deploy -Penv=authoring' before 'stop'.")
}
stopAuth(commandLinePrefix, shutDownFile).execute()
break
case "delivery":
if (!file(deliveryEnv).exists()) {
throw new InvalidUserDataException("The 'delivery' enviroment is not deployed. Please run 'deploy -Penv=delivery' before 'stop'.")
}
stopDelivery(commandLinePrefix, shutDownFile).execute()
break
default:
throw new InvalidUserDataException("'${env}' is not a valid environment, please use 'authoring' or 'delivery'.")
}
}
}
task("clear") {
description "Clean Crafter CMS stack"
doFirst {
def module = getModule();
}
doLast {
if (module.equals("all")) {
VALID_MODULES.each {
if(!it.equalsIgnoreCase("studio2-ui")) { // Not a pom project ignore
cleanModule(commandLinePrefix, it).execute()
}
}
} else if (VALID_MODULES.contains(module)) {
if(!module.equalsIgnoreCase("studio2-ui")) { // Not a pom project ignore
cleanModule(commandLinePrefix, module).execute()
}
} else {
throw new InvalidUserDataException("${module} is not a valid module, please use one of ${VALID_MODULES}")
}
}
}
task("build") {
description "Builds Crafter CMS"
doFirst {
def module = getModule();
}
doLast {
if (module.equals("all")) {
VALID_MODULES.each {
if (!it.equalsIgnoreCase("studio2-ui")) {
buildModule(commandLinePrefix, it).execute()
}
}
} else if (VALID_MODULES.contains(module)) {
if (!module.equalsIgnoreCase("studio2-ui")) {
buildModule(commandLinePrefix, module).execute()
}
} else {
throw new InvalidUserDataException("${module} is not a valid module, please use one of ${VALID_MODULES}")
}
}
}
task("deploy") {
description "Setup the Crafter CMS environment/s"
doLast {
def module = getModule()
def env = getEnv()
switch (env){
case "all":
checkEnvWD(authEnv, "authoring")
checkEnvWD(deliveryEnv, "delivery")
break
case "authoring":
checkEnvWD(authEnv, env)
break
case "delivery":
checkEnvWD(deliveryEnv, env)
break
default:
throw new InvalidUserDataException("'${env}' is not a valid environment, please use 'authoring' or 'delivery'.")
}
if (module.equals("all")) {
VALID_DEPLOYABLE_MODULES.keySet().each {
if (isAllEnvs()) {
deployModule(commandLinePrefix, it, "authoring").execute()
deployModule(commandLinePrefix, it, "delivery").execute()
} else {
deployModule(commandLinePrefix, it, env).execute()
}
if(!keepBin) {
file(VALID_DEPLOYABLE_MODULES[it]).delete()
}
}
} else if (VALID_DEPLOYABLE_MODULES.containsKey(module)) {
if (isAllEnvs()) {
deployModule(commandLinePrefix, module, "authoring").execute()
deployModule(commandLinePrefix, module, "delivery").execute()
} else {
deployModule(commandLinePrefix, module, env).execute()
}
if (!keepBin) {
file(VALID_DEPLOYABLE_MODULES[module]).delete()
}
} else {
throw new InvalidUserDataException("${module} is not a valid module, please use one of ${VALID_DEPLOYABLE_MODULES.keySet()}")
}
utils.execute()
}
}
task("bundle") {
description "Builds a deployable and distributable bundle of Crafter CMS"
doLast {
def env = getEnv()
switch (env) {
case "all":
pack(authEnv, "authoring")
pack(deliveryEnv, "delivery")
break
case "authoring":
pack(authEnv, "authoring")
break
case "delivery":
pack(deliveryEnv, "delivery")
break
default:
throw new InvalidUserDataException("'${env}' is not a valid environment, please use 'authoring' or 'delivery'.")
}
}
}
def cleanModule(commandLinePrefix, module) {
def ctask=tasks.findByPath("clean${module}")
if (ctask!= null) {
return ctask
} else {
return tasks.create("clean${module}") {
executeProcess(commandLinePrefix + commandLinePrefix + ["mvn", "clean"], "./src/${module}".toString())
}
}
}
def updateModule(commandLinePrefix, module) {
def btask=tasks.findByPath("update${module}")
if (btask!= null) {
return btask
} else {
return tasks.create("update${module}") {
if (!file("./src/${module}").exists()) {
throw new InvalidUserDataException("Module ${module} is not initialized. Please use 'update' to initialize it.")
} else {
def arry = commandLinePrefix + ["git", "pull", gitRepo, gitSourceBranch]
executeProcess(arry,"./src/${module}".toString())
}
}
}
}
def cloneModule(commandLinePrefix, module){
def btask=tasks.findByPath("clone${module}")
if (btask!= null) {
return btask
} else {
return tasks.create("clone${module}") {
def finalUrl = "${gitURLTemplate}${module}.git".toString()
def path = "src/${module}".toString()
def arry = commandLinePrefix + ["git","clone","-b",gitSourceBranch,finalUrl,path]
if (file(path).exists()) {
println ("Module ${module} has already been initialized, please use " +
"'update' to update it.")
} else {
println "Cloning: ${module}, branch: ${gitSourceBranch}, url: ${finalUrl}"
executeProcess(arry,".")
}
}
}
}
def startAuth(commandLinePrefix, startUpFile) {
def btask = tasks.findByPath("startAuth")
if (btask!= null){
return btask
} else {
tasks.create("startAuth") {
executeProcess(commandLinePrefix + [startUpFile], "$authEnv/bin",true)
}
}
}
def startDelivery(commandLinePrefix, startUpFile){
def btask=tasks.findByPath("startDelivery")
if (btask!= null) {
return btask
} else {
tasks.create("startDelivery") {
executeProcess(commandLinePrefix + [startUpFile], "$deliveryEnv/bin", true)
}
}
}
def stopAuth(commandLinePrefix, shutDownFile) {
def btask = tasks.findByPath("stopAuth")
if (btask!= null) {
return btask
} else {
tasks.create("stopAuth") {
executeProcess(commandLinePrefix + [shutDownFile], "$authEnv/bin", true)
}
}
}
def stopDelivery(commandLinePrefix, shutDownFile) {
def btask=tasks.findByPath("stopDelivery")
if (btask!= null) {
return btask
} else {
tasks.create("stopDelivery") {
executeProcess(commandLinePrefix + [shutDownFile], "$deliveryEnv/bin", true)
}
}
}
def buildModule(commandLinePrefix, module, path="./src/") {
def btask=tasks.findByPath("build${module}")
if (btask!= null) {
return btask
} else {
return tasks.create("build${module}") {
def commandLinePost=[]
if (!project.hasProperty("test")) {
commandLinePost.add("-Dmaven.test.skip=true")
}
if(module.equalsIgnoreCase("studio") && !studioUIFromRepo) {
commandLinePost.add("-Dstudio.ui.path=../studio2-ui/".toString())
commandLinePost.add("-Dexec.skip=true")
}
def array=commandLinePrefix + commandLinePrefix + ["mvn", "clean", "install"] + commandLinePost
executeProcess(array, "${path}${module}".toString())
}
}
}
def deployBinary(deployable){
if (deployable.exists()) {
def path = env.equals("authoring")? authEnv : deliveryEnv
path += deployable.absolutePath.endsWith(".jar")? "bin/crafter-deployer/" : "bin/apache-tomcat/webapps/"
path += deployable.name
delete {
delete path
delete path.take(path.lastIndexOf('.'))
}
copy {
from deployable.absolutePath
into path.take(path.lastIndexOf('/'))
}
} else {
println "WARN: You haven't built ${module} module"
}
}
def deployModule(commandLinePrefix, module, env) {
def dtask=tasks.findByPath("deploy${module}")
if (dtask!= null) {
return dtask
} else {
return tasks.create("deploy${module}") {
println "Deploy ${module} to ${env}"
if (module.equals("studio") && env.equals("delivery")) {
println "Ignoring module 'studio' can not be deployed in environment 'delivery'"
} else {
def deployable = file(VALID_DEPLOYABLE_MODULES[module])
deployBinary(deployable)
if(module.equalsIgnoreCase("profile")){
deployBinary(file("./src/profile/admin-console/target/crafter-profile-admin-console.war"))
} else if (module.equalsIgnoreCase("social")) {
deployBinary(file("./src/social/admin/target/crafter-social-admin.war"))
}
}
}
}
}
def checkEnvWD(path, env) {
def ddir = file(path)
if (!ddir.exists() || forceDeploy) {
ddir.mkdir()
def deployerDir = file("${path}/bin/crafter-deployer")
deployerDir.mkdir()
downloadTomcat.execute()
downloadSolr.execute()
if (env.equals("authoring")) {
authoringEnvironment.execute()
} else if (env.equals("delivery")) {
deliveryEnvironment.execute()
}
}
}
def pack(environmentPath,envName){
def dtask=tasks.findByPath("tar${envName}")
if (dtask!= null) {
return dtask
} else {
def zipTask = tasks.findByPath("zip${envName}")
if (zipTask != null) {
zipTask.execute()
} else {
tasks.create("zip${envName}", Zip.class) {
from(environmentPath) {
into("crafter")
}
archiveName "crafter-${envName}-cms.zip"
fileMode null
dirMode null
includeEmptyDirs true
metadataCharset "UTF-8"
caseSensitive false
destinationDir(file("bundles/"))
}.execute()
}
def tarTask = tasks.findByPath("tar${envName}")
if (tarTask != null) {
tarTask.execute()
} else {
tasks.create("tar${envName}", Tar.class) {
from(environmentPath) {
into("crafter")
}
archiveName "crafter-${envName}-cms.tar.gz"
fileMode null
dirMode null
includeEmptyDirs true
caseSensitive false
compression = Compression.GZIP
destinationDir(file("bundles/"))
}.execute()
}
}
}
def executeProcess(command, workingDir, printOutputUntilFinish = false) {
ProcessBuilder pb = new ProcessBuilder(command)
def processOutput = File.createTempFile("process", "out")
pb.directory(new File(workingDir))
pb.redirectOutput(processOutput)
Process proc = pb.start();
watchProcessOutput(proc,processOutput)
proc.waitFor()
if (proc.exitValue() != 0) {
throw new GradleException("${command} @ ${workingDir} return a non-zero value (${proc.exitValue()})")
}
}
// Poor's man tail (but get's the job done
def watchProcessOutput(process, processOutputFile){
Thread.start {
def reader=Files.newBufferedReader(Paths.get(processOutputFile.path))
while(process.alive) {
// Thread.sleep(100)
def line=null
while((line=reader.readLine()) != null) {
println line
}
}
processOutputFile.delete()
}
}