Skip to content

Commit

Permalink
Merge pull request #15 from mtkhawaja/feature/system-d-service-setup
Browse files Browse the repository at this point in the history
Feature/system d service setup
  • Loading branch information
mtkhawaja authored Feb 9, 2023
2 parents ddc6c49 + f70d49c commit ffc5d8d
Show file tree
Hide file tree
Showing 8 changed files with 142 additions and 63 deletions.
19 changes: 10 additions & 9 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -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
WORKDIR /opt/cthulhu-text/bin
COPY --from=builder /opt/cthulhu-text/build/target/api-*.jar ./ctext.jar
CMD java -jar ctext.jar
15 changes: 1 addition & 14 deletions docker-compose.yaml
Original file line number Diff line number Diff line change
@@ -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"
5 changes: 3 additions & 2 deletions docker/dev/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -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
3 changes: 2 additions & 1 deletion docker/dev/override.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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
30 changes: 10 additions & 20 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -35,32 +35,12 @@
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-data-jpa -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-validation -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-validation</artifactId>
</dependency>


<!-- https://mvnrepository.com/artifact/org.postgresql/postgresql -->
<dependency>
<groupId>org.postgresql</groupId>
<artifactId>postgresql</artifactId>
<version>${postgresql.version}</version>
</dependency>
<!-- https://mvnrepository.com/artifact/com.h2database/h2 -->
<dependency>
<groupId>com.h2database</groupId>
<version>${h2.version}</version>
<artifactId>h2</artifactId>
<scope>test</scope>
</dependency>

<!-- Test & Optional Dependencies -->
<!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-devtools -->
<dependency>
Expand Down Expand Up @@ -111,6 +91,16 @@
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<executable>true</executable>
</configuration>
<executions>
<execution>
<configuration>
<mainClass>com.cthulhutext.api.CthulhuTextApiApplication</mainClass>
</configuration>
</execution>
</executions>
</plugin>
<!-- Core Maven Plugins -->
<!-- https://mvnrepository.com/artifact/org.apache.maven.plugins/maven-compiler-plugin -->
Expand Down
114 changes: 114 additions & 0 deletions setup.sh
Original file line number Diff line number Diff line change
@@ -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" <<EOF
[Unit]
Description=cthulhu-text
After=syslog.target
[Service]
User=${SERVICE_USER}
ExecStart=/opt/cthulhu-text/bin/${JAR_NAME}
SuccessExitStatus=143
[Install]
WantedBy=multi-user.target
EOF
successMessage "Service configuration updated"
configureService "${SERVICE_NAME}"
8 changes: 2 additions & 6 deletions src/main/resources/application.yaml
Original file line number Diff line number Diff line change
@@ -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:
Expand Down
11 changes: 0 additions & 11 deletions src/test/resources/application.yaml

This file was deleted.

0 comments on commit ffc5d8d

Please sign in to comment.