forked from liferay/liferay-environment-composer
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdocker-database-saas.gradle
More file actions
578 lines (418 loc) · 17.1 KB
/
Copy pathdocker-database-saas.gradle
File metadata and controls
578 lines (418 loc) · 17.1 KB
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
import com.liferay.docker.workspace.environments.Util
import groovy.json.JsonSlurper
import java.nio.file.Files
import java.nio.file.StandardCopyOption
import java.util.regex.Matcher
import java.util.regex.Pattern
ext {
SSO_CONFIGS = [".ldap.", ".multi.factor.authentication.", ".saml.", ".openid."]
PROD_ONLY_CONFIGS = [".liferay.analytics.", ".captcha.", ".content.security.policy.", ".elasticsearch7.", ".k8s."]
clearSingleSignOnConfigurations = {
String schema ->
SSO_CONFIGS.each {
executeSQLQuery("delete from Configuration_ where configurationId like '%${it}%'", schema)
}
truncateTables('OpenId%', schema)
truncateTables('Saml%', schema)
println "Deleted known problematic single sign on entries from schema ${schema}"
}
copyLiferayLXCRepositoryConfiguration = {
File configFolder, String sourceName, String targetName ->
File sourceFile = new File(configFolder, sourceName)
if (!sourceFile.exists()) {
return false
}
File targetFile = file("configs/${targetName}")
if (sourceFile.isDirectory()) {
for (String fileName : sourceFile.list()) {
copyLiferayLXCRepositoryConfiguration(sourceFile, fileName, "${targetName}/${fileName}".toString())
}
}
else {
targetFile.parentFile.mkdirs()
Files.copy(sourceFile.toPath(), targetFile.toPath(), StandardCopyOption.REPLACE_EXISTING)
}
return true
}
copyLiferayLXCRepositoryConfigurations = {
println ""
if (config.lxcRepositoryPath == null) {
println "Unable to copy configurations from LXC repository, because no LXC repository has been set in gradle.properties"
return
}
File environmentFolder = null
if (config.lxcEnvironmentName == null) {
if (config.defaultCompanyVirtualHost == null) {
println "Unable to copy configurations from LXC repository, because the company default virtual host is unknown"
return
}
String expectedWebId = config.defaultCompanyVirtualHost["webId"]
String expectedHostName = config.defaultCompanyVirtualHost["hostname"]
if (expectedHostName.endsWith(".localhost")) {
expectedHostName = expectedHostName.substring(0, expectedHostName.length() - ".localhost".length())
}
List<File> environmentFolders = fileTree("${config.lxcRepositoryPath}/liferay/configs") {
"**/portal-env.properties"
}.filter {
File envPropertiesFile ->
(expectedWebId.equals("liferay.com") || envPropertiesFile.text.contains("company.default.web.id=${expectedWebId}")) &&
envPropertiesFile.text.contains("company.default.virtual.host.name=${expectedHostName}")
}.collect {
File envPropertiesFile ->
envPropertiesFile.parentFile
}
if (!environmentFolders.isEmpty()) {
environmentFolder = environmentFolders.last()
}
if (environmentFolder != null) {
config.lxcEnvironmentName = environmentFolder.name
}
if (config.lxcEnvironmentName == null) {
println "Unable to copy configurations from ${config.lxcRepositoryPath}, because there is no metadata for LXC environment company.default.web.id=${expectedWebId}, company.default.virtual.host.name=${expectedHostName}"
return
}
}
else {
environmentFolder = new File(config.lxcRepositoryPath, "liferay/configs/${config.lxcEnvironmentName}")
}
if (!environmentFolder.exists()) {
println "Unable to copy configurations from ${config.lxcRepositoryPath}, because ${environmentFolder.absolutePath} does not exist"
return
}
if (!config.databasePartitioningEnabled) {
config.databasePartitioningEnabled = true
updateGradleLocalProperties(["lr.docker.environment.database.partitioning.enabled": "true"])
}
String environment = config.lxcEnvironmentName
List<String> includeAndOverrideFileNames = ["portal-liferay-online-database-partition.properties"]
if (copyLiferayLXCRepositoryConfiguration(environmentFolder, "portal-env.properties", "common/properties/lxc-portal-env-${environment}.properties")) {
includeAndOverrideFileNames.add("./properties/lxc-portal-env-${environment}.properties")
println "Copied ${environment}/portal-env.properties from ${config.lxcRepositoryPath}"
}
if (copyLiferayLXCRepositoryConfiguration(environmentFolder, "scripts", "docker")) {
println "Copied ${environment}/scripts from ${config.lxcRepositoryPath}"
}
if (copyLiferayLXCRepositoryConfiguration(environmentFolder, "osgi", "common/osgi")) {
println "Copied ${environment}/osgi from ${config.lxcRepositoryPath}"
}
file("configs/common/properties/lxc-customer.properties").withOutputStream {
BufferedOutputStream outputStream ->
includeAndOverrideFileNames.each {
String includeAndOverrideFileName ->
outputStream << "include-and-override=" << includeAndOverrideFileName << "\n"
}
}
String latestReleaseInfoDate = updateGradlePropertiesWithLiferayVersion()
if (latestReleaseInfoDate != null) {
println ""
println "Updated gradle-local.properties to environment dated ${latestReleaseInfoDate}"
}
}
deleteMeantForProductionOnlyConfigurations = {
String schema ->
PROD_ONLY_CONFIGS.each {
executeSQLQuery("delete from Configuration_ where configurationId like '%${it}%'", schema)
}
Map<String, String> sanitizedPreferences = [
"liferayAnalyticsEndpointURL": "fake://fakeAEU",
"liferayAnalyticsFaroBackendURL": "fake://fakeFBU",
"liferayAnalyticsURL": "fake://fakeAU",
]
sanitizePortalPreferenceValues(schema, sanitizedPreferences)
println "Deleted known Analytics Cloud, Captcha, CSP, Elasticsearch, Kubernetes entries from schema ${schema}"
}
disableUserObjectValidations = {
String schema ->
executeSQLQuery("update ObjectValidationRule set active_ = 'false' where objectDefinitionId in (select objectDefinitionId from ObjectDefinition where externalReferenceCode = 'L_USER')", schema)
println "Disabled object validation users for User system object in schema ${schema}"
}
getReleaseInfo = {
String descriptorText ->
JsonSlurper jsonSlurper = new JsonSlurper()
Object environmentDescriptor = jsonSlurper.parseText(descriptorText)
String liferayImage = environmentDescriptor["liferay-image"]
String hotfixId = environmentDescriptor["hotfix"]
Pattern releasePattern = ~'[0-9]+\\.q[1-4]\\.[0-9]+'
Matcher releaseMatcher = releasePattern.matcher(liferayImage)
if (!releaseMatcher.find()) {
println "did not match ${liferayImage}"
return null
}
String release = releaseMatcher.group()
if ((release.indexOf(".q1") != -1) && !release.startsWith("2024.")) {
release = release + "-lts"
}
String workspaceProduct = "dxp-${release}"
Map<String, String> releaseInfo = [
"liferay.workspace.product": workspaceProduct,
"liferay.workspace.docker.image.liferay": liferayImage,
]
if (hotfixId != null && !hotfixId.isEmpty()) {
releaseInfo["lr.docker.environment.hotfix.urls"] = "https://releases-cdn.liferay.com/dxp/hotfix/${release}/liferay-dxp-${release}-${hotfixId}.zip"
}
return releaseInfo
}
reactivateDisabledUsers = {
String schema ->
executeSQLQuery("update User_ set status = 0", schema)
executeSQLQuery("update User_ set emailAddress = 'liferaydevsecops@liferay.com', screenName='liferaydevsecops', firstName='Liferay', lastName='DevSecOps' where emailAddress = 'test@lxc.app' and screenName = 'test'", schema)
println "Re-activated all users in schema ${schema}"
}
replaceUserPasswords = {
String schema ->
executeSQLQuery("update User_ set password_ = '${config.liferayUserPassword}', passwordEncrypted = false, passwordReset = false, lockout = false", schema)
println "Reset all user passwords to '${config.liferayUserPassword}' in schema ${schema}"
}
sanitizeMailConfigurations = {
String schema ->
Map<String, String> sanitizedPreferences = [
"mail.session.mail.pop3.host": "fake-pop3-host",
"mail.session.mail.smtp.host": "fake-smtp-host",
"mail.session.mail.pop3.user": "fake-pop3-user",
"mail.session.mail.smtp.user": "fake-stmp-host",
"mail.session.mail.pop3.password": "fake-pop3-password",
"mail.session.mail.smtp.password": "fake-smtp-password",
"mail.session.mail": "false",
"pop.server.notifications.enabled": "false"
]
sanitizePortalPreferenceValues(schema, sanitizedPreferences)
executeSQLQuery("update MBMailingList set inServerName = 'fake-pop3-host', outServerName = 'fake-smtp-host', inPassword = 'fake-pop3-password', outPassword = 'fake-smtp-password', active_ = 'false'", schema)
println "Sanitized known POP and SMTP configurations in schema ${schema}"
}
sanitizePortalPreferenceValues = {
schema, Map<String, String> sanitizedValues ->
sanitizedValues.each {
Map.Entry<String, String> entry ->
executeSQLQuery("update PortalPreferenceValue set smallValue = '${entry.value}' where key_ = '${entry.key}'", schema)
}
}
truncateTables = {
String tableNamePattern, String schema ->
List<Map<String, String>> tableNames = executeSQLQuery("select TABLE_NAME from information_schema.TABLES WHERE TABLE_SCHEMA = '${schema}' and TABLE_NAME like '${tableNamePattern}' OR TABLE_NAME like '${tableNamePattern.toLowerCase()}'", schema)
tableNames.each {
Map<String, String> resultRow ->
executeSQLQuery("truncate table ${resultRow.get("TABLE_NAME")}", schema)
}
}
updateDatabaseForLocalDevelopment = {
forEachCompanyId {
String companyId, String hostname, String webId, String schema ->
replaceUserPasswords(schema)
clearSingleSignOnConfigurations(schema)
reactivateDisabledUsers(schema)
disableUserObjectValidations(schema)
sanitizeMailConfigurations(schema)
deleteMeantForProductionOnlyConfigurations(schema)
}
updateVirtualHosts()
copyLiferayLXCRepositoryConfigurations()
}
updateGradlePropertiesWithLiferayVersion = {
String environment = config.lxcEnvironmentName
File lxcRepositoryFolder = file(config.lxcRepositoryPath)
String descriptorPath = "automation/environment-descriptors/${environment}.json"
File environmentDescriptorFile = new File(lxcRepositoryFolder, descriptorPath)
if (!environmentDescriptorFile.exists()) {
return null
}
Map<String, String> latestReleaseInfo = null
String latestReleaseInfoDate = null
waitForCommand("git log -5 --pretty='%H %cd' --date=format-local:'%Y-%m-%d %H:%M:%S %Z' -- ${descriptorPath}", lxcRepositoryFolder).eachLine {
String line ->
int pos = line.indexOf(" ")
String commit = line.substring(0, pos)
String releaseInfoDate = line.substring(pos + 1)
println ""
println releaseInfoDate
Map<String, String> releaseInfo = getReleaseInfo(waitForCommand("git show ${commit}:${descriptorPath}", lxcRepositoryFolder)).each {
Map.Entry<String, String> entry ->
println entry
}
if (latestReleaseInfo == null) {
latestReleaseInfo = releaseInfo
latestReleaseInfoDate = releaseInfoDate
}
}
if (latestReleaseInfo != null) {
latestReleaseInfo.put("lr.docker.environment.lxc.environment.name", environment)
String product = latestReleaseInfo["liferay.workspace.product"]
if (product != null && !product.equals(config.product)) {
config.product = product
project.gradle.liferayWorkspace.product = product
}
String dockerImageLiferay = latestReleaseInfo["liferay.workspace.docker.image.liferay"]
if (dockerImageLiferay != null && !dockerImageLiferay.equals(config.dockerImageLiferay)) {
config.dockerImageLiferay = dockerImageLiferay
project.gradle.liferayWorkspace.dockerImageLiferay = dockerImageLiferay
}
String hotfixURL = latestReleaseInfo["lr.docker.environment.hotfix.urls"]
if (hotfixURL != null && !config.hotfixURLs.contains(hotfixURL)) {
config.hotfixURLs.add(hotfixURL)
}
updateGradleLocalProperties(latestReleaseInfo)
}
return latestReleaseInfoDate
}
updateVirtualHosts = {
executeSQLQuery("update VirtualHost set hostname = concat(hostname, '.localhost') where hostname <> 'localhost' and hostname not like '%.localhost'", config.databaseName)
println "Added .localhost to the end of all virtual host names that were not localhost"
}
}
tasks.register("copyLiferayLXCRepositoryConfigurations") {
doFirst {
copyLiferayLXCRepositoryConfigurations();
}
}
tasks.register("decryptCloudBackupDatabase") {
onlyIf("using an external database") {
config.useDatabase
}
onlyIf("there is a database dump file") {
!Util.isEmpty(project.fileTree("dumps"))
}
doFirst {
FileCollection decryptedBackups = project.fileTree("dumps") {
include "**/*.sql"
include "**/*.sql.gz"
include "**/*.gz"
}
if (!Util.isEmpty(decryptedBackups)) {
print("Found existing unencrypted backups in dumps/ folder")
return
}
FileCollection encryptedBackups = project.fileTree("dumps") {
include "*.7z"
exclude "*doclib*"
}
if (Util.isEmpty(encryptedBackups)) {
encryptedBackups = project.fileTree("dumps") {
include "*.zip"
exclude "*doclib*"
}
}
if (encryptedBackups.isEmpty()) {
return
}
else if (encryptedBackups.size() != 1) {
throw new GradleException("Aborting because multiple potential backups where found in the dumps/ folder")
}
if (config.lxcBackupPassword != null && config.lxcBackupPassword.length() > 0) {
println "Extracting LXC database backup (using password specified in gradle.properties)"
waitForCommand("7z x -aos ${encryptedBackups[0].absolutePath} -odumps/ -p${config.lxcBackupPassword}")
}
else {
println "Extracting LXC database backup (assuming no password, because none was set in gradle.properties)"
waitForCommand("7z x -aos ${encryptedBackups[0].absolutePath} -odumps/ -p")
}
}
}
tasks.register("copyDatabaseDumpsToDumpsVolume") {
dependsOn ":decryptCloudBackupDatabase"
onlyIf("using an external database") {
config.useDatabase
}
onlyIf("there is a database dump file") {
!Util.isEmpty(project.fileTree("dumps"))
}
doFirst {
FileCollection backupFiles = project.fileTree(config.dataDirectory)
if (config.dataDirectory != null && !config.dataDirectory.isEmpty() && !Util.isEmpty(backupFiles)) {
return;
}
String dumpsVolumeName = "${config.namespace}_dumps"
if (getExistingVolumeNames().contains(dumpsVolumeName)) {
waitForCommand("docker compose down database")
waitForCommand("docker volume rm ${dumpsVolumeName}")
}
waitForCommand("docker compose build database")
waitForCommand("docker compose create database")
fileTree("dumps") {
include "**/*.sql"
include "**/*.sql.gz"
include "**/*.gz"
}.forEach {
File dumpFile ->
println dumpFile
String oldPath = dumpFile.absolutePath
String newPath = null
if (dumpFile.name.endsWith(".gz") && !dumpFile.name.endsWith(".sql.gz")) {
newPath = "${oldPath.substring(0, oldPath.length() - 3)}.sql.gz"
}
else if (!dumpFile.name.contains(".")) {
newPath = "${dumpFile.absolutePath}.sql"
}
if (newPath != null) {
dumpFile.renameTo(newPath)
dumpFile = file(newPath)
}
addToVolume(dumpsVolumeName, dumpFile)
}
println "Loading database backup into database dumps volume"
File globalVariableScript = file("0.sql")
if (config.useDatabaseMySQL) {
Map<String, String> newVariables = [
"max_allowed_packet": String.valueOf(1L << 30),
"autocommit": "0",
"unique_checks": "0",
"foreign_key_checks": "0",
"innodb_stats_auto_recalc": "0",
]
globalVariableScript.withOutputStream {
BufferedOutputStream initSQLOutputStream ->
initSQLOutputStream << newVariables.collect {
Map.Entry<String, String> entry ->
"SET GLOBAL ${entry.key}=${entry.value};"
}.join("\n") << ";"
initSQLOutputStream << ["lportal", "dxpcloud", "cloudsqlimport"].collect {
String userName ->
"create user if not exists ${userName} identified by 'lportal';\ngrant all on *.* to '${userName}'@'%';\n"
}.join("\n")
}
}
else if (config.useDatabasePostgreSQL) {
globalVariableScript.withOutputStream {
BufferedOutputStream initSQLOutputStream ->
initSQLOutputStream << "create role cloudsqlsuperuser with login superuser password 'lportal';"
}
}
if (globalVariableScript.exists()) {
addToVolume(dumpsVolumeName, globalVariableScript)
globalVariableScript.delete()
}
}
}
tasks.register("importDatabaseDumps") {
dependsOn ":copyDatabaseDumpsToDumpsVolume"
onlyIf("using an external database") {
config.useDatabase
}
onlyIf("there is a database dump file") {
!Util.isEmpty(project.fileTree("dumps"))
}
doFirst {
FileCollection backupFiles = project.fileTree(config.dataDirectory)
if (config.dataDirectory != null && !config.dataDirectory.isEmpty() && !Util.isEmpty(backupFiles)) {
println "Skipping database dumps import because a data backup will be imported from ${config.dataDirectory}"
return;
}
println "Initializing database via first start scripts"
waitForContainer("database")
if (config.useDatabaseMySQL) {
Map<String, String> oldVariables = [
"max_allowed_packet": String.valueOf(1L << 26),
"autocommit": "1",
"unique_checks": "1",
"foreign_key_checks": "1",
"innodb_stats_auto_recalc": "1",
]
String query = oldVariables.collect {
Map.Entry<String, String> oldVariable ->
"SET GLOBAL ${oldVariable.key}=${oldVariable.value};\n"
}.join("")
executeSQLQuery(query)
}
println "Attempting to update database for local development"
updateDatabaseForLocalDevelopment()
}
}
project.plugins.apply "docker-common"