Skip to content

Commit

Permalink
Merge pull request #20 from SELab-2/9-continuous-deployment
Browse files Browse the repository at this point in the history
9 continuous deployment
  • Loading branch information
robinpdev authored Feb 29, 2024
2 parents 9ec186f + b0379b7 commit 075a2d7
Show file tree
Hide file tree
Showing 51 changed files with 5,848 additions and 3 deletions.
12 changes: 12 additions & 0 deletions .env
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
DEBUG=1
SECRET_KEY=development_key
DJANGO_ALLOWED_HOSTS='localhost 127.0.0.1 [::1] django'
SQL_ENGINE=django.db.backends.postgresql
SQL_DATABASE=pigeonhole_dev
SQL_USER=pigeonhole
SQL_PASSWORD=password
SQL_HOST=pigeonhole-database
SQL_PORT=5432
DATABASE=postgres
DJANGO_SUPERUSER_PASSWORD=abc
DJANGO_SUPERUSER_EMAIL=abc@example.com
16 changes: 16 additions & 0 deletions .github/workflows/deploy.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# Continous deployment of the app

name: CD

on:
push:
branches: ["develop"]

jobs:
build:
runs-on: self-hosted
steps:
- uses: actions/checkout@v3

- name: Run deploy script
run: bash /home/selab2/hosting/UGent-1/deploy.sh
45 changes: 45 additions & 0 deletions .github/workflows/linter.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
name: Lint CI

on:
- pull_request

jobs:
flake8:
runs-on: self-hosted
steps:
- uses: actions/checkout@v3
- name: Set up Python 3.11
uses: actions/setup-python@v4
with:
python-version: "3.11"
- name: Install dependencies
working-directory: ./backend
run: |
python -m pip install --upgrade pip
pip install -r requirements.txt
- name: Analysing the code with flake8
working-directory: ./backend
run: |
flake8 pigeonhole/ testapi/
ESLint:
runs-on: self-hosted
steps:

# Check out the repository
- uses: actions/checkout@v3

# Install Node.js
- uses: actions/setup-node@v3
with:
node-version: 18.17.0

# Install your dependencies
- name: Install your dependencies
working-directory: ./frontend
run: npm ci

# Run ESLint
- name: Run ESLint
working-directory: ./frontend
run: npm run lint
12 changes: 12 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
**/__pycache__
.env
django/db.sqlite3
venv/

# development environment
.idea/
.vscode/

#deploy
letsencrypt
.env.prod
9 changes: 9 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
start:
docker compose up -d

stop:
docker compose down

lint:
docker exec pigeonhole-backend flake8 .
docker exec pigeonhole-frontend npm run lint
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
- Alexander Van Oyen
- Axel Lorreyne: Projectleider
- Gilles Arnout
- Pieter-Jan De Smijter
- Reinhard De Paepe
- Pieter-Jan De Smijter: Technical lead
- Reinhard De Paepe: Backend lead
- Robin Paret: Systeembeheerder
- Rune Dyselinck
- Thibaud Collyn: Customer relations officer
- Thibaud Collyn : Customer relations officer, Frontend lead
3 changes: 3 additions & 0 deletions backend/.flake8
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[flake8]
max-line-length=120
exclude=testapi/migrations
29 changes: 29 additions & 0 deletions backend/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
# pull official base image
FROM python:3.11.7-alpine

# set work directory
WORKDIR /usr/src/app

# set environment variables
ENV PYTHONDONTWRITEBYTECODE 1
ENV PYTHONUNBUFFERED 1

# install psycopg2 dependencies
RUN apk update \
&& apk add postgresql-dev gcc python3-dev musl-dev

# install dependencies
RUN pip install --upgrade pip
COPY ./requirements.txt .
RUN pip install -r requirements.txt

# copy entrypoint.sh
COPY ./entrypoint.sh .
RUN sed -i 's/\r$//g' /usr/src/app/entrypoint.sh
RUN chmod +x /usr/src/app/entrypoint.sh

# copy project
COPY . .

# run entrypoint.sh
ENTRYPOINT ["/usr/src/app/entrypoint.sh"]
73 changes: 73 additions & 0 deletions backend/Dockerfile.prod
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
###########
# BUILDER #
###########

# pull official base image
FROM python:3.11.7-alpine as builder

