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

adds docker #980

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
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
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -56,3 +56,6 @@ exception.log

# Pycharm
.idea/
deploy.sh
DOCKER.md
traefik.toml
3 changes: 3 additions & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,6 @@
[submodule "osmtm/static/js/lib/colorselector"]
path = osmtm/static/js/lib/colorselector
url = https://github.com/flaute/bootstrap-colorselector.git
[submodule "tasking-manager-stats"]
path = tasking-manager-stats
url = https://github.com/AmericanRedCross/tasking-manager-stats
46 changes: 46 additions & 0 deletions Dockerfile.app
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
FROM python:2.7.12

RUN apt-get update \
&& apt-get install -y \
--no-install-recommends \
libgeos-dev \
software-properties-common \
python-software-properties \
transifex-client \
wget \
&& apt-get clean \
&& apt-get autoremove \
&& rm -r /var/lib/apt/lists/*

RUN add-apt-repository "deb http://apt.postgresql.org/pub/repos/apt/ jessie-pgdg main"
RUN wget --quiet -O - https://www.postgresql.org/media/keys/ACCC4CF8.asc | apt-key add -
RUN apt-get update \
&& apt-get install -y \
--no-install-recommends \
postgresql-client-common \
postgresql-client-9.6 \
&& apt-get clean \
&& apt-get autoremove \
&& rm -r /var/lib/apt/lists/*

ADD requirements.txt /opt/app/requirements.txt
ADD setup.py /opt/app/setup.py
ADD setup.cfg /opt/app/setup.cfg
ADD README.md /opt/app/README.md
ADD CHANGELOG.md /opt/app/CHANGELOG.md

WORKDIR /opt/app

RUN easy_install virtualenv
RUN virtualenv --no-site-packages env
RUN ./env/bin/pip install -r requirements.txt

ADD . /opt/app

RUN ./env/bin/python setup.py extract_messages
RUN ./env/bin/python setup.py init_catalog -l en
RUN ./env/bin/python setup.py compile_catalog

CMD ./env/bin/pserve --reload development.ini

EXPOSE 6543
22 changes: 22 additions & 0 deletions Dockerfile.db
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
FROM library/postgres:9.6

RUN apt-get update \
&& apt-get install -y \
--no-install-recommends \
postgis \
postgresql-9.6-postgis-2.3 \
postgresql-contrib-9.6 \
postgresql-9.6-postgis-scripts \
postgresql-server-dev-9.6 \
pgtop \
&& apt-get clean \
&& apt-get autoremove \
&& rm -r /var/lib/apt/lists/*

COPY docker-entrypoint-initdb.d /docker-entrypoint-initdb.d

RUN dpkg-query -l postgis

RUN ls -lAh /usr/share/postgresql/9.6/extension/postgis*

RUN echo "listen_addresses='*'" >> /var/lib/postgresql/data/postgresql.conf
7 changes: 7 additions & 0 deletions Dockerfile.static
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
FROM nginx:alpine

RUN rm -v /etc/nginx/nginx.conf

ADD nginx.conf /etc/nginx/

CMD ["nginx", "-g", "daemon off;"]
20 changes: 20 additions & 0 deletions Dockerfile.stats
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
FROM python:2.7.12

RUN apt-get update \
&& apt-get upgrade -y \
&& apt-get clean \
&& apt-get autoremove \
&& rm -r /var/lib/apt/lists/*

ADD tasking-manager-stats/requirements.txt /opt/app/requirements.txt

WORKDIR /opt/app

RUN easy_install virtualenv
RUN virtualenv --no-site-packages env
RUN ./env/bin/pip install -r requirements.txt

ADD tasking-manager-stats /opt/app

# CMD ./env/bin/python taskingDbEndpoint.py
CMD ./run_stats.sh
5 changes: 5 additions & 0 deletions Dockerfile.web
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
FROM traefik:camembert
ADD traefik.toml .
EXPOSE 80
EXPOSE 8080
EXPOSE 443
153 changes: 153 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,153 @@
export SHELL := /bin/bash
.DEFAULT_GOAL := help

# Platform-specific variables
# ---------------------------
PLATFORM_INFO:= $(shell python -m platform)
ifeq ($(findstring Ubuntu,$(PLATFORM_INFO)),Ubuntu)
PLATFORM:= ubuntu
ENV?= $(shell pwd | sed -n 's/.*osm-tasking-manager2-staging/staging/p; s/.*osm-tasking-manager2-production/production/p')
endif
ifeq ($(findstring Darwin,$(PLATFORM_INFO)),Darwin)
PLATFORM:= darwin
ENV?= development
APP_LIVE:= app
AWS_PROFILE:= hotosm
endif

ENVS = development staging production
## Check ENV
ifeq ($(filter $(ENV),$(ENVS)),)
$(error Valid ENV values include: $(ENVS))
endif

# PHONY (non-file) Targets
# ------------------------
.PHONY: build env check up destroy ps logs restart exec-app exec-db backup restore help

## Docker

BUILD_DB := docker build -t hotosm/taskmanager-db:$(ENV) -f ./Dockerfile.db .
BUILD_APP := docker build -t hotosm/taskmanager-app:$(ENV) -f ./Dockerfile.app .
BUILD_WEB := docker build -t hotosm/taskmanager-web:$(ENV) -f ./Dockerfile.web .
BUILD_STATS := docker build -t hotosm/taskmanager-stats:$(ENV) -f ./Dockerfile.stats .
BUILD_STATIC := docker build -t hotosm/taskmanager-static:$(ENV) -f ./Dockerfile.static .

build: ## build containers
@$(BUILD_DB)
@$(BUILD_APP)
@$(BUILD_STATS)
@$(BUILD_STATIC)
ifeq ($(ENV),development)
@$(BUILD_WEB)
endif

## Docker Compose

DOCKER_COMPOSE_CLI := docker-compose -f docker-compose.yml -f docker-compose.$(ENV).yml

ifneq ($(ENV),development)
LIVE_COLOR:=$(shell $(DOCKER_COMPOSE_CLI) ps | sed -n 's/.*blue.*Up*/blue/p; s/.*green.*Up*/green/p' | awk '{print $$1}')
APP_LIVE:= app_$(LIVE_COLOR)
endif

