diff --git a/Dockerfile b/Dockerfile index 32acfd17..7012a382 100644 --- a/Dockerfile +++ b/Dockerfile @@ -5,7 +5,7 @@ FROM alpine:3.20 AS base # Add OpenJDK17 -RUN apk add openjdk17=17.0.12_p7-r0 +RUN apk add --no-cache openjdk17=17.0.12_p7-r0 # Uses /kepler directory WORKDIR /kepler @@ -17,16 +17,16 @@ WORKDIR /kepler FROM base AS build # Add unzip -RUN apk add unzip=6.0-r14 +RUN apk add --no-cache unzip=6.0-r14 # Copy every files/folders that are not in .dockerignore COPY . . # Convert CRLF to LF executable files (failing build for Windows without this) -RUN sed -i 's/\r$//' gradlew tools/scripts/run.sh entrypoint.sh +RUN sed -i 's/\r$//' gradlew tools/scripts/run.sh -# Make gradlew, entrypoint.sh and run.sh executable -RUN chmod +x gradlew entrypoint.sh tools/scripts/run.sh +# Make gradlew and run.sh executable +RUN chmod +x gradlew tools/scripts/run.sh # Run gradle build RUN ./gradlew distZip @@ -39,7 +39,6 @@ RUN rm -rf ./release/Kepler-Server/bin && \ mkdir -p ./build/lib && \ mv ./release/Kepler-Server/lib/Kepler-Server.jar ./build/kepler.jar && \ mv ./release/Kepler-Server/lib/* ./build/lib && \ - mv ./entrypoint.sh ./build/entrypoint.sh && \ cp tools/scripts/run.sh ./build/ #################### @@ -51,6 +50,4 @@ FROM base AS production # Copy builded Kepler server COPY --from=build /kepler/build ./ -ENTRYPOINT [ "sh", "entrypoint.sh" ] - CMD [ "sh", "run.sh" ] \ No newline at end of file diff --git a/Kepler-Server/src/main/java/org/alexdev/kepler/dao/Storage.java b/Kepler-Server/src/main/java/org/alexdev/kepler/dao/Storage.java index fa14530a..71355950 100644 --- a/Kepler-Server/src/main/java/org/alexdev/kepler/dao/Storage.java +++ b/Kepler-Server/src/main/java/org/alexdev/kepler/dao/Storage.java @@ -66,7 +66,7 @@ public static boolean connect() { storage = new Storage(ServerConfiguration.getString("mysql.hostname"), ServerConfiguration.getInteger("mysql.port"), - ServerConfiguration.getString("mysql.username"), + ServerConfiguration.getString("mysql.user"), ServerConfiguration.getString("mysql.password"), ServerConfiguration.getString("mysql.database")); diff --git a/Kepler-Server/src/main/java/org/alexdev/kepler/util/config/ServerConfiguration.java b/Kepler-Server/src/main/java/org/alexdev/kepler/util/config/ServerConfiguration.java index f924c575..9dd9a69d 100644 --- a/Kepler-Server/src/main/java/org/alexdev/kepler/util/config/ServerConfiguration.java +++ b/Kepler-Server/src/main/java/org/alexdev/kepler/util/config/ServerConfiguration.java @@ -48,6 +48,25 @@ public static boolean getBoolean(String key) { } + /** + * Helper method to get value from environment variable if exists + * Converts the key by replacing dots with underscores and converting to uppercase + * to match common environment variable naming conventions. + * + * @param key the key to use + * @return value from environment variable or null if not found + */ + private static String getEnvOrNull(String key) { + String envKey = key.replace('.', '_').toUpperCase(); + String envValue = System.getenv(envKey); + + if(envValue != null && !envValue.isEmpty()) { + return envValue; + } + + return null; + } + /** * Get value from configuration * @@ -55,6 +74,12 @@ public static boolean getBoolean(String key) { * @return value */ public static String getString(String key) { + String envValue = getEnvOrNull(key); + + if(envValue != null) { + return envValue; + } + return config.getOrDefault(key, key); } @@ -65,6 +90,12 @@ public static String getString(String key) { * @return value */ public static String getStringOrDefault(String key, String value) { + String envValue = getEnvOrNull(key); + + if(envValue != null) { + return envValue; + } + return config.getOrDefault(key, value); } @@ -75,6 +106,18 @@ public static String getStringOrDefault(String key, String value) { * @return value as int */ public static int getInteger(String key) { + String envValue = getEnvOrNull(key); + + if(envValue != null) { + try { + return Integer.parseInt(envValue); + } catch (NumberFormatException e) { + // Handle the case where the env value is not a valid integer + System.err.println("Environment variable for key " + key + " is not a valid integer: " + envValue); + // Using default value below + } + } + return Integer.parseInt(config.getOrDefault(key, "0")); } diff --git a/Kepler-Server/src/main/java/org/alexdev/kepler/util/config/writer/DefaultConfigWriter.java b/Kepler-Server/src/main/java/org/alexdev/kepler/util/config/writer/DefaultConfigWriter.java index 9297c7a0..c3e8c8dc 100644 --- a/Kepler-Server/src/main/java/org/alexdev/kepler/util/config/writer/DefaultConfigWriter.java +++ b/Kepler-Server/src/main/java/org/alexdev/kepler/util/config/writer/DefaultConfigWriter.java @@ -24,7 +24,7 @@ public Map setConfigurationDefaults() { config.put("mysql.hostname", "127.0.0.1"); config.put("mysql.port", "3306"); - config.put("mysql.username", "kepler"); + config.put("mysql.user", "kepler"); config.put("mysql.password", "verysecret"); config.put("mysql.database", "kepler"); @@ -49,7 +49,7 @@ public void setConfigurationData(Map config, PrintWriter writer) writer.println("[Database]"); writer.println("mysql.hostname=" + config.get("mysql.hostname")); writer.println("mysql.port=" + config.get("mysql.port")); - writer.println("mysql.username=" + config.get("mysql.username")); + writer.println("mysql.user=" + config.get("mysql.user")); writer.println("mysql.password=" + config.get("mysql.password")); writer.println("mysql.database=" + config.get("mysql.database")); writer.println(""); diff --git a/entrypoint.sh b/entrypoint.sh deleted file mode 100644 index 0ac77e06..00000000 --- a/entrypoint.sh +++ /dev/null @@ -1,79 +0,0 @@ -#!/bin/bash -set -e - -INI_FILE="/kepler/server.ini" - -# Function to check for the existence of server.ini -check_for_ini_file() { - if [ -f $INI_FILE ]; then - return 0 - else - return 1 - fi -} - -if ! check_for_ini_file; then - echo "[+] server.ini file not detected, running Kepler to create it..." - ./run.sh &> /dev/null - echo "[+] Cleaning up logs..." - rm -f server.log error.log -fi - -echo "[+] server.ini file detected !" - -# Function to update the .ini file -update_ini_file() { - local key=$1 - local value=$2 - local ini_file=$3 - local default_value=$4 - - if [ -n "$value" ]; then - if grep -q "^$key=" "$ini_file"; then - sed -i "s|^$key=.*|$key=$value|" "$ini_file" - else - echo "$key=$value" >> "$ini_file" - fi - else - if grep -q "^$key=" "$ini_file"; then - sed -i "s|^$key=.*|$key=$default_value|" "$ini_file" - else - echo "$key=$default_value" >> "$ini_file" - fi - fi - - -} - -echo "[+] Configuring server.ini with environment variables..." - -# Server -update_ini_file "server.bind" "$SERVER_BIND" "$INI_FILE" "127.0.0.1" -update_ini_file "server.port" "$SERVER_PORT" "$INI_FILE" "12321" - -# Rcon -update_ini_file "rcon.bind" "$RCON_BIND" "$INI_FILE" "127.0.0.1" -update_ini_file "rcon.port" "$RCON_PORT" "$INI_FILE" "12309" - -# Mus -update_ini_file "mus.bind" "$MUS_BIND" "$INI_FILE" "127.0.0.1" -update_ini_file "mus.port" "$MUS_PORT" "$INI_FILE" "12322" - -# Database -update_ini_file "mysql.hostname" "$MYSQL_HOSTNAME" "$INI_FILE" "127.0.0.1" -update_ini_file "mysql.port" "$MYSQL_PORT" "$INI_FILE" "3306" -update_ini_file "mysql.username" "$MYSQL_USER" "$INI_FILE" "kepler" -update_ini_file "mysql.password" "$MYSQL_PASSWORD" "$INI_FILE" "verysecret" -update_ini_file "mysql.database" "$MYSQL_DATABASE" "$INI_FILE" "kepler" - -# Logging -update_ini_file "log.received.packets" "$LOG_RECEIVED_PACKETS" "$INI_FILE" "false" -update_ini_file "log.sent.packets" "$LOG_SENT_PACKETS" "$INI_FILE" "false" - -# Console -update_ini_file "debug" "$DEBUG" "$INI_FILE" "false" - -echo "[+] Running Kepler..." - -# Execute the command passed to the entrypoint -exec "$@" \ No newline at end of file