Skip to content

Commit

Permalink
feature: work towards running in docker enviroment
Browse files Browse the repository at this point in the history
  • Loading branch information
utfunderscore committed Aug 22, 2024
1 parent 8f1e30d commit 5221a87
Show file tree
Hide file tree
Showing 10 changed files with 80 additions and 41 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ class OrchestratorApi(
private val retrofit: Retrofit =
Retrofit
.Builder()
.baseUrl("http://localhost:9393")
.baseUrl("http://$hostname:$port")
.addConverterFactory(FastJsonConverterFactory.create())
.build()

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ import java.util.concurrent.Executors
import java.util.concurrent.ScheduledExecutorService

class ClientManager(
orchestratorHost: String,
orchestratorPort: Int,
serverId: UUID,
serverAddress: ServerAddress,
gameFinderTypes: MutableList<GameFinderType>,
Expand All @@ -27,7 +29,7 @@ class ClientManager(
) {
private val packetManager: PacketManager<*> =
PacketManager
.nettyClient("localhost", 2980, KryoPacketSerializer(KryoCreator.build()))
.nettyClient(orchestratorHost, orchestratorPort, KryoPacketSerializer(KryoCreator.build()))
.exception(SocketException::class.java) {
println("ERROR: Socket exception")
shutdown()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ import java.util.concurrent.Executors
import java.util.concurrent.TimeUnit

class ShepardClient(
private val orchestratorHost: String,
private val orchestratorPort: Int,
val serverAddress: ServerAddress,
) {
private val serverId = UUID.randomUUID()
Expand All @@ -32,10 +34,19 @@ class ShepardClient(
}

private fun start(games: Map<UUID, Game>) {
logger.info { "Connecting to server" }
logger.info { "Connecting to server ($serverAddress)" }

reconnecting = false
ClientManager(serverId, serverAddress, gameFinderTypes, supportedGameTypes, games, gameRequestHandler) {
ClientManager(
orchestratorHost,
orchestratorPort,
serverId,
serverAddress,
gameFinderTypes,
supportedGameTypes,
games,
gameRequestHandler,
) {
onDisconnect(it)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import java.util.UUID
class ClientIntegrationTest {
private var client: ShepardClient =
ShepardClient(
"localhost",
2980,
serverAddress = ServerAddress("localhost", 25565),
).registerGameTypes("test")
.registerFinderTypes(GameFinderType.PRE_EXISTING, GameFinderType.ON_REQUEST)
Expand Down
45 changes: 42 additions & 3 deletions Server/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import com.github.jengelman.gradle.plugins.shadow.tasks.ShadowJar
import java.util.Properties

plugins {
id("com.github.johnrengelman.shadow") version "8.1.1"
kotlin("jvm")
id("com.github.johnrengelman.shadow") version "8.1.1"
}

group = "org.readutf.orchestrator"
Expand Down Expand Up @@ -61,12 +62,19 @@ tasks.jar {
manifest {
attributes["Main-Class"] = "org.readutf.orchestrator.server.ServerStarterKt"
}
finalizedBy("shadowJar")
}

tasks.named<ShadowJar>("shadowJar") {
finalizedBy("generateDockerFile")
}

tasks.register("generateDockerFile") {
tasks.register<Exec>("generateDockerFile") {

File("$buildDir/Dockerfile").writeText(
val buildFile = file("/build/docker/")
buildFile.mkdirs()

File(buildFile, "Dockerfile").writeText(
"""
FROM eclipse-temurin:21-jdk-jammy as deps
Expand All @@ -80,6 +88,37 @@ tasks.register("generateDockerFile") {
CMD ["java", "-jar", "Server-$version-all.jar"]
""".trimIndent(),
)

commandLine("docker", "build", "-t", "utfunderscore/orchestrator:$version", buildFile.absolutePath)
}

tasks.register<Exec>("generateDevDockerFile") {

val devFolder = file("/build/docker/dev/")
devFolder.mkdirs()

val targetFile = devFolder.resolve("Server-$version-all.jar")
if (!targetFile.exists()) {
file("/build/libs/Server-$version-all.jar").copyTo(targetFile)
}

File(devFolder, "Dockerfile").writeText(
"""
FROM eclipse-temurin:21-jdk-jammy as deps
WORKDIR /orchestrator
ADD /Server-$version-all.jar /orchestrator
ADD /settings.yml /orchestrator
EXPOSE 2980
EXPOSE 9393
CMD ["java", "-jar", "Server-$version-all.jar"]
""".trimIndent(),
)

commandLine("docker", "build", "-t", "utfunderscore/orchestrator-dev:$version", devFolder.absolutePath)
}

tasks.register("createProperties") {
Expand Down
30 changes: 0 additions & 30 deletions Server/src/main/kotlin/org/readutf/orchestrator/server/Test.kt

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package org.readutf.orchestrator.server.api.endpoint

import com.alibaba.fastjson.JSON
import io.javalin.community.routing.annotations.Endpoints
import io.javalin.community.routing.annotations.Get
import io.javalin.community.routing.annotations.Query
import io.javalin.http.Context
import org.readutf.orchestrator.server.docker.DockerManager
import org.readutf.orchestrator.shared.utils.ApiResponse
Expand All @@ -13,14 +15,14 @@ class DockerEndpoint(
@Get("port")
fun getPort(
context: Context,
shortId: String,
@Query("shortId") shortId: String,
) {
val containerByShortId = dockerManager.getContainerByShortId(shortId)

if (containerByShortId.isError()) {
context.json(ApiResponse.failure<String>(containerByShortId.getError()))
context.json(JSON.toJSONString(ApiResponse.failure<String>(containerByShortId.getError())))
}

context.json(ApiResponse.success(containerByShortId.get().getPorts()))
context.json(JSON.toJSONString(ApiResponse.success(containerByShortId.get().getPorts())))
}
}
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
package org.readutf.orchestrator.shared.utils

import com.alibaba.fastjson2.annotation.JSONField

class ApiResponse<T>(
val success: Boolean,
var failureReason: String?,
val response: T?,
) {
@JSONField(serialize = false)
fun getError(): String = failureReason!!

@JSONField(serialize = false)
fun get(): T = response!!

companion object {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package org.readutf.orchestrator.shared.utils

import com.alibaba.fastjson2.annotation.JSONField

class Result<T>(
private val value: T?,
private val error: String?,
Expand All @@ -10,10 +12,17 @@ class Result<T>(

fun <U> map(f: (T) -> U): Result<U> = if (isOk()) ok(f(value!!)) else error(error!!)

fun <U> mapTo(
success: (T) -> U,
failure: (String) -> U,
): U = if (isOk()) success(value!!) else failure(error!!)

fun <U> flatMap(f: (T) -> Result<U>): Result<U> = if (isOk()) f(value!!) else error(error!!)

@JSONField(serialize = false)
fun get(): T = value!!

@JSONField(serialize = false)
fun getError(): String = error!!

companion object {
Expand Down
2 changes: 1 addition & 1 deletion build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ plugins {
}

group = "org.readutf.orchestrator"
version = "1.4.2"
version = "1.4.4"

dependencies {
testImplementation(kotlin("test"))
Expand Down

0 comments on commit 5221a87

Please sign in to comment.