diff --git a/.gitignore b/.gitignore index 4df2228..1c9e176 100644 --- a/.gitignore +++ b/.gitignore @@ -4,3 +4,4 @@ build/ .gradle/ **/.env /src/main/resources/spells.csv +.vscode \ No newline at end of file diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..e9a7c27 --- /dev/null +++ b/Dockerfile @@ -0,0 +1,27 @@ +ARG APP_DIR=/opt/wylx +ARG BUILD_DIR=/tmp/wylx +ARG GIT_COMMIT="Unknown" + +# Build Container +FROM gradle:jdk17-alpine AS BUILD + +ARG BUILD_DIR +ARG GIT_COMMIT +ENV GIT_COMMIT=$GIT_COMMIT +WORKDIR $BUILD_DIR +COPY --chown=gradle:gradle build.gradle settings.gradle $BUILD_DIR/ +COPY --chown=gradle:gradle src $BUILD_DIR/src + +RUN gradle build --no-daemon + +RUN unzip $BUILD_DIR/build/distributions/WylxBot.zip -d $BUILD_DIR/unzip + +# Final Application Container +FROM eclipse-temurin:17-jre-alpine +ARG BUILD_DIR +ARG APP_DIR +WORKDIR $APP_DIR + +COPY --from=BUILD $BUILD_DIR/unzip $APP_DIR/ + +ENTRYPOINT ./WylxBot/bin/WylxBot \ No newline at end of file diff --git a/build.gradle b/build.gradle index c44db42..7c33c9e 100644 --- a/build.gradle +++ b/build.gradle @@ -5,7 +5,7 @@ plugins { } group 'org.wylx' -version '1.0-SNAPSHOT' +version = System.getenv("GIT_COMMIT") ?: "git describe --dirty --always --exclude '*'".execute().text.trim() repositories { mavenCentral() @@ -24,16 +24,41 @@ repositories { } dependencies { - implementation("org.apache.commons:commons-text:1.10.0") - implementation("io.github.cdimascio:dotenv-kotlin:6.4.1") - implementation("net.dv8tion:JDA:5.0.0-beta.23") - implementation("ch.qos.logback:logback-classic:1.4.5") - implementation("dev.arbjerg:lavaplayer:2.2.1") // https://github.com/lavalink-devs/lavaplayer - implementation("dev.lavalink.youtube:v2:1.7.2") - implementation("org.mongodb:mongodb-driver-sync:4.8.2") + implementation("org.apache.commons:commons-text:1.12.0") + implementation("io.github.cdimascio:dotenv-kotlin:6.4.2") + implementation("net.dv8tion:JDA:5.1.0") + implementation("ch.qos.logback:logback-classic:1.5.8") + implementation("dev.arbjerg:lavaplayer:2.2.2") // https://github.com/lavalink-devs/lavaplayer + implementation("dev.lavalink.youtube:v2:1.8.0") + implementation("org.mongodb:mongodb-driver-sync:5.1.4") implementation ("org.scilab.forge:jlatexmath:1.0.7") } application { mainClassName="com.wylxbot.wylx.Wylx" } + +// Remove version from output jar name +tasks.jar { + archiveFileName.set("${project.name}.jar") +} + +tasks.distZip { + archiveFileName.set("${project.name}.zip") +} + +// Create version.properties with git hash +task createProperties(dependsOn: processResources) { + doLast { + new File("$buildDir/resources/main/version.properties").withWriter { w -> + Properties p = new Properties() + p['version'] = version.toString() + p['build-date'] = new Date().format("EEE MMM dd HH':'mm':'ss zzz yyyy") + p.store w, null + } + } +} + +classes { + dependsOn createProperties +} \ No newline at end of file diff --git a/src/main/java/com/wylxbot/wylx/Commands/BotUtil/StatusCommand.java b/src/main/java/com/wylxbot/wylx/Commands/BotUtil/StatusCommand.java index c9012e9..ce53384 100644 --- a/src/main/java/com/wylxbot/wylx/Commands/BotUtil/StatusCommand.java +++ b/src/main/java/com/wylxbot/wylx/Commands/BotUtil/StatusCommand.java @@ -2,6 +2,7 @@ import com.wylxbot.wylx.Core.Events.Commands.CommandContext; import com.wylxbot.wylx.Core.Events.Commands.ServerCommand; +import com.wylxbot.wylx.Core.Util.EnvUtils; import com.wylxbot.wylx.Core.Util.ProgressBar; import com.wylxbot.wylx.Core.Util.WylxStats; import com.wylxbot.wylx.Database.DbElements.DiscordGlobal; @@ -10,14 +11,20 @@ import net.dv8tion.jda.api.EmbedBuilder; import net.dv8tion.jda.api.events.message.MessageReceivedEvent; -import java.io.BufferedReader; -import java.io.InputStreamReader; +import java.io.*; import java.lang.management.ManagementFactory; import java.net.InetAddress; +import java.net.UnknownHostException; +import java.nio.file.Files; +import java.nio.file.Paths; +import java.util.Properties; +import java.util.stream.Stream; public class StatusCommand extends ServerCommand { - private final String commitID = getCommitID(); + private final String mCommitID; + private final String mBuildDate; + private final boolean mIsDocker = EnvUtils.isRunningInsideDocker(); private static final long BYTES_PER_MEGABYTE = 1024 * 1024; private static final long MILLI_PER_SECOND = 1000; @@ -27,6 +34,15 @@ public class StatusCommand extends ServerCommand { public StatusCommand() { super("status", CommandPermission.EVERYONE, "Provides information on the host machine of the bot", "system", "stats"); + Properties prop = new Properties(); + try (InputStream inputStream = StatusCommand.class.getResourceAsStream("/version.properties")) { + prop.load(inputStream); + } catch (IOException e) { + e.printStackTrace(System.out); + System.exit(1); + } + mBuildDate = prop.getProperty("build-date"); + mCommitID = prop.getProperty("version"); } private String getCommitID() { @@ -71,11 +87,15 @@ public void runCommand(CommandContext ctx) { embed.setTitle("Wylx Status"); // Build elements for system section - String systemName = ""; - try { - systemName = String.format("System: %s", InetAddress.getLocalHost().getHostName()); - } catch (Exception e) { - e.printStackTrace(); + String systemName = "System: "; + if (mIsDocker) { + systemName += "Docker"; + } else { + try { + systemName += InetAddress.getLocalHost().getHostName(); + } catch (UnknownHostException e) { + systemName += "Unknown"; + } } Runtime rt = Runtime.getRuntime(); @@ -86,7 +106,8 @@ public void runCommand(CommandContext ctx) { String systemBuilder = String.format("OS: %s\n", System.getProperty("os.name")) + - String.format("Commit: %s\n", commitID) + + String.format("Commit: %s\n", mCommitID) + + String.format("Build Date: %s\n", mBuildDate) + String.format("Threads: %d\n", rt.availableProcessors()) + String.format("Bot Uptime: %d Days, %d Hours, and %d minutes\n", milliToDays(uptime), milliToHours(uptime), milliToMinutes(uptime)) + diff --git a/src/main/java/com/wylxbot/wylx/Commands/BotUtil/UpdateCommand.java b/src/main/java/com/wylxbot/wylx/Commands/BotUtil/UpdateCommand.java index 034633c..bf4a5bf 100644 --- a/src/main/java/com/wylxbot/wylx/Commands/BotUtil/UpdateCommand.java +++ b/src/main/java/com/wylxbot/wylx/Commands/BotUtil/UpdateCommand.java @@ -2,6 +2,7 @@ import com.wylxbot.wylx.Core.Events.Commands.CommandContext; import com.wylxbot.wylx.Core.Events.Commands.ThreadedCommand; +import com.wylxbot.wylx.Core.Util.EnvUtils; import com.wylxbot.wylx.Wylx; import net.dv8tion.jda.api.events.message.MessageReceivedEvent; import net.dv8tion.jda.api.utils.FileUpload; diff --git a/src/main/java/com/wylxbot/wylx/Core/Util/EnvUtils.java b/src/main/java/com/wylxbot/wylx/Core/Util/EnvUtils.java new file mode 100644 index 0000000..8371f7b --- /dev/null +++ b/src/main/java/com/wylxbot/wylx/Core/Util/EnvUtils.java @@ -0,0 +1,17 @@ +package com.wylxbot.wylx.Core.Util; + +import java.io.IOException; +import java.nio.file.Files; +import java.nio.file.Paths; +import java.util.stream.Stream; + +public class EnvUtils { + // From https://stackoverflow.com/a/52581380 + public static Boolean isRunningInsideDocker() { + try (Stream stream = Files.lines(Paths.get("/proc/1/cgroup"))) { + return stream.anyMatch(line -> line.contains("/docker")); + } catch (IOException e) { + return false; + } + } +}