Skip to content

Commit

Permalink
Adjust a couple of things for docker hosting
Browse files Browse the repository at this point in the history
  • Loading branch information
rainbowdashlabs committed Aug 18, 2024
1 parent a74ed46 commit 0d1bee0
Show file tree
Hide file tree
Showing 17 changed files with 156 additions and 27 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -44,3 +44,4 @@ buildNumber.properties
/wait.sh
/conf/
.direnv
data
26 changes: 26 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -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"]
14 changes: 0 additions & 14 deletions bot/Dockerfile

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -28,8 +29,8 @@ public int maxPort() {
return maxPort;
}

public int velocityApi() {
return velocityApi;
public int velocityPort() {
return velocityPort;
}

public List<String> parameter() {
Expand All @@ -47,4 +48,8 @@ public int memory() {
public String serverDir() {
return serverDir;
}

public String getVelocityHost() {
return velocityHost;
}
}
7 changes: 4 additions & 3 deletions bot/src/main/java/de/chojo/gamejam/server/ServerService.java
Original file line number Diff line number Diff line change
Expand Up @@ -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<String> 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);
Expand Down
3 changes: 2 additions & 1 deletion bot/src/main/java/de/chojo/gamejam/server/TeamServer.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
28 changes: 28 additions & 0 deletions compose.yml
Original file line number Diff line number Diff line change
@@ -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
32 changes: 32 additions & 0 deletions dev.compose.yml
Original file line number Diff line number Diff line change
@@ -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
31 changes: 31 additions & 0 deletions docker/resources/bot/loop.sh
Original file line number Diff line number Diff line change
@@ -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
2 changes: 2 additions & 0 deletions docker/resources/bot/start.sh
Original file line number Diff line number Diff line change
@@ -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
7 changes: 7 additions & 0 deletions docker/resources/bot/wait.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#!/usr/bin/env sh
while screen -ls | grep -q "$1"
do
sleep 1
echo "wait"
done
sleep 1
2 changes: 2 additions & 0 deletions docker/resources/docker-entrypoint.sh
Original file line number Diff line number Diff line change
@@ -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
2 changes: 2 additions & 0 deletions docker/resources/lobby/start.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
#!/bin/bash
screen -dmS lobby java -Xms1G -Xmx1G -Dcom.mojang.eula.agree=true -jar server.jar --nogui
1 change: 1 addition & 0 deletions docker/resources/velocity/start.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
screen -dmS velocity java -Djavalin.port="29999" -jar velocity.jar
1 change: 0 additions & 1 deletion plugin-paper/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ tasks {
val shadebase = "de.chojo.pluginjam."
relocate("de.eldoria.eldoutilities", shadebase + "eldoutilities")
mergeServiceFiles()
archiveFileName.set("pluginjam.jar")
}

register<Copy>("copyToServer") {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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"));
}

Expand Down Expand Up @@ -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));
}
}
5 changes: 4 additions & 1 deletion plugin-velocity/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,10 @@ tasks {
//relocate("io.javalin", "$shadebase.javalin")
//relocate("org.eclipse", shadebase)
mergeServiceFiles()
archiveFileName.set("PluginJam.jar")
}

build{
dependsOn(shadowJar)
}

register<Copy>("copyToServer") {
Expand Down

0 comments on commit 0d1bee0

Please sign in to comment.