Skip to content

Commit e1f1957

Browse files
committed
Initial commit.
0 parents  commit e1f1957

37 files changed

+1331
-0
lines changed

Diff for: .gitignore

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
**/*.eggs/
2+
**/*.pyc
3+
**/*.pyo

Diff for: MANIFEST.in

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
recursive-include ixc_django_docker *

Diff for: README.rst

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
Overview
2+
--------
3+
4+
A collection of scripts and config files that make it easier to run Django
5+
projects consistently with and without Docker (for Mac/Windows, Cloud, etc.)
6+
7+
It includes:
8+
9+
* Entrypoint, setup, and program wrapper scripts. See ``help.sh``.
10+
11+
* A Django project with safe default settings and hooks to integrate context
12+
processors, settings, static files, templates, URLs, etc., from your
13+
project.
14+
15+
* Supervisord config that runs Gunicorn behind a buffering proxy (Nginx) and
16+
logs to stderr/stdout.
17+
18+
Django project
19+
--------------
20+
21+
* Adds an ``environment`` context processor that returns settings referenced in
22+
the ``CONTEXT_PROCESSOR_SETTINGS`` setting.
23+
24+
* Adds ``static`` and ``url`` functions to the Jinja2 environment, that
25+
correspond with the built in Django template tags of the same names.
26+
27+
* Has project hooks for context processors, settings, static files, templates,
28+
etc.

Diff for: go.sh

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
#!/bin/bash
2+
3+
# Configure the environment so we can run `entrypoint.sh` and other scripts.
4+
5+
cat <<EOF
6+
# `whoami`@`hostname`:$PWD$ go.sh $@
7+
EOF
8+
9+
set -e
10+
11+
# Get absolute project directory from the location of this script.
12+
# See: http://stackoverflow.com/a/4774063
13+
export PROJECT_DIR=$(cd $(dirname "${BASH_SOURCE[0]}"); pwd -P)
14+
15+
# Set location of virtualenv.
16+
export PROJECT_VENV_DIR="$PROJECT_DIR/var/go.sh-venv"
17+
18+
# Create virtualenv.
19+
if [[ ! -d "$PROJECT_VENV_DIR" ]]; then
20+
virtualenv "$PROJECT_VENV_DIR"
21+
fi
22+
23+
# Install `ixc-django-docker` package.
24+
if [[ -z $("$PROJECT_VENV_DIR/bin/python" -m pip freeze | grep ixc-django-docker) ]]; then
25+
"$PROJECT_VENV_DIR/bin/python" -m pip install -r ixc-django-docker
26+
fi
27+
28+
# Execute entrypoint and command.
29+
exec "$PROJECT_VENV_DIR/bin/entrypoint.sh" ${@:-setup-django.sh bash.sh}

Diff for: ixc_django_docker/__init__.py

Whitespace-only changes.

Diff for: ixc_django_docker/bin/bash.sh

+47
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
#!/bin/bash
2+
3+
# Run a pre-configured interactive Bash shell, with some help text.
4+
5+
cat <<EOF
6+
# `whoami`@`hostname`:$PWD$ bash.sh $@
7+
EOF
8+
9+
set -e
10+
11+
cat <<EOF
12+
13+
You are running a pre-onfigured interactive Bash shell. Here is a list of
14+
frequently used scripts you might want to run:
15+
16+
bower-install.sh <DIR>
17+
celery.sh
18+
celerybeat.sh
19+
celeryflower.sh
20+
gunicorn.sh
21+
manage.py [COMMAND [ARGS]]
22+
migrate.sh
23+
nginx.sh
24+
npm-install.sh <DIR>
25+
pip-install.sh <DIR>
26+
runserver.sh [ARGS]
27+
runtests.sh [ARGS]
28+
setup-django.sh [COMMAND]
29+
setup-git-secret.sh [COMMAND]
30+
setup-postgres.sh
31+
supervisor.sh [OPTIONS] [ACTION [ARGS]]
32+
transfer.sh <FILE>
33+
waitlock.sh <COMMAND>
34+
35+
Most of these scripts are minimal wrappers that specify configuration and
36+
provide automation or integration with Docker and various 'ixc-django-docker'
37+
components.
38+
39+
For more info on each script, run:
40+
41+
help.sh
42+
43+
EOF
44+
45+
# Run bash by default without any user customisations from rc or profile files
46+
# to reduce the chance of user customisations clashing with our paths etc.
47+
exec bash --norc --noprofile

Diff for: ixc_django_docker/bin/bower-install.sh

+37
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
#!/bin/bash
2+
3+
# Install Bower components in the given directory, if they have changed.
4+
5+
cat <<EOF
6+
# `whoami`@`hostname`:$PWD$ bower-install.sh $@
7+
EOF
8+
9+
set -e
10+
11+
DIR="${1:-$PWD}"
12+
13+
mkdir -p "$DIR"
14+
cd "$DIR"
15+
16+
if [[ ! -s bower.json ]]; then
17+
cat <<EOF > bower.json
18+
{
19+
"name": "$PROJECT_NAME",
20+
"dependencies": {
21+
},
22+
"private": true
23+
}
24+
EOF
25+
fi
26+
27+
touch bower.json.md5
28+
29+
if [[ ! -s bower.json.md5 ]] || ! md5sum --status -c bower.json.md5 > /dev/null 2>&1; then
30+
echo "Bower components in '$DIR' directory are out of date."
31+
if [[ -d bower_components ]]; then
32+
echo 'Removing old Bower components directory.'
33+
rm -rf bower_components
34+
fi
35+
bower install --allow-root
36+
md5sum bower.json > bower.json.md5
37+
fi

Diff for: ixc_django_docker/bin/celery.sh

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
#!/bin/bash
2+
3+
cat <<EOF
4+
# `whoami`@`hostname`:$PWD$ celery.sh $@
5+
EOF
6+
7+
set -e
8+
9+
# Allow Celery to run as root.
10+
export C_FORCE_ROOT=1
11+
12+
exec celery --app=ixc_django_docker worker --loglevel=INFO "$@"

Diff for: ixc_django_docker/bin/celerybeat.sh

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
#!/bin/bash
2+
3+
cat <<EOF
4+
# `whoami`@`hostname`:$PWD$ celerybeat.sh $@
5+
EOF
6+
7+
set -e
8+
9+
exec celery --app=ixc_django_docker beat --loglevel=INFO -S djcelery.schedulers.DatabaseScheduler --pidfile= "$@"

Diff for: ixc_django_docker/bin/celeryflower.sh

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
#!/bin/bash
2+
3+
cat <<EOF
4+
# `whoami`@`hostname`:$PWD$ celeryflower.sh $@
5+
EOF
6+
7+
set -e
8+
9+
exec celery --app=ixc_django_docker flower "$@"

Diff for: ixc_django_docker/bin/entrypoint.sh

+135
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,135 @@
1+
#!/bin/bash
2+
3+
# Configure the environment and execute a command.
4+
5+
cat <<EOF
6+
# `whoami`@`hostname`:$PWD$ entrypoint.sh $@
7+
EOF
8+
9+
set -e
10+
11+
if [[ -n "${DOCKER+1}" ]]; then
12+
# When run via Docker, the only system site packages are the ones that we
13+
# have installed, so we do not need a virtualenv for isolation. Using an
14+
# isolated virtualenv would mean we have to reinstall everything during
15+
# development, even when no versions have changed. Using a virtualenv
16+
# created with `--system-site-packages` would mean we can avoid
17+
# reinstalling everything, but pip would try to uninstall existing packages
18+
# when we try to install a new version, which can fail with permissions
19+
# errors (e.g. when running as an unprivileged user or when the image is
20+
# read-only). The alternate installation user scheme avoids these problems
21+
# by ignoring existing system site packages when installing a new version,
22+
# instead of trying to uninstall them.
23+
# See: https://pip.pypa.io/en/stable/user_guide/#user-installs
24+
25+
# Make `pip install --user` the default, to ensure we always install into
26+
# the userbase directory.
27+
pip() {
28+
if [[ "$1" == install ]]; then
29+
shift
30+
set -- install --user "$@"
31+
fi
32+
command pip "$@"
33+
}
34+
export -f pip
35+
36+
# Set location of userbase directory.
37+
export PYTHONUSERBASE="$PROJECT_DIR/var/docker-pythonuserbase"
38+
39+
# Add userbase bin directory to PATH.
40+
export PATH="$PYTHONUSERBASE/bin:$PATH"
41+
42+
# For some reason pip allows us to install sdist packages, but not editable
43+
# packages, when this directory doesn't exist. So make sure it does.
44+
mkdir -p "$PYTHONUSERBASE/lib/python2.7/site-packages"
45+
else
46+
# When run via 'go.sh', we need a virtualenv for isolation from system site
47+
# packages, and we also verify that required environment variables and
48+
# programs are available.
49+
50+
# Fail loudly when required environment variables are missing.
51+
for var in PROJECT_DIR PROJECT_VENV_DIR; do
52+
eval [[ -z \${$var+1} ]] && {
53+
>&2 echo "ERROR: Missing environment variable: $var"
54+
exit 1
55+
}
56+
done
57+
58+
# Fail loudly when required programs are missing.
59+
for cmd in md5sum nginx npm psql python pv redis-server; do # elasticsearch
60+
hash $cmd 2>/dev/null || {
61+
>&2 echo "ERROR: Missing program: $cmd"
62+
>&2 echo 'See: https://github.com/ic-labs/django-icekit/blob/develop/docs/intro/manual-setup.md'
63+
exit 1
64+
}
65+
done
66+
67+
# Add virtualenv bin directory to PATH.
68+
export PATH="$PROJECT_VENV_DIR/bin:$PATH"
69+
fi
70+
71+
# Set default base settings module.
72+
export BASE_SETTINGS_MODULE="${BASE_SETTINGS_MODULE:-develop}"
73+
74+
# Get number of CPU cores, so we know how many processes to run.
75+
export CPU_CORES=$(python -c "import multiprocessing; print(multiprocessing.cpu_count());")
76+
77+
# Set location of GPG home directory.
78+
export GNUPGHOME="$PROJECT_DIR/.gnupg"
79+
80+
# Get absolute directory for the `ixc_django_docker` package.
81+
export IXC_DJANGO_DOCKER_DIR=$(python -c "import ixc_django_docker, os; print(os.path.dirname(ixc_django_docker.__file__));")
82+
83+
# Add project and `ixc-django-docker` bin directories to PATH.
84+
export PATH="$PROJECT_DIR/bin:$IXC_DJANGO_DOCKER_DIR/bin:$PATH"
85+
86+
# Configure Pip.
87+
export PIP_DISABLE_PIP_VERSION_CHECK=on
88+
export PIP_SRC="$PROJECT_DIR/var/src"
89+
90+
# Get project name from the project directory.
91+
export PROJECT_NAME=$(basename "$PROJECT_DIR")
92+
93+
# Configure Python.
94+
export PYTHONHASHSEED=random
95+
export PYTHONWARNINGS=ignore
96+
export PYTHONPATH="$PROJECT_DIR:$PYTHONPATH"
97+
98+
# Derive 'PGDATABASE' from 'PROJECT_NAME' and git branch or
99+
# 'BASE_SETTINGS_MODULE', if not already defined.
100+
if [[ -z "$PGDATABASE" ]]; then
101+
if [[ -d .git ]]; then
102+
export PGDATABASE="${PROJECT_NAME}_$(git rev-parse --abbrev-ref HEAD | sed 's/[^0-9A-Za-z]/_/g')"
103+
echo "Derived database name '$PGDATABASE' from 'PROJECT_NAME' environment variable and git branch."
104+
elif [[ -n "$DOTENV" ]]; then
105+
export PGDATABASE="${PROJECT_NAME}_$DOTENV"
106+
echo "Derived database name '$PGDATABASE' from 'PROJECT_NAME' and 'DOTENV' environment variables."
107+
elif [[ -n "$BASE_SETTINGS_MODULE" ]]; then
108+
export PGDATABASE="${PROJECT_NAME}_$BASE_SETTINGS_MODULE"
109+
echo "Derived database name '$PGDATABASE' from 'PROJECT_NAME' and 'BASE_SETTINGS_MODULE' environment variables."
110+
else
111+
export PGDATABASE="$PROJECT_NAME"
112+
echo "Derived database name '$PGDATABASE' from 'PROJECT_NAME' environment variable."
113+
fi
114+
fi
115+
116+
# Default PostgreSQL credentials.
117+
export PGHOST="${PGHOST:-localhost}"
118+
export PGPORT="${PGPORT:-5432}"
119+
export PGUSER="${PGUSER:-$(whoami)}"
120+
121+
# Get Redis host and port.
122+
export REDIS_ADDRESS="${REDIS_ADDRESS:-localhost:6379}"
123+
124+
# Decrypt dotenv files.
125+
setup-git-secret.sh || true # Don't exit if we can't decrypt secrets
126+
127+
# Source dotenv file.
128+
set -o allexport
129+
if [[ -f "$PROJECT_DIR/.env.${DOTENV:-local}" ]]; then
130+
source "$PROJECT_DIR/.env.${DOTENV:-local}"
131+
fi
132+
set +o allexport
133+
134+
# Execute command.
135+
exec "${@:-bash.sh}"

Diff for: ixc_django_docker/bin/gunicorn.sh

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
#!/bin/bash
2+
3+
cat <<EOF
4+
# `whoami`@`hostname`:$PWD$ gunicorn.sh $@
5+
EOF
6+
7+
set -e
8+
9+
# See: http://docs.gunicorn.org/en/stable/design.html#how-many-workers
10+
let "GUNICORN_WORKERS = ${GUNICORN_WORKERS:-${CPU_CORES:-1} * 2 + 1}"
11+
12+
exec gunicorn --bind 0.0.0.0:8080 --timeout "${GUNICORN_TIMEOUT:-60}" --workers "$GUNICORN_WORKERS" "${@:-ixc_django_docker.wsgi:application}"

0 commit comments

Comments
 (0)