Skip to content

Commit

Permalink
chore: deployment improvements (#372)
Browse files Browse the repository at this point in the history
* chore: reduce image size

* feat: tool-versions

* chore: networking

* feat: dns servers

* chore: remove unnecessary copy's

* chore: I hate fixtures

* chore: remove unneccessary rm

* chore: added back container names
  • Loading branch information
Topvennie authored Apr 27, 2024
1 parent fbc59ad commit 981a944
Show file tree
Hide file tree
Showing 19 changed files with 84 additions and 105 deletions.
3 changes: 1 addition & 2 deletions .dev.env
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ FRONTEND_DIR="./frontend"
SSL_DIR="./data/nginx/ssl"

# Redis
REDIS_IP=192.168.90.10
REDIS_PORT=6379

# Django
Expand All @@ -20,5 +19,5 @@ DJANGO_DOMAIN_NAME=localhost
DJANGO_CAS_URL_PREFIX=""
DJANGO_CAS_PORT=8080
DJANGO_DB_ENGINE="django.db.backends.sqlite3"
DJANGO_REDIS_HOST=${REDIS_IP}
DJANGO_REDIS_HOST="redis"
DJANGO_REDIS_PORT=${REDIS_PORT}
1 change: 0 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
.tool-versions
.env
.venv
.idea
Expand Down
6 changes: 2 additions & 4 deletions .prod.env
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,12 @@ FRONTEND_DIR="./frontend"
SSL_DIR=""

# Postgress DB
POSTGRES_IP=192.168.90.9
POSTGRES_PORT=5432
POSTGRES_DB=selab
POSTGRES_USER=selab_user
POSTGRES_PASSWORD=""

# Redis
REDIS_IP=192.168.90.10
REDIS_PORT=6379

# Django
Expand All @@ -30,7 +28,7 @@ DJANGO_DB_ENGINE=django.db.backends.postgresql
DJANGO_DB_NAME=${POSTGRES_DB}
DJANGO_DB_USER=${POSTGRES_USER}
DJANGO_DB_PASSWORD=${POSTGRES_PASSWORD}
DJANGO_DB_HOST=${POSTGRES_IP}
DJANGO_DB_HOST="postgres"
DJANGO_DB_PORT=${POSTGRES_PORT}
DJANGO_REDIS_HOST=${REDIS_IP}
DJANGO_REDIS_HOST="redis"
DJANGO_REDIS_PORT=${REDIS_PORT}
2 changes: 2 additions & 0 deletions .tool-versions
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
python 3.11.8
nodejs 18.17.1
19 changes: 14 additions & 5 deletions backend/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,11 +1,20 @@
FROM python:3.11.4-alpine3.18
FROM python:3.11.4-alpine3.18 as requirements

RUN apk update && apk add --no-cache gettext libintl && pip install -U poetry
RUN poetry config virtualenvs.create false
RUN pip install poetry-plugin-export

WORKDIR /code

COPY pyproject.toml poetry.lock ./
RUN poetry install --only main

COPY . ./
RUN poetry export --without-hashes --format=requirements.txt > requirements.txt


FROM python:3.11.4-alpine3.18

RUN apk add --no-cache gettext libintl

WORKDIR /code

COPY --from=requirements /code/requirements.txt .

RUN pip install -r requirements.txt --no-cache-dir
2 changes: 1 addition & 1 deletion backend/gunicorn_config.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
workers = 4
bind = "0.0.0.0:8080"
bind = "0.0.0.0:8000"
chdir = "/code/"
module = "ypovoli.wsgi:application"
11 changes: 4 additions & 7 deletions backend/setup.sh
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
echo "Installing dependencies..."
pip install poetry > /dev/null 2>&1
poetry install > /dev/null
echo "Migrating database..."
python manage.py migrate > /dev/null
python manage.py migrate > /dev/null 2>&1
echo "Compiling translations..."
django-admin compilemessages > /dev/null
django-admin compilemessages > /dev/null 2>&1
echo "Generating Swagger documentation..."
echo "yes" | python manage.py collectstatic > /dev/null
echo "Done"
echo "yes" | python manage.py collectstatic > /dev/null 2>&1
echo "Setup complete"
1 change: 0 additions & 1 deletion backend/ypovoli/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
BASE_DIR = Path(__file__).resolve().parent.parent
MEDIA_ROOT = os.path.normpath(os.path.join(BASE_DIR, "data/production"))

# TODO: What does this do?
TESTING_BASE_LINK = "http://testserver"

# SECURITY WARNING: keep the secret key used in production secret!
Expand Down
2 changes: 1 addition & 1 deletion data/nginx/nginx.dev.conf
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ events {

http {
upstream backend {
server backend:8080;
server backend:8000;
}

upstream frontend {
Expand Down
2 changes: 1 addition & 1 deletion data/nginx/nginx.prod.conf
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ events {

http {
upstream backend {
server backend:8080;
server backend:8000;
}

upstream frontend {
Expand Down
6 changes: 3 additions & 3 deletions data/nginx/nginx.test.conf
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,15 @@ events {

http {
upstream backend {
server test_backend:8080;
server backend:8000;
}

upstream frontend {
server test_frontend:5173;
server frontend:5173;
}

upstream hmr {
server test_frontend:443;
server frontend:443;
}

server {
Expand Down
4 changes: 2 additions & 2 deletions development.sh
Original file line number Diff line number Diff line change
Expand Up @@ -79,8 +79,8 @@ if [ "$data" != "" ]; then
echo "This can take some time..."
echo "--------------------------"

rm -f backend/db.sqlite3 > /dev/null 2>&1
docker run -v backend:/code ugent-7_backend sh -c "python manage.py migrate; python manage.py loaddata */fixtures/$data/*" > /dev/null
docker run -v backend:/code ugent-7_backend sh -c "python manage.py flush --no-input; python manage.py migrate; python manage.py loaddata */fixtures/$data/*" > /dev/null 2>&1
echo "Database filled."
fi

# Build Docker images
Expand Down
17 changes: 7 additions & 10 deletions development.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ networks:
driver: bridge
ipam:
config:
- subnet: 192.168.90.0/24
- subnet: 192.168.91.0/24

############################# EXTENSIONS

Expand All @@ -24,14 +24,18 @@ x-common-keys-selab: &common-keys-selab
PGID: $PGID
env_file:
- .env
dns:
- 1.1.1.1
- 4.4.4.4
- 8.8.8.8

############################# SERVICES

services:
nginx:
<<: *common-keys-selab
image: nginx:latest
container_name: nginx
image: nginx:latest
ports:
- 80:80
- 443:443
Expand All @@ -49,9 +53,7 @@ services:
build:
context: $BACKEND_DIR
dockerfile: Dockerfile
command: sh -c "./setup.sh; python manage.py runsslserver 192.168.90.2:8080"
expose:
- 8080
command: sh -c "./setup.sh; python manage.py runsslserver 0.0.0.0:8000"
volumes:
- ${BACKEND_DIR}:/code

Expand All @@ -76,8 +78,6 @@ services:
context: $FRONTEND_DIR
dockerfile: Dockerfile.dev
command: sh -c "npm install && npm run host"
expose:
- 5173
volumes:
- ${FRONTEND_DIR}:/app
depends_on:
Expand All @@ -87,9 +87,6 @@ services:
<<: *common-keys-selab
container_name: redis
image: redis:latest
networks:
selab_network:
ipv4_address: $REDIS_IP
expose:
- $REDIS_PORT
entrypoint: redis-server --appendonly yes --maxmemory 512mb --maxmemory-policy allkeys-lru
Expand Down
2 changes: 0 additions & 2 deletions frontend/Dockerfile.dev
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,3 @@ WORKDIR /app
COPY package*.json ./

RUN npm install

COPY . /app/
2 changes: 1 addition & 1 deletion frontend/Dockerfile.prod
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ RUN npm install
COPY ./ .
RUN npm run build

FROM nginx as production-stage
FROM nginx:alpine-slim as production-stage
EXPOSE 3000
RUN mkdir /app
COPY nginx.conf /etc/nginx/conf.d/default.conf
Expand Down
2 changes: 1 addition & 1 deletion frontend/cypress.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { defineConfig } from 'cypress';

export default defineConfig({
e2e: {
baseUrl: 'http://test_nginx',
baseUrl: 'http://nginx',
specPattern: 'src/test/e2e/**/*.cy.{js,jsx,ts,tsx}',
},
});
36 changes: 13 additions & 23 deletions production.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ version: "3.9"
############################# NETWORKS

networks:
selab_network:
name: selab_network
selab_network_prod:
name: selab_network_prod
driver: bridge
ipam:
config:
Expand All @@ -14,7 +14,7 @@ networks:

x-common-keys-selab: &common-keys-selab
networks:
- selab_network
- selab_network_prod
security_opt:
- no-new-privileges:true
restart: unless-stopped
Expand All @@ -24,14 +24,18 @@ x-common-keys-selab: &common-keys-selab
PGID: $PGID
env_file:
- .env
dns:
- 1.1.1.1
- 4.4.4.4
- 8.8.8.8

############################# SERVICES

services:
nginx:
<<: *common-keys-selab
image: nginx:latest
container_name: nginx
container_name: nginx_prod
ports:
- 80:80
- 443:443
Expand All @@ -45,47 +49,35 @@ services:
postgres:
<<: *common-keys-selab
image: postgres:15.2
container_name: postgres
networks:
selab_network:
ipv4_address: $POSTGRES_IP
container_name: postgres_prod
environment:
POSTGRES_DB: $POSTGRES_DB
POSTGRES_USER: $POSTGRES_USER
POSTGRES_PASSWORD: $POSTGRES_PASSWORD
expose:
- $POSTGRES_PORT
volumes:
- ${DATA_DIR}/postgres:/var/lib/postgresql/data

backend:
<<: *common-keys-selab
container_name: backend
container_name: backend_prod
build:
context: $BACKEND_DIR
dockerfile: Dockerfile
command: sh -c "./setup.sh && gunicorn --config gunicorn_config.py ypovoli.wsgi:application"
expose:
- 8080
depends_on:
- postgres

redis:
<<: *common-keys-selab
container_name: redis
container_name: redis_prod
image: redis:latest
networks:
selab_network:
ipv4_address: $REDIS_IP
expose:
- $REDIS_PORT
entrypoint: redis-server --appendonly yes --maxmemory 512mb --maxmemory-policy allkeys-lru
volumes:
- ${DATA_DIR}/redis:/data

celery:
<<: *common-keys-selab
container_name: celery
container_name: celery_prod
build:
context: $BACKEND_DIR
dockerfile: Dockerfile
Expand All @@ -98,11 +90,9 @@ services:

frontend:
<<: *common-keys-selab
container_name: frontend
container_name: frontend_prod
build:
context: $FRONTEND_DIR
dockerfile: Dockerfile.prod
expose:
- 3000
depends_on:
- backend
16 changes: 8 additions & 8 deletions test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ if [ "$build" = true ]; then
fi

echo "Starting services..."
docker-compose -f test.yml up -d --scale test_cypress=0
docker-compose -f test.yml up -d --scale cypress=0

cypress_exit=0
vitest_exit=0
Expand All @@ -67,25 +67,25 @@ django_exit=0
if [ "$frontend" = true ]; then
echo "Running frontend tests..."
echo "Running Cypress tests..."
docker-compose -f test.yml up --exit-code-from test_cypress --abort-on-container-exit test_cypress
docker-compose -f test.yml up --exit-code-from cypress --abort-on-container-exit cypress
cypress_exit=$?
echo "Running Vitest tests..."
docker exec test_frontend npm run test
docker exec frontend npm run test
vitest_exit=$?
elif [ "$backend" = true ]; then
echo "Running backend tests..."
docker exec test_backend python manage.py test
docker exec backend python manage.py test
django_exit=$?
else
echo "Running backend tests..."
docker exec test_backend python manage.py test
docker exec backend python manage.py test
django_exit=$?
echo "Running frontend tests..."
echo "Running frontend_test tests..."
echo "Running Cypress tests..."
docker-compose -f test.yml up --exit-code-from test_cypress --abort-on-container-exit test_cypress
docker-compose -f test.yml up --exit-code-from cypress --abort-on-container-exit cypress
cypress_exit=$?
echo "Running Vitest tests..."
docker exec test_frontend npm run test
docker exec frontend npm run test
vitest_exit=$?
fi

Expand Down
Loading

0 comments on commit 981a944

Please sign in to comment.