ifeq ($(APP_LIVE),app_green)
APP_DEPLOY=app_blue
else
APP_DEPLOY=app_green
endif

env: ## check live color
echo $(APP_LIVE)

up: ## start TM2 or update current services after making compose changes
ifeq ($(ENV),development)
@$(DOCKER_COMPOSE_CLI) up -d
else ifeq ($(APP_LIVE),app_blue)
@$(DOCKER_COMPOSE_CLI) up -d db stats static app_blue
else
@$(DOCKER_COMPOSE_CLI) up -d db stats static app_green
endif

deploy1: ## deploy
@$(BUILD_APP)

deploy2:
@$(DOCKER_COMPOSE_CLI) up -d --force-recreate $(APP_DEPLOY)

deploy3:
@$(DOCKER_COMPOSE_CLI) stop $(APP_LIVE)

deploy: deploy1 deploy2 deploy3

destroy: ## destroy TM2
@$(DOCKER_COMPOSE_CLI) down -v

ps: ## ps TM2
@$(DOCKER_COMPOSE_CLI) ps

logs: ## logs TM2
@$(DOCKER_COMPOSE_CLI) logs -f

restart: ## logs confluence
@$(DOCKER_COMPOSE_CLI) restart

stats-run: ## bash app
@$(DOCKER_COMPOSE_CLI) run stats ./env/bin/python taskingDbEndpoint.py

stats-exec: ## bash app
@$(DOCKER_COMPOSE_CLI) run stats bash

stats-logs: ## bash app
@$(DOCKER_COMPOSE_CLI) logs -f stats

app-exec: ## bash app
@$(DOCKER_COMPOSE_CLI) exec $(APP_LIVE) bash

db-exec: ## bash db
@$(DOCKER_COMPOSE_CLI) exec db bash

static-exec: ## bash static
@$(DOCKER_COMPOSE_CLI) exec static sh

static-logs: ## bash static
@$(DOCKER_COMPOSE_CLI) logs -f static

psql: ## connect to database
@$(DOCKER_COMPOSE_CLI) exec db su -c "psql -h localhost -p 5432 -U postgres -d osmtm"

