Skip to content

Commit

Permalink
- Make sure the DB is available
Browse files Browse the repository at this point in the history
- Make sure the DB is available

 - Make sure we have updated migrations

 - Tuning spc docker-compose
  • Loading branch information
afabiani committed Jul 8, 2019
1 parent 75c5f20 commit 5bcbe01
Show file tree
Hide file tree
Showing 4 changed files with 157 additions and 116 deletions.
107 changes: 54 additions & 53 deletions scripts/spcgeonode/django/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,53 +1,54 @@
# TODO : use python:2.7.13-alpine3.6 to make this lighter ( it is what we use for letsencryipt as well)
# But it seems it's not possible for now because alpine only has geos 3.6 which is not supported by django 1.8
# (probably because of https://code.djangoproject.com/ticket/28441)

FROM python:2.7.14-slim-stretch

# Install system dependencies
RUN echo "Updating apt-get" && \
apt-get update && \
echo "Installing build dependencies" && \
apt-get install -y gcc make libc-dev musl-dev libpcre3 libpcre3-dev g++ && \
echo "Installing Pillow dependencies" && \
# RUN apt-get install -y NOTHING ?? It was probably added in other packages... ALPINE needed jpeg-dev zlib-dev && \
echo "Installing GDAL dependencies" && \
apt-get install -y libgeos-dev libgdal-dev && \
echo "Installing Psycopg2 dependencies" && \
# RUN apt-get install -y NOTHING ?? It was probably added in other packages... ALPINE needed postgresql-dev && \
echo "Installing other dependencies" && \
apt-get install -y libxml2-dev libxslt-dev && \
echo "Installing GeoIP dependencies" && \
apt-get install -y geoip-bin geoip-database && \
echo "Installing healthceck dependencies" && \
apt-get install -y curl && \
echo "Python server" && \
pip install uwsgi && \
echo "Removing build dependencies and cleaning up" && \
# TODO : cleanup apt-get with something like apt-get -y --purge autoremove gcc make libc-dev musl-dev libpcre3 libpcre3-dev g++ && \
rm -rf /var/lib/apt/lists/* && \
rm -rf ~/.cache/pip

# Install python dependencies
RUN echo "Geonode python dependencies"
RUN pip install celery==4.1.0 # see https://github.com/GeoNode/geonode/pull/3714

# Install geonode dependencies
ADD requirements.txt /requirements.txt
RUN pip install -r requirements.txt
RUN rm requirements.txt

# Install pygdal (after requirements https://github.com/GeoNode/geonode/pull/4599)
RUN pip install pygdal==$(gdal-config --version).*

# Install geonode
RUN mkdir /spcgeonode
WORKDIR /spcgeonode/
ADD . /spcgeonode/
RUN pip install -e .
RUN chmod +x scripts/spcgeonode/django/docker-entrypoint.sh

# Export ports
EXPOSE 8000

# We provide no command or entrypoint as this image can be used to serve the django project or run celery tasks
# TODO : use python:2.7.13-alpine3.6 to make this lighter ( it is what we use for letsencryipt as well)
# But it seems it's not possible for now because alpine only has geos 3.6 which is not supported by django 1.8
# (probably because of https://code.djangoproject.com/ticket/28441)

FROM python:2.7.16-slim-stretch

# Install system dependencies
RUN echo "Updating apt-get" && \
apt-get update && \
echo "Installing build dependencies" && \
apt-get install -y gcc make libc-dev musl-dev libpcre3 libpcre3-dev g++ && \
echo "Installing Pillow dependencies" && \
# RUN apt-get install -y NOTHING ?? It was probably added in other packages... ALPINE needed jpeg-dev zlib-dev && \
echo "Installing GDAL dependencies" && \
apt-get install -y libgeos-dev libgdal-dev && \
echo "Installing Psycopg2 dependencies" && \
# RUN apt-get install -y NOTHING ?? It was probably added in other packages... ALPINE needed postgresql-dev && \
echo "Installing other dependencies" && \
apt-get install -y libxml2-dev libxslt-dev && \
echo "Installing GeoIP dependencies" && \
apt-get install -y geoip-bin geoip-database && \
echo "Installing healthceck dependencies" && \
apt-get install -y curl && \
echo "Python server" && \
pip install uwsgi && \
echo "Removing build dependencies and cleaning up" && \
# TODO : cleanup apt-get with something like apt-get -y --purge autoremove gcc make libc-dev musl-dev libpcre3 libpcre3-dev g++ && \
rm -rf /var/lib/apt/lists/* && \
rm -rf ~/.cache/pip

# Install python dependencies
RUN echo "Geonode python dependencies"
RUN pip install celery==4.2.1 # see https://github.com/GeoNode/geonode/pull/3714
RUN pip install django-celery-monitor==1.1.2

# Install geonode dependencies
ADD requirements.txt /requirements.txt
RUN pip install -r requirements.txt
RUN rm requirements.txt

# Install pygdal (after requirements https://github.com/GeoNode/geonode/pull/4599)
RUN pip install pygdal==$(gdal-config --version).*

# Install geonode
RUN mkdir /spcgeonode
WORKDIR /spcgeonode/
ADD . /spcgeonode/
RUN pip install -e .
RUN chmod +x scripts/spcgeonode/django/docker-entrypoint.sh

# Export ports
EXPOSE 8000

# We provide no command or entrypoint as this image can be used to serve the django project or run celery tasks
46 changes: 42 additions & 4 deletions scripts/spcgeonode/django/initialize.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,20 +6,23 @@
# Setting up the context
#########################################################

import os, requests, json, uuid, django, time
import os, requests, json, uuid, django, datetime, time
django.setup()

#########################################################
# Imports
#########################################################

from django.core.management import call_command
from django.conf import settings
from django.db import connection
from django.utils import timezone
from django.db.utils import OperationalError
from django.contrib.auth import get_user_model
from django.core.management import call_command
from requests.exceptions import ConnectionError
from geonode.people.models import Profile
from oauth2_provider.models import Application
from django.conf import settings
from oauthlib.common import generate_token
from oauth2_provider.models import AccessToken, get_application_model

# Getting the secrets
admin_username = os.getenv('ADMIN_USERNAME')
Expand Down Expand Up @@ -49,6 +52,7 @@

print("-----------------------------------------------------")
print("2. Running the migrations")
call_command('makemigrations')
call_command('migrate', '--noinput')


Expand Down Expand Up @@ -80,6 +84,8 @@

print("-----------------------------------------------------")
print("4. Create/update an OAuth2 provider to use authorisations keys")

Application = get_application_model()
app, created = Application.objects.get_or_create(
pk=1,
name='GeoServer',
Expand Down Expand Up @@ -174,3 +180,35 @@
r2.raise_for_status()
else:
print("Master password was already changed. No changes made.")


#########################################################
# 10. Test User Model
#########################################################

print("-----------------------------------------------------")
print("10. Test User Model")

def make_token_expiration(seconds=86400):
_expire_seconds = getattr(settings, 'ACCESS_TOKEN_EXPIRE_SECONDS', seconds)
_expire_time = datetime.datetime.now(timezone.get_current_timezone())
_expire_delta = datetime.timedelta(seconds=_expire_seconds)
return _expire_time + _expire_delta

user = get_user_model().objects.get(username=admin_username)
expires = make_token_expiration()
(access_token, created) = AccessToken.objects.get_or_create(
user=user,
application=app,
expires=expires,
token=generate_token())

# #########################################################
# 11. Close all db connections
#########################################################

print("-----------------------------------------------------")
print("11. Close all db connections")

from django import db
db.connections.close_all()
1 change: 1 addition & 0 deletions scripts/spcgeonode/docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ x-common-django:
- C_FORCE_ROOT=True
# We get an exception after migrations on startup (it seems the monitoring app tries to resolve the geoserver domain name after it's migration, which can happen before oauth migrations on which geoserver startup depends...)
- MONITORING_ENABLED=False
# - GEONODE_CLIENT_LAYER_PREVIEW_LIBRARY=geoext
volumes:
- static:/spcgeonode-static/
- media:/spcgeonode-media/
Expand Down
119 changes: 60 additions & 59 deletions scripts/spcgeonode/nginx/spcgeonode.conf
Original file line number Diff line number Diff line change
@@ -1,59 +1,60 @@
# This is the main gepgeonode conf

charset utf-8;

# max upload size
client_max_body_size 100G;

# compression
gzip on;
gzip_proxied any;
gzip_types
text/css
text/javascript
text/xml
text/plain
application/javascript
application/x-javascript
application/json;

# Geoserver
location /geoserver {

# Using a variable is a trick to let Nginx start even if upstream host is not up yet
# (see https://sandro-keil.de/blog/2017/07/24/let-nginx-start-if-upstream-host-is-unavailable-or-down/)
set $upstream geoserver:8080;

proxy_set_header Host $http_host;
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_pass http://$upstream;
}

# Django media
location /uploaded {
alias /spcgeonode-media; # your Django project's media files - amend as required
include /etc/nginx/mime.types;
expires 365d;
}

location /static {
alias /spcgeonode-static; # your Django project's static files - amend as required
include /etc/nginx/mime.types;
expires 365d;
}

# Finally, send all non-media requests to the Django server.
location / {

# Using a variable is a trick to let Nginx start even if upstream host is not up yet
# (see https://sandro-keil.de/blog/2017/07/24/let-nginx-start-if-upstream-host-is-unavailable-or-down/)
set $upstream django:8000;

uwsgi_pass $upstream;

# uwsgi_params
include /etc/nginx/uwsgi_params;
}
# This is the main gepgeonode conf

charset utf-8;

# max upload size
client_max_body_size 100G;

# compression
gzip on;
gzip_proxied any;
gzip_types
text/css
text/javascript
text/xml
text/plain
application/javascript
application/x-javascript
application/json;

# Geoserver
location /geoserver {

# Using a variable is a trick to let Nginx start even if upstream host is not up yet
# (see https://sandro-keil.de/blog/2017/07/24/let-nginx-start-if-upstream-host-is-unavailable-or-down/)
set $upstream geoserver:8080;

proxy_set_header Host $http_host;
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_pass http://$upstream;
}

# Django media
location /uploaded {
alias /spcgeonode-media; # your Django project's media files - amend as required
include /etc/nginx/mime.types;
expires 365d;
}

location /static {
alias /spcgeonode-static; # your Django project's static files - amend as required
include /etc/nginx/mime.types;
expires 365d;
}

# Finally, send all non-media requests to the Django server.
location / {
# uwsgi_params
include /etc/nginx/uwsgi_params;

# Using a variable is a trick to let Nginx start even if upstream host is not up yet
# (see https://sandro-keil.de/blog/2017/07/24/let-nginx-start-if-upstream-host-is-unavailable-or-down/)
set $upstream django:8000;
uwsgi_pass $upstream;

# when a client closes the connection then keep the channel to uwsgi open. Otherwise uwsgi throws an IOError
uwsgi_ignore_client_abort on;
}

0 comments on commit 5bcbe01

Please sign in to comment.