From 2bbeab841229231cc6f781c2bc123c79928c1476 Mon Sep 17 00:00:00 2001 From: Ziedelth Date: Mon, 21 Aug 2023 16:20:01 +0200 Subject: [PATCH] Add Docker support and improve error handling in Application.kt --- .github/workflows/pull_requests.yml | 27 ++++- .github/workflows/push.yml | 101 ++++++++++++++++++ .github/workflows/release.yml | 40 ------- .gitignore | 5 +- Dockerfile | 13 +++ data/hibernate.cfg.xml | 15 +++ docker-compose.yml | 60 +++++++++++ pom.xml | 2 +- src/main/kotlin/fr/ziedelth/Application.kt | 16 +-- src/main/kotlin/fr/ziedelth/utils/Database.kt | 23 +++- .../kotlin/fr/ziedelth/utils/Notifications.kt | 7 +- .../ziedelth/utils/plugins/PluginManager.kt | 4 +- 12 files changed, 253 insertions(+), 60 deletions(-) create mode 100644 .github/workflows/push.yml delete mode 100644 .github/workflows/release.yml create mode 100644 Dockerfile create mode 100644 data/hibernate.cfg.xml create mode 100644 docker-compose.yml diff --git a/.github/workflows/pull_requests.yml b/.github/workflows/pull_requests.yml index 41f8336..18a25bb 100644 --- a/.github/workflows/pull_requests.yml +++ b/.github/workflows/pull_requests.yml @@ -5,17 +5,34 @@ on: jobs: test: - name: Test runs-on: ubuntu-latest steps: - uses: actions/checkout@v4 - - name: Set up JDK 11 + - name: Set up JDK 17 uses: actions/setup-java@v3 with: - java-version: 11 + java-version: 17 distribution: 'adopt' - - name: Test - run: mvn clean test \ No newline at end of file + - name: Tests + run: mvn test + + build: + runs-on: ubuntu-latest + + needs: + - test + + steps: + - uses: actions/checkout@v4 + + - name: Set up JDK 17 + uses: actions/setup-java@v3 + with: + java-version: 17 + distribution: 'adopt' + + - name: Build + run: mvn -B package -DskipTests \ No newline at end of file diff --git a/.github/workflows/push.yml b/.github/workflows/push.yml new file mode 100644 index 0000000..08f1428 --- /dev/null +++ b/.github/workflows/push.yml @@ -0,0 +1,101 @@ +name: Push + +on: + push: + branches: + - master + - stable + +jobs: + test: + runs-on: ubuntu-latest + + steps: + - uses: actions/checkout@v4 + + - name: Set up JDK 17 + uses: actions/setup-java@v3 + with: + java-version: 17 + distribution: 'adopt' + + - name: Tests + run: mvn test + + build: + runs-on: ubuntu-latest + + needs: + - test + + steps: + - uses: actions/checkout@v4 + + - name: Set up JDK 17 + uses: actions/setup-java@v3 + with: + java-version: 17 + distribution: 'adopt' + + - name: Build + run: mvn -B package -DskipTests + + docker: + runs-on: ubuntu-latest + + needs: + - build + + steps: + - uses: actions/checkout@v4 + + - name: Set up QEMU + uses: docker/setup-qemu-action@v3 + + - name: Set up Docker Buildx + uses: docker/setup-buildx-action@v3 + + - name: Login to GitHub Container Registry + uses: docker/login-action@v3 + with: + registry: registry.ziedelth.fr:5000 + username: ziedelth + password: ${{ secrets.REGISTRY_PASSWORD }} + + - name: Build and push snapshot + uses: docker/build-push-action@v5 + if: github.ref == 'refs/heads/master' + with: + context: . + push: true + tags: registry.ziedelth.fr:5000/jais-api:snapshot + + - name: Build and push production + uses: docker/build-push-action@v5 + if: github.ref == 'refs/heads/stable' + with: + context: . + push: true + tags: registry.ziedelth.fr:5000/jais-api:latest + + release: + if: github.ref == 'refs/heads/stable' + name: Release + runs-on: ubuntu-latest + + needs: + - docker + + steps: + - name: Install SSH KEY + uses: shimataro/ssh-key-action@v2 + with: + key: ${{ secrets.SSH_PRIVATE_KEY }} + known_hosts: 'just-a-placeholder-so-we-dont-get-errors' + + - name: Adding Known Hosts + run: ssh-keyscan -p 2222 -H ${{ secrets.SSH_HOST_IP }} >> ~/.ssh/known_hosts + + - name: SSH Commands + run: | + ssh -C -p 2222 ${{ secrets.SSH_DESTINATION }} "docker pull localhost:5000/jais-api:latest && cd ${{ secrets.SSH_FOLDER }} && docker compose down jais-api && docker compose up jais-api -d" diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml deleted file mode 100644 index be516e9..0000000 --- a/.github/workflows/release.yml +++ /dev/null @@ -1,40 +0,0 @@ -name: Release - -on: - push: - branches: - - stable - -env: - FOLDER: /home/pi/api/ - -jobs: - build: - name: Build and release - runs-on: ubuntu-latest - - steps: - - uses: actions/checkout@v4 - - - name: Set up JDK 11 - uses: actions/setup-java@v3 - with: - java-version: 11 - distribution: 'adopt' - - - name: Build - run: mvn clean package - - - name: Install SSH KEY - uses: shimataro/ssh-key-action@v2 - with: - key: ${{ secrets.SSH_PRIVATE_KEY }} - known_hosts: 'just-a-placeholder-so-we-dont-get-errors' - - - name: Adding Known Hosts - run: ssh-keyscan -p 2222 -H ${{ secrets.SSH_HOST_IP }} >> ~/.ssh/known_hosts - - - name: SSH Commands - run: | - scp -P 2222 target/api-1.0.0-jar-with-dependencies.jar ${{ secrets.SSH_DESTINATION }}:"$FOLDER" - ssh -C -p 2222 ${{ secrets.SSH_DESTINATION }} "screen -XS API quit && cd $FOLDER && ./start.sh" \ No newline at end of file diff --git a/.gitignore b/.gitignore index c43edd1..650cf52 100644 --- a/.gitignore +++ b/.gitignore @@ -35,7 +35,6 @@ out/ ### VS Code ### .vscode/ /target/ -/hibernate.cfg.xml /test.http -/firebase_key.json -/plugins/ +**/firebase_key.json +**/plugins/ diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..ef404c2 --- /dev/null +++ b/Dockerfile @@ -0,0 +1,13 @@ +FROM maven:3.9.4-amazoncorretto-17 AS build +COPY . /app +WORKDIR /app +RUN mvn clean package -DskipTests + +FROM amazoncorretto:17-alpine +COPY --from=build /app/target/api-1.0.0-jar-with-dependencies.jar /app/api.jar +COPY --from=build /app/data/ /app/data/ + +RUN apk update && apk add gcompat opencv-dev +EXPOSE 8080 +WORKDIR /app +ENTRYPOINT ["java", "-jar", "api.jar"] \ No newline at end of file diff --git a/data/hibernate.cfg.xml b/data/hibernate.cfg.xml new file mode 100644 index 0000000..151d10d --- /dev/null +++ b/data/hibernate.cfg.xml @@ -0,0 +1,15 @@ + + + + + + jdbc:postgresql://localhost:5432/jais + postgres + mysecretpassword + false + update + org.hibernate.context.internal.ThreadLocalSessionContext + + \ No newline at end of file diff --git a/docker-compose.yml b/docker-compose.yml new file mode 100644 index 0000000..60fdc84 --- /dev/null +++ b/docker-compose.yml @@ -0,0 +1,60 @@ +version: '3.8' +services: + jais-db: + image: postgres:16-alpine + restart: always + container_name: jais-db + environment: + POSTGRES_PASSWORD: "mysecretpassword" + POSTGRES_USER: postgres + POSTGRES_DB: jais + healthcheck: + test: ["CMD", "pg_isready", "-U", "postgres"] + volumes: + - ./tmp_data.sql:/docker-entrypoint-initdb.d/dummy_dump.sql + jais-api: + image: localhost:5000/jais-api:latest + ports: + - "8080:8080" + restart: on-failure + container_name: jais-api + depends_on: + jais-db: + condition: service_healthy + environment: + DATABASE_URL: jdbc:postgresql://jais-db:5432/jais + DATABASE_USERNAME: postgres + DATABASE_PASSWORD: "mysecretpassword" + SEND_NOTIFICATIONS: "false" + volumes: + - jais-api-data:/app/data + jais-scraper: + image: localhost:5000/jais-scraper:latest + restart: on-failure + container_name: jais-scraper + depends_on: + jais-api: + condition: service_started + environment: + API_URL: http://jais-api:8080/ + volumes: + - jais-scraper-data:/app/data + jais-website: + image: localhost:5000/jais-website:latest + ports: + - "8081:80" + restart: on-failure + container_name: jais-website + depends_on: + jais-api: + condition: service_started + volumes: + - jais-website-data:/usr/local/apache2/htdocs/attachments/ + +volumes: + jais-api-data: + name: jais-api-data + jais-scraper-data: + name: jais-scraper-data + jais-website-data: + name: jais-website-data \ No newline at end of file diff --git a/pom.xml b/pom.xml index bd6cd4f..61f0e05 100644 --- a/pom.xml +++ b/pom.xml @@ -12,7 +12,7 @@ 2.3.4 official 1.9.10 - 11 + 17 1.4.11 UTF-8 true diff --git a/src/main/kotlin/fr/ziedelth/Application.kt b/src/main/kotlin/fr/ziedelth/Application.kt index 381fd36..0e110a2 100644 --- a/src/main/kotlin/fr/ziedelth/Application.kt +++ b/src/main/kotlin/fr/ziedelth/Application.kt @@ -38,13 +38,17 @@ fun main(args: Array) { val scanner = Scanner(System.`in`) while (true) { - val line = scanner.nextLine() + try { + val line = scanner.nextLine() - if (line == "reload") { - PluginManager.reload() - ListenerManager() - } else if (line == "invalid-cache") { - ImageCache.invalidCache(database) + if (line == "reload") { + PluginManager.reload() + ListenerManager() + } else if (line == "invalid-cache") { + ImageCache.invalidCache(database) + } + } catch (_: Exception) { + Thread.sleep(1000) } } }.start() diff --git a/src/main/kotlin/fr/ziedelth/utils/Database.kt b/src/main/kotlin/fr/ziedelth/utils/Database.kt index 3da66ac..9a27fb8 100644 --- a/src/main/kotlin/fr/ziedelth/utils/Database.kt +++ b/src/main/kotlin/fr/ziedelth/utils/Database.kt @@ -23,8 +23,27 @@ open class Database { Configuration().let { configuration -> getEntities().forEach { configuration.addAnnotatedClass(it) } - configuration.configure(file) + + val url: String? = System.getenv("DATABASE_URL") + val username: String? = System.getenv("DATABASE_USERNAME") + val password: String? = System.getenv("DATABASE_PASSWORD") + + if (url?.isNotBlank() == true) { + println("Bypassing hibernate.cfg.xml with system environment variable DATABASE_URL") + configuration.setProperty("hibernate.connection.url", url) + } + + if (username?.isNotBlank() == true) { + println("Bypassing hibernate.cfg.xml with system environment variable DATABASE_USERNAME") + configuration.setProperty("hibernate.connection.username", username) + } + + if (password?.isNotBlank() == true) { + println("Bypassing hibernate.cfg.xml with system environment variable DATABASE_PASSWORD") + configuration.setProperty("hibernate.connection.password", password) + } + sessionFactory = configuration.buildSessionFactory( StandardServiceRegistryBuilder().applySettings(configuration.properties).build() ) @@ -35,7 +54,7 @@ open class Database { } } - constructor() : this(File("hibernate.cfg.xml")) + constructor() : this(File("data/hibernate.cfg.xml")) protected fun getEntities(): MutableSet> = Reflections("fr.ziedelth.entities").getSubTypesOf(Serializable::class.java) diff --git a/src/main/kotlin/fr/ziedelth/utils/Notifications.kt b/src/main/kotlin/fr/ziedelth/utils/Notifications.kt index e3e9c0b..58a7a27 100644 --- a/src/main/kotlin/fr/ziedelth/utils/Notifications.kt +++ b/src/main/kotlin/fr/ziedelth/utils/Notifications.kt @@ -15,7 +15,7 @@ object Notifications { init { println("Initializing Firebase") - val file = File("firebase_key.json") + val file = File("data/firebase_key.json") if (file.exists()) { FirebaseApp.initializeApp( @@ -29,6 +29,11 @@ object Notifications { } fun send(title: String? = null, body: String? = null, topic: String = "all") { + with(System.getenv("SEND_NOTIFICATIONS")) { + println("SEND_NOTIFICATIONS: $this") + if (this.isNullOrBlank() || this == "false") return + } + if (initialized) { FirebaseMessaging.getInstance().send( Message.builder().setAndroidConfig( diff --git a/src/main/kotlin/fr/ziedelth/utils/plugins/PluginManager.kt b/src/main/kotlin/fr/ziedelth/utils/plugins/PluginManager.kt index 521028c..da03e06 100644 --- a/src/main/kotlin/fr/ziedelth/utils/plugins/PluginManager.kt +++ b/src/main/kotlin/fr/ziedelth/utils/plugins/PluginManager.kt @@ -7,7 +7,7 @@ import org.pf4j.DefaultPluginManager import java.io.File object PluginManager { - private var defaultPluginManager = DefaultPluginManager(File("plugins").toPath()) + private var defaultPluginManager = DefaultPluginManager(File("data/plugins").toPath()) val listeners = mutableListOf() fun loadPlugins() { @@ -23,7 +23,7 @@ object PluginManager { defaultPluginManager.stopPlugins() defaultPluginManager.unloadPlugins() listeners.clear() - defaultPluginManager = DefaultPluginManager(File("plugins").toPath()) + defaultPluginManager = DefaultPluginManager(File("data/plugins").toPath()) loadPlugins() }