From adb8819c573e1963295aa939064ba6d80e8ca56a Mon Sep 17 00:00:00 2001 From: Muneeb Khawaja Date: Tue, 7 Feb 2023 22:26:35 -0500 Subject: [PATCH 1/2] feat: Removed unused db related config, updated active spring profiles for compose files and cleaned up property files. --- Dockerfile | 19 +++++++++--------- docker-compose.yaml | 15 +-------------- docker/dev/Dockerfile | 5 +++-- docker/dev/override.yaml | 3 ++- pom.xml | 30 ++++++++++------------------- src/main/resources/application.yaml | 8 ++------ src/test/resources/application.yaml | 11 ----------- 7 files changed, 28 insertions(+), 63 deletions(-) delete mode 100644 src/test/resources/application.yaml diff --git a/Dockerfile b/Dockerfile index a178bb3..649c56d 100644 --- a/Dockerfile +++ b/Dockerfile @@ -2,20 +2,21 @@ # Download all dependencies & cache them # FROM maven:latest AS source -WORKDIR /opt/app/ +ENV APPLICATION_HOME=/opt/cthulhu-text/build/ +WORKDIR "${APPLICATION_HOME}" COPY pom.xml ./pom.xml -RUN mvn dependency:go-offline +RUN mvn dependency:resolve-plugins dependency:go-offline # -# Build +# Build & Package # FROM source AS builder -WORKDIR /opt/app +WORKDIR "${APPLICATION_HOME}" COPY src ./src -RUN mvn clean install +RUN mvn clean package spring-boot:repackage -Dspring.profiles.active="test" # -# Package +# Install # FROM openjdk:17-alpine as production -WORKDIR /opt/app -COPY --from=builder /opt/app/target/api-*.jar /app.jar -CMD java -jar /app.jar \ No newline at end of file +WORKDIR /opt/cthulhu-text/bin +COPY --from=builder /opt/cthulhu-text/build/target/api-*.jar ./ctext.jar +CMD java -jar ctext.jar \ No newline at end of file diff --git a/docker-compose.yaml b/docker-compose.yaml index f7c5a2c..f406f5a 100644 --- a/docker-compose.yaml +++ b/docker-compose.yaml @@ -1,24 +1,11 @@ version: "3.9" services: - db: - image: postgres:14.2-alpine - restart: always - environment: - POSTGRES_USER: ${POSTGRES_USER:-postgres} - POSTGRES_PASSWORD: ${POSTGRES_PASSWORD:-postgres} - POSTGRES_DB: ${POSTGRES_DB:-cthulhu_text} - ports: - - "5433:5432" api: - depends_on: - - db build: context: ./ dockerfile: ./Dockerfile environment: debug: ${DEBUG:-false} - SPRING_DATASOURCE_URL: ${SPRING_DATASOURCE_URL:-jdbc:postgresql://db:5432/${POSTGRES_DB:-cthulhu_text}} - SPRING_DATASOURCE_USERNAME: ${POSTGRES_USER:-postgres} - SPRING_DATASOURCE_PASSWORD: ${POSTGRES_PASSWORD:-postgres} + SPRING_PROFILES_ACTIVE: prod ports: - "8080:8080" \ No newline at end of file diff --git a/docker/dev/Dockerfile b/docker/dev/Dockerfile index 1be168c..897bd44 100644 --- a/docker/dev/Dockerfile +++ b/docker/dev/Dockerfile @@ -2,13 +2,14 @@ # Download all dependencies & cache them # FROM maven:latest AS source -WORKDIR /opt/app/ +ENV APPLICATION_HOME=/opt/cthulhu-text/dev/ +WORKDIR "${APPLICATION_HOME}" COPY pom.xml ./pom.xml RUN mvn dependency:go-offline # # Development (requires volume mounting for hot reloading) # FROM source AS development -WORKDIR /opt/app +WORKDIR "${APPLICATION_HOME}" COPY . ./ CMD mvn -Ddebug=true spring-boot:run \ No newline at end of file diff --git a/docker/dev/override.yaml b/docker/dev/override.yaml index 7c73335..c7db110 100644 --- a/docker/dev/override.yaml +++ b/docker/dev/override.yaml @@ -6,6 +6,7 @@ services: context: ./ dockerfile: docker/dev/Dockerfile volumes: - - ./:/opt/app/ + - ./:/opt/cthulhu-text/dev/ environment: debug: ${DEBUG:-true} + SPRING_PROFILES_ACTIVE: dev \ No newline at end of file diff --git a/pom.xml b/pom.xml index e2ecc4d..f82b30c 100644 --- a/pom.xml +++ b/pom.xml @@ -35,32 +35,12 @@ org.springframework.boot spring-boot-starter-actuator - - - org.springframework.boot - spring-boot-starter-data-jpa - org.springframework.boot spring-boot-starter-validation - - - - org.postgresql - postgresql - ${postgresql.version} - - - - com.h2database - ${h2.version} - h2 - test - - @@ -111,6 +91,16 @@ org.springframework.boot spring-boot-maven-plugin + + true + + + + + com.cthulhutext.api.CthulhuTextApiApplication + + + diff --git a/src/main/resources/application.yaml b/src/main/resources/application.yaml index fe9ecd0..41bc4cc 100644 --- a/src/main/resources/application.yaml +++ b/src/main/resources/application.yaml @@ -1,14 +1,10 @@ server: - port : 8080 + port: 8080 application: name: '@project.artifactId@' build: version: '@project.version@' -spring: - datasource: - url: "${SPRING_DATASOURCE_URL}" - username: "${SPRING_DATASOURCE_USERNAME}" - password: "${SPRING_DATASOURCE_PASSWORD}" + springdoc: version: '@springdoc.version@' swagger-ui: diff --git a/src/test/resources/application.yaml b/src/test/resources/application.yaml deleted file mode 100644 index 8f38a23..0000000 --- a/src/test/resources/application.yaml +++ /dev/null @@ -1,11 +0,0 @@ -spring: - datasource: - url: jdbc:h2:mem:testdb; - jpa: - hibernate: - ddl-auto: create-drop - -springdoc: - version: '@springdoc.version@' - swagger-ui: - use-root-path: true \ No newline at end of file From f70d49ce797708437a93e570ee775ce0ccd70805 Mon Sep 17 00:00:00 2001 From: Muneeb Khawaja Date: Wed, 8 Feb 2023 23:11:35 -0500 Subject: [PATCH 2/2] feat: Added setup.sh for installing application as a systemd service. --- setup.sh | 114 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 114 insertions(+) create mode 100644 setup.sh diff --git a/setup.sh b/setup.sh new file mode 100644 index 0000000..21fd235 --- /dev/null +++ b/setup.sh @@ -0,0 +1,114 @@ +#!/usr/bin/env bash + +# See SpringBoot docs for more information: +# https://docs.spring.io/spring-boot/docs/current/reference/html/deployment.html#deployment.installing + +SERVICE_USER="${1:-$(whoami)}" +APPLICATION_HOME="/opt/cthulhu-text/bin" +JAR_NAME="ctext.jar" +SERVICE_NAME="ctext.service" +SERVICE_REGISTRATION_PATH="/etc/systemd/system/${SERVICE_NAME}" +SOURCE_CODE_LOCATION="/tmp/ctext-source" +DOWNLOAD_LINK="https://github.com/mtkhawaja/cthulhu-text.git" +BUILD_LOG="$(date +%Y-%m-%d-%H%M%S)-ctext-build-log.txt" + +function abortSetup() { + echo "❌ Setup Aborted: $1" | tee -a "${BUILD_LOG}" && exit 1 +} +function successMessage() { + echo "✅ $1" | tee -a "${BUILD_LOG}" +} + +function pendingMessage() { + echo "⌛ $1" | tee -a "${BUILD_LOG}" +} + +function cleanupPreviousExecutions() { + sourceCodeLocation="$1" + applicationHome=$2 + serviceName="$3" + searchLocations=("$sourceCodeLocation" "$applicationHome") + + if systemctl is-active --quiet "${serviceName}"; then + pendingMessage "${serviceName} is running. Attempting to stop it..." + sudo systemctl stop "${serviceName}" || abortSetup "Unable to stop running service: ${serviceName}" + successMessage "${serviceName} stopped. Proceeding" + + else + successMessage "${serviceName} is not running. Proceeding" + fi + + for installationLocation in "${searchLocations[@]}"; do + if [ -d "${installationLocation}" ]; then + pendingMessage "Cleaning up previous installation artifacts from (${installationLocation})" + sudo rm -rf "${installationLocation}" || abortSetup "Unable to cleanup installation artifacts in: '${installationLocation}'" + successMessage "Clean up completed" + fi + done +} + +function downloadApplication() { + sourceCodeLocation="$1" + repositoryUrl="$2" + pendingMessage "Creating temporary installation directories... (${sourceCodeLocation})" + mkdir -p "${sourceCodeLocation}" || abortSetup "Unable to create temporary installation directory ${sourceCodeLocation}" + pendingMessage "Cloning source code from git..." + git clone "${repositoryUrl}" "${sourceCodeLocation}" --quiet || abortSetup "Unable to download ${repositoryUrl}" + successMessage "Source code downloaded successfully." +} + +function buildApplication() { + sourceCodeLocation="$1" + originalDirectory="$(pwd)" + cd "$sourceCodeLocation" || abortSetup "Unable to access ${sourceCodeLocation}" + pendingMessage "Building application" + mvn clean package spring-boot:repackage -DskipTests -q >"${BUILD_LOG}" || abortSetup "Maven build failed" + cd "${originalDirectory}" || abortSetup "Unable to access ${originalDirectory}" + successMessage "Build completed successfully" +} + +function installApplication() { + sourceCodeLocation=$1 + applicationHome=$2 + jarName=$3 + applicationPath="${applicationHome}/${jarName}" + sudo mkdir -p "${applicationHome}" || abortSetup "Unable to create installation directory: ${applicationHome}" + sudo find "${sourceCodeLocation}/target/" -name "api-*.jar" -exec mv '{}' "${applicationPath}" \; +} + +function fixPermissions() { + applicationPath="$1" + serviceUser="$2" + chmod +rx "${applicationPath}" || abortSetup "Unable to fix permissions for ${applicationPath}" + sudo chown "${serviceUser}:${serviceUser}" "${applicationPath}" || abortSetup "Unable to update ownership for ${applicationPath}" +} + +function configureService() { + serviceName="$1" + sudo systemctl enable "${serviceName}" + sudo systemctl start "${serviceName}" + sudo systemctl status "${serviceName}" +} + +cleanupPreviousExecutions "${SOURCE_CODE_LOCATION}" "${APPLICATION_HOME}" ${SERVICE_NAME} +downloadApplication "${SOURCE_CODE_LOCATION}" "${DOWNLOAD_LINK}" +buildApplication "${SOURCE_CODE_LOCATION}" +installApplication "${SOURCE_CODE_LOCATION}" "${APPLICATION_HOME}" "${JAR_NAME}" +fixPermissions "${APPLICATION_HOME}/${JAR_NAME}" "${SERVICE_USER}" +pendingMessage "Updating service configuration:" +# Create Service File +sudo tee "$SERVICE_REGISTRATION_PATH" <