# set work directory
WORKDIR /usr/src/app

# set environment variables
ENV PYTHONDONTWRITEBYTECODE 1
ENV PYTHONUNBUFFERED 1

# install psycopg2 dependencies
RUN apk update \
&& apk add postgresql-dev gcc python3-dev musl-dev

# lint
RUN pip install --upgrade pip
COPY . .

# install dependencies
COPY requirements.txt .
RUN pip wheel --no-cache-dir --no-deps --wheel-dir /usr/src/app/wheels -r requirements.txt


#########
# FINAL #
#########

# pull official base image
FROM python:3.11.7-alpine

# create directory for the app user
RUN mkdir -p /home/app

# create the app user
RUN addgroup -S app && adduser -S app -G app

# create the appropriate directories
ENV HOME=/home/app
ENV APP_HOME=/home/app/web
RUN mkdir $APP_HOME
RUN mkdir $APP_HOME/static
RUN mkdir $APP_HOME/uploads
WORKDIR $APP_HOME

# install dependencies
RUN apk update && apk add libpq
COPY --from=builder /usr/src/app/wheels /wheels
COPY --from=builder /usr/src/app/requirements.txt .
RUN pip install --no-cache /wheels/*

# copy entrypoint.prod.sh
COPY ./entrypoint.prod.sh .
RUN sed -i 's/\r$//g' $APP_HOME/entrypoint.prod.sh
RUN chmod +x $APP_HOME/entrypoint.prod.sh

# copy project
COPY . $APP_HOME

RUN chmod +x $APP_HOME/entrypoint.prod.sh


# chown all the files to the app user
RUN chown -R app:app $APP_HOME/

# change to the app user
USER app

# run entrypoint.prod.sh
ENTRYPOINT ["/home/app/web/entrypoint.prod.sh"]
14 changes: 14 additions & 0 deletions backend/entrypoint.prod.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#!/bin/sh

if [ "$DATABASE" = "postgres" ]
then
echo "Waiting for postgres..."

while ! nc -z $SQL_HOST $SQL_PORT; do
sleep 0.1
done

echo "PostgreSQL started"
fi

exec "$@"
20 changes: 20 additions & 0 deletions backend/entrypoint.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
#!/bin/sh

if [ "$DATABASE" = "postgres" ]
then
echo "Waiting for postgres..."

while ! nc -z $SQL_HOST $SQL_PORT; do
sleep 0.1
done

echo "PostgreSQL started"
fi

#python manage.py flush --no-input
#python manage.py makemigrations
python manage.py migrate

python manage.py createsuperuser --noinput --email $DJANGO_SUPERUSER_EMAIL

exec "$@"
22 changes: 22 additions & 0 deletions backend/manage.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
#!/usr/bin/env python
"""Django's command-line utility for administrative tasks."""
import os
import sys


def main():
"""Run administrative tasks."""
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'pigeonhole.settings')
try:
from django.core.management import execute_from_command_line
except ImportError as exc:
raise ImportError(
"Couldn't import Django. Are you sure it's installed and "
"available on your PYTHONPATH environment variable? Did you "
"forget to activate a virtual environment?"
) from exc
execute_from_command_line(sys.argv)


if __name__ == '__main__':
main()
Empty file added backend/pigeonhole/__init__.py
Empty file.
Empty file.
Empty file.
17 changes: 17 additions & 0 deletions backend/pigeonhole/apps/users/models.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
from django.contrib.auth.models import AbstractUser


class User(AbstractUser):
class Meta(AbstractUser.Meta):
db_table = "auth_user"

def delete_account(self):
self.first_name = ""
self.last_name = ""
self.email = ""
self.is_active = False
self.save()

@property
def name(self):
return f"{self.first_name.strip()} {self.last_name.strip()}"
16 changes: 16 additions & 0 deletions backend/pigeonhole/asgi.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
"""
ASGI config for pigeonhole project.
It exposes the ASGI callable as a module-level variable named ``application``.
For more information on this file, see
https://docs.djangoproject.com/en/5.0/howto/deployment/asgi/
"""

import os

from django.core.asgi import get_asgi_application

os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'pigeonhole.settings')

application = get_asgi_application()
Loading

0 comments on commit 075a2d7

Please sign in to comment.