migrations:
@$(DOCKER_COMPOSE_CLI) exec $(APP_LIVE) su -c "./env/bin/alembic upgrade head"

BACKUP_BUCKET:=s3://hotosm-backups/osm-tasking-managerv2

backup: ## dump tm2 db and post to S3
@$(DOCKER_COMPOSE_CLI) exec db su -c "time pg_dump -v -Fc -f /srv/osmtm2.dmp -h localhost -U postgres osmtm"
@aws s3 cp /tmp/osmtm2.dmp $(BACKUP_BUCKET)/osmtm2_$(ENV)_$(shell date +'%Y%m%d_%H%M%S').dmp

LASTEST_BACKUP = $(shell aws s3 ls $(BACKUP_BUCKET)/ | grep -v staging | sort | tail -n 1 | awk '{print $$4}')
download-backup-s3:
@echo $(LASTEST_BACKUP)
@aws s3 cp $(BACKUP_BUCKET)/$(LASTEST_BACKUP) /tmp/osmtm2.dmp

restore-remote: download-backup-s3 restore

fresh-db: env
@$(DOCKER_COMPOSE_CLI) exec $(APP_LIVE) su -c "./wait-for-postgres.sh db"
@$(DOCKER_COMPOSE_CLI) stop db
@$(DOCKER_COMPOSE_CLI) rm -v -f db
@$(DOCKER_COMPOSE_CLI) up -d db
@$(DOCKER_COMPOSE_CLI) exec $(APP_LIVE) su -c "./wait-for-postgres.sh db"

populate-db: env fresh-db
@$(DOCKER_COMPOSE_CLI) exec db su -c "psql -h localhost -U postgres -d osmtm -c 'CREATE EXTENSION postgis;'"
@echo "beginning db initialization"
@$(DOCKER_COMPOSE_CLI) exec $(APP_LIVE) su -c "./env/bin/initialize_osmtm_db"

restore: env fresh-db ## restore latest backup from S3
@$(DOCKER_COMPOSE_CLI) exec db su -c "pg_restore -v -h localhost -p 5432 -U postgres -d osmtm /srv/osmtm2.dmp"

# `make help` - see http://marmelab.com/blog/2016/02/29/auto-documented-makefile.html
# ------------------------------------------------------------------------------------
help: ## Show this help
@grep -E '^[a-zA-Z_-]+:.*?## .*$$' $(MAKEFILE_LIST) | awk 'BEGIN {FS = ":.*?## "}; {printf "\033[36m%-30s\033[0m %s\n", $$1, $$2}'
81 changes: 81 additions & 0 deletions docker-compose.development.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
version: '2'

services:

db:
image: hotosm/taskmanager-db:development
cpu_shares: 2
cpuset: 0-1
expose:
- 5432
ports:
- "5432:5432"
networks:
- database_network
volumes:
- postgres-db-volume:/data/postgres

app:
image: hotosm/taskmanager-app:development
labels:
- traefik.enable=true
- traefik.port=80
- traefik.frontend.rule=Host:tasks.dev
- traefik.docker.network=traefik_net
ports:
- "6543:6543"
volumes:
- postgres-db-volume:/data/postgres
networks:
- traefik_net
- database_network

web:
image: hotosm/taskmanager-web:development
command: -c /dev/null --web --docker --docker.domain=docker.localhost --logLevel=DEBUG
ports:
- "80:80"
- "443:443"
- "8080:8080"
volumes:
- /var/run/docker.sock:/var/run/docker.sock
- ./local:/tls
networks:
- traefik_net
cap_drop:
- all
cap_add:
- net_bind_service

stats:
image: hotosm/taskmanager-stats:development
volumes:
- stats-volume:/opt/app/user_data
networks:
- database_network

static:
image: hotosm/taskmanager-static:development
volumes:
- stats-volume:/var/www/static
expose:
- 80
ports:
- "8081:80"
networks:
- traefik_net
labels:
- traefik.enable=true
- traefik.port=80
- traefik.frontend.rule=Path:/user_data/
- traefik.docker.network=traefik_net

networks:
database_network:
driver: bridge
traefik_net:
driver: bridge

volumes:
postgres-db-volume:
stats-volume:
Loading