Skip to content

Commit

Permalink
Dockerization rework (#21)
Browse files Browse the repository at this point in the history
* Update Dockerfile

* retrieve docker-compose file from other branch

* wip on docker-compose file

* local init script

* Example docker-composition

* Remove dotenv

* Envvar for ADMIN_USERNAME

* chore: setup docker compose dev environment

* chore: improve fixtures generation to be compatible with initialized DBs

* chore: missing import gettext_lazy import

* chore(Dockerfile): remove /mnt/geoshop_data directory creation

---------

Co-authored-by: Andrea Borghi <[email protected]>
  • Loading branch information
hbollon and danduk82 authored Jul 16, 2024
1 parent 98a32fa commit 82f1654
Show file tree
Hide file tree
Showing 6 changed files with 113 additions and 29 deletions.
4 changes: 4 additions & 0 deletions .env.sample
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ ROOTURL=

DEFAULT_FROM_EMAIL=[email protected]
EMAIL_HOST=smtp.example.com
ADMIN_USERNAME=admin
ADMIN_EMAIL_LIST=[email protected]

# This is used for CORS and to generate links in email templates
Expand All @@ -45,3 +46,6 @@ DEFAULT_LANGUAGE=en
#GDAL_PATH="C:\Applications\Mapserver72_x64\bin"
#GDAL_LIBRARY_PATH="C:\Applications\Mapserver72_x64\bin\gdal300.dll"
#GEOS_LIBRARY_PATH="C:\Applications\Mapserver72_x64\bin\geos_c.dll"

# Geometric settings
DEFAULT_SRID=2056
21 changes: 8 additions & 13 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
FROM osgeo/gdal:ubuntu-small-3.5.2
FROM ghcr.io/osgeo/gdal:ubuntu-small-3.5.2

RUN apt-get update --fix-missing
RUN apt-get install gettext python3-pip libcairo2-dev build-essential python3-dev \
RUN apt-get update --fix-missing && \
apt-get install gettext python3-pip libcairo2-dev build-essential python3-dev \
pipenv python3-setuptools python3-wheel python3-cffi libcairo2 libpango-1.0-0 \
libpangocairo-1.0-0 libgdk-pixbuf2.0-0 libffi-dev shared-mime-info libpq-dev -y
RUN pip3 install gunicorn
libpangocairo-1.0-0 libgdk-pixbuf2.0-0 libffi-dev shared-mime-info libpq-dev -y && \
apt-get clean && \
rm -rf /var/lib/apt/lists/*

COPY ./requirements.txt /app/geoshop_back/requirements.txt
WORKDIR /app/geoshop_back/
RUN pip3 install -r requirements.txt
Expand All @@ -15,11 +17,4 @@ ENV C_INCLUDE_PATH=/usr/include/gdal
ENV PYTHONUNBUFFERED 1

COPY . /app/geoshop_back/

ARG ENV_FILE
RUN mv ${ENV_FILE} .env && mkdir /mnt/geoshop_data

RUN export $(egrep -v '^#' .env | xargs) && \
python manage.py migrate && \
python manage.py collectstatic --noinput && \
python manage.py compilemessages --locale=fr
RUN mv /app/geoshop_back/default_settings.py /app/geoshop_back/settings.py
28 changes: 16 additions & 12 deletions api/management/commands/fixturize.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,23 +15,27 @@ class Command(BaseCommand):
Creates internal group
"""
def handle(self, *args, **options):
admin_user = UserModel.objects.get(username='admin')
admin_user = UserModel.objects.get(username=os.environ.get('ADMIN_USERNAME', 'admin'))
admin_user.set_password(os.environ['ADMIN_PASSWORD'])
admin_user.save()

content_type = ContentType.objects.get_for_model(Order)
extract_group = Group.objects.create(name='extract')
Group.objects.create(name='internal')
extract_permission = Permission.objects.create(
Group.objects.update_or_create(name='internal')
extract_permission = Permission.objects.update_or_create(
codename='is_extract',
name='Is extract service',
content_type=content_type)
extract_permission.save()
extract_permission[0].save()

extract_group = Group.objects.update_or_create(name='extract')
extract_group[0].permissions.add(extract_permission[0])
extract_group[0].save()

extract_user = UserModel.objects.create_user(
username='external_provider', password=os.environ['EXTRACT_USER_PASSWORD'])
extract_group.permissions.add(extract_permission)
extract_group.save()
extract_user.groups.add(extract_group)
extract_user.identity.company_name = 'ACME'
extract_user.save()
if not UserModel.objects.filter(username='external_provider').exists():
extract_user = UserModel.objects.create_user(
username='external_provider',
password=os.environ['EXTRACT_USER_PASSWORD']
)
extract_user.groups.add(extract_group[0])
extract_user.identity.company_name = 'ACME'
extract_user.save()
6 changes: 2 additions & 4 deletions default_settings.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
import os
from dotenv import load_dotenv
from django.utils.translation import gettext_lazy as _

load_dotenv()

# Build paths inside the project like this: os.path.join(BASE_DIR, ...)
BASE_DIR = os.path.dirname(os.path.abspath(__file__))

Expand Down Expand Up @@ -255,7 +252,8 @@
INTRA_LEGEND_URL = os.environ.get('INTRA_LEGEND_URL', '')

# Geometries settings
DEFAULT_SRID = 2056
# FIXME: Does this work with another SRID?
DEFAULT_SRID = int(os.environ.get('DEFAULT_SRID', '2056'))

# Default Extent
# default extent is set to the BBOX of switzerland
Expand Down
71 changes: 71 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
version: "3"

networks:
geoshop:
driver: bridge

services:
db:
image: postgis/postgis:15-3.4-alpine
volumes:
- "./resources/db:/docker-entrypoint-initdb.d:ro"
environment:
POSTGRES_USER: "${PGUSER}"
POSTGRES_PASSWORD: "${PGPASSWORD}"
POSTGRES_DB: "${PGDATABASE}"
LANG: "en_US.utf8"
LC_COLLATE: "en_US.utf8"
LC_CTYPE: "en_US.utf8"
env_file: .env
ports:
- 5432:5432
networks:
- geoshop
healthcheck:
test:
["CMD-SHELL", "pg_isready -h 127.0.0.1 -U ${PGUSER} -d ${PGDATABASE}"]
interval: 10s
timeout: 5s
retries: 5

migrate:
image: geoshop-api
build:
context: .
dockerfile: Dockerfile
depends_on:
db:
condition: service_healthy
env_file: .env
environment:
PGHOST: "db"
command:
- bash
- -c
- "python manage.py migrate && python manage.py collectstatic --noinput && python manage.py compilemessages --locale=fr && python manage.py fixturize"
volumes:
- "static-files:/app/geoshop_back/static:rw"
networks:
- geoshop

api:
image: geoshop-api
depends_on:
db:
condition: service_healthy
migrate:
condition: service_completed_successfully
env_file: .env
environment:
PGHOST: "db"
command: "gunicorn wsgi -b :8000 --timeout 90"
restart: unless-stopped
volumes:
- "static-files:/app/geoshop_back/static:ro"
ports:
- "8080:8000"
networks:
- geoshop

volumes:
static-files:
12 changes: 12 additions & 0 deletions resources/db/01_init_db.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@

CREATE EXTENSION IF NOT EXISTS postgis;
CREATE EXTENSION IF NOT EXISTS unaccent;
CREATE EXTENSION IF NOT EXISTS "uuid-ossp";

CREATE SCHEMA IF NOT EXISTS geoshop AUTHORIZATION geoshop;

DROP TEXT SEARCH CONFIGURATION IF EXISTS fr;
CREATE TEXT SEARCH CONFIGURATION fr (COPY = simple);

ALTER TEXT SEARCH CONFIGURATION fr ALTER MAPPING FOR hword, hword_part, word
WITH unaccent, simple;

0 comments on commit 82f1654

Please sign in to comment.