diff --git a/apps/authentication/.env b/apps/authentication/.env deleted file mode 100644 index afa895c..0000000 --- a/apps/authentication/.env +++ /dev/null @@ -1,23 +0,0 @@ -APP_NAME=authentication -export SERVICE_API_ACCESS_HOST=0.0.0.0 -export SERVICE_API_ACCESS_PORT=8004 -export CONTAINER_APP_ROOT=/app -export LOG_BASE_PATH=$CONTAINER_APP_ROOT/log/$APP_NAME -export BACKEND_LOG_FILE_NAME=$APP_NAME -export APPLICATION_ACTIVITY_LOG=$APP_NAME-activity -export MONGODB_NAME=freeleaps2 -export MONGODB_PORT=27017 -export JWT_SECRET_KEY=ea84edf152976b2fcec12b78aa8e45bc26a5cf0ef61bf16f5c317ae33b3fd8b0 -GIT_REPO_ROOT=/mnt/freeleaps/freeleaps-service-hub -CODEBASE_ROOT=/mnt/freeleaps/freeleaps-service-hub/apps/authentication -SITE_DEPLOY_FOLDER=/mnt/freeleaps/freeleaps-service-hub/sites/authentication/deploy -#!/bin/bash -export VENV_DIR=venv_t -export VENV_ACTIVATE=venv_t/bin/activate -export DOCKER_HOME=/var/lib/docker -export DOCKER_APP_HOME=$DOCKER_HOME/app -export DOCKER_BACKEND_HOME=$DOCKER_APP_HOME/$APP_NAME -export DOCKER_BACKEND_LOG_HOME=$DOCKER_BACKEND_HOME/log -export MONGODB_URI=mongodb://localhost:27017/ -export FREELEAPS_ENV=local - diff --git a/apps/authentication/common/config/log_settings.py b/apps/authentication/common/config/log_settings.py index 856281d..6d86462 100644 --- a/apps/authentication/common/config/log_settings.py +++ b/apps/authentication/common/config/log_settings.py @@ -1,11 +1,11 @@ +from pydantic_settings import BaseSettings +from .app_settings import app_settings class LogSettings(): LOG_LEVEL: str = "DEBUG" - LOG_PATH_BASE: str = ( - "./logs" - ) - LOG_PATH: str = LOG_PATH_BASE + '/' + "app" + '.log' + LOG_BASE_PATH: str = app_settings.LOG_BASE_PATH + LOG_PATH: str = LOG_BASE_PATH + '/' + app_settings.BACKEND_LOG_FILE_NAME + '.log' LOG_RETENTION: str = "14 days" LOG_ROTATION: str = "00:00" # mid night diff --git a/apps/central_storage/common/config/log_settings.py b/apps/central_storage/common/config/log_settings.py index 856281d..6d86462 100644 --- a/apps/central_storage/common/config/log_settings.py +++ b/apps/central_storage/common/config/log_settings.py @@ -1,11 +1,11 @@ +from pydantic_settings import BaseSettings +from .app_settings import app_settings class LogSettings(): LOG_LEVEL: str = "DEBUG" - LOG_PATH_BASE: str = ( - "./logs" - ) - LOG_PATH: str = LOG_PATH_BASE + '/' + "app" + '.log' + LOG_BASE_PATH: str = app_settings.LOG_BASE_PATH + LOG_PATH: str = LOG_BASE_PATH + '/' + app_settings.BACKEND_LOG_FILE_NAME + '.log' LOG_RETENTION: str = "14 days" LOG_ROTATION: str = "00:00" # mid night diff --git a/apps/content/common/config/log_settings.py b/apps/content/common/config/log_settings.py index 856281d..6d86462 100644 --- a/apps/content/common/config/log_settings.py +++ b/apps/content/common/config/log_settings.py @@ -1,11 +1,11 @@ +from pydantic_settings import BaseSettings +from .app_settings import app_settings class LogSettings(): LOG_LEVEL: str = "DEBUG" - LOG_PATH_BASE: str = ( - "./logs" - ) - LOG_PATH: str = LOG_PATH_BASE + '/' + "app" + '.log' + LOG_BASE_PATH: str = app_settings.LOG_BASE_PATH + LOG_PATH: str = LOG_BASE_PATH + '/' + app_settings.BACKEND_LOG_FILE_NAME + '.log' LOG_RETENTION: str = "14 days" LOG_ROTATION: str = "00:00" # mid night diff --git a/apps/notification/Dockerfile b/apps/notification/Dockerfile index 4136969..2268ab8 100644 --- a/apps/notification/Dockerfile +++ b/apps/notification/Dockerfile @@ -1,19 +1,46 @@ -# Dockerfile for Python Service -FROM python:3.10-slim +FROM python:3.10-slim-buster -# Set the working directory inside the container -WORKDIR /app +# docker settings +ARG CONTAINER_APP_ROOT= +ENV APP_NAME= -# Copy the requirements.txt to the working directory and install dependencies -COPY requirements.txt ./ + +#site_settings +ENV SERVICE_API_ACCESS_HOST=0.0.0.0 +ENV SERVICE_API_ACCESS_PORT=8003 +ENV RABBITMQ_HOST= +ENV RABBITMQ_PORT= + +ENV SYSTEM_USER_ID= +ENV SMS_FROM= +ENV EMAIL_FROM= + +ENV SECRET_KEY= + +ENV SENDGRID_API_KEY= + +ENV TWILIO_ACCOUNT_SID= +ENV TWILIO_AUTH_TOKEN= + + + +#log_settings +ENV LOG_BASE_PATH= +ENV BACKEND_LOG_FILE_NAME= +ENV APPLICATION_ACTIVITY_LOG= + + +WORKDIR ${CONTAINER_APP_ROOT} +COPY requirements.txt . + +RUN pip install --upgrade pip RUN pip install --no-cache-dir -r requirements.txt -# Copy the application code to the working directory -COPY . /app +COPY . ${CONTAINER_APP_ROOT} -# Expose the port used by the FastAPI app -EXPOSE 8003 +RUN apt update +RUN apt install -y netcat +RUN ln -s /bin/bash /usr/bin/bash - -# Run the application using the start script -CMD ["uvicorn", "app.notification.webapi.main:app", "--reload", "--port=8003", "--host=0.0.0.0"] +EXPOSE ${SERVICE_API_ACCESS_PORT} +CMD ["uvicorn", "webapi.main:app", "--reload", "--port=${SERVICE_API_ACCESS_PORT}", "--host=${SERVICE_API_ACCESS_HOST}"] diff --git a/apps/notification/common/config/log_settings.py b/apps/notification/common/config/log_settings.py index 55a36da..6d86462 100644 --- a/apps/notification/common/config/log_settings.py +++ b/apps/notification/common/config/log_settings.py @@ -1,8 +1,11 @@ -## This would be the app specific log setting file -class LogSettings: + +from pydantic_settings import BaseSettings +from .app_settings import app_settings + +class LogSettings(): LOG_LEVEL: str = "DEBUG" - LOG_PATH_BASE: str = "./logs" - LOG_PATH: str = LOG_PATH_BASE + "/" + "app" + ".log" + LOG_BASE_PATH: str = app_settings.LOG_BASE_PATH + LOG_PATH: str = LOG_BASE_PATH + '/' + app_settings.BACKEND_LOG_FILE_NAME + '.log' LOG_RETENTION: str = "14 days" LOG_ROTATION: str = "00:00" # mid night diff --git a/apps/notification/start_fastapi.sh b/apps/notification/start_fastapi.sh new file mode 100755 index 0000000..cafe5b4 --- /dev/null +++ b/apps/notification/start_fastapi.sh @@ -0,0 +1,40 @@ +#! /bin/bash +rp=$(dirname "$(realpath '$1'))") +pushd $rp + +APP_NAME=notification +APP_PARENT_FOLDER=apps + +GIT_REPO_ROOT=$(git rev-parse --show-toplevel) +CODEBASE_ROOT=$GIT_REPO_ROOT/$APP_PARENT_FOLDER/$APP_NAME +SITE_DEPLOY_FOLDER=$GIT_REPO_ROOT/sites/$APP_NAME/deploy + + +echo APP_NAME=$APP_NAME > .env +cat $SITE_DEPLOY_FOLDER/common/.env >> .env +echo GIT_REPO_ROOT=$(git rev-parse --show-toplevel) >> .env +echo CODEBASE_ROOT=$GIT_REPO_ROOT/$APP_PARENT_FOLDER/$APP_NAME >> .env +echo SITE_DEPLOY_FOLDER=$GIT_REPO_ROOT/sites/$APP_NAME/deploy >> .env +cat $SITE_DEPLOY_FOLDER/common/.host.env >> .env +cat $SITE_DEPLOY_FOLDER/local/.env >> .env + +. .env + +if [ -d "$VENV_DIR" ] +then + echo "Folder $VENV_DIR exists. Proceed to next steps" +else + echo "Folder $VENV_DIR dosen't exist. create it" + sudo apt install python3-pip + python3 -m pip install virtualenv + python3 -m virtualenv $VENV_DIR +fi + +source $VENV_DIR/bin/activate +pip install --upgrade pip +pip install -r requirements.txt + +uvicorn webapi.main:app --reload --host 0.0.0.0 --port $SERVICE_API_ACCESS_PORT + + +popd diff --git a/sites/notification/deploy/alpha/.env b/sites/notification/deploy/alpha/.env new file mode 100755 index 0000000..68beb14 --- /dev/null +++ b/sites/notification/deploy/alpha/.env @@ -0,0 +1,3 @@ +export MONGODB_URI='mongodb+srv://jetli:8IHKx6dZK8BfugGp@freeleaps2.hanbj.mongodb.net/' +export FREELEAPS_ENV=alpha + diff --git a/sites/notification/.env b/sites/notification/deploy/common/.env old mode 100644 new mode 100755 similarity index 50% rename from sites/notification/.env rename to sites/notification/deploy/common/.env index 7a825c1..86017aa --- a/sites/notification/.env +++ b/sites/notification/deploy/common/.env @@ -1,5 +1,10 @@ +export SERVICE_API_ACCESS_HOST=0.0.0.0 +export SERVICE_API_ACCESS_PORT=8004 +export CONTAINER_APP_ROOT=/app +export LOG_BASE_PATH=$CONTAINER_APP_ROOT/log/$APP_NAME +export BACKEND_LOG_FILE_NAME=$APP_NAME +export APPLICATION_ACTIVITY_LOG=$APP_NAME-activity export SENDGRID_API_KEY='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' export EMAIL_FROM=freeleaps@freeleaps.com export TWILIO_ACCOUNT_SID=ACf8c9283a6acda060258eadb29be58bc8 export TWILIO_AUTH_TOKEN=ef160748cc22c8b7195b49df4b8eca7e -export JWT_SECRET_KEY=ea84edf152976b2fcec12b78aa8e45bc26a5cf0ef61bf16f5c317ae33b3fd8b0 \ No newline at end of file diff --git a/sites/notification/deploy/common/.host.env b/sites/notification/deploy/common/.host.env new file mode 100755 index 0000000..73c2468 --- /dev/null +++ b/sites/notification/deploy/common/.host.env @@ -0,0 +1,7 @@ +#!/bin/bash +export VENV_DIR=venv_t +export VENV_ACTIVATE=venv_t/bin/activate +export DOCKER_HOME=/var/lib/docker +export DOCKER_APP_HOME=$DOCKER_HOME/app +export DOCKER_BACKEND_HOME=$DOCKER_APP_HOME/$APP_NAME +export DOCKER_BACKEND_LOG_HOME=$DOCKER_BACKEND_HOME/log diff --git a/sites/notification/deploy/common/docker-compose.yaml b/sites/notification/deploy/common/docker-compose.yaml new file mode 100755 index 0000000..e407d61 --- /dev/null +++ b/sites/notification/deploy/common/docker-compose.yaml @@ -0,0 +1,36 @@ +services: + authentication: + container_name: $APP_NAME + build: + context: ${CODEBASE_ROOT} + args: + CONTAINER_APP_ROOT: ${CONTAINER_APP_ROOT} + profiles: [prod,alpha,dev] + restart: always + environment: + - APP_NAME=${APP_NAME} + - MONGODB_NAME=${MONGODB_NAME} + - MONGODB_PORT=${MONGODB_PORT} + - MONGODB_URI=${MONGODB_URI} + - SERVICE_API_ACCESS_HOST=${SERVICE_API_ACCESS_HOST} + - SERVICE_API_ACCESS_PORT=${SERVICE_API_ACCESS_PORT} + - LOG_BASE_PATH=${LOG_BASE_PATH} + - BACKEND_LOG_FILE_NAME=${BACKEND_LOG_FILE_NAME} + - APPLICATION_ACTIVITY_LOG=${APPLICATION_ACTIVITY_LOG} + - JWT_SECRET_KEY=${JWT_SECRET_KEY} + ports: + - ${SERVICE_API_ACCESS_PORT}:${SERVICE_API_ACCESS_PORT} + command: + - /bin/sh + - -c + - | + uvicorn webapi.main:app --reload --port=${SERVICE_API_ACCESS_PORT} --host=${SERVICE_API_ACCESS_HOST} + networks: + - devbox_freeleaps2-network + volumes: + - type: bind + source: $DOCKER_BACKEND_LOG_HOME + target: $LOG_BASE_PATH +networks: + devbox_freeleaps2-network: + external: true \ No newline at end of file diff --git a/sites/notification/deploy/deploy.sh b/sites/notification/deploy/deploy.sh new file mode 100755 index 0000000..470f1ef --- /dev/null +++ b/sites/notification/deploy/deploy.sh @@ -0,0 +1,91 @@ +#!/bin/bash +DW_BASE_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" >/dev/null 2>&1 && pwd)" +APP_NAME=authentication +APP_PARENT_FOLDER=apps +PROJECT_NAME=authentication + +while [ $# -gt 0 ]; do + case "$1" in + --target* | -u*) + if [[ "$1" != *=* ]]; then shift; fi # Value is next arg if no `=` + TARGET_ENV="${1#*=}" + ;; + --help | -h) + printf "$BASE_NAME --target=\n" # Flag argument + return 0 + ;; + *) + printf >&2 "Error: Invalid argument\n" + return 1 + ;; + esac + shift +done + +if [[ "${TARGET_ENV}" != "prod" && "${TARGET_ENV}" != "alpha" && "${TARGET_ENV}" != "dev" ]]; then + printf "$BASE_NAME --target=\n" # Flag argument + return 0 +fi +if git rev-parse --git-dir > /dev/null 2>&1; then + # git repo! + GIT_REPO_ROOT=$(git rev-parse --show-toplevel) + CODEBASE_ROOT=$GIT_REPO_ROOT/$APP_PARENT_FOLDER/$APP_NAME + WORKING_DIR=$GIT_REPO_ROOT/sites/$APP_NAME/deploy + +else + # NOT a git repo! + printf "Please run this command under a git repo" + return 0 +fi + +. $DW_BASE_DIR/common/.host.env + + +DW_PUSHD_COUNTER=0 + +ENV_FOLDER=$WORKING_DIR/$TARGET_ENV +COMMON_ENV_FOLDER=$WORKING_DIR/common + + +pushd $WORKING_DIR +DW_PUSHD_COUNTER=$((DW_PUSHD_COUNTER + 1)) + +echo export APP_NAME=$APP_NAME > $WORKING_DIR/.env +echo export GIT_REPO_ROOT=$GIT_REPO_ROOT >> $WORKING_DIR/.env +echo export APP_PARENT_FOLDER=$APP_PARENT_FOLDER >> $WORKING_DIR/.env + +cat $COMMON_ENV_FOLDER/.env >> $WORKING_DIR/.env +echo export CODEBASE_ROOT=$CODEBASE_ROOT >> $WORKING_DIR/.env +echo export WORKING_DIR=$WORKING_DIR >> $WORKING_DIR/.env +cat $COMMON_ENV_FOLDER/.host.env >> $WORKING_DIR/.env +cat $ENV_FOLDER/.env >>$WORKING_DIR/.env + +DOCKER_COMPOSE_YAML=$WORKING_DIR/docker-compose-$APP_NAME.yaml +cp $DW_BASE_DIR/common/docker-compose.yaml $DOCKER_COMPOSE_YAML -u + +. $WORKING_DIR/.env + +sudo mkdir $DOCKER_BACKEND_LOG_HOME -p + +sudo docker compose -p $PROJECT_NAME -f $DOCKER_COMPOSE_YAML --profile $TARGET_ENV down --remove-orphans +sudo docker compose -p $PROJECT_NAME -f $DOCKER_COMPOSE_YAML --profile $TARGET_ENV build --no-cache + +# Clean up any previous resources that are not needed +# sudo docker system prune -f --volumes +# sudo docker image prune -f +# sudo docker container prune -f +# sudo docker network prune -f + +# Start up the Docker containers in detached mode and remove orphans +sudo docker compose -p $PROJECT_NAME -f $DOCKER_COMPOSE_YAML --profile $TARGET_ENV up --detach --remove-orphans +echo 'You can use "sudo docker compose logs -f" to check the output of the containers' +sudo docker ps -a + +rm $WORKING_DIR/.env +rm $DOCKER_COMPOSE_YAML + +while [[ "$DW_PUSHD_COUNTER" -gt 0 ]]; do + DW_PUSHD_COUNTER=$((DW_PUSHD_COUNTER - 1)) + popd +done + diff --git a/sites/notification/deploy/dev/.env b/sites/notification/deploy/dev/.env new file mode 100755 index 0000000..a43a6fe --- /dev/null +++ b/sites/notification/deploy/dev/.env @@ -0,0 +1,3 @@ +export MONGODB_URI=mongodb://freeleaps2-mongodb:27017/ +export FREELEAPS_ENV=dev + diff --git a/sites/notification/deploy/local/.env b/sites/notification/deploy/local/.env new file mode 100644 index 0000000..4938c5e --- /dev/null +++ b/sites/notification/deploy/local/.env @@ -0,0 +1,3 @@ +export MONGODB_URI=mongodb://localhost:27017/ +export FREELEAPS_ENV=local + diff --git a/sites/notification/deploy/prod/.env b/sites/notification/deploy/prod/.env new file mode 100755 index 0000000..0167e99 --- /dev/null +++ b/sites/notification/deploy/prod/.env @@ -0,0 +1,2 @@ +export MONGODB_URI='mongodb+srv://freeadmin:0eMV0bt8oyaknA0m@freeleaps2.zmsmpos.mongodb.net/?retryWrites=true&w=majority' +export FREELEAPS_ENV=prod