Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Docker #136

Open
wants to merge 8 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@ build/
.gradle/
**/.env
/src/main/resources/spells.csv
.vscode
27 changes: 27 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -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
41 changes: 33 additions & 8 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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
}
39 changes: 30 additions & 9 deletions src/main/java/com/wylxbot/wylx/Commands/BotUtil/StatusCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand All @@ -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() {
Expand Down Expand Up @@ -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();
Expand All @@ -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)) +
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
17 changes: 17 additions & 0 deletions src/main/java/com/wylxbot/wylx/Core/Util/EnvUtils.java
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I know WylxEnvConfig.java is currently just the .env files but I strongly feel that this belongs in there setting a variable instead of being a method that is called any time we check if Wylx is running in docker. Its not like thats going to change mid run, its an env variable and as such should be in the same file.

Original file line number Diff line number Diff line change
@@ -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<String> stream = Files.lines(Paths.get("/proc/1/cgroup"))) {
return stream.anyMatch(line -> line.contains("/docker"));
} catch (IOException e) {
return false;
}
}
}
Loading