Skip to content

Commit 51dfb6a

Browse files
authored
Dev infra cleanup (#1939)
1 parent 0437441 commit 51dfb6a

File tree

6 files changed

+68
-88
lines changed

6 files changed

+68
-88
lines changed

build.gradle.kts

+1-51
Original file line numberDiff line numberDiff line change
@@ -7,62 +7,12 @@ plugins {
77
id("com.saveourtool.save.buildutils.code-quality-convention")
88
id("com.saveourtool.save.buildutils.publishing-configuration")
99
alias(libs.plugins.talaiot.base)
10-
alias(libs.plugins.liquibase.gradle)
1110
java
1211
}
1312

1413
val profile = properties.getOrDefault("save.profile", "dev") as String
1514

16-
liquibase {
17-
activities {
18-
val commonArguments = mapOf(
19-
"logLevel" to "info",
20-
"contexts" to when (profile) {
21-
"prod" -> "prod"
22-
"dev" -> "dev"
23-
else -> throw GradleException("Profile $profile not configured to map on a particular liquibase context")
24-
}
25-
)
26-
// Configuring liquibase
27-
register("main") {
28-
arguments = mapOf("changeLogFile" to "db/db.changelog-master.xml") +
29-
getBackendDatabaseCredentials(profile).toLiquibaseArguments() +
30-
commonArguments
31-
}
32-
register("sandbox") {
33-
arguments = mapOf(
34-
"changeLogFile" to "save-sandbox/db/db.changelog-sandbox.xml",
35-
"liquibaseSchemaName" to "save_sandbox",
36-
"defaultSchemaName" to "save_sandbox",
37-
) +
38-
getSandboxDatabaseCredentials(profile).toLiquibaseArguments() +
39-
commonArguments
40-
}
41-
register("demo") {
42-
arguments = mapOf(
43-
"changeLogFile" to "save-demo/db/db.changelog-demo.xml",
44-
"liquibaseSchemaName" to "save_demo",
45-
"defaultSchemaName" to "save_demo",
46-
) +
47-
getDemoDatabaseCredentials(profile).toLiquibaseArguments() +
48-
commonArguments
49-
}
50-
}
51-
}
52-
53-
dependencies {
54-
liquibaseRuntime(libs.liquibase.core)
55-
liquibaseRuntime(libs.mysql.connector.java)
56-
liquibaseRuntime(libs.picocli)
57-
}
58-
59-
tasks.withType<org.liquibase.gradle.LiquibaseTask>().configureEach {
60-
@Suppress("MAGIC_NUMBER")
61-
this.javaLauncher.set(project.extensions.getByType<JavaToolchainService>().launcherFor {
62-
// liquibase-core 4.7.0 and liquibase-gradle 2.1.1 fails on Java >= 13 on Windows; works on Mac
63-
languageVersion.set(JavaLanguageVersion.of(11))
64-
})
65-
}
15+
registerLiquibaseTask(profile)
6616

6717
talaiot {
6818
metrics {

buildSrc/src/main/kotlin/com/saveourtool/save/buildutils/DatabaseUtils.kt

+6-22
Original file line numberDiff line numberDiff line change
@@ -16,34 +16,18 @@ data class DatabaseCredentials(
1616
val password: String
1717
) {
1818
/**
19-
* @return arguments for liquibase task
19+
* @return adjusted [databaseUrl] for liquibase runner in docker
2020
*/
21-
fun toLiquibaseArguments(): Map<String, String> = mapOf(
22-
"url" to "$databaseUrl?createDatabaseIfNotExist=true",
23-
"username" to username,
24-
"password" to password,
25-
)
21+
fun getDatabaseUrlForLiquibaseInDocker(): String =
22+
"${databaseUrl.replace("localhost", "mysql")}?createDatabaseIfNotExist=true"
2623
}
2724

2825
/**
26+
* @param projectName save-backend, save-sandbox or save-demo
2927
* @param profile a profile to get credentials for
30-
* @return an instance of [DatabaseCredentials] for [profile] in backend
28+
* @return an instance of [DatabaseCredentials] for [profile] in [projectName]
3129
*/
32-
fun Project.getBackendDatabaseCredentials(profile: String): DatabaseCredentials = getDatabaseCredentials("save-backend", profile)
33-
34-
/**
35-
* @param profile a profile to get credentials for
36-
* @return an instance of [DatabaseCredentials] for [profile] in sandbox
37-
*/
38-
fun Project.getSandboxDatabaseCredentials(profile: String): DatabaseCredentials = getDatabaseCredentials("save-sandbox", profile)
39-
40-
/**
41-
* @param profile a profile to get credentials for
42-
* @return an instance of [DatabaseCredentials] for [profile] in demo
43-
*/
44-
fun Project.getDemoDatabaseCredentials(profile: String): DatabaseCredentials = getDatabaseCredentials("save-demo", profile)
45-
46-
private fun Project.getDatabaseCredentials(projectName: String, profile: String): DatabaseCredentials {
30+
fun Project.getDatabaseCredentials(projectName: String, profile: String): DatabaseCredentials {
4731
val props = java.util.Properties()
4832
// Branch for other environments, e.g. local deployment or server deployment
4933
file("$projectName/src/main/resources/application-$profile.properties").inputStream().use(props::load)

buildSrc/src/main/kotlin/com/saveourtool/save/buildutils/DockerStackConfiguration.kt

+59-13
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
package com.saveourtool.save.buildutils
66

7+
import org.gradle.api.GradleException
78
import org.gradle.api.Project
89
import org.gradle.api.tasks.Exec
910
import org.gradle.api.tasks.TaskProvider
@@ -23,6 +24,63 @@ const val NEO4J_STARTUP_DELAY_MILLIS = 30_000L
2324
const val MINIO_STARTUP_DELAY_MILLIS = 5_000L
2425
const val KAFKA_STARTUP_DELAY_MILLIS = 5_000L
2526

27+
fun Project.registerLiquibaseTask(profile: String) {
28+
val registerLiquibaseTaskBackend = reqisterLiquibaseTask(
29+
projectName = "save-backend",
30+
relativeChangeLogFile = "db/db.changelog-master.xml",
31+
profile = profile
32+
)
33+
val registerLiquibaseTaskSandbox = reqisterLiquibaseTask(
34+
projectName = "save-sandbox",
35+
relativeChangeLogFile = "save-sandbox/db/db.changelog-sandbox.xml",
36+
profile = profile
37+
)
38+
val registerLiquibaseTaskDemo = reqisterLiquibaseTask(
39+
projectName = "save-demo",
40+
relativeChangeLogFile = "save-demo/db/db.changelog-demo.xml",
41+
profile = profile
42+
)
43+
tasks.register("liquibaseUpdate") {
44+
dependsOn(
45+
registerLiquibaseTaskBackend,
46+
registerLiquibaseTaskSandbox,
47+
registerLiquibaseTaskDemo
48+
)
49+
}
50+
}
51+
52+
private fun Project.reqisterLiquibaseTask(projectName: String, relativeChangeLogFile: String, profile: String): TaskProvider<Exec> {
53+
val taskName = "liquibaseUpdate" + projectName.split("-").map { it.capitalized() }.joinToString("")
54+
val credentials = getDatabaseCredentials(projectName, profile)
55+
56+
return tasks.register<Exec>(taskName) {
57+
val contexts = when (profile) {
58+
"prod" -> "prod"
59+
"dev" -> "dev"
60+
else -> throw GradleException("Profile $profile not configured to map on a particular liquibase context")
61+
}
62+
63+
val changeLogFile = rootDir.resolve(relativeChangeLogFile)
64+
val changeLogFileSource = "${changeLogFile.parent}"
65+
val changeLogFileTarget = relativeChangeLogFile.substringBeforeLast("/")
66+
commandLine(
67+
"docker", "run",
68+
"-v", "$changeLogFileSource:/liquibase/changelog/$changeLogFileTarget",
69+
"--rm",
70+
"--env", "INSTALL_MYSQL=true",
71+
"--network", "build_default",
72+
"liquibase/liquibase:4.15",
73+
"--url=${credentials.getDatabaseUrlForLiquibaseInDocker()}",
74+
"--changeLogFile=$relativeChangeLogFile",
75+
"--username=${credentials.username}",
76+
"--password=${credentials.password}",
77+
"--log-level=info",
78+
"--contexts=$contexts",
79+
"update"
80+
)
81+
}
82+
}
83+
2684
/**
2785
* @param profile deployment profile, used, for example, to start SQL database in dev profile only
2886
*/
@@ -101,18 +159,6 @@ fun Project.createStackDeployTask(profile: String) {
101159
| MINIO_ROOT_USER: admin
102160
| MINIO_ROOT_PASSWORD: adminadmin
103161
|
104-
| minio-create-bucket:
105-
| image: minio/mc:latest
106-
| depends_on:
107-
| - minio
108-
| entrypoint:
109-
| - /bin/sh
110-
| - -c
111-
| - |
112-
| /usr/bin/mc alias set minio http://minio:9000 admin adminadmin
113-
| /usr/bin/mc mb --ignore-existing minio/cnb
114-
| /usr/bin/mc policy set public minio/cnb
115-
|
116162
|${declareDexService().prependIndent(" ")}
117163
""".trimMargin()
118164
} else if (profile == "dev" && it.trim().startsWith("logging:")) {
@@ -226,7 +272,7 @@ fun Project.createStackDeployTask(profile: String) {
226272
dependsOn(kafkaTaskName)
227273
}
228274

229-
val minioTaskName = registerService("minio-create-bucket", MINIO_STARTUP_DELAY_MILLIS, "startMinioService")
275+
val minioTaskName = registerService("minio", MINIO_STARTUP_DELAY_MILLIS)
230276
tasks.register("startMinio") {
231277
dependsOn(minioTaskName)
232278
}

gradle/libs.versions.toml

-2
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@ testcontainers = "1.17.6"
2929
okhttp3 = "4.10.0"
3030
reckon = "0.16.1"
3131
commons-compress = "1.22"
32-
picocli = "4.7.1"
3332
zip4j = "2.11.3"
3433
ktoml = "0.4.1"
3534
springdoc = "1.6.14"
@@ -181,7 +180,6 @@ diktat-gradle-plugin = { module = "org.cqfn.diktat:diktat-gradle-plugin", versio
181180
detekt-gradle-plugin = { module = "io.gitlab.arturbosch.detekt:detekt-gradle-plugin", version.ref = "detekt" }
182181
reckon-gradle-plugin = { module = "org.ajoberstar.reckon:reckon-gradle", version.ref = "reckon" }
183182
commons-compress = { module = "org.apache.commons:commons-compress", version.ref = "commons-compress" }
184-
picocli = { module = "info.picocli:picocli", version.ref = "picocli" }
185183
zip4j = { module = "net.lingala.zip4j:zip4j", version.ref = "zip4j" }
186184
kotlinx-cli = { module = "org.jetbrains.kotlinx:kotlinx-cli", version.ref = "kotlinx-cli" }
187185
gradle-plugin-spotless = { module = "com.diffplug.spotless:spotless-plugin-gradle", version.ref = "spotless" }

save-demo/src/main/resources/application-dev.properties

+1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ demo.s3-storage.endpoint=http://localhost:9000
77
demo.s3-storage.bucketName=cnb
88
demo.s3-storage.credentials.accessKeyId=admin
99
demo.s3-storage.credentials.secretAccessKey=adminadmin
10+
demo.s3-storage.createBucketIfNotExists=true
1011
demo.backend-url=http://localhost:5800/internal
1112
# Doesn't make sense, when kubernetes profile is off, overwritten by demo-configmap.yaml when kubernetes profile is on
1213
demo.agent-config.demo-url=http://localhost:5421

save-sandbox/src/main/resources/application-dev.properties

+1
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,5 @@ sandbox.s3-storage.endpoint=http://localhost:9000
55
sandbox.s3-storage.bucketName=cnb
66
sandbox.s3-storage.credentials.accessKeyId=admin
77
sandbox.s3-storage.credentials.secretAccessKey=adminadmin
8+
sandbox.s3-storage.createBucketIfNotExists=true
89
orchestrator.agents-count=1

0 commit comments

Comments
 (0)