-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
220 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
.gradle | ||
**/build/ | ||
!src/**/build/ | ||
!**/src/**/build/ | ||
|
||
# Ignore Gradle GUI config | ||
gradle-app.setting | ||
|
69 changes: 69 additions & 0 deletions
69
gradle/plugins/src/main/kotlin/com/saveourtool/malware/detection/build/DockerLocalRun.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
package com.saveourtool.malware.detection.build | ||
|
||
import org.gradle.api.Project | ||
import org.gradle.api.file.RegularFile | ||
import org.gradle.api.provider.Provider | ||
import org.gradle.api.tasks.Exec | ||
import org.gradle.api.tasks.TaskProvider | ||
import org.gradle.configurationcache.extensions.capitalized | ||
import org.gradle.kotlin.dsl.register | ||
import org.intellij.lang.annotations.Language | ||
import java.io.ByteArrayOutputStream | ||
|
||
const val DEFAULT_STARTUP_TIMEOUT = 5_000L | ||
|
||
/** | ||
* Registers tasks to start [serviceName] specified in [dockerComposeContent] | ||
* | ||
* @param serviceName | ||
* @param startupDelayInMillis | ||
* @param dockerComposeContent | ||
* @return task which starts [serviceName] | ||
*/ | ||
internal fun Project.registerDockerService( | ||
serviceName: String, | ||
startupDelayInMillis: Long, | ||
@Language("yaml") | ||
dockerComposeContent: String, | ||
): TaskProvider<Exec> { | ||
val serviceNameCapitalized = serviceName.split("-").joinToString("") { it.capitalized() } | ||
val composeFileProvider: Provider<RegularFile> = layout.buildDirectory.dir(serviceName).map { it.file("docker-compose.yaml") } | ||
val generateComposeFileTaskName = "generateComposeFileFor$serviceNameCapitalized" | ||
tasks.register(generateComposeFileTaskName) { | ||
description = "Generate a compose file for $serviceName service" | ||
outputs.file(composeFileProvider) | ||
doLast { | ||
composeFileProvider.get().asFile | ||
.writeText(""" | ||
|version: '3.9' | ||
|services: | ||
|${dockerComposeContent.prependIndent( )} | ||
""".trimMargin()) | ||
} | ||
} | ||
|
||
val startServiceTaskName = "start${serviceNameCapitalized}Service" | ||
return tasks.register<Exec>(startServiceTaskName) { | ||
description = "Start $serviceName locally as a service" | ||
dependsOn(generateComposeFileTaskName) | ||
doFirst { | ||
logger.lifecycle("Running the following command: [docker compose --file ${composeFileProvider.get()} up -d $serviceName]") | ||
} | ||
standardOutput = ByteArrayOutputStream() | ||
errorOutput = ByteArrayOutputStream() | ||
commandLine("docker", "compose", "--file", composeFileProvider.get(), "up", "-d", serviceName) | ||
isIgnoreExitValue = true | ||
doLast { | ||
val execResult = executionResult.get() | ||
if (execResult.exitValue != 0) { | ||
logger.lifecycle("${this.name} failed with following output:") | ||
logger.info(standardOutput.toString()) | ||
logger.error(errorOutput.toString()) | ||
execResult.assertNormalExitValue() | ||
execResult.rethrowFailure() | ||
} | ||
logger.lifecycle("Waiting $startupDelayInMillis millis for $serviceName to start") | ||
Thread.sleep(startupDelayInMillis) | ||
} | ||
} | ||
} |
29 changes: 29 additions & 0 deletions
29
...n/kotlin/com/saveourtool/malware/detection/build/kafka-local-run-configuration.gradle.kts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
package com.saveourtool.malware.detection.build | ||
|
||
registerDockerService( | ||
serviceName = "kafka", | ||
startupDelayInMillis = DEFAULT_STARTUP_TIMEOUT, | ||
dockerComposeContent = """ | ||
|zookeeper: | ||
| image: confluentinc/cp-zookeeper:latest | ||
|environment: | ||
| ZOOKEEPER_CLIENT_PORT: 2181 | ||
| ZOOKEEPER_TICK_TIME: 2000 | ||
|ports: | ||
| - 22181:2181 | ||
| | ||
|kafka: | ||
| image: confluentinc/cp-kafka:latest | ||
| depends_on: | ||
| - zookeeper | ||
| ports: | ||
| - 29092:29092 | ||
| environment: | ||
| KAFKA_BROKER_ID: 1 | ||
| KAFKA_ZOOKEEPER_CONNECT: zookeeper:2181 | ||
| KAFKA_ADVERTISED_LISTENERS: PLAINTEXT://kafka:9092,PLAINTEXT_HOST://localhost:29092 | ||
| KAFKA_LISTENER_SECURITY_PROTOCOL_MAP: PLAINTEXT:PLAINTEXT,PLAINTEXT_HOST:PLAINTEXT | ||
| KAFKA_INTER_BROKER_LISTENER_NAME: PLAINTEXT | ||
| KAFKA_OFFSETS_TOPIC_REPLICATION_FACTOR: 1 | ||
""".trimMargin() | ||
) |
79 changes: 79 additions & 0 deletions
79
...main/kotlin/com/saveourtool/malware/detection/build/s3-local-run-configuration.gradle.kts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
package com.saveourtool.malware.detection.build | ||
|
||
import org.intellij.lang.annotations.Language | ||
|
||
interface S3LocalRunExtension { | ||
val bucketName: Property<String> | ||
val user: Property<String> | ||
val password: Property<String> | ||
val startupPath: Property<String> | ||
} | ||
|
||
val extension: S3LocalRunExtension = extensions.create("s3LocalRun") | ||
|
||
afterEvaluate { | ||
val bucketName: String = extension.bucketName.getOrElse("test") | ||
val user: String = extension.user.getOrElse("admin") | ||
val password: String = extension.password.getOrElse("adminadmin") | ||
|
||
registerDockerService( | ||
serviceName = "minio", | ||
startupDelayInMillis = DEFAULT_STARTUP_TIMEOUT, | ||
dockerComposeContent = """ | ||
|minio: | ||
| image: minio/minio:latest | ||
| container_name: minio | ||
| command: server /data --console-address ":9090" | ||
| ports: | ||
| - 9000:9000 | ||
| - 9090:9090 | ||
| environment: | ||
| MINIO_ROOT_USER: $user | ||
| MINIO_ROOT_PASSWORD: $password | ||
""".trimMargin() | ||
).also { startTask -> | ||
extension.startupPath.orNull?.let { startupPath -> | ||
registerMinioStartupTask( | ||
bucketName, | ||
user, | ||
password, | ||
startupPath, | ||
).also { | ||
startTask.configure { finalizedBy(it) } | ||
} | ||
} | ||
} | ||
} | ||
|
||
fun Project.registerMinioStartupTask( | ||
bucketName: String, | ||
user: String, | ||
password: String, | ||
startupPath: String, | ||
): TaskProvider<Exec> = tasks.register<Exec>("minioStartup") { | ||
val workingDirectory = layout.buildDirectory.dir("minio-startup") | ||
val runScriptProvider = workingDirectory.map { it.file("run.sh") } | ||
outputs.file(runScriptProvider) | ||
|
||
@Language("sh") | ||
val shellScript: String = """ | ||
#!/bin/sh | ||
/usr/bin/mc alias set minio http://host.docker.internal:9000 $user $password | ||
/usr/bin/mc mb --ignore-existing minio/$bucketName | ||
/usr/bin/mc policy set public minio/$bucketName | ||
/usr/bin/mc cp --recursive /data/ minio/$bucketName | ||
""".trimIndent() | ||
|
||
doFirst { | ||
runScriptProvider.get().asFile.writeText(shellScript) | ||
} | ||
|
||
commandLine( | ||
"docker", "run", | ||
"-v", "${project.rootProject.layout.projectDirectory.asFile}/$startupPath:/data", | ||
"-v", "${workingDirectory.get().asFile}:/run", | ||
"--rm", | ||
"--entrypoint=/run/run.sh", | ||
"minio/mc:latest", | ||
) | ||
} |
42 changes: 42 additions & 0 deletions
42
...otlin/com/saveourtool/malware/detection/build/spring-boot-kotlin-configuration.gradle.kts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
package com.saveourtool.malware.detection.build | ||
|
||
import org.gradle.accessors.dm.LibrariesForLibs | ||
import org.gradle.api.tasks.testing.Test | ||
import org.gradle.kotlin.dsl.* | ||
import org.jetbrains.kotlin.gradle.tasks.KotlinCompile | ||
|
||
plugins { | ||
kotlin("multiplatform") | ||
kotlin("plugin.spring") | ||
kotlin("plugin.jpa") | ||
id("org.springframework.boot") | ||
id("io.spring.dependency-management") | ||
} | ||
|
||
@Suppress("GENERIC_VARIABLE_WRONG_DECLARATION") | ||
val libs = the<LibrariesForLibs>() | ||
val javaVersion: JavaVersion = JavaVersion.toVersion(libs.versions.java.get()) | ||
|
||
java { | ||
sourceCompatibility = javaVersion | ||
} | ||
|
||
kotlin { | ||
jvmToolchain { | ||
this.languageVersion.set(JavaLanguageVersion.of(javaVersion.majorVersion)) | ||
} | ||
} | ||
|
||
tasks.withType<KotlinCompile> { | ||
kotlinOptions { | ||
freeCompilerArgs += "-Xjsr305=strict" | ||
jvmTarget = javaVersion.majorVersion | ||
} | ||
compilerOptions { | ||
freeCompilerArgs.add("-opt-in=kotlin.RequiresOptIn") | ||
} | ||
} | ||
|
||
tasks.withType<Test> { | ||
useJUnitPlatform() | ||
} |