-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathbuild.gradle
218 lines (163 loc) · 5.18 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
import com.bmuschko.gradle.docker.tasks.container.DockerCopyFileFromContainer
import de.undercouch.gradle.tasks.download.Download
import groovy.json.JsonSlurper
import java.time.format.DateTimeFormatter
plugins {
id "com.avast.gradle.docker-compose" version "0.17.10"
}
static List toList(String s) {
return s.trim().split(",").grep()
}
File projectDir = project.projectDir as File
class Config {
public int clusterNodes = 0
public List<String> composeFiles = new ArrayList<String>()
public String databaseName = "lportal"
public String dataDirectory = "data"
public List<String> hotfixURLs = new ArrayList<String>()
public String liferayDockerImageId = ""
public String namespace = "lrswde"
public List<String> services = new ArrayList<String>()
@Override
public String toString() {
return """
Config:
------------------------
clusterNodes: ${clusterNodes}
composeFiles: ${composeFiles}
databaseName: ${databaseName}
dataDirectory: ${dataDirectory}
hotfixURLs: ${hotfixURLs}
liferayDockerImageId: ${liferayDockerImageId}
namespace: ${namespace}
services: ${services}
"""
}
}
Config config = new Config()
config.composeFiles.add("docker-compose.yaml")
config.liferayDockerImageId = gradle.liferayWorkspace.dockerImageId as String
Integer clusterNodes = project.getProperty("lr.docker.environment.cluster.nodes") as Integer
def composeFiles = toList(project.getProperty("lr.docker.environment.compose.files"))
String dataDirectory = project.getProperty("lr.docker.environment.data.directory")
String databaseName = project.getProperty("lr.docker.environment.database.name")
List hotfixURLs = toList(project.getProperty("lr.docker.environment.hotfix.urls"))
String namespace = project.getProperty("lr.docker.environment.namespace")
List services = project.properties.findAll {
it.key =~ /^lr.docker.environment.service.enabled\[\w+\]$/
}.findAll {
it.value =~ /true|1/
}.collect {
it.key.substring(it.key.indexOf("[") + 1, it.key.indexOf("]"))
}
if (clusterNodes != null) {
config.clusterNodes = clusterNodes
}
if (databaseName != null) {
config.databaseName = databaseName
}
if (dataDirectory != null && dataDirectory.length() > 0) {
config.dataDirectory = dataDirectory
}
if (!hotfixURLs.isEmpty()) {
config.hotfixURLs = hotfixURLs
}
if (namespace != null) {
config.namespace = namespace
}
if (!services.isEmpty()) {
config.services = services
}
boolean useLiferay = config.services.contains("liferay")
boolean useClustering = useLiferay && config.clusterNodes > 0
ConfigurableFileTree dockerComposeFileTree = project.fileTree(projectDir) {
include "**/service.*.yaml"
if (useClustering) {
include "**/clustering.*.yaml"
}
if (useLiferay) {
include "**/liferay.*.yaml"
}
}
List<String> serviceComposeFiles = config.services.collect {
String serviceName ->
FileTree matchingFileTree = dockerComposeFileTree.matching {
include "**/*.${serviceName}.yaml"
}
if (matchingFileTree.isEmpty()) {
List<String> possibleServices = dockerComposeFileTree.findAll{
it.name.startsWith("service.")
}.collect {
it.name.substring("service.".length(), it.name.indexOf(".yaml"))
}
throw new GradleException(
"The service '${serviceName}' does not have a matching service.*.yaml file. Possible services are: ${possibleServices}");
}
matchingFileTree.getFiles()
}.flatten().collect {
projectDir.relativePath(it)
}
config.composeFiles.addAll(serviceComposeFiles)
tasks.register("exportContainerData", DockerCopyFileFromContainer) {
containerId = "${config.namespace}-data-helper"
doFirst {
mkdir hostPath.get()
}
hostPath = provider {
DateTimeFormatter formatter = DateTimeFormatter.
ofPattern("yyyyMMdd.HHmmss").
withZone(ZoneId.systemDefault())
"exported_data/data_${formatter.format(Instant.now())}"
}
remotePath = "/data/container-data"
}
tasks.register("prepareHotfixes", Download) {
dest project.layout.dir(project.provider {project.file("configs/common/patching")})
onlyIf {
!config.hotfixURLs.isEmpty()
}
src config.hotfixURLs
}
clean {
dependsOn ":cleanPrepareHotfixes"
}
dockerDeploy {
if (useLiferay) {
dependsOn ":prepareHotfixes"
}
}
composeUp {
if (useLiferay) {
dependsOn ":buildDockerImage"
}
doFirst {
println "Using config: ${config}"
}
onlyIf {
!config.composeFiles.isEmpty()
}
}
Map<String, Object> environmentMap = [:]
environmentMap.put "DATA_DIRECTORY", config.dataDirectory
environmentMap.put "DATABASE_NAME", config.databaseName
environmentMap.put "NAMESPACE", config.namespace
if (useClustering) {
environmentMap.put "LIFERAY_CLUSTER_NODES", config.clusterNodes
}
if (useLiferay) {
environmentMap.put "LIFERAY_IMAGE_NAME", config.liferayDockerImageId
}
dockerCompose {
// DEBUG: Set to true to print container startup output to the console
captureContainersOutput = false
// DEBUG: Uncomment to dump the container logs to a directory for inspection
// captureContainersOutputToFiles = project.file('containerLogs')
environment.putAll environmentMap
projectName = config.namespace
// DEBUG: Set to false to retain volume data on restart
removeVolumes = true
tcpPortsToIgnoreWhenWaiting = [8000, 11311]
useComposeFiles = config.composeFiles
// DEBUG: Set to true if container startup is failing
retainContainersOnStartupFailure = false
}