From b77b6331d52c33ab6e5d696b755d2bc1f425e3df Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sybren=20A=2E=20St=C3=BCvel?= Date: Tue, 7 Aug 2018 16:42:11 +0200 Subject: [PATCH] Start of dockerisation + running on uWSGI, not complete yet --- Dockerfile | 16 ---------- docker/Dockerfile | 59 +++++++++++++++++++++++++++++++++++ docker/build_docker_img.sh | 64 ++++++++++++++++++++++++++++++++++++++ docker/entrypoint.sh | 5 +++ docker/pg_hba.conf | 15 +++++++++ docker/uwsgi/mydata.ini | 12 +++++++ 6 files changed, 155 insertions(+), 16 deletions(-) delete mode 100644 Dockerfile create mode 100644 docker/Dockerfile create mode 100755 docker/build_docker_img.sh create mode 100755 docker/entrypoint.sh create mode 100644 docker/pg_hba.conf create mode 100644 docker/uwsgi/mydata.ini diff --git a/Dockerfile b/Dockerfile deleted file mode 100644 index dae6dc3..0000000 --- a/Dockerfile +++ /dev/null @@ -1,16 +0,0 @@ -FROM nikolaik/python-nodejs:latest - -ENV PYTHONUNBUFFERED 1 -RUN pip3 install pipenv - -RUN mkdir /code -WORKDIR /code -ADD . /code/ -RUN pipenv install --deploy --system - -RUN python manage.py collectstatic --noinput - -RUN npm install --ignore-optional -RUN npm run build - -VOLUME /code/assets \ No newline at end of file diff --git a/docker/Dockerfile b/docker/Dockerfile new file mode 100644 index 0000000..e1acb7a --- /dev/null +++ b/docker/Dockerfile @@ -0,0 +1,59 @@ +FROM ubuntu:18.04 + +RUN set -ex; \ + apt-get update; \ + DEBIAN_FRONTEND=noninteractive apt-get install -y --no-install-recommends \ + python3 python3-pip uwsgi uwsgi-plugin-python3 nginx postgresql-10 postgresql-client-10; \ + rm -rf /var/lib/apt/lists/*; \ + locale-gen en_US.UTF-8; + +# Configure Postgresql +RUN set -ex; \ + pg_conftool set lc_messages 'en_US.UTF-8'; \ + pg_conftool set lc_monetary 'en_US.UTF-8'; \ + pg_conftool set lc_numeric 'en_US.UTF-8'; \ + pg_conftool set lc_time 'en_US.UTF-8'; \ + pg_conftool set listen_addresses ''; +COPY pg_hba.conf /etc/postgresql/10/main + +ENV LANG en_US.UTF-8 +VOLUME /var/lib/postgresql/10/main + +# Create users and a group for the Django apps. +# Their home dir does not contain the web files; they are in /var/www/{appname} +RUN set -ex; \ + groupadd django; \ + useradd -g django --no-user-group -m -d /home/mydata mydata; \ + useradd -g django --no-user-group -m -d /home/opendata opendata + +# Configure uWSGI +COPY uwsgi/mydata.ini /etc/uwsgi/apps-available +RUN set -ex; \ + cd /etc/uwsgi/apps-enabled; \ + ln -s ../apps-available/*.ini . + +# Set up the virtualenvs +RUN pip3 install pipenv +# This creates the Virtualenv inside {project}/.venv +ENV PIPENV_VENV_IN_PROJECT=1 +COPY deploy/ /var/www/ +WORKDIR /var/www/mydata +RUN pipenv install --deploy +# WORKDIR /var/www/opendata +# RUN pipenv install --deploy + +# RUN python manage.py collectstatic --noinput +# +# RUN npm install --ignore-optional +# RUN npm run build +# +# VOLUME /code/assets + + +VOLUME /var/log + +COPY entrypoint.sh / +# ENTRYPOINT ./entrypoint.sh + +EXPOSE 80 +WORKDIR / diff --git a/docker/build_docker_img.sh b/docker/build_docker_img.sh new file mode 100755 index 0000000..4174b01 --- /dev/null +++ b/docker/build_docker_img.sh @@ -0,0 +1,64 @@ +#!/bin/bash -e + +DEPLOY_BRANCH=${DEPLOY_BRANCH:-production} + +# macOS does not support readlink -f, so we use greadlink instead +if [[ `uname` == 'Darwin' ]]; then + command -v greadlink 2>/dev/null 2>&1 || { echo >&2 "Install greadlink using brew."; exit 1; } + readlink='greadlink' +else + readlink='readlink' +fi + +ROOT="$(dirname "$(dirname "$($readlink -f "$0")")")" +DEPLOYDIR="$ROOT/docker/deploy" +PROJECT_NAME="mydata" + +if [ -e "$DEPLOYDIR" ]; then + echo "$DEPLOYDIR already exists, press [ENTER] to DESTROY AND COPY, Ctrl+C to abort." + read dummy + rm -rf "$DEPLOYDIR" +else + echo -n "Deploying to $DEPLOYDIR… " + echo "press [ENTER] to continue, Ctrl+C to abort." + read dummy +fi + +cd ${ROOT} +mkdir -p $DEPLOYDIR +REMOTE_ROOT="$DEPLOYDIR/$PROJECT_NAME" + +# Check that production branch has been pushed. +if [ -n "$(git log origin/$DEPLOY_BRANCH..$DEPLOY_BRANCH --oneline)" ]; then + echo "WARNING: not all changes to the $DEPLOY_BRANCH branch have been pushed." + echo "Press [ENTER] to continue deploying current origin/$DEPLOY_BRANCH, CTRL+C to abort." + read dummy +fi + +function git_clone() { + PROJECT_NAME="$1" + URL="$2" + BRANCH="$DEPLOY_BRANCH" + + set -e + echo "===================================================================" + echo "CLONING REPO ON $PROJECT_NAME @$BRANCH" + git -C $DEPLOYDIR clone --depth 1 --branch $BRANCH $URL $PROJECT_NAME +} + +git_clone mydata git@gitlab.com:armadillica/blender-my-data.git +git_clone opendata git@gitlab.com:armadillica/blender-open-data.git + +# Gulp everywhere +GULP=$ROOT/node_modules/.bin/gulp +if [ ! -e $GULP -o gulpfile.js -nt $GULP ]; then + npm install + touch $GULP # installer doesn't always touch this after a build, so we do. +fi +$GULP --cwd $DEPLOYDIR/mydata --production +$GULP --cwd $DEPLOYDIR/opendata --production + +echo +echo "===================================================================" +echo "Deploy of ${PROJECT_NAME} is ready for dockerisation." +echo "===================================================================" diff --git a/docker/entrypoint.sh b/docker/entrypoint.sh new file mode 100755 index 0000000..5ba6dfd --- /dev/null +++ b/docker/entrypoint.sh @@ -0,0 +1,5 @@ +#!/bin/sh -ex + +# Start our daemons +pg_ctlcluster 10 main start +nginx diff --git a/docker/pg_hba.conf b/docker/pg_hba.conf new file mode 100644 index 0000000..efc0ebb --- /dev/null +++ b/docker/pg_hba.conf @@ -0,0 +1,15 @@ +# DO NOT DISABLE! +# If you change this first entry you will need to make sure that the +# database superuser can access the database using some other method. +# Noninteractive access to all databases is required during automatic +# maintenance (custom daily cronjobs, replication, and similar tasks). +# +# Database administrative login by Unix domain socket +local all postgres peer + +# TYPE DATABASE USER ADDRESS METHOD +# "local" is for Unix domain socket connections only. + +# Allow Django to connect via Unix domain sockets the same way as +# usually is done via TCP/IP sockets (so 'md5' instead of 'peer') +local all all md5 diff --git a/docker/uwsgi/mydata.ini b/docker/uwsgi/mydata.ini new file mode 100644 index 0000000..2db1202 --- /dev/null +++ b/docker/uwsgi/mydata.ini @@ -0,0 +1,12 @@ +[uwsgi] +vhost = true +plugins = python3 +master = true +enable-threads = true +processes = 1 +socket = /var/www/mydata/uwsgi.sock +virtualenv = /var/www/mydata/.venv/ +chdir = /var/www/mydata +wsgi-file = /var/www/mydata/mydata/wsgi.py +uid = mydata +gid = django