From 8456416954b49ed3fe4f0a7016e80224ceda708c Mon Sep 17 00:00:00 2001 From: Adarsh Murthy Date: Wed, 29 Mar 2017 16:12:21 -0400 Subject: [PATCH 1/7] Changed location of static files and ignore static files --- .gitignore | 4 ++++ lms/settings.py | 2 +- 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/.gitignore b/.gitignore index 8310350..cf67637 100644 --- a/.gitignore +++ b/.gitignore @@ -15,3 +15,7 @@ node_modules # misc .idea + +# static files +static + diff --git a/lms/settings.py b/lms/settings.py index 44ad0f4..8aa8c0a 100644 --- a/lms/settings.py +++ b/lms/settings.py @@ -126,4 +126,4 @@ STATIC_URL = config('STATIC_URL') STATICFILES_DIRS = [ os.path.join(BASE_DIR, 'lms/static'), ] -STATIC_ROOT = os.path.join(BASE_DIR, 'staticfiles') +STATIC_ROOT = os.path.join(BASE_DIR, 'static') -- GitLab From d4b8dea4162626c7e4bf6fafdbe0aa252688c5b7 Mon Sep 17 00:00:00 2001 From: Adarsh Murthy Date: Wed, 29 Mar 2017 16:14:49 -0400 Subject: [PATCH 2/7] Add elastic beanstalk deployment configuration and Dockerfile --- .elasticbeanstalk/config.yml | 12 ++++++ Dockerfile | 76 ++++++++++++++++++++++++++++++++++++ config/nginx.conf | 34 ++++++++++++++++ config/supervisor.conf | 5 +++ config/uwsgi.ini | 22 +++++++++++ 5 files changed, 149 insertions(+) create mode 100644 .elasticbeanstalk/config.yml create mode 100644 Dockerfile create mode 100644 config/nginx.conf create mode 100644 config/supervisor.conf create mode 100644 config/uwsgi.ini diff --git a/.elasticbeanstalk/config.yml b/.elasticbeanstalk/config.yml new file mode 100644 index 0000000..cd558c9 --- /dev/null +++ b/.elasticbeanstalk/config.yml @@ -0,0 +1,12 @@ +branch-defaults: + $GIT_BRANCH: + environment: $EB_ENV +global: + application_name: $EB_APP + default_ec2_keyname: EastCoastBPKey + default_platform: 64bit Amazon Linux 2015.09 v2.0.8 running Docker 1.9.1 + default_region: us-east-1 + profile: null + sc: git +deploy: + artifact: deployment.zip diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..d1c83e2 --- /dev/null +++ b/Dockerfile @@ -0,0 +1,76 @@ +# Dockerfile + +# FROM directive instructing base image to build upon +FROM ubuntu:16.10 + +ENV CODEROOT=/home/docker/code + +# Log enironment variables. +ENV NGINXERR=/var/log/nginx.err.log +ENV NGINXREQ=/var/log/nginx.req.log +ENV UWSGIERR=/var/log/uwsgi.err.log +ENV UWSGIREQ=/var/log/uwsgi.req.log + +# Custom environment variables. These change from project to project. +ARG DOMAIN +ENV DOMAIN=${DOMAIN} +ARG NUM_PROCESSES +ENV NUM_PROCESSES=${NUM_PROCESSES} +ARG NUM_THREADS +ENV NUM_THREADS=${NUM_THREADS} + +# Add the nginx reposoitory. We need 1.8 in order to support adding CORS headers to error responses. +RUN apt-get update +RUN apt-get install -y --no-install-recommends software-properties-common +RUN add-apt-repository ppa:nginx/stable + +# Install dependencies. +RUN apt-get update +RUN apt-get install -y --no-install-recommends python3 python3-software-properties python3-dev python3-setuptools python3-pip +RUN apt-get install -y --no-install-recommends nginx supervisor +RUN apt-get install -y --no-install-recommends build-essential git +RUN apt-get install -y --no-install-recommends libpq-dev +RUN apt-get install -y libpcre3 libpcre3-dev +RUN pip3 install uwsgi -I +RUN rm -rf /var/lib/apt/lists/* + +COPY ./requirements.txt $CODEROOT/requirements.txt + +# Install application requirements. +RUN pip3 install -r $CODEROOT/requirements.txt + +# Put the code somewhere. +ADD . $CODEROOT/ + +# Create the www user. +RUN useradd -ms /bin/bash www + +# Create the log files. +RUN \ + touch $NGINXERR && touch $NGINXREQ && \ + touch $UWSGIERR && touch $UWSGIREQ + +RUN \ + replace_vars() { \ + echo "Copying environment variables from $1 to $2."; \ + sed -e "s/\$DOMAIN/$DOMAIN/g" \ + -e "s@\$CODEROOT@$CODEROOT@g" \ + -e "s@\$NUM_PROCESSES@$NUM_PROCESSES@g" \ + -e "s@\$NUM_THREADS@$NUM_THREADS@g" \ + -e "s@\$NGINXERR@$NGINXERR@g" \ + -e "s@\$NGINXREQ@$NGINXREQ@g" \ + -e "s@\$UWSGIERR@$UWSGIERR@g" \ + -e "s@\$UWSGIREQ@$UWSGIREQ@g" $1 > $2; \ + }; \ + replace_vars $CODEROOT/config/nginx.conf /etc/nginx/nginx.conf && \ + replace_vars $CODEROOT/config/supervisor.conf /etc/supervisor/conf.d/supervisor.conf && \ + replace_vars $CODEROOT/config/uwsgi.ini $CODEROOT/uwsgi.ini + +# Collect static files +RUN cd $CODEROOT && python manage.py collectstatic + + +# EXPOSE port 80 to allow communication to/from server +EXPOSE 80 + +CMD ["supervisord", "-n"] diff --git a/config/nginx.conf b/config/nginx.conf new file mode 100644 index 0000000..cdf6e0e --- /dev/null +++ b/config/nginx.conf @@ -0,0 +1,34 @@ +daemon off; +user www; + +events { + worker_connections 1024; +} + +http { + include mime.types; + default_type application/octet-stream; + sendfile on; + keepalive_timeout 65; + gzip on; + charset utf-8; + client_max_body_size 10M; + + access_log $NGINXREQ; + error_log $NGINXERR; + + server { + listen 80; + listen [::]:80; + server_name $DOMAIN; + + location / { + include uwsgi_params; + uwsgi_pass unix:$CODEROOT/uwsgi.sock; + } + location /static/ { + autoindex on; + root $CODEROOT; + } + } +} diff --git a/config/supervisor.conf b/config/supervisor.conf new file mode 100644 index 0000000..477f24b --- /dev/null +++ b/config/supervisor.conf @@ -0,0 +1,5 @@ +[program:uwsgi] +command = /usr/local/bin/uwsgi --ini $CODEROOT/uwsgi.ini -O 2 + +[program:nginx] +command = /usr/sbin/nginx diff --git a/config/uwsgi.ini b/config/uwsgi.ini new file mode 100644 index 0000000..e42dab4 --- /dev/null +++ b/config/uwsgi.ini @@ -0,0 +1,22 @@ +[uwsgi] +# This is used if no configuration is specified. +ini = :base + +socket = $CODEROOT/uwsgi.sock +# Nginx should be able to find this socket. +chmod-socket = 666 + +master = true +# Number of processes and workers should be determined at build time +# Should be based on the type of instance that it is running on +processes = $NUM_PROCESSES +threads = $NUM_THREADS + +# Set loggers. +req-logger = file:$UWSGIREQ +logger = file:$UWSGIERR + +[base] +# cd into the app directory +chdir = $CODEROOT/ +module = lms.wsgi:application -- GitLab From b963c0ecf4df0b815a9c40d7d88f4f60bb93ef0a Mon Sep 17 00:00:00 2001 From: Adarsh Murthy Date: Thu, 30 Mar 2017 11:36:14 -0400 Subject: [PATCH 3/7] Fix Dockerfile for build. Generalize environment file --- Dockerfile | 5 ++--- env.default | 16 ++++++++-------- 2 files changed, 10 insertions(+), 11 deletions(-) diff --git a/Dockerfile b/Dockerfile index d1c83e2..08469d1 100644 --- a/Dockerfile +++ b/Dockerfile @@ -66,9 +66,8 @@ RUN \ replace_vars $CODEROOT/config/supervisor.conf /etc/supervisor/conf.d/supervisor.conf && \ replace_vars $CODEROOT/config/uwsgi.ini $CODEROOT/uwsgi.ini -# Collect static files -RUN cd $CODEROOT && python manage.py collectstatic - +# Collect static files +RUN cd $CODEROOT && python3 manage.py collectstatic --noinput # EXPOSE port 80 to allow communication to/from server EXPOSE 80 diff --git a/env.default b/env.default index f898f9e..c6fe427 100644 --- a/env.default +++ b/env.default @@ -1,9 +1,9 @@ -SECRET_KEY= -DEBUG=True -ALLOWED_HOSTS=127.0.0.1,localhost +SECRET_KEY=$SECRET_KEY +DEBUG=$DEBUG +ALLOWED_HOSTS=$ALLOWED_HOSTS STATIC_URL='/static/' -DB_NAME=lms -DB_USER= -DB_PASSWORD= -DB_HOST=localhost -DB_PORT=5432 +DB_NAME=$DB_NAME +DB_USER=$DB_USER +DB_PASSWORD=$DB_PASSWORD +DB_HOST=$DB_HOST +DB_PORT=$DB_PORT -- GitLab From 9d0a5e0d340f0e40c534794676a535289cee837b Mon Sep 17 00:00:00 2001 From: Adarsh Murthy Date: Thu, 30 Mar 2017 12:08:10 -0400 Subject: [PATCH 4/7] Comment out collecting static files from lms static --- lms/settings.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/lms/settings.py b/lms/settings.py index 8aa8c0a..98f7fa7 100644 --- a/lms/settings.py +++ b/lms/settings.py @@ -123,7 +123,7 @@ USE_TZ = True # https://docs.djangoproject.com/en/1.10/howto/static-files/ STATIC_URL = config('STATIC_URL') -STATICFILES_DIRS = [ - os.path.join(BASE_DIR, 'lms/static'), -] +# STATICFILES_DIRS = [ +# os.path.join(BASE_DIR, 'lms/static'), +# ] STATIC_ROOT = os.path.join(BASE_DIR, 'static') -- GitLab From 4c3cc7d26d1e075fe2de061717ca5790b4a7fd63 Mon Sep 17 00:00:00 2001 From: Adarsh Murthy Date: Thu, 30 Mar 2017 12:30:40 -0400 Subject: [PATCH 5/7] Make migrations in docker. Include Docker.aws.json --- Dockerfile | 3 +++ Dockerrun.aws.json | 16 ++++++++++++++++ 2 files changed, 19 insertions(+) create mode 100644 Dockerrun.aws.json diff --git a/Dockerfile b/Dockerfile index 08469d1..5fd4898 100644 --- a/Dockerfile +++ b/Dockerfile @@ -66,6 +66,9 @@ RUN \ replace_vars $CODEROOT/config/supervisor.conf /etc/supervisor/conf.d/supervisor.conf && \ replace_vars $CODEROOT/config/uwsgi.ini $CODEROOT/uwsgi.ini +# Make migrations +RUN python3 manage.py migrate --noinput + # Collect static files RUN cd $CODEROOT && python3 manage.py collectstatic --noinput diff --git a/Dockerrun.aws.json b/Dockerrun.aws.json new file mode 100644 index 0000000..d334d79 --- /dev/null +++ b/Dockerrun.aws.json @@ -0,0 +1,16 @@ +{ + "AWSEBDockerrunVersion": "1", + "Authentication": { + "Bucket": "dockerauth.blocpower.org", + "Key": "$DOCKER_REPO.json" + }, + "Image": { + "Name": "blocp/$DOCKER_REPO" + }, + "Ports": [ + { + "HostPort": "80", + "ContainerPort": "80" + } + ] +} -- GitLab From 8ea4443a0b604463365e127f0ae19441458c6761 Mon Sep 17 00:00:00 2001 From: Adarsh Murthy Date: Thu, 30 Mar 2017 12:32:34 -0400 Subject: [PATCH 6/7] Fix Dockerfile for build --- Dockerfile | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Dockerfile b/Dockerfile index 5fd4898..d8d1b6b 100644 --- a/Dockerfile +++ b/Dockerfile @@ -67,7 +67,7 @@ RUN \ replace_vars $CODEROOT/config/uwsgi.ini $CODEROOT/uwsgi.ini # Make migrations -RUN python3 manage.py migrate --noinput +RUN cd $CODEROOT && python3 manage.py migrate --noinput # Collect static files RUN cd $CODEROOT && python3 manage.py collectstatic --noinput -- GitLab From 04f15c819bbf5f329d2ed1310c561d3a0f1ec20b Mon Sep 17 00:00:00 2001 From: Adarsh Murthy Date: Fri, 31 Mar 2017 14:08:49 -0400 Subject: [PATCH 7/7] Rename env.default to .env.default --- env.default => .env.default | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename env.default => .env.default (100%) diff --git a/env.default b/.env.default similarity index 100% rename from env.default rename to .env.default -- GitLab