From c4eea181fbd301956f300238b7900254f8e2290c Mon Sep 17 00:00:00 2001 From: Matheus Veleci dos Santos Date: Mon, 10 Apr 2017 12:48:51 -0300 Subject: [PATCH] Ajusta Docker do SAPL com NGINX e GUNICORN. (#997) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * Adiciona Docker baseado em Alpine-Python3 com Ngnix e Gunicorn * Atualiza docker-compose com gunicorn, ngnix e postgres * Ajusta gitignore para ignorar postgres-data * Adiciona script bash para conexao ao banco * Ajusta busy-wait.sh script para sincronizar o banco(postgres) com a aplicação django no docker * Add alias to sapldb --- .gitignore | 3 +- .travis.yml | 4 +++ Dockerfile | 59 ++++++++++-------------------------- busy-wait.sh | 10 ++++++ config/nginx/sapl.conf | 39 ++++++++++++++++++++++++ docker-compose.yml | 21 ++++++++++--- gunicorn_start.sh | 12 ++------ sapl/.env_test | 2 +- scripts_docker/shell_sapl.sh | 3 +- 9 files changed, 93 insertions(+), 60 deletions(-) create mode 100644 busy-wait.sh create mode 100644 config/nginx/sapl.conf diff --git a/.gitignore b/.gitignore index 83de47a86..e8bac47aa 100644 --- a/.gitignore +++ b/.gitignore @@ -92,4 +92,5 @@ collected_static bower bower_components media -whoosh/ \ No newline at end of file +whoosh/ +postgres-data/ diff --git a/.travis.yml b/.travis.yml index bf5ee38fd..0e0a27ad4 100644 --- a/.travis.yml +++ b/.travis.yml @@ -20,3 +20,7 @@ script: - ./manage.py bower install - py.test --create-db # - ./test_and_check_qa.sh + +addons: + hosts: + - 127.0.0.1 sapldb diff --git a/Dockerfile b/Dockerfile index 9880dc4ef..a7bf01657 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,53 +1,26 @@ -FROM ubuntu:15.04 +FROM alpine:3.5 -RUN locale-gen en_US.UTF-8 -ENV LANG en_US.UTF-8 -ENV LANGUAGE en_US:en -ENV LC_ALL en_US.UTF-8 +ENV BUILD_PACKAGES postgresql-dev graphviz-dev graphviz build-base git pkgconfig \ +python3-dev libxml2-dev jpeg-dev libressl-dev libffi-dev libxslt-dev nodejs py3-lxml \ +py3-magic postgresql-client -RUN mkdir /sapl +RUN apk add --no-cache python3 && \ + python3 -m ensurepip && \ + rm -r /usr/lib/python*/ensurepip && \ + pip3 install --upgrade pip setuptools && \ + rm -r /root/.cache -RUN echo "deb http://archive.ubuntu.com/ubuntu/ vivid universe" | tee -a "/etc/apt/sources.list" - -RUN \ - apt-get update && \ - apt-get install -y -f \ - software-properties-common \ - libpq-dev \ - graphviz-dev \ - graphviz \ - build-essential \ - git \ - pkg-config \ - python3-dev \ - libxml2-dev \ - libjpeg-dev \ - libssl-dev \ - libffi-dev \ - libxslt1-dev \ - python3-setuptools \ - curl - -# use python3 in pip -RUN easy_install3 pip lxml - -# install nodejs -RUN DEBIAN_FRONTEND=noninteractive curl -sL https://deb.nodesource.com/setup_5.x | bash - -RUN DEBIAN_FRONTEND=noninteractive apt-get install -y nodejs - -# install bower -RUN npm install -g bower +RUN mkdir /sapl && apk add --update --no-cache $BUILD_PACKAGES && npm install -g bower # Bower aceitar root -RUN touch /root/.bowerrc -RUN chmod 751 /root/.bowerrc -RUN echo "{ \"allow_root\": true }" >> /root/.bowerrc +RUN touch /root/.bowerrc \ +&& chmod 751 /root/.bowerrc \ +&& echo "{ \"allow_root\": true }" >> /root/.bowerrc \ +&& npm cache clean WORKDIR /sapl ADD . /sapl -RUN pip install -r requirements/dev-requirements.txt -RUN pip install --upgrade setuptools - -# RUN python3 manage.py bower install +RUN pip install -r requirements/dev-requirements.txt --upgrade setuptools --no-cache-dir \ +&& python3 manage.py bower install diff --git a/busy-wait.sh b/busy-wait.sh new file mode 100644 index 000000000..128592ffc --- /dev/null +++ b/busy-wait.sh @@ -0,0 +1,10 @@ +#!/bin/sh + +while true; do + COUNT_PG=`psql --dbname=postgresql://sapl:sapl@sapldb/sapl -c '\l \q' | grep sapl | wc -l` + if ! [ "$COUNT_PG" -eq "0" ]; then + break + fi + echo "Esperando Database Setup" + sleep 10 +done diff --git a/config/nginx/sapl.conf b/config/nginx/sapl.conf new file mode 100644 index 000000000..513188ed9 --- /dev/null +++ b/config/nginx/sapl.conf @@ -0,0 +1,39 @@ +upstream sapl_server { + + server unix:/sapl/run/gunicorn.sock fail_timeout=0; + +} + +server { + + listen 80; + server_name sapl.test; + + client_max_body_size 4G; + + location /static/ { + alias /sapl/collected_static/; + } + + location /media/ { + alias /sapl/media/; + } + + location / { + proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; + + proxy_set_header Host $http_host; + + proxy_redirect off; + + + if (!-f $request_filename) { + proxy_pass http://sapl_server; + break; + } + } + error_page 500 502 503 504 /500.html; + location = /500.html { + root /sapl/sapl/static/; + } +} diff --git a/docker-compose.yml b/docker-compose.yml index 06c9d2cec..ce5c283fb 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -1,17 +1,28 @@ -localhost: +server: + image: nginx:latest + ports: + - "80:80" + volumes: + - ./config/nginx:/etc/nginx/conf.d + volumes_from: + - web + links: + - web +sapldb: image: postgres environment: POSTGRES_PASSWORD: sapl POSTGRES_USER: sapl POSTGRES_DB: sapl + PGDATA : /var/lib/postgresql/data/ + volumes: + - ./postgres-data/data/:/var/lib/postgresql/data/ ports: - "5532:5432" web: build: . - command: bash -c "python3 manage.py bower install && python3 manage.py migrate && python3 manage.py runserver 0.0.0.0:8000" + command: /bin/sh -c "/bin/sh busy-wait.sh && python3 manage.py bower install && python3 manage.py migrate && /bin/sh gunicorn_start.sh" volumes: - .:/sapl - ports: - - "8000:8000" links: - - localhost + - sapldb diff --git a/gunicorn_start.sh b/gunicorn_start.sh index 904d76a3a..aebd45c83 100755 --- a/gunicorn_start.sh +++ b/gunicorn_start.sh @@ -3,23 +3,17 @@ # As seen in http://tutos.readthedocs.org/en/latest/source/ndg.html NAME="SAPL" # Name of the application (*) -DJANGODIR=/var/interlegis/sapl # Django project directory (*) -SOCKFILE=/var/interlegis/sapl/run/gunicorn.sock # we will communicate using this unix socket (*) +DJANGODIR=/sapl/ # Django project directory (*) +SOCKFILE=/sapl/run/gunicorn.sock # we will communicate using this unix socket (*) USER=`whoami` # the user to run as (*) GROUP=`whoami` # the group to run as (*) -NUM_WORKERS=9 # how many worker processes should Gunicorn spawn (*) +NUM_WORKERS=3 # how many worker processes should Gunicorn spawn (*) # NUM_WORKERS = 2 * CPUS + 1 DJANGO_SETTINGS_MODULE=sapl.settings # which settings file should Django use (*) DJANGO_WSGI_MODULE=sapl.wsgi # WSGI module name (*) echo "Starting $NAME as `whoami`" -# Activate the virtual environment -cd $DJANGODIR -source /var/interlegis/.virtualenvs/sapl/bin/activate -export DJANGO_SETTINGS_MODULE=$DJANGO_SETTINGS_MODULE -export PYTHONPATH=$DJANGODIR:$PYTHONPATH - # Create the run directory if it doesn't exist RUNDIR=$(dirname $SOCKFILE) test -d $RUNDIR || mkdir -p $RUNDIR diff --git a/sapl/.env_test b/sapl/.env_test index 5c2c60ec1..9416d402d 100644 --- a/sapl/.env_test +++ b/sapl/.env_test @@ -1,4 +1,4 @@ -DATABASE_URL = postgresql://postgres:@localhost:/sapl +DATABASE_URL = postgresql://postgres:@sapldb:/sapl SECRET_KEY=TravisTest DEBUG=False EMAIL_USE_TLS = True diff --git a/scripts_docker/shell_sapl.sh b/scripts_docker/shell_sapl.sh index cfddefcdc..4c993a61d 100755 --- a/scripts_docker/shell_sapl.sh +++ b/scripts_docker/shell_sapl.sh @@ -1,2 +1,3 @@ #!/bin/bash -docker run -ti sapl_web /bin/bash + +docker run --rm -ti sapl_web /bin/sh