From 63c92ac2722db587c5d107ed1cc081f0f0c927c3 Mon Sep 17 00:00:00 2001 From: Daniel Legt Date: Mon, 22 Jan 2024 01:49:13 +0200 Subject: [PATCH] Docker Implementation --- .gitignore | 3 ++- Dockerfile | 52 +++++++++++++++++++++++++++++++++++++++ build.sh | 14 ++++++----- deploy.sh | 56 ++++++++++++++++++++++++++++++++++++++++++ docker-compose.dev.yml | 14 +++++++++++ lib/config/config.go | 12 +++++++++ 6 files changed, 144 insertions(+), 7 deletions(-) create mode 100644 Dockerfile create mode 100755 deploy.sh create mode 100644 docker-compose.dev.yml diff --git a/.gitignore b/.gitignore index 36747db..b148049 100644 --- a/.gitignore +++ b/.gitignore @@ -2,4 +2,5 @@ snapshots.dat .env dist data.db -data.sqlite \ No newline at end of file +data.sqlite +dev_data \ No newline at end of file diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..855843a --- /dev/null +++ b/Dockerfile @@ -0,0 +1,52 @@ +# Build Stage +FROM debian:bullseye +ENV IS_DOCKER TRUE + +# Install build dependencies and runtime dependencies +RUN apt-get update && apt-get install -y \ + gcc \ + musl-dev \ + libsqlite3-dev \ + libsqlite3-0 \ + wget \ + && rm -rf /var/lib/apt/lists/* + +# Manually install Go 1.21 +ENV GOLANG_VERSION 1.21.0 +RUN wget https://golang.org/dl/go${GOLANG_VERSION}.linux-amd64.tar.gz -O go.tgz \ + && tar -C /usr/local -xzf go.tgz \ + && rm go.tgz +ENV PATH /usr/local/go/bin:$PATH + +# Set the environment variable for Go +ENV GOPATH=/go +ENV PATH=$GOPATH/bin:$PATH +ENV GO111MODULE=on +ENV DIST_DIR=/app + +# Create the directory and set it as the working directory +WORKDIR $DIST_DIR + +# Copy the Go files and download dependencies +COPY go.mod go.sum ./ +RUN go mod download + +# Copy the source from the current directory to the Working Directory inside the container +COPY . . + +# Build the Go app +RUN go build -o drive-health + +# Cleanup build dependencies to reduce image size +RUN apt-get purge -y gcc musl-dev libsqlite3-dev wget \ + && apt-get autoremove -y \ + && apt-get clean + +# Expose the necessary port +EXPOSE 8080 + +# Volume for external data +VOLUME [ "/data" ] + +# Command to run the executable +CMD ["./drive-health"] \ No newline at end of file diff --git a/build.sh b/build.sh index b1d8c38..fb366c9 100755 --- a/build.sh +++ b/build.sh @@ -1,17 +1,19 @@ -#!/bin/bash +#!/bin/sh + +set -o pipefail +set -u APP_NAME="drive-health" -DIST_DIR="dist" +DIST_DIR="${DIST_DIR:-dist}" # Create the dist directory if it doesn't exist mkdir -p $DIST_DIR # Build the application echo "Building the application..." -GOOS=linux GOARCH=amd64 go build -o $DIST_DIR/$APP_NAME +GOOS=linux CGO_ENABLED=1 GOARCH=amd64 go build -o $DIST_DIR/$APP_NAME -# Copy additional resources (like .env, static files, templates) to the dist directory -echo "Copying additional resources..." -cp -r .env static templates $DIST_DIR/ +# echo "Copying additional resources..." +cp -r static templates $DIST_DIR/ echo "Compilation and packaging completed." diff --git a/deploy.sh b/deploy.sh new file mode 100755 index 0000000..581f724 --- /dev/null +++ b/deploy.sh @@ -0,0 +1,56 @@ +#!/bin/bash + +# Function to display messages in color +echo_color() { + color=$1 + text=$2 + case $color in + "green") echo -e "\033[0;32m$text\033[0m" ;; + "yellow") echo -e "\033[0;33m$text\033[0m" ;; + "red") echo -e "\033[0;31m$text\033[0m" ;; + *) echo "$text" ;; + esac +} + +# Getting GIT_VERSION from the most recent tag or commit hash +GIT_VERSION=$(git describe --tags --always) +if [ -z "$GIT_VERSION" ]; then + echo_color red "Error: Unable to determine GIT_VERSION." + exit 1 +fi + +# Run tests before proceeding +echo_color yellow "Running tests..." +if ! go test; then + echo_color red "Tests failed. Cancelling build process." + exit 1 +fi +echo_color green "All tests passed successfully." + +echo_color green "Starting the Docker build process with version $GIT_VERSION..." + +IMAGE_NAME="ghcr.io/JustKato/drive-health:$GIT_VERSION" +echo_color yellow "Image to be built: $IMAGE_NAME" + +# Confirmation to build +read -p "Are you sure you want to build an image? (y/N) " response +if [[ "$response" =~ ^([yY][eE][sS]|[yY])$ ]]; then + # Building the Docker image + echo "Building Docker image: $IMAGE_NAME" + docker build --no-cache -t $IMAGE_NAME . +else + echo_color red "Build cancelled." + exit 1 +fi + +# Prompt to push the image +read -p "Push image to repository? (y/N) " push_response +if [[ "$push_response" =~ ^([yY][eE][sS]|[yY])$ ]]; then + # Pushing the image + echo "Pushing image: $IMAGE_NAME" + docker push $IMAGE_NAME +else + echo_color red "Push cancelled." +fi + +echo_color green "Ending the Docker build process..." \ No newline at end of file diff --git a/docker-compose.dev.yml b/docker-compose.dev.yml new file mode 100644 index 0000000..62bc990 --- /dev/null +++ b/docker-compose.dev.yml @@ -0,0 +1,14 @@ +version: "3.8" + +services: + drive-health: + # Build the current image + build: . + # Read straight from the .env file, or use the environment path + env_file: + - .env + volumes: + - ./dev_data:/data + # Setup application ports + ports: + - 8080:8080 \ No newline at end of file diff --git a/lib/config/config.go b/lib/config/config.go index 3dcf0f9..2794b37 100644 --- a/lib/config/config.go +++ b/lib/config/config.go @@ -17,6 +17,8 @@ type DHConfig struct { IdentityUsername string `json:"identityUsername"` IdentityPassword string `json:"identityPassword"` + IsDocker bool `json:isDocker` + DebugMode bool `json:"debugMode"` } @@ -36,6 +38,8 @@ func GetConfiguration() *DHConfig { IdentityUsername: "admin", IdentityPassword: "admin", + IsDocker: false, + Listen: ":8080", } @@ -79,5 +83,13 @@ func GetConfiguration() *DHConfig { } } + if val, exists := os.LookupEnv("IS_DOCKER"); exists { + if isDocker, err := strconv.ParseBool(val); err == nil { + config.IsDocker = isDocker + + config.DatabaseFilePath = "/data/data.sqlite" + } + } + return config }