From 0d1bee0d004e27f62aded38660606ff604c84fd7 Mon Sep 17 00:00:00 2001 From: Lilly <46890129+RainbowDashLabs@users.noreply.github.com> Date: Sun, 18 Aug 2024 16:50:25 +0200 Subject: [PATCH] Adjust a couple of things for docker hosting --- .gitignore | 1 + Dockerfile | 26 +++++++++++++++ bot/Dockerfile | 14 -------- .../elements/ServerManagement.java | 11 +++++-- .../chojo/gamejam/server/ServerService.java | 7 ++-- .../de/chojo/gamejam/server/TeamServer.java | 3 +- compose.yml | 28 ++++++++++++++++ dev.compose.yml | 32 +++++++++++++++++++ docker/resources/bot/loop.sh | 31 ++++++++++++++++++ docker/resources/bot/start.sh | 2 ++ docker/resources/bot/wait.sh | 7 ++++ docker/resources/docker-entrypoint.sh | 2 ++ docker/resources/lobby/start.sh | 2 ++ docker/resources/velocity/start.sh | 1 + plugin-paper/build.gradle.kts | 1 - .../pluginjam/velocity/ReportService.java | 10 +++--- plugin-velocity/build.gradle.kts | 5 ++- 17 files changed, 156 insertions(+), 27 deletions(-) create mode 100644 Dockerfile delete mode 100644 bot/Dockerfile create mode 100644 compose.yml create mode 100644 dev.compose.yml create mode 100755 docker/resources/bot/loop.sh create mode 100755 docker/resources/bot/start.sh create mode 100755 docker/resources/bot/wait.sh create mode 100755 docker/resources/docker-entrypoint.sh create mode 100755 docker/resources/lobby/start.sh create mode 100755 docker/resources/velocity/start.sh diff --git a/.gitignore b/.gitignore index 1097b10..da29b22 100644 --- a/.gitignore +++ b/.gitignore @@ -44,3 +44,4 @@ buildNumber.properties /wait.sh /conf/ .direnv +data diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..49940b9 --- /dev/null +++ b/Dockerfile @@ -0,0 +1,26 @@ +# This dockerfile is far from best practice and is a pure "Make it work" approach. Please do not use it as a reference of any kind. +FROM gradle:jdk21-alpine as build + +COPY . . +RUN gradle clean build --no-daemon + +# We use a jammy image because we need some more stuff than alpine provides +FROM eclipse-temurin:21-jammy as runtime + +WORKDIR /app +RUN apt update +# Make sure screen exists. +RUN apt install -y screen + +# Setting up the bot +COPY --from=build /home/gradle/bot/build/libs/bot-*-all.jar ./bot.jar +RUN mkdir plugins +RUN mkdir servers +RUN mkdir template +RUN mkdir template/plugins +COPY docker/resources/bot/wait.sh . +# Copy the plugin jam plugin into the template. +COPY --from=build /home/gradle/plugin-paper/build/libs/plugin-paper-*-all.jar ./bot/template/plugins/pluginjam.jar + +COPY docker/resources/docker-entrypoint.sh . +ENTRYPOINT ["bash", "docker-entrypoint.sh"] diff --git a/bot/Dockerfile b/bot/Dockerfile deleted file mode 100644 index e0d7933..0000000 --- a/bot/Dockerfile +++ /dev/null @@ -1,14 +0,0 @@ -FROM gradle:jdk21-alpine as build - -COPY . . -RUN gradle clean build --no-daemon - -FROM eclipse-temurin:21-jammy as runtime - -WORKDIR /app -RUN apt update -RUN apt install -y screen - -COPY --from=build /home/gradle/bot/build/libs/bot-*-all.jar bot.jar - -ENTRYPOINT ["java", "-Dbot.config=config/config.json", "-Dlog4j.configurationFile=config/log4j2.xml", "-Dcjda.localisation.error.name=false", "-jar" , "bot.jar"] diff --git a/bot/src/main/java/de/chojo/gamejam/configuration/elements/ServerManagement.java b/bot/src/main/java/de/chojo/gamejam/configuration/elements/ServerManagement.java index aa6207b..672e085 100644 --- a/bot/src/main/java/de/chojo/gamejam/configuration/elements/ServerManagement.java +++ b/bot/src/main/java/de/chojo/gamejam/configuration/elements/ServerManagement.java @@ -14,7 +14,8 @@ public class ServerManagement { private String serverDir= "server"; private int minPort = 30001; private int maxPort = 30500; - private int velocityApi = 30000; + private String velocityHost = "velocity"; + private int velocityPort = 30000; private int maxPlayers = 50; private int memory = 1024; @@ -28,8 +29,8 @@ public int maxPort() { return maxPort; } - public int velocityApi() { - return velocityApi; + public int velocityPort() { + return velocityPort; } public List parameter() { @@ -47,4 +48,8 @@ public int memory() { public String serverDir() { return serverDir; } + + public String getVelocityHost() { + return velocityHost; + } } diff --git a/bot/src/main/java/de/chojo/gamejam/server/ServerService.java b/bot/src/main/java/de/chojo/gamejam/server/ServerService.java index 5b0183e..02dea68 100644 --- a/bot/src/main/java/de/chojo/gamejam/server/ServerService.java +++ b/bot/src/main/java/de/chojo/gamejam/server/ServerService.java @@ -71,16 +71,17 @@ public void syncVelocity() { freePorts.clear(); IntStream.rangeClosed(configuration.serverManagement().minPort(), configuration.serverManagement().maxPort()) .forEach(freePorts::add); - var velocityApi = configuration.serverManagement().velocityApi(); + var velocityPort = configuration.serverManagement().velocityPort(); + var velocityHost = configuration.serverManagement().getVelocityHost(); var httpClient = HttpClient.newHttpClient(); - var req = HttpRequest.newBuilder(URI.create("http://localhost:%d/v1/server".formatted(velocityApi))) + var req = HttpRequest.newBuilder(URI.create("http://%s:%d/v1/server".formatted(velocityHost, velocityPort))) .GET() .build(); HttpResponse response; try { response = httpClient.send(req, HttpResponse.BodyHandlers.ofString()); } catch (IOException e) { - log.error("Could not reach velocity inteance", e); + log.error("Could not reach velocity instance", e); return; } catch (InterruptedException e) { log.error("Interrupted", e); diff --git a/bot/src/main/java/de/chojo/gamejam/server/TeamServer.java b/bot/src/main/java/de/chojo/gamejam/server/TeamServer.java index 49258bd..d0d12e4 100644 --- a/bot/src/main/java/de/chojo/gamejam/server/TeamServer.java +++ b/bot/src/main/java/de/chojo/gamejam/server/TeamServer.java @@ -174,7 +174,8 @@ public boolean start() { command.add("-Xms%dM".formatted(server.memory())); command.addAll(AIKAR); command.addAll(server.parameter()); - command.add("-Dpluginjam.port=" + server.velocityApi()); + command.add("-Dpluginjam.port=" + server.velocityPort()); + command.add("-Dpluginjam.host=" + server.getVelocityHost()); command.add("-Dpluginjam.team.id=" + team.id()); command.add("-Dpluginjam.team.name=" + teamName()); command.add("-Djavalin.port=" + apiPort); diff --git a/compose.yml b/compose.yml new file mode 100644 index 0000000..38c8131 --- /dev/null +++ b/compose.yml @@ -0,0 +1,28 @@ +services: + bot: + image: ghcr.io/devcordde/plugin-jam-bot:latest + volumes: + - ./data/bot/config:/app/config + - ./data/bot/plugins:/app/plugins + - ./data/bot/servers:/app/servers + - ./data/bot/template:/app/template + velocity: + image: itzg/mc-proxy:latest + tty: true + environment: + EULA: "TRUE" + TYPE: VELOCITY + JVM_DD_OPTS: javalin.port=30000 + volumes: + - ./data/velocity/config:/config + - ./data/velocity/plugins:/plugins + lobby: + image: itzg/minecraft-server:latest + stdin_open: true + tty: true + environment: + VERSION: 1.21 + TYPE: PAPER + MEMORY: 2G + volumes: + - ./data/lobby:/data diff --git a/dev.compose.yml b/dev.compose.yml new file mode 100644 index 0000000..9c6e6dd --- /dev/null +++ b/dev.compose.yml @@ -0,0 +1,32 @@ +services: + bot: + build: . + volumes: + - ./data/bot/config:/app/config + - ./data/bot/plugins:/app/plugins + - ./data/bot/servers:/app/servers + - ./data/bot/template:/app/template + database: + image: postgres:16 + environment: + POSTGRES_PASSWORD: postgres + velocity: + image: itzg/mc-proxy:latest + tty: true + environment: + EULA: "TRUE" + TYPE: VELOCITY + JVM_DD_OPTS: javalin.port=30000 + volumes: + - ./data/velocity/config:/config + - ./data/velocity/plugins:/plugins + lobby: + image: itzg/minecraft-server:latest + stdin_open: true + tty: true + environment: + VERSION: 1.21 + TYPE: PAPER + MEMORY: 2G + volumes: + - ./data/lobby:/data diff --git a/docker/resources/bot/loop.sh b/docker/resources/bot/loop.sh new file mode 100755 index 0000000..ffc9bc6 --- /dev/null +++ b/docker/resources/bot/loop.sh @@ -0,0 +1,31 @@ +#!/bin/bash + + +# switch to config directory + +while true; do +# Of course you can use any other executable file here. We use java. + java \ + -Xms256m \ + -Xmx2048m \ + -Dbot.config=config/config.json -Dlog4j.configurationFile=config/log4j2.xml -Dcjda.localisation.error.name=false -Dcjda.interactions.cleanguildcommands=true \ + -jar ./bot.jar + + code=$? + + case $code in + 0) # proper shutdown + echo "Performed proper shutdown, exiting restart loop" + exit 0 + ;; + 10) # restart request + echo "Requested to restart" + continue + ;; + *) # Recovering + echo "Unknown exit code, attempting recovery restart in a few seconds" + sleep 2 + continue + ;; + esac +done diff --git a/docker/resources/bot/start.sh b/docker/resources/bot/start.sh new file mode 100755 index 0000000..b5c212b --- /dev/null +++ b/docker/resources/bot/start.sh @@ -0,0 +1,2 @@ +#!/bin/sh +java -Xms256m -Xmx2048m -Dbot.config=config/config.json -Dlog4j.configurationFile=config/log4j2.xml -Dcjda.localisation.error.name=false -Dcjda.interactions.cleanguildcommands=true -jar ./bot.jar diff --git a/docker/resources/bot/wait.sh b/docker/resources/bot/wait.sh new file mode 100755 index 0000000..afc8240 --- /dev/null +++ b/docker/resources/bot/wait.sh @@ -0,0 +1,7 @@ +#!/usr/bin/env sh +while screen -ls | grep -q "$1" + do + sleep 1 + echo "wait" + done +sleep 1 diff --git a/docker/resources/docker-entrypoint.sh b/docker/resources/docker-entrypoint.sh new file mode 100755 index 0000000..b5c212b --- /dev/null +++ b/docker/resources/docker-entrypoint.sh @@ -0,0 +1,2 @@ +#!/bin/sh +java -Xms256m -Xmx2048m -Dbot.config=config/config.json -Dlog4j.configurationFile=config/log4j2.xml -Dcjda.localisation.error.name=false -Dcjda.interactions.cleanguildcommands=true -jar ./bot.jar diff --git a/docker/resources/lobby/start.sh b/docker/resources/lobby/start.sh new file mode 100755 index 0000000..8da46d5 --- /dev/null +++ b/docker/resources/lobby/start.sh @@ -0,0 +1,2 @@ +#!/bin/bash +screen -dmS lobby java -Xms1G -Xmx1G -Dcom.mojang.eula.agree=true -jar server.jar --nogui diff --git a/docker/resources/velocity/start.sh b/docker/resources/velocity/start.sh new file mode 100755 index 0000000..e6b6fa2 --- /dev/null +++ b/docker/resources/velocity/start.sh @@ -0,0 +1 @@ +screen -dmS velocity java -Djavalin.port="29999" -jar velocity.jar diff --git a/plugin-paper/build.gradle.kts b/plugin-paper/build.gradle.kts index 4abfbff..6504f67 100644 --- a/plugin-paper/build.gradle.kts +++ b/plugin-paper/build.gradle.kts @@ -20,7 +20,6 @@ tasks { val shadebase = "de.chojo.pluginjam." relocate("de.eldoria.eldoutilities", shadebase + "eldoutilities") mergeServiceFiles() - archiveFileName.set("pluginjam.jar") } register("copyToServer") { diff --git a/plugin-paper/src/main/java/de/chojo/pluginjam/velocity/ReportService.java b/plugin-paper/src/main/java/de/chojo/pluginjam/velocity/ReportService.java index 8cd5548..8719ac2 100644 --- a/plugin-paper/src/main/java/de/chojo/pluginjam/velocity/ReportService.java +++ b/plugin-paper/src/main/java/de/chojo/pluginjam/velocity/ReportService.java @@ -27,14 +27,16 @@ public class ReportService implements Runnable { private final int id; private final Gson gson = new Gson(); private final String name; - private final int velocityApi; + private final int velocityPort; private final int apiPort; + private final String velocityHost; private ReportService(Plugin plugin) { this.plugin = plugin; id = Integer.parseInt(System.getProperty("pluginjam.team.id")); name = System.getProperty("pluginjam.team.name"); - velocityApi = Integer.parseInt(System.getProperty("pluginjam.port")); + velocityPort = Integer.parseInt(System.getProperty("pluginjam.port")); + velocityHost = System.getProperty("pluginjam.host"); apiPort = Integer.parseInt(System.getProperty("javalin.port", "30000")); } @@ -103,10 +105,10 @@ public void shutdown() { } private URI apiUrl(String... path) { - return URI.create("http://localhost:%d/%s".formatted(velocityApi, String.join("/", path))); + return URI.create("http://%s:%d/%s".formatted(velocityHost,velocityPort, String.join("/", path))); } private URI queryApiUrl(String query, String... path) { - return URI.create("http://localhost:%d/%s?%s".formatted(velocityApi, String.join("/", path), query)); + return URI.create("http://%s:%d/%s?%s".formatted(velocityHost, velocityPort, String.join("/", path), query)); } } diff --git a/plugin-velocity/build.gradle.kts b/plugin-velocity/build.gradle.kts index 946e6cb..c1d1a2e 100644 --- a/plugin-velocity/build.gradle.kts +++ b/plugin-velocity/build.gradle.kts @@ -21,7 +21,10 @@ tasks { //relocate("io.javalin", "$shadebase.javalin") //relocate("org.eclipse", shadebase) mergeServiceFiles() - archiveFileName.set("PluginJam.jar") + } + + build{ + dependsOn(shadowJar) } register("copyToServer") {