Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added support for issue #22 #23

Merged
merged 3 commits into from
Oct 28, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .envs/.django
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
DB_HOST=db
DB_PORT=5432
DB_PASSWORD=password
DB_USER=user
DATABASE=django_db
5 changes: 5 additions & 0 deletions .envs/.postgres
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
POSTGRES_HOST=db
POSTGRES_PORT=5432
POSTGRES_USER=user
POSTGRES_PASSWORD=password
POSTGRES_DATABASE=django_db
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,5 @@ src/mydjango/__pycache__/settings.cpython-35.pyc
*/static/
.DS_Store
.env
**/.DS_Store
**/.DS_Store
src/mydjango/local_settings.py
18 changes: 0 additions & 18 deletions Dockerfile

This file was deleted.

58 changes: 58 additions & 0 deletions compose/django/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
# More Datascience frendly gist can be found here: https://gist.github.com/ruddra/870d7a51238ddfa4b50375086c12a4f5
# pull official python alpine image
FROM python:3.7-alpine

# Set Environment Variable
ENV PYTHONUNBUFFERED 1
ENV C_FORCE_ROOT true

# Making source and static directory
RUN mkdir /src
RUN mkdir /static

# Creating Work Directory
WORKDIR /src

# Adding mandatory packages to docker
RUN apk update && apk add --no-cache \
postgresql \
zlib \
jpeg
# un-comment the following two dependecies if you want to add library like pandas, scipy and numpy
# openblas \
# libstdc++

# Installing temporary packages required for installing requirements.pip
RUN apk add --no-cache --virtual build-deps \
gcc \
python3-dev \
musl-dev \
postgresql-dev\
zlib-dev \
jpeg-dev
# un-comment if you want to install numpy, pandas, scipy etc and their supported dependencies
# g++ \
# openblas-dev \
# cmake \
# && ln -s /usr/include/locale.h /usr/include/xlocale.h

# Update pip
RUN pip install --upgrade pip

# **if you want to install scipy uncomment the following file**
# RUN pip3 install --no-cache-dir --disable-pip-version-check scipy==1.3.1

# Installing requirements.pip from project
COPY ./src/requirements.pip /scripts/
RUN pip install --no-cache-dir -r /scripts/requirements.pip

# *install psycopg2 if you don't have it requirements.pip*
# RUN pip install --no-cache-dir psycopg2

# removing temporary packages from docker and removing cache
RUN apk del build-deps && \
find -type d -name __pycache__ -prune -exec rm -rf {} \; && \
rm -rf ~/.cache/pip

# CMD will run when this dockerfile is running
CMD ["sh", "-c", "python manage.py collectstatic --no-input; python manage.py migrate; gunicorn mydjango.wsgi -b 0.0.0.0:8000 & celery worker --app=myapp.tasks"]
5 changes: 5 additions & 0 deletions compose/postgres/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
FROM postgres:11-alpine

COPY ./compose/postgres/maintenance /usr/local/bin/maintenance
RUN chmod +x /usr/local/bin/maintenance/*
RUN mv /usr/local/bin/maintenance/* /usr/local/bin && rmdir /usr/local/bin/maintenance
5 changes: 5 additions & 0 deletions compose/postgres/maintenance/_sourced/constants.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#!/usr/bin/env bash


BACKUP_DIR_PATH='/backups'
BACKUP_FILE_PREFIX='backup'
12 changes: 12 additions & 0 deletions compose/postgres/maintenance/_sourced/countdown.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#!/usr/bin/env bash


countdown() {
declare desc="A simple countdown. Source: https://superuser.com/a/611582"
local seconds="${1}"
local d=$(($(date +%s) + "${seconds}"))
while [ "$d" -ge `date +%s` ]; do
echo -ne "$(date -u --date @$(($d - `date +%s`)) +%H:%M:%S)\r";
sleep 0.1
done
}
41 changes: 41 additions & 0 deletions compose/postgres/maintenance/_sourced/messages.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
#!/usr/bin/env bash


message_newline() {
echo
}

message_debug()
{
echo -e "DEBUG: ${@}"
}

message_welcome()
{
echo -e "\e[1m${@}\e[0m"
}

message_warning()
{
echo -e "\e[33mWARNING\e[0m: ${@}"
}

message_error()
{
echo -e "\e[31mERROR\e[0m: ${@}"
}

message_info()
{
echo -e "\e[37mINFO\e[0m: ${@}"
}

message_suggestion()
{
echo -e "\e[33mSUGGESTION\e[0m: ${@}"
}

message_success()
{
echo -e "\e[32mSUCCESS\e[0m: ${@}"
}
16 changes: 16 additions & 0 deletions compose/postgres/maintenance/_sourced/yes_no.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#!/usr/bin/env bash


yes_no() {
declare desc="Prompt for confirmation. \$\"\{1\}\": confirmation message."
local arg1="${1}"

local response=
read -r -p "${arg1} (y/[n])? " response
if [[ "${response}" =~ ^[Yy]$ ]]
then
exit 0
else
exit 1
fi
}
38 changes: 38 additions & 0 deletions compose/postgres/maintenance/backup
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
#!/usr/bin/env bash


### Create a database backup.
###
### Usage:
### $ docker-compose -f <environment>.yml (exec |run --rm) postgres backup


set -o errexit
set -o pipefail
set -o nounset


working_dir="$(dirname ${0})"
source "${working_dir}/_sourced/constants.sh"
source "${working_dir}/_sourced/messages.sh"


message_welcome "Backing up the '${POSTGRES_DB}' database..."


if [[ "${POSTGRES_USER}" == "postgres" ]]; then
message_error "Backing up as 'postgres' user is not supported. Assign 'POSTGRES_USER' env with another one and try again."
exit 1
fi

export PGHOST="${POSTGRES_HOST}"
export PGPORT="${POSTGRES_PORT}"
export PGUSER="${POSTGRES_USER}"
export PGPASSWORD="${POSTGRES_PASSWORD}"
export PGDATABASE="${POSTGRES_DB}"

backup_filename="${BACKUP_FILE_PREFIX}_$(date +'%Y_%m_%dT%H_%M_%S').sql.gz"
pg_dump | gzip > "${BACKUP_DIR_PATH}/${backup_filename}"


message_success "'${POSTGRES_DB}' database backup '${backup_filename}' has been created and placed in '${BACKUP_DIR_PATH}'."
22 changes: 22 additions & 0 deletions compose/postgres/maintenance/backups
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
#!/usr/bin/env bash


### View backups.
###
### Usage:
### $ docker-compose -f <environment>.yml (exec |run --rm) postgres backups


set -o errexit
set -o pipefail
set -o nounset


working_dir="$(dirname ${0})"
source "${working_dir}/_sourced/constants.sh"
source "${working_dir}/_sourced/messages.sh"


message_welcome "These are the backups you have got:"

ls -lht "${BACKUP_DIR_PATH}"
55 changes: 55 additions & 0 deletions compose/postgres/maintenance/restore
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
#!/usr/bin/env bash


### Restore database from a backup.
###
### Parameters:
### <1> filename of an existing backup.
###
### Usage:
### $ docker-compose -f <environment>.yml (exec |run --rm) postgres restore <1>


set -o errexit
set -o pipefail
set -o nounset


working_dir="$(dirname ${0})"
source "${working_dir}/_sourced/constants.sh"
source "${working_dir}/_sourced/messages.sh"


if [[ -z ${1+x} ]]; then
message_error "Backup filename is not specified yet it is a required parameter. Make sure you provide one and try again."
exit 1
fi
backup_filename="${BACKUP_DIR_PATH}/${1}"
if [[ ! -f "${backup_filename}" ]]; then
message_error "No backup with the specified filename found. Check out the 'backups' maintenance script output to see if there is one and try again."
exit 1
fi

message_welcome "Restoring the '${POSTGRES_DB}' database from the '${backup_filename}' backup..."

if [[ "${POSTGRES_USER}" == "postgres" ]]; then
message_error "Restoring as 'postgres' user is not supported. Assign 'POSTGRES_USER' env with another one and try again."
exit 1
fi

export PGHOST="${POSTGRES_HOST}"
export PGPORT="${POSTGRES_PORT}"
export PGUSER="${POSTGRES_USER}"
export PGPASSWORD="${POSTGRES_PASSWORD}"
export PGDATABASE="${POSTGRES_DB}"

message_info "Dropping the database..."
dropdb "${PGDATABASE}"

message_info "Creating a new database..."
createdb --owner="${POSTGRES_USER}"

message_info "Applying the backup to the new database..."
gunzip -c "${backup_filename}" | psql "${POSTGRES_DB}"

message_success "The '${POSTGRES_DB}' database has been restored from the '${backup_filename}' backup."
2 changes: 2 additions & 0 deletions config/nginx/mydjango.conf
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
client_max_body_size 10M;

upstream web {
ip_hash;
server web:8000;
Expand Down
29 changes: 24 additions & 5 deletions docker-compose.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
version: '2'
services:
version: "3"

volumes:
local_postgres_data: {}
local_postgres_data_backups: {}

services:
nginx:
image: nginx:alpine
container_name: nz01
Expand All @@ -10,8 +15,11 @@ services:
- ./config/nginx:/etc/nginx/conf.d
depends_on:
- web

web:
build: .
build:
context: .
dockerfile: compose/django/Dockerfile
container_name: dz01
depends_on:
- db
Expand All @@ -21,11 +29,22 @@ services:
- "8000"
links:
- redis
env_file:
- ./.envs/.django

db:
image: postgres:alpine
build:
context: .
dockerfile: compose/postgres/Dockerfile
container_name: pz01
env_file:
- ./.envs/.postgres
volumes:
- local_postgres_data:/var/lib/postgresql/data
- local_postgres_data_backups:/backups

redis:
image: redis:alpine
container_name: rz01
ports:
- '6379:6379'
- "6379:6379"
Loading