Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Initial incubator resources for container build #819

Merged
merged 7 commits into from
Oct 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
84 changes: 84 additions & 0 deletions .incubator/app.Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
ARG NODE_VERSION=20.18.0-bookworm
ARG HUU_BASE_VERSION=1.1
ARG HUU_ECR_REPOSITORY=035866691871.dkr.ecr.us-west-2.amazonaws.com/homeuniteus

# use the official Node image for building the UI bundle
FROM node:${NODE_VERSION} AS client-builder


# navigate to dir where client app source and build will live
WORKDIR /app

# copy the UI source code and configurations into our working dir
COPY ./frontend ./

ENV VITE_HUU_API_BASE_URL=https://qa.homeunite.us/api

# install the NPM packages and generate the UI build
RUN npm install && npm run build

# use our custom base image with pre-installed Python
# note: if we adjust to work with a common stable
# version of Python (i.e. latest available package
# for distro), then we should merge the base image
# dockerfile here and install directly
FROM ${HUU_ECR_REPOSITORY}:base-${HUU_BASE_VERSION}

# expose parameters for custom configuration of build SDK/runtime versions
# and build process configurations
ARG POETRY_VERSION=1.8
ARG HUU_TARGET_ENV=qa

# navigate to dir where API sources and scripts will live
WORKDIR /opt/huu

# copy the API source into our working dir
COPY ./backend ./

# set some env vars to configure poetry behavior
ENV POETRY_HOME=/opt/poetry
ENV POETRY_NO_INTERACTION=1
ENV PYTHONDONTWRITEBYTECODE=1
ENV PYTHONUNBUFFERED=1

# Tell Poetry where to place its cache and virtual environment
ENV POETRY_CACHE_DIR=/opt/.cache

# create a virtual env for the poetry workspace
RUN python3 -m venv venv

# install poetry using the virtual env's version of `pip`
RUN ./venv/bin/pip install "poetry==${POETRY_VERSION}"

# Install the dependencies and clear the cache afterwards.
# This may save some MBs.
# RUN ./venv/bin/poetry install --no-root && rm -rf $POETRY_CACHE_DIR
RUN ./venv/bin/poetry install

# pull our UI build (generated in previous stage) into the conventional
# location for its nginx `server` block
COPY --from=client-builder /app/dist /var/www/${HUU_TARGET_ENV}.homeunite.us/html/

# overwrite the default nginx `server` block with ours - note that
# if we ever want to acutally deploy separate server blocks, this
# file should be deleted in favor of a conf file at:
# /etc/nginx/sites-available/${HUU_TARGET_ENV}.homeunite.us
# which should be symlinked to: `/etc/nginx/sites-enabled/`
COPY ./.incubator/default.conf /etc/nginx/conf.d/default.conf

# replace the template values for the environment - this corresponds
# to the subdomain of *.homeunite.us where this image will be deployed
RUN sed -i "s/<HUU_ENVIRONMENT>/${HUU_TARGET_ENV}/g" /etc/nginx/conf.d/default.conf

# add our entrypoint script, which will start the API process and fork
# it into the background, then run the default entrypoint for nginx
COPY ./.incubator/huu-entrypoint.sh ./

# set the `x` flag to allow execution by the dockerd process
RUN chmod +x /opt/huu/huu-entrypoint.sh

# create the target directory for the nginx access and error logs
RUN mkdir -p /var/log/${HUU_TARGET_ENV}.homeunite.us/

# launch the application from our custom entrypoint script
ENTRYPOINT [ "/opt/huu/huu-entrypoint.sh" ]
37 changes: 37 additions & 0 deletions .incubator/base.Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
ARG NGINX_VERSION=1.27.2

FROM nginx:${NGINX_VERSION}

ARG PYTHON_VERSION=3.12.5

# use latest available system package listings and installations
RUN apt-get update -y && apt-get upgrade -y

# we need `curl` to download things, and `build-essential` to
# install python and node from source
RUN apt-get install -y \
curl \
build-essential \
libsqlite3-dev \
libpq-dev \
zlib1g-dev

# keep dependency installation resources separate from source
WORKDIR /opt

# download and install python from source
# as of this writing, the latest package supported by apt is Python 3.9, and
# this is the simplest way to get Python 3.12+ installed

# download and extract Python source
RUN curl -LO https://www.python.org/ftp/python/${PYTHON_VERSION}/Python-${PYTHON_VERSION}.tgz \
&& tar xzf Python-${PYTHON_VERSION}.tgz

# navigate to root of Python source for installation
WORKDIR /opt/Python-${PYTHON_VERSION}

# build and install Python from source
RUN ./configure --enable-loadable-sqlite-extensions \
&& make \
&& make install

34 changes: 34 additions & 0 deletions .incubator/default.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
server {

listen 80;
listen [::]:80;

root /var/www/<HUU_ENVIRONMENT>.homeunite.us/html;
index index.html index.htm index.nginx-debian.html;

server_name <HUU_ENVIRONMENT>.homeunite.us www.<HUU_ENVIRONMENT>.homeunite.us localhost;

access_log /var/log/<HUU_ENVIRONMENT>.homeunite.us/nginx-access.log;
error_log /var/log/<HUU_ENVIRONMENT>.homeunite.us/nginx-error.log;

location / {
try_files $uri $uri/ /index.html =404;
}

location /img {
try_files $uri =404;
}

location /api/ {
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_set_header X-NginX-Proxy true;
proxy_pass http://127.0.0.1:8000/api/;
proxy_ssl_session_reuse off;
proxy_set_header Host $http_host;
proxy_cache_bypass $http_upgrade;
proxy_redirect off;
}

}
7 changes: 7 additions & 0 deletions .incubator/huu-entrypoint.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#!/bin/bash

pushd /opt/huu
./venv/bin/poetry run fastapi run app/main.py --port 8000 &
popd

/docker-entrypoint.sh nginx -g 'daemon off;'
1 change: 1 addition & 0 deletions backend/app/core/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ class Settings(BaseSettings):
COGNITO_ACCESS_ID: str
COGNITO_ACCESS_KEY: str
COGNITO_ENDPOINT_URL: str | None = None
HUU_ENVIRONMENT: str
ROOT_URL: str
DATABASE_URL: str

Expand Down
30 changes: 29 additions & 1 deletion backend/app/health.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,36 @@
from fastapi import APIRouter, status
from backend.app.modules.deps import SettingsDep
from fastapi import APIRouter, status, HTTPException
from fastapi.responses import JSONResponse

health_router = APIRouter()


@health_router.get("/", status_code=status.HTTP_200_OK)
def health():
return "UP"

@health_router.get("/nginx-logs", status_code=status.HTTP_200_OK)
def nginx_logs(settings: SettingsDep):

if not settings.HUU_ENVIRONMENT:
raise HTTPException(
status_code=400,
detail=f'Not an nginx environment'
)

nginx_logs_dir = f'/var/log/{settings.HUU_ENVIRONMENT}.homeunite.us'

with open(f'{nginx_logs_dir}/nginx-access.log') as f:
access_logs = f.read()

with open('{nginx_logs_dir}/nginx-error.log') as f:
error_logs = f.read()

return JSONResponse(content={
'accessLogs': access_logs,
'errorLogs': error_logs
})




Loading