Skip to content

Commit

Permalink
📦(richie) add base richie packaging suite
Browse files Browse the repository at this point in the history
Following python packaging best practices, we have handcrafted a
setup.cfg file to package richie as a Django application. This was
required in order to be able to install it during our container image
builds and development.

TODO: dependencies are pinned and exhaustive (even secondary
dependencies). This will be improved in a follow-up pull request.
  • Loading branch information
jmaupetit committed Jul 2, 2018
1 parent 00b83ee commit fade56d
Show file tree
Hide file tree
Showing 11 changed files with 221 additions and 158 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@
.venv
venv

# Packaging
build

# sqlite DB
db.sqlite3
*.db
Expand Down
14 changes: 8 additions & 6 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
# Once mounted, you will need to collect static files via the eponym django
# admin command:
#
# python ./manage.py collectstatic
# python sandbox/manage.py collectstatic
#

# ---- base image to inherit from ----
Expand All @@ -20,11 +20,13 @@ FROM python:3.6-stretch as base
# ---- back-end builder image ----
FROM base as back-builder

WORKDIR /install
WORKDIR /builder

COPY requirements/base.txt /requirements.txt
COPY setup.py setup.cfg MANIFEST.in /builder/
COPY ./src /builder/src/

RUN pip install --prefix=/install -r /requirements.txt
RUN mkdir /install && \
pip install --prefix=/install .

# ---- front-end builder image ----
FROM node:9 as front-builder
Expand All @@ -47,8 +49,8 @@ COPY --from=back-builder /install /usr/local
COPY . /app/

# Copy front-end dependencies
COPY --from=front-builder /app/richie/build /app/richie/build
COPY --from=front-builder /app/richie/static /app/richie/static
COPY --from=front-builder /app/sandbox/build /app/sandbox/build
COPY --from=front-builder /app/sandbox/static /app/sandbox/static

WORKDIR /app

Expand Down
3 changes: 3 additions & 0 deletions MANIFEST.in
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
include LICENSE
include README.md
recursive-include src/richie *.html *.png *.gif *.js *.css *.jpg *.jpeg
80 changes: 41 additions & 39 deletions docker/images/alpine/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
# Once mounted, you will need to collect static files via the eponym django
# admin command:
#
# python ./manage.py collectstatic
# python sandbox/manage.py collectstatic
#

# ---- base image to inherit from ----
Expand All @@ -23,30 +23,32 @@ FROM base as back-builder
# Install development libraries required for compiled python dependencies (lxml,
# Pillow and psycopg2)
RUN apk --no-cache add --update \
# lxml dependencies
libxml2-dev \
libxslt-dev \
# Pillow dependencies
build-base \
freetype-dev \
fribidi-dev \
harfbuzz-dev \
jpeg-dev \
lcms2-dev \
openjpeg-dev \
tcl-dev \
tiff-dev \
tk-dev \
zlib-dev \
# psycopg2 dependencies
postgresql-dev && \
# lxml dependencies
libxml2-dev \
libxslt-dev \
# Pillow dependencies
build-base \
freetype-dev \
fribidi-dev \
harfbuzz-dev \
jpeg-dev \
lcms2-dev \
openjpeg-dev \
tcl-dev \
tiff-dev \
tk-dev \
zlib-dev \
# psycopg2 dependencies
postgresql-dev && \
rm -rf /var/cache/apk/*

WORKDIR /install
WORKDIR /builder

COPY requirements/base.txt /requirements.txt
COPY setup.py setup.cfg MANIFEST.in /builder/
COPY ./src /builder/src/

RUN pip install --prefix=/install -r /requirements.txt
RUN mkdir /install && \
pip install --prefix=/install .

# ---- front-end builder image ----
FROM node:9-alpine as front-builder
Expand All @@ -68,30 +70,30 @@ COPY --from=back-builder /install /usr/local
# Install only (linked) libraries required for compiled python dependencies
# (lxml, Pillow and psycopg2)
RUN apk --no-cache add --update \
# lxml dependencies
libxml2 \
libxslt \
# Pillow dependencies
freetype \
fribidi \
harfbuzz \
jpeg \
lcms2 \
openjpeg \
tcl \
tiff \
tk \
zlib \
# psycopg2 dependencies
postgresql && \
# lxml dependencies
libxml2 \
libxslt \
# Pillow dependencies
freetype \
fribidi \
harfbuzz \
jpeg \
lcms2 \
openjpeg \
tcl \
tiff \
tk \
zlib \
# psycopg2 dependencies
postgresql && \
rm -rf /var/cache/apk/*

# Copy richie application (see .dockerignore)
COPY . /app/

# Copy front-end dependencies
COPY --from=front-builder /app/richie/build /app/richie/build
COPY --from=front-builder /app/richie/static /app/richie/static
COPY --from=front-builder /app/sandbox/build /app/sandbox/build
COPY --from=front-builder /app/sandbox/static /app/sandbox/static

WORKDIR /app

Expand Down
12 changes: 6 additions & 6 deletions docker/images/alpine/dev/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,13 @@ RUN apk --no-cache add --update \
vim

# Install development dependencies
RUN pip install -r requirements/dev.txt
RUN pip install -e .[dev]

# Install dockerize. It is used to ensure that the database service is accepting
# connections before trying to access it from the main application.
ENV DOCKERIZE_VERSION v0.6.1
RUN curl -L \
--output dockerize-linux-amd64-$DOCKERIZE_VERSION.tar.gz \
https://github.com/jwilder/dockerize/releases/download/$DOCKERIZE_VERSION/dockerize-linux-amd64-$DOCKERIZE_VERSION.tar.gz && \
tar -C /usr/local/bin -xzvf dockerize-linux-amd64-$DOCKERIZE_VERSION.tar.gz && \
rm dockerize-linux-amd64-$DOCKERIZE_VERSION.tar.gz
RUN curl -sL \
--output dockerize-linux-amd64-$DOCKERIZE_VERSION.tar.gz \
https://github.com/jwilder/dockerize/releases/download/$DOCKERIZE_VERSION/dockerize-linux-amd64-$DOCKERIZE_VERSION.tar.gz && \
tar -C /usr/local/bin -xzvf dockerize-linux-amd64-$DOCKERIZE_VERSION.tar.gz && \
rm dockerize-linux-amd64-$DOCKERIZE_VERSION.tar.gz
10 changes: 5 additions & 5 deletions docker/images/dev/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -13,17 +13,17 @@ USER root:root
# Install vim
RUN apt-get update && \
apt-get install -y \
vim && \
vim && \
rm -rf /var/lib/apt/lists/*

# Install development dependencies
RUN pip install -r requirements/dev.txt
RUN pip install -e .[dev]

# Install dockerize. It is used to ensure that the database service is accepting
# connections before trying to access it from the main application.
ENV DOCKERIZE_VERSION v0.6.1
RUN curl -L \
--output dockerize-linux-amd64-$DOCKERIZE_VERSION.tar.gz \
https://github.com/jwilder/dockerize/releases/download/$DOCKERIZE_VERSION/dockerize-linux-amd64-$DOCKERIZE_VERSION.tar.gz && \
RUN curl -sL \
--output dockerize-linux-amd64-$DOCKERIZE_VERSION.tar.gz \
https://github.com/jwilder/dockerize/releases/download/$DOCKERIZE_VERSION/dockerize-linux-amd64-$DOCKERIZE_VERSION.tar.gz && \
tar -C /usr/local/bin -xzvf dockerize-linux-amd64-$DOCKERIZE_VERSION.tar.gz && \
rm dockerize-linux-amd64-$DOCKERIZE_VERSION.tar.gz
79 changes: 43 additions & 36 deletions docs/native_installation.md
Original file line number Diff line number Diff line change
@@ -1,31 +1,32 @@
# Installing Richie on your machine

This document aims to list all needed steps to have a working `Richie` installation on your laptop.

A better approach is to use [`Docker`](https://docs.docker.com) as explained in our
guide for [container-native](../README.md) instructions.
This document aims to list all needed steps to have a working `Richie`
installation on your laptop.

A better approach is to use [`Docker`](https://docs.docker.com) as explained in
our guide for [container-native](../README.md) instructions.

## Installing a fresh server

### Version

You need a `Ubuntu 18.04 Bionic Beaver` (the latest LTS version) fresh installation.

If you are using another operating system or distribution, you can use [`Vagrant`](https://docs.vagrantup.com/v2/getting-started/index.html) to get a running Ubuntu 18.04 server in
seconds.
You need a `Ubuntu 18.04 Bionic Beaver` (the latest LTS version) fresh
installation.

If you are using another operating system or distribution, you can use
[`Vagrant`](https://docs.vagrantup.com/v2/getting-started/index.html) to get a
running Ubuntu 18.04 server in seconds.

### System update

Be sure to have fresh packages on the server (kernel, libc, ssl patches...):
post

```sh
sudo apt-get -y update
sudo apt-get -y dist-upgrade
```


## Database part

You must first install `postgresql`.
Expand All @@ -42,14 +43,16 @@ brew services start postgresql@10
`Postgresql` is now running.
Then you can create the database owner and the database itself, using the `postgres` user:
Then you can create the database owner and the database itself, using the
`postgres` user:
```sh
sudo -u postgres -i // skip this on OS X as the default install will use your local user
createuser fun -sP
```
Note: we created the user as a superuser. This should only be done in dev/test environments.
Note: we created the user as a superuser. This should only be done in dev/test
environments.
Now, create the database with this user:
Expand All @@ -66,7 +69,8 @@ Download and install the Public Signing Key
$ wget -qO - https://artifacts.elastic.co/GPG-KEY-elasticsearch | sudo apt-key add -
You may need to install the apt-transport-https package on Debian before proceeding:
You may need to install the apt-transport-https package on Debian before
proceeding:
$ sudo apt-get install apt-transport-https
Expand All @@ -80,28 +84,25 @@ Update repository and install
$ sudo apt-get install elasticsearch
$ sudo /etc/init.d/elasticsearch start
### OS X
$ brew install elasticsearch
## Application part
### Python and other requirements
We use `Python 3.6` which is the one installed by default in `Ubuntu 18.04`.
You can install it on OS X using the following commands. Make sure to always run `python3` instead
of `python` and `pip3` instead of `pip` to ensure the correct version of Python (your homebrew
install of 3) is used.
You can install it on OS X using the following commands. Make sure to always run
`python3` instead of `python` and `pip3` instead of `pip` to ensure the correct
version of Python (your homebrew install of 3) is used.
```
brew install python3
brew postinstall python3
```
### The virtualenv
Place yourself in the application directory `app`:
Expand All @@ -114,53 +115,58 @@ For this, we'll install `virtualenvwrapper` and add an environment:

pip install virtualenvwrapper

You can open a new shell to activate the virtualenvwrapper commands, or simply do:
You can open a new shell to activate the virtualenvwrapper commands, or simply
do:

source $(which virtualenvwrapper.sh)

Then create the virtual environment for `richie`:

mkvirtualenv richie --no-site-packages --python=python3

The virtualenv should now be activated and you can install the Python dependencies for development:
The virtualenv should now be activated and you can install the Python
dependencies for development:

pip install -r requirements/dev.txt

The "dev.txt" requirement file installs packages specific to a dev environment and should not be
used in production.
pip install -e .[dev]

The "dev.txt" requirement file installs packages specific to a dev environment
and should not be used in production.

### Frontend build

This project is a hybrid that uses both Django generated pages and frontend JS code. As such, it
includes a frontend build process that comes in two parts: JS & CSS.
This project is a hybrid that uses both Django generated pages and frontend JS
code. As such, it includes a frontend build process that comes in two parts: JS
& CSS.

We need NPM to install the dependencies and run the build, which depends on a version of Nodejs
specified in `.nvmrc`. See [the repo](https://github.com/creationix/nvm) for instructions on how
to install NVM. To take advantage of `.nvmrc`, run this in the context of the repository:
We need NPM to install the dependencies and run the build, which depends on a
version of Nodejs specified in `.nvmrc`. See [the
repo](https://github.com/creationix/nvm) for instructions on how to install NVM.
To take advantage of `.nvmrc`, run this in the context of the repository:

nvm install
nvm use

As a prerequisite to running the frontend build for either JS or CSS, you'll need to [install yarn](https://yarnpkg.com/lang/en/docs/install/) and download dependencies _via_:
As a prerequisite to running the frontend build for either JS or CSS, you'll
need to [install yarn](https://yarnpkg.com/lang/en/docs/install/) and download
dependencies _via_:
yarn install
- JS build
npm run build
npm run build
- CSS build
This will compile all our SCSS files into one bundle and put it in the static folder we're serving.
This will compile all our SCSS files into one bundle and put it in the static
folder we're serving.

npm run sass


### Run server

Make sure your database is up-to-date before running the application the first time and after each
modification to your models:
Make sure your database is up-to-date before running the application the first
time and after each modification to your models:

python sandbox/manage.py migrate

Expand All @@ -172,6 +178,7 @@ Run the tests

python sandbox/manage.py test

You should now be able to start Django and view the site at [localhost:8000](http://localhost:8000)
You should now be able to start Django and view the site at
[localhost:8000](http://localhost:8000)

python sandbox/manage.py runserver
Loading

0 comments on commit fade56d

Please sign in to comment.