From 9eaca049b79adfd16ea77d6109be2029bed7973d Mon Sep 17 00:00:00 2001 From: Liam Date: Mon, 29 Apr 2024 06:39:32 +0100 Subject: [PATCH] untested, might work, might not --- .gitea/workflows/ci.yml | 30 +++++++++++ .gitea/workflows/fetch-new.yml | 53 ------------------- Dockerfile | 30 +++++++++++ .../java/cc/fascinated/piaservers/Main.java | 8 +-- .../piaservers/common/GitUtils.java | 41 ++++++++++++++ .../fascinated/piaservers/pia/PiaManager.java | 32 +++++++---- .../piaservers/readme/ReadMeManager.java | 8 +-- 7 files changed, 130 insertions(+), 72 deletions(-) create mode 100644 .gitea/workflows/ci.yml delete mode 100644 .gitea/workflows/fetch-new.yml create mode 100644 Dockerfile create mode 100644 src/main/java/cc/fascinated/piaservers/common/GitUtils.java diff --git a/.gitea/workflows/ci.yml b/.gitea/workflows/ci.yml new file mode 100644 index 0000000..c72eaee --- /dev/null +++ b/.gitea/workflows/ci.yml @@ -0,0 +1,30 @@ +name: Deploy to Dokku + +on: + push: + branches: ["master"] + paths-ignore: + - .gitignore + - README.md + - LICENSE + - servers.json + +jobs: + docker: + strategy: + matrix: + arch: ["ubuntu-latest"] + runs-on: ${{ matrix.arch }} + + # Steps to run + steps: + # Checkout the repo + - name: Checkout + uses: actions/checkout@v4 + + # Deploy to Dokku + - name: Push to dokku + uses: dokku/github-action@master + with: + git_remote_url: "ssh://dokku@10.0.50.65:22/pia-servers" + ssh_private_key: ${{ secrets.SSH_PRIVATE_KEY }} \ No newline at end of file diff --git a/.gitea/workflows/fetch-new.yml b/.gitea/workflows/fetch-new.yml deleted file mode 100644 index 63e0097..0000000 --- a/.gitea/workflows/fetch-new.yml +++ /dev/null @@ -1,53 +0,0 @@ -name: Fetch new Pia Servers - -on: - schedule: -# - cron: "@hourly" - - cron: "*/30 * * * *" - push: - branches: ["master"] - paths-ignore: - - .gitignore - - README.md - - LICENSE - - servers.json - -jobs: - docker: - strategy: - matrix: - arch: ["ubuntu-latest"] - git-version: ["2.44.0"] - java-version: ["17"] - maven-version: ["3.8.5"] - runs-on: ${{ matrix.arch }} - - # Steps to run - steps: - # Checkout the repo - - name: Checkout - uses: actions/checkout@v4 - - # Setup Java and Maven - - name: Set up JDK and Maven - uses: s4u/setup-maven-action@v1.12.0 - with: - java-version: ${{ matrix.java-version }} - distribution: "zulu" - maven-version: ${{ matrix.maven-version }} - - - name: Build PIA Servers - run: mvn clean package -T 2C -q - - - name: Fetch new PIA servers - run: java -jar target/PIA-Servers.jar - - - name: Commit and push changes - run: | - git config --global user.email "liam+pia-servers-ci@fascinated.cc" - git config --global user.name "PIA Servers CI" - git add servers.json - git add README.md - git commit -m "Scheduled update" - git push https://pia-servers-ci:${{ secrets.AUTH_TOKEN }}@git.fascinated.cc/Fascinated/PIA-Servers - diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..1a0f430 --- /dev/null +++ b/Dockerfile @@ -0,0 +1,30 @@ +# Stage 1: Build the application +FROM maven:3.9.6-eclipse-temurin-17-alpine AS builder + +# Set the working directory +WORKDIR /home/container + +# Copy the current directory contents into the container at /home/container +COPY . . + +# Build the jar +RUN mvn package -q -Dmaven.test.skip -DskipTests -T2C + +# Stage 2: Create the final lightweight image +FROM eclipse-temurin:17.0.11_9-jre-focal + +# Set the app to be in production mode +ENV ENVIRONMENT=production + +# Set the working directory +WORKDIR /home/container + +# Copy the built jar file from the builder stage +COPY --from=builder /home/container/target/PIA-Servers.jar . + +# Make port 80 available to the world outside this container +EXPOSE 80 +ENV PORT=80 + +# Run the jar file +CMD java -jar PIA-Servers.jar -Djava.awt.headless=true \ No newline at end of file diff --git a/src/main/java/cc/fascinated/piaservers/Main.java b/src/main/java/cc/fascinated/piaservers/Main.java index 1fb7e43..b2ff547 100644 --- a/src/main/java/cc/fascinated/piaservers/Main.java +++ b/src/main/java/cc/fascinated/piaservers/Main.java @@ -1,13 +1,10 @@ package cc.fascinated.piaservers; import cc.fascinated.piaservers.pia.PiaManager; -import cc.fascinated.piaservers.readme.ReadMeManager; import com.google.gson.Gson; import com.google.gson.GsonBuilder; import lombok.SneakyThrows; -import java.util.concurrent.TimeUnit; - public class Main { public static final Gson GSON = new GsonBuilder() .setPrettyPrinting() @@ -15,9 +12,6 @@ public class Main { @SneakyThrows public static void main(String[] args) { - PiaManager.updateServers(); - Thread.sleep(TimeUnit.MINUTES.toMillis(3)); - PiaManager.updateServers(); - new ReadMeManager(); + new PiaManager(); } } \ No newline at end of file diff --git a/src/main/java/cc/fascinated/piaservers/common/GitUtils.java b/src/main/java/cc/fascinated/piaservers/common/GitUtils.java new file mode 100644 index 0000000..cebb9c4 --- /dev/null +++ b/src/main/java/cc/fascinated/piaservers/common/GitUtils.java @@ -0,0 +1,41 @@ +package cc.fascinated.piaservers.common; + +import java.nio.file.Path; + +public class GitUtils { + + /** + * Commit files to git + * + * @param message The commit message + * @param files The files to commit + */ + public static void commitFiles(String message, Path... files) { + if (System.getenv("ENVIRONMENT").equals("production")) { + runCommand("git", "config", "--global", "user.email", "liam+pia-servers-ci@fascinated.cc"); + runCommand("git", "config", "--global", "user.name", "PIA Servers CI"); + } + for (Path file : files) { + runCommand("git", "add", file.toAbsolutePath().toString()); + } + runCommand("git", "commit", "-m", message); + runCommand("git", "push", "https://pia-servers-ci:%s@git.fascinated.cc/Fascinated/PIA-Servers".formatted(System.getenv("AUTH_TOKEN"))); + } + + /** + * Run a system command + * + * @param args The command to run (with arguments) + */ + private static void runCommand(String... args) { + ProcessBuilder processBuilder = new ProcessBuilder(args); + processBuilder.redirectOutput(ProcessBuilder.Redirect.INHERIT); + processBuilder.redirectError(ProcessBuilder.Redirect.INHERIT); + try { + Process process = processBuilder.start(); + process.waitFor(); + } catch (Exception ex) { + ex.printStackTrace(); + } + } +} diff --git a/src/main/java/cc/fascinated/piaservers/pia/PiaManager.java b/src/main/java/cc/fascinated/piaservers/pia/PiaManager.java index 65b6ff8..87b51be 100644 --- a/src/main/java/cc/fascinated/piaservers/pia/PiaManager.java +++ b/src/main/java/cc/fascinated/piaservers/pia/PiaManager.java @@ -1,13 +1,15 @@ package cc.fascinated.piaservers.pia; import cc.fascinated.piaservers.Main; +import cc.fascinated.piaservers.common.GitUtils; import cc.fascinated.piaservers.model.PiaServer; import cc.fascinated.piaservers.model.PiaServerToken; +import cc.fascinated.piaservers.readme.ReadMeManager; import com.google.gson.reflect.TypeToken; import lombok.SneakyThrows; import org.codehaus.plexus.archiver.zip.ZipUnArchiver; -import org.xbill.DNS.*; import org.xbill.DNS.Record; +import org.xbill.DNS.*; import java.io.File; import java.net.InetAddress; @@ -17,9 +19,7 @@ import java.net.http.HttpRequest; import java.net.http.HttpResponse; import java.nio.file.Files; import java.nio.file.Path; -import java.util.ArrayList; -import java.util.Date; -import java.util.List; +import java.util.*; import java.util.concurrent.TimeUnit; public class PiaManager { @@ -29,21 +29,35 @@ public class PiaManager { public static List SERVERS = new ArrayList<>(); @SneakyThrows - public static void updateServers() { + public PiaManager() { File serversFile = new File("servers.json"); if (!serversFile.exists()) { System.out.println("serversFile.json does not exist, creating..."); serversFile.createNewFile(); } - - List piaDomain = getPiaDomains(); - System.out.println("Found " + piaDomain.size() + " pia domains"); - // Load the serversFile from the file SERVERS = Main.GSON.fromJson(Files.readString(serversFile.toPath()), new TypeToken>() {}.getType()); if (SERVERS == null) { SERVERS = new ArrayList<>(); } + + new Timer().scheduleAtFixedRate(new TimerTask() { + @Override + public void run() { + updateServers(serversFile); // Update the servers + Path readmePath = ReadMeManager.updateReadme(); // Update the README.md + + // Commit the changes to the git repository + GitUtils.commitFiles("Scheduled update", serversFile.toPath(), readmePath); + } + }, 0, TimeUnit.MINUTES.toMillis(5)); + } + + @SneakyThrows + public static void updateServers(File serversFile) { + List piaDomain = getPiaDomains(); + System.out.println("Found " + piaDomain.size() + " pia domains"); + List toRemove = new ArrayList<>(); System.out.println("Removing old servers..."); diff --git a/src/main/java/cc/fascinated/piaservers/readme/ReadMeManager.java b/src/main/java/cc/fascinated/piaservers/readme/ReadMeManager.java index 013aba1..836326d 100644 --- a/src/main/java/cc/fascinated/piaservers/readme/ReadMeManager.java +++ b/src/main/java/cc/fascinated/piaservers/readme/ReadMeManager.java @@ -8,20 +8,21 @@ import lombok.SneakyThrows; import java.io.File; import java.io.InputStream; import java.nio.file.Files; +import java.nio.file.Path; import java.text.DecimalFormat; import java.util.Date; import java.util.HashMap; import java.util.Map; public class ReadMeManager { - private final DecimalFormat decimalFormat = new DecimalFormat("#,###"); + private static final DecimalFormat decimalFormat = new DecimalFormat("#,###"); @SneakyThrows - public ReadMeManager() { + public static Path updateReadme() { InputStream readmeStream = Main.class.getResourceAsStream("/README.md"); if (readmeStream == null) { System.out.println("Failed to find README.md"); - return; + return null; } File readmeFile = new File("README.md"); if (!readmeFile.exists()) { // Create the file if it doesn't exist @@ -48,5 +49,6 @@ public class ReadMeManager { .reduce((a, b) -> a + "\n" + b).orElse("")); // Reduce the entries to a single string Files.write(readmeFile.toPath(), contents.getBytes()); + return readmeFile.toPath(); } }