-
Notifications
You must be signed in to change notification settings - Fork 0
/
build.gradle.kts
150 lines (134 loc) · 3.67 KB
/
build.gradle.kts
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
import nebula.plugin.contacts.Contact
import java.io.ByteArrayOutputStream
import java.nio.file.Files
plugins {
id("nebula.contacts") version "6.0.0"
}
contacts {
addPerson("[email protected]", delegateClosureOf<Contact> {
moniker = "Szymon Andrzejewski"
roles("developer", "maintainer")
})
}
subprojects {
version = File("${projectDir}/project.version").readText().trim()
}
apply(from = rootProject.file("gradle/util/misc.gradle.kts"))
apply(from = rootProject.file("gradle/util/git.gradle.kts"))
tasks {
fun loadFile(file: File): HashMap<String, String> {
val variables = HashMap<String, String>()
file.forEachLine { line ->
if (!line.startsWith("#")) {
val keyValue = line.split('=')
variables[keyValue[0].trim()] = keyValue[1].trim()
}
}
return variables
}
fun generateCompose(map: Map<String, String>, templateFile: File, outputFile: File) {
var template = templateFile.readText()
map.forEach { (key, value) ->
template = template.replace("\${$key}", value)
}
Files.write(outputFile.toPath(), template.toByteArray())
}
fun isContainerRunning(containerName: String): Boolean {
val output = ByteArrayOutputStream()
exec {
standardOutput = output
commandLine("docker", "container", "inspect", "-f", "'{{.State.Running}}'", containerName)
}
return output.toString().trim() == "'true'"
}
fun waitUntilRunning(containerName: String) {
var running: Boolean
val delay = 3000L
while (true) {
running = isContainerRunning(containerName)
if (running) return
Thread.sleep(delay)
}
}
register("generateBaseCompose") {
doLast {
val versionMap = loadFile(file("docker/versions"))
val template = "docker/template/"
val destination = "docker/"
generateCompose(
versionMap,
file("${template}compose-observability.yml"),
file("${destination}compose-observability.yml")
)
generateCompose(
versionMap,
file("${template}compose-data.yml"),
file("${destination}compose-data.yml")
)
generateCompose(
versionMap,
file("${template}compose-networking.yml"),
file("${destination}compose-networking.yml")
)
}
}
fun composeUp(projectName: Any) {
exec {
workingDir("./docker/")
commandLine(
"docker", "compose",
"-p", projectName,
"-f", "compose-$projectName.yml",
"up",
"-d",
"--no-recreate"
)
}
}
register("composeUp") {
doLast {
val projectName = project.properties["pName"] ?: throw GradleException("-PpName=name not provided")
val versionMap = loadFile(file("docker/versions"))
val template = "docker/template/"
val destination = "docker/"
generateCompose(
versionMap,
file("${template}compose-$projectName.yml"),
file("${destination}compose-$projectName.yml")
)
composeUp(projectName)
}
}
register("composeBaseUp") {
dependsOn("generateBaseCompose")
doLast {
composeUp("observability")
composeUp("data")
}
}
fun composeDown(projectName: Any) {
exec {
commandLine("docker", "compose", "-p", projectName, "down")
}
}
register("composeBaseDown") {
composeDown("data")
composeDown("observability")
}
register("composeDown") {
doLast {
val pName = project.properties["pName"] ?: throw GradleException("-PpName=name not provided")
composeDown(pName)
}
}
register("runTunnel") {
doLast {
exec {
workingDir("./docker/tunnel/")
commandLine(
"bash", "run_tunnel.sh", "&"
)
}
}
}
}