Commit ea15a777 authored by Florent Chehab's avatar Florent Chehab

Project dockerized

parent 5b03c824
stages: stages:
- check
- test - test
- linting - lint
- documentation - documentation
- deploy
variables: variables:
ENV: CI ENV: DEV
POSTGRES_DB: outgoing SECRET_KEY: stupid_key_for_CI
POSTGRES_USER: outgoing POSTGRES_DB: postgres
POSTGRES_PASSWORD: "" POSTGRES_USER: postgres
POSTGRES_PASSWORD: postgres
DB_HOST: postgres
DB_PORT: 5432
testing:
check_back:
stage: check
image: registry.gitlab.utc.fr/rex-dri/rex-dri/backend
services:
- postgres:10.5
script:
- cd backend
- ./manage.py check
tags:
- docker
check_front:
stage: check
image: node:10.15.1-alpine
cache:
paths:
- frontend/node_modules/
before_script:
- cd frontend && npm i
script:
- npm run build
tags:
- docker
test_back:
stage: test stage: test
image: python:3.6.5-jessie image: registry.gitlab.utc.fr/rex-dri/rex-dri/backend
services: services:
- postgres:9.6.8 - postgres:10.5
script: script:
- make install_backend - cd backend
- make check_backend - pytest base_app/ frontend_app/ backend_app/ --cov-report html
- make test_backend_server
artifacts: artifacts:
paths: paths:
- backend/htmlcov/ - backend/htmlcov/
...@@ -27,19 +54,23 @@ testing: ...@@ -27,19 +54,23 @@ testing:
- docker - docker
flake8: flake8:
stage: linting stage: lint
image: python:3.6.5-jessie image: registry.gitlab.utc.fr/rex-dri/rex-dri/backend
script: script:
- pip install flake8
- cd backend && flake8 - cd backend && flake8
tags: tags:
- docker - docker
eslint: eslint:
stage: linting stage: lint
image: pipelinecomponents/eslint:latest image: node:10.15.1-alpine
cache:
paths:
- frontend/node_modules/
before_script:
- cd frontend && npm i
script: script:
- cd frontend && npm run lint - npm run lint
tags: tags:
- docker - docker
...@@ -47,7 +78,7 @@ pages: ...@@ -47,7 +78,7 @@ pages:
stage: documentation stage: documentation
image: floawfloaw/plantuml image: floawfloaw/plantuml
dependencies: dependencies:
- testing - test_back
script: script:
- mkdir .public - mkdir .public
- mv backend/htmlcov/ .public/coverage - mv backend/htmlcov/ .public/coverage
......
# You need to have source the env before using the makefile .PHONY: documentation
.PHONY: docs
install_backend: up:
cd backend && pip install -r requirements.txt --quiet docker-compose up
build_frontend: docker-pull:
cd frontend && npm run build docker-compose pull
test_backend: up--build:
cd backend && pytest base_app/ frontend_app/ backend_app/ docker-compose up --build
test_backend_server: test_backend:
cd backend && pytest -n 4 base_app/ frontend_app/ backend_app/ --cov-report html docker-compose exec backend sh -c "cd backend && pytest base_app/ frontend_app/ backend_app/"
check_backend: check_backend:
cd backend && ./manage.py check docker-compose exec backend sh -c "cd backend && ./manage.py check"
lint_backend:
docker-compose exec backend sh -c "cd backend && flake8"
lint_frontend:
docker-compose exec frontend sh -c "cd frontend && npm run lint"
build_frontend:
docker-compose exec frontend sh -c "cd frontend && npm run build"
run_backend: shell_backend:
cd backend && ./manage.py runserver docker-compose exec backend sh
shell_frontend:
docker-compose exec frontend sh
documentation: documentation:
cd documentation && $(MAKE) all cd documentation && $(MAKE) all
......
# pull official base image
# Use of stretch instead of Alpine for faster install of python packages (especially pandas)
# Overall performance might be slightly better (with bigger image size obviously)
FROM python:3.7.2-slim-stretch
# set work directory
WORKDIR /usr/src/app
# runtime dependencies
# libpq-dev and gcc is for psycopg2-binary
RUN apt-get update && apt-get install -y --no-install-recommends \
libpq-dev \
python3-dev \
gcc \
make \
&& rm -rf /var/lib/apt/lists/*
# install dependencies
RUN pip install --upgrade pip
COPY requirements.txt /usr/src/app/requirements.txt
RUN pip install -r requirements.txt
""" """
Django settings for outgoing_rex project. Django settings
Generated by 'django-admin startproject' using Django 2.0.3.
For more information on this file, see For more information on this file, see
https://docs.djangoproject.com/en/2.0/topics/settings/ https://docs.djangoproject.com/en/2.0/topics/settings/
...@@ -14,29 +12,10 @@ import os ...@@ -14,29 +12,10 @@ import os
from os.path import dirname from os.path import dirname
import sys import sys
# Build paths inside the project like this: os.path.join(BASE_DIR, ...) # Build paths inside the project like this: os.path.join(BASE_DIR, ...)
BASE_DIR = dirname(dirname(dirname(os.path.abspath(__file__)))) BASE_DIR = dirname(dirname(dirname(os.path.abspath(__file__))))
########## SECRET_KEY = os.environ["SECRET_KEY"]
# Webpack loader related
WEBPACK_LOADER = {
"DEFAULT": {
"BUNDLE_DIR_NAME": "frontend_app/bundles/",
"STATS_FILE": os.path.join(BASE_DIR, "../frontend/webpack-stats.json"),
}
}
STATICFILES_DIRS = (
os.path.join(
BASE_DIR, "static/frontend_app/bundles/"
), # We do this so that django's collectstatic copies or our bundles to the STATIC_ROOT or syncs them to whatever storage we use.
)
# End of webpack loader related
###########
# Application definition # Application definition
INSTALLED_APPS = [ INSTALLED_APPS = [
...@@ -66,15 +45,41 @@ MIDDLEWARE = [ ...@@ -66,15 +45,41 @@ MIDDLEWARE = [
"django.middleware.clickjacking.XFrameOptionsMiddleware", "django.middleware.clickjacking.XFrameOptionsMiddleware",
"base_app.middleware.LoginRequiredMiddleware", "base_app.middleware.LoginRequiredMiddleware",
] ]
MODERATION_ACTIVATED = True
TESTING = "pytest" in sys.modules if os.environ["ENV"] == "DEV":
DEBUG = True
ALLOWED_HOSTS = ["*"]
INSTALLED_APPS += ["django_extensions", "debug_toolbar"]
MIDDLEWARE = ["debug_toolbar.middleware.DebugToolbarMiddleware"] + MIDDLEWARE
ALLOWED_PHOTOS_EXTENSION = ["jpg", "jpeg", "png", "svg"]
else:
DEBUG = False
##########
# Webpack loader related
WEBPACK_LOADER = {
"DEFAULT": {
"BUNDLE_DIR_NAME": "frontend_app/bundles/",
"STATS_FILE": os.path.join(BASE_DIR, "frontend/webpack-stats.json"),
}
}
STATICFILES_DIRS = (
os.path.join(
BASE_DIR, "static/frontend_app/bundles/"
), # We do this so that django's collectstatic copies or our bundles to the STATIC_ROOT or syncs them to whatever storage we use.
)
# End of webpack loader related
###########
ALLOWED_PHOTOS_EXTENSION = ["jpg", "jpeg", "png", "svg"]
INTERNAL_IPS = ["127.0.0.1"] INTERNAL_IPS = ["127.0.0.1"]
LOGIN_URL = "/user/login" LOGIN_URL = "/user/login"
LOGIN_EXEMPT_URLS = [LOGIN_URL, "/admin/", "api"] LOGIN_EXEMPT_URLS = [LOGIN_URL, "/admin/"]
AUTHENTICATION_BACKENDS = [ AUTHENTICATION_BACKENDS = [
"django.contrib.auth.backends.ModelBackend", "django.contrib.auth.backends.ModelBackend",
...@@ -85,6 +90,11 @@ CAS_SERVER_URL = "https://cas.utc.fr/cas/" ...@@ -85,6 +90,11 @@ CAS_SERVER_URL = "https://cas.utc.fr/cas/"
CAS_APPLY_ATTRIBUTES_TO_USER = True CAS_APPLY_ATTRIBUTES_TO_USER = True
CAS_RENAME_ATTRIBUTES = {"mail": "email", "givenName": "first_name", "sn": "last_name"} CAS_RENAME_ATTRIBUTES = {"mail": "email", "givenName": "first_name", "sn": "last_name"}
TESTING = "pytest" in sys.modules
if TESTING:
LOGIN_EXEMPT_URLS.append("api")
REST_FRAMEWORK = { REST_FRAMEWORK = {
# Use Django's standard `django.contrib.auth` permissions, # Use Django's standard `django.contrib.auth` permissions,
"DEFAULT_PERMISSION_CLASSES": ( "DEFAULT_PERMISSION_CLASSES": (
...@@ -122,8 +132,25 @@ WSGI_APPLICATION = "base_app.wsgi.application" ...@@ -122,8 +132,25 @@ WSGI_APPLICATION = "base_app.wsgi.application"
# Database # Database
# https://docs.djangoproject.com/en/2.0/ref/settings/#databases # https://docs.djangoproject.com/en/2.0/ref/settings/#databases
# DB are set in other config files DATABASES = {
DATABASES = {"default": None} "default": {
"ENGINE": "django.db.backends.postgresql_psycopg2",
"NAME": os.environ["POSTGRES_DB"],
"USER": os.environ["POSTGRES_USER"],
"PASSWORD": os.environ["POSTGRES_PASSWORD"],
"HOST": os.environ["DB_HOST"],
"PORT": os.environ["DB_PORT"],
}
}
# To use Sqlite, uncomment below
# PROJECT_DIR = os.path.abspath(os.path.dirname(__file__))
# DATABASES = {
# "default": {
# "ENGINE": "django.db.backends.sqlite3",
# "NAME": os.path.join(PROJECT_DIR, "../database.db"),
# }
# }
# Password validation # Password validation
...@@ -149,5 +176,8 @@ USE_I18N = True ...@@ -149,5 +176,8 @@ USE_I18N = True
USE_L10N = True USE_L10N = True
USE_TZ = True USE_TZ = True
STATIC_URL = "/static/"
STATIC_ROOT = os.path.join(BASE_DIR, "static")
MEDIA_ROOT = os.path.join(BASE_DIR, "media") MEDIA_ROOT = os.path.join(BASE_DIR, "media")
MEDIA_URL = "/media/" MEDIA_URL = "/media/"
from .base import *
INSTALLED_APPS += ["django_extensions", "debug_toolbar"]
MIDDLEWARE = ["debug_toolbar.middleware.DebugToolbarMiddleware"] + MIDDLEWARE
MODERATION_ACTIVATED = True
DEBUG = True
ALLOWED_HOSTS = ["*"]
SECRET_KEY = "0a1kbtt%as4624u*9+r+c%j1tlq&dc08==c3w604(4igc+t6tw"
# DATABASE
# After you have read the documentation, uncomment the
# block with the database system you would like to use.
# POSTGRESQL
# If bellow fails, your need to add DATABASE_URL and ENV to your virutalenv
try:
CI_ENV = os.environ["ENV"] == "CI"
except KeyError:
CI_ENV = False
# Database config
if CI_ENV:
# Should reflect gitlab-ci.yml
DATABASES = {
"default": {
"ENGINE": "django.db.backends.postgresql_psycopg2",
"NAME": "outgoing",
"USER": "outgoing",
"PASSWORD": "",
"HOST": "postgres",
"PORT": "5432",
}
}
else:
# DATABASES = {
# 'default': {
# 'ENGINE': 'django.db.backends.postgresql_psycopg2',
# 'NAME': 'outgoing',
# 'USER': 'outgoing',
# 'PASSWORD': 'outgoing',
# 'HOST': '127.0.0.1',
# 'PORT': '5432',
# }
# }
# Sqlite
PROJECT_DIR = os.path.abspath(os.path.dirname(__file__))
DATABASES = {
"default": {
"ENGINE": "django.db.backends.sqlite3",
"NAME": os.path.join(PROJECT_DIR, "../database.db"),
}
}
# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/2.0/howto/static-files/
STATIC_URL = "/static/"
STATIC_ROOT = os.path.join(BASE_DIR, "static")
...@@ -3,7 +3,7 @@ import os ...@@ -3,7 +3,7 @@ import os
import sys import sys
if __name__ == "__main__": if __name__ == "__main__":
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "base_app.settings.development") os.environ.setdefault("DJANGO_SETTINGS_MODULE", "base_app.settings")
try: try:
from django.core.management import execute_from_command_line from django.core.management import execute_from_command_line
except ImportError as exc: except ImportError as exc:
......
[pytest] [pytest]
DJANGO_SETTINGS_MODULE = base_app.settings.development DJANGO_SETTINGS_MODULE = base_app.settings
python_files = tests.py test_*.py *_tests.py python_files = tests.py test_*.py *_tests.py
addopts = --cov --cov-config .coveragerc --cov-report term addopts = --cov --cov-config .coveragerc --cov-report term
version: '3.7'
services:
backend:
image: registry.gitlab.utc.fr/rex-dri/rex-dri/backend:latest
# build: ./backend
volumes:
- .:/usr/src/app/
ports:
- 8000:8000 # Replicate the python server port
environment:
- ENV=DEV
- SECRET_KEY=please_change_me
- DB_HOST=database
- DB_PORT=5432
- POSTGRES_DB=postgres
- POSTGRES_USER=postgres
- POSTGRES_PASSWORD=postgres
command: /bin/sh -c "cd backend && ./manage.py runserver 0.0.0.0:8000"
depends_on:
- database
database:
image: postgres:10.5-alpine
environment:
- POSTGRES_DB=postgres
- POSTGRES_USER=postgres
- POSTGRES_PASSWORD=postgres
volumes:
- postgres_data:/var/lib/postgresql/data/
frontend:
build: ./frontend
command: /bin/sh -c "cd frontend && npm i --verbose && npm run dev"
volumes:
- .:/usr/src/app/
ports:
- 3000:3000 # Replicate the node server port
volumes:
postgres_data:
# pull official base image
FROM node:10.15.1-alpine
WORKDIR /usr/src/app
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment