Skip to content
This repository was archived by the owner on Mar 7, 2025. It is now read-only.

Commit c624137

Browse files
author
David Neudorfer
committed
adds docker files
1 parent ec15261 commit c624137

File tree

11 files changed

+321
-16
lines changed

11 files changed

+321
-16
lines changed

.dockerignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
.git
2+
.gitignore

.travis.yml

Lines changed: 33 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,49 @@
1+
branches:
2+
only:
3+
- gh-pages
4+
- /.*/
5+
16
language: python
27
python:
38
- 2.7
49

510
cache:
611
directories:
7-
- $HOME/.cache/pip
8-
- $HOME/build/hotosm/osm-tasking-manager2/env
12+
- "$HOME/.cache/pip"
13+
- "$HOME/build/hotosm/osm-tasking-manager2/env"
914

1015
sudo: false
1116

1217
before_install:
13-
- deactivate
18+
- deactivate
1419

1520
install:
16-
- virtualenv env
17-
- env/bin/pip install --upgrade pip
18-
- env/bin/pip install -r requirements.txt
19-
- createuser www-data --no-superuser --no-createdb --no-createrole -U postgres
20-
- createdb -O www-data osmtm_tests -U postgres
21-
- psql -d osmtm_tests -c "CREATE EXTENSION postgis;" -U postgres
22-
- env/bin/pip install flake8
21+
- virtualenv env
22+
- env/bin/pip install --upgrade pip
23+
- env/bin/pip install -r requirements.txt
24+
- createuser www-data --no-superuser --no-createdb --no-createrole -U postgres
25+
- createdb -O www-data osmtm_tests -U postgres
26+
- psql -d osmtm_tests -c "CREATE EXTENSION postgis;" -U postgres
27+
- env/bin/pip install flake8
2328

2429
script:
25-
- echo [app:main] >> local.test.ini
26-
- echo sqlalchemy.url = postgresql://www-data:www-data@localhost/osmtm_tests >> local.test.ini
27-
- env/bin/nosetests
28-
- env/bin/flake8 osmtm --exclude=osmtm/static
30+
- echo [app:main] >> local.test.ini
31+
- echo sqlalchemy.url = postgresql://www-data:www-data@localhost/osmtm_tests >> local.test.ini
32+
- env/bin/nosetests
33+
- env/bin/flake8 osmtm --exclude=osmtm/static
2934

3035
after_success:
31-
- env/bin/pip install coveralls
32-
- env/bin/coveralls
36+
- env/bin/pip install coveralls
37+
- env/bin/coveralls
38+
39+
before_deploy:
40+
- 'curl "https://s3.amazonaws.com/aws-cli/awscli-bundle.zip" -o "awscli-bundle.zip"'
41+
- 'unzip awscli-bundle.zip'
42+
- './awscli-bundle/install -b ~/bin/aws'
43+
- 'export PATH=~/bin:$PATH'
44+
- 'rm awscli-bundle.zip'
45+
- 'rm -rf awscli-bundle/'
46+
47+
deploy:
48+
provider: script
49+
script: scripts/deploy.sh

Dockerfile.app

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
FROM python:2.7.12
2+
3+
RUN apt-get update \
4+
&& apt-get install -y \
5+
--no-install-recommends \
6+
libgeos-dev \
7+
software-properties-common \
8+
python-software-properties \
9+
wget \
10+
&& apt-get clean \
11+
&& apt-get autoremove \
12+
&& rm -r /var/lib/apt/lists/*
13+
14+
RUN add-apt-repository "deb http://apt.postgresql.org/pub/repos/apt/ jessie-pgdg main"
15+
RUN wget --quiet -O - https://www.postgresql.org/media/keys/ACCC4CF8.asc | apt-key add -
16+
RUN apt-get update \
17+
&& apt-get install -y \
18+
--no-install-recommends \
19+
postgresql-client-common \
20+
postgresql-client-9.6 \
21+
&& apt-get clean \
22+
&& apt-get autoremove \
23+
&& rm -r /var/lib/apt/lists/*
24+
25+
ADD requirements.txt /opt/app/requirements.txt
26+
ADD setup.py /opt/app/setup.py
27+
ADD setup.cfg /opt/app/setup.cfg
28+
ADD README.md /opt/app/README.md
29+
ADD CHANGELOG.md /opt/app/CHANGELOG.md
30+
31+
WORKDIR /opt/app
32+
33+
RUN easy_install virtualenv
34+
RUN virtualenv --no-site-packages env
35+
RUN ./env/bin/pip install -r requirements.txt
36+
37+
ADD . /opt/app
38+
39+
CMD ./startup-tm.sh

Dockerfile.db

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
FROM library/postgres
2+
3+
RUN apt-get update \
4+
&& apt-get install -y \
5+
--no-install-recommends \
6+
postgis \
7+
postgresql-9.6-postgis-2.3 \
8+
postgresql-contrib-9.6 \
9+
postgresql-9.6-postgis-scripts \
10+
pgtop \
11+
&& apt-get clean \
12+
&& apt-get autoremove \
13+
&& rm -r /var/lib/apt/lists/*
14+
15+
COPY docker-entrypoint-initdb.d /docker-entrypoint-initdb.d
16+
17+
RUN dpkg-query -l postgis
18+
19+
RUN ls -lAh /usr/share/postgresql/9.6/extension/postgis*

docker-compose.production.yml

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
version: '2'
2+
3+
services:
4+
5+
app:
6+
command: ["true"]
7+
8+
# https://github.com/vincetse/docker-compose-zero-downtime-deployment
9+
app_green:
10+
container_name: app_green
11+
ports:
12+
- 3000:6543
13+
environment:
14+
- VIRTUAL_PORT=3000
15+
- VIRTUAL_HOST=tasks.hotosm.org,tasks-green.hotosm.org
16+
extends:
17+
service: app
18+
19+
app_blue:
20+
container_name: app_blue
21+
ports:
22+
- 3001:6543
23+
environment:
24+
- VIRTUAL_PORT=3001
25+
- VIRTUAL_HOST=tasks.hotosm.org,tasks-blue.hotosm.org
26+
extends:
27+
service: app

docker-compose.staging.yml

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
version: '2'
2+
3+
services:
4+
5+
app:
6+
command: ["true"]
7+
8+
# https://github.com/vincetse/docker-compose-zero-downtime-deployment
9+
app_green:
10+
container_name: app_green
11+
ports:
12+
- 3000:6543
13+
environment:
14+
- VIRTUAL_PORT=3000
15+
- VIRTUAL_HOST=tasks-staging.hotosm.org,tasks-staging-green.hotosm.org
16+
extends:
17+
service: app
18+
19+
app_blue:
20+
container_name: app_blue
21+
ports:
22+
- 3001:6543
23+
environment:
24+
- VIRTUAL_PORT=3001
25+
- VIRTUAL_HOST=tasks-staging.hotosm.org,tasks-staging-blue.hotosm.org
26+
extends:
27+
service: app

docker-compose.yml

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
version: '2'
2+
3+
services:
4+
# https://hub.docker.com/_/postgres/
5+
# https://store.docker.com/images/022689bf-dfd8-408f-9e1c-19acac32e57b
6+
7+
db:
8+
build:
9+
context: ./
10+
dockerfile: Dockerfile.db
11+
container_name: db
12+
restart: always
13+
volumes:
14+
- postgres-db-volume:/data/postgres
15+
- /tmp:/srv
16+
cpu_shares: 8
17+
cpuset: 0-8
18+
networks:
19+
- database_network
20+
21+
app:
22+
build:
23+
context: ./
24+
dockerfile: Dockerfile.app
25+
container_name: app
26+
environment:
27+
- VIRTUAL_HOST=tasks-staging.hotosm.org
28+
restart: always
29+
# https://docs.docker.com/compose/startup-order/
30+
command: ["./wait-for-postgres.sh", "db", "./startup-tm.sh"]
31+
networks:
32+
- database_network
33+
- code_network
34+
35+
# cpuset, such as `docker run –cpuset-cpus`, allows users to specify
36+
# which CPUs to allow execution in. accepts a string that looks like
37+
# 0
38+
# 0,1
39+
# 0-2
40+
# cpu_shares
41+
42+
volumes:
43+
postgres-db-volume:
44+
45+
networks:
46+
code_network:
47+
driver: bridge
48+
database_network:
49+
driver: bridge
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
CREATE USER "www-data" WITH PASSWORD 'password';
2+
ALTER USER "www-data" WITH NOSUPERUSER NOCREATEROLE NOCREATEDB;
3+
CREATE DATABASE osmtm OWNER "www-data" ENCODING 'UTF8' TEMPLATE template0;
4+
\connect osmtm
5+
CREATE EXTENSION postgis;
6+
SELECT postgis_full_version();

scripts/deploy.sh

Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
#!/bin/bash -eux
2+
3+
DEPLOY_USER="mozart"
4+
HOST="62.210.100.219"
5+
SSH_OPTS="-o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null -i mozart_rsa -o ForwardAgent=yes -o IPQoS=throughput -tt"
6+
BASE_DIR="/srv/deploy/osm-tasking-manager2"
7+
8+
# download mozarts key
9+
aws s3 cp s3://hotosm-secure/keys/mozart_rsa.enc mozart_rsa.enc
10+
# unencrypt mozarts key
11+
openssl aes-256-cbc -K $encrypted_e101044e37e0_key -iv $encrypted_e101044e37e0_iv \
12+
-in mozart_rsa.enc -out mozart_rsa -d
13+
# fix permissions
14+
chmod 700 mozart_rsa
15+
16+
# set environment variable based on branch or tag
17+
if [ ! -z $TRAVIS_TAG ]; then
18+
echo "Deploying $TRAVIS_TAG to prod"
19+
URL_PREFIX="tasks"
20+
ENV="production"
21+
elif [ "$TRAVIS_BRANCH" == "develop" -o "$TRAVIS_PULL_REQUEST" != "false" ]; then
22+
echo "Deploying develop branch to staging"
23+
URL_PREFIX="tasks-staging"
24+
ENV="staging"
25+
else
26+
echo "Not a tag or develop branch. Doing nothing"
27+
exit 0
28+
fi
29+
30+
# rsync files to server and respective environment path
31+
rsync -arvz --progress . \
32+
--exclude='.git/' \
33+
-e "ssh -o StrictHostKeyChecking=no \
34+
-o UserKnownHostsFile=/dev/null \
35+
-i mozart_rsa" \
36+
$DEPLOY_USER@$HOST:$BASE_DIR-$ENV
37+
38+
# find the live environment
39+
LIVE_COLOR=$(ssh $SSH_OPTS $DEPLOY_USER@$HOST \
40+
"sudo su -c 'cd $BASE_DIR-$ENV && docker-compose \
41+
-f docker-compose.yml \
42+
-f docker-compose.$ENV.yml \
43+
ps'" | awk '/app.*Up/ {print $1}')
44+
45+
# set values for live color and deploy color
46+
if [[ $LIVE_COLOR == *"blue"* ]]; then
47+
deploy="green"; live="blue"
48+
elif [[ $LIVE_COLOR == *"green"* ]]; then
49+
deploy="blue"; live="green"
50+
else
51+
echo "app not running"
52+
fi
53+
54+
# echo build color
55+
echo "building app_$deploy"
56+
57+
# build new color
58+
ssh $DEPLOY_USER@$HOST $SSH_OPTS \
59+
"sudo su -c 'cd $BASE_DIR-$ENV && docker-compose \
60+
-f docker-compose.yml \
61+
-f docker-compose.$ENV.yml \
62+
build app app_$deploy'"
63+
64+
# echo deploy color
65+
echo "deploying app_$deploy"
66+
67+
# deploy new color
68+
ssh $DEPLOY_USER@$HOST $SSH_OPTS \
69+
"sudo su -c 'cd $BASE_DIR-$ENV && docker-compose \
70+
-f docker-compose.yml \
71+
-f docker-compose.$ENV.yml \
72+
up -d \
73+
--force-recreate app_$deploy'"
74+
75+
# wait a little in case the service takes a bit to start
76+
sleep 5
77+
78+
# test new server alt url
79+
until $(curl --output /dev/null --silent --head --fail http://$URL_PREFIX-$deploy.hotosm.org); do
80+
printf '.'
81+
sleep 2
82+
done
83+
84+
# TODO
85+
# add timeout
86+
# add fail check to tear down new servers
87+
88+
# stop old server
89+
ssh $DEPLOY_USER@$HOST $SSH_OPTS \
90+
"sudo su -c 'cd $BASE_DIR-$ENV && docker-compose \
91+
-f docker-compose.yml \
92+
-f docker-compose.$ENV.yml \
93+
stop app_$live'"
94+
95+
# test main site
96+
until $(curl --output /dev/null --silent --head --fail http://$URL_PREFIX.hotosm.org); do
97+
printf '.'
98+
sleep 2
99+
done

startup-tm.sh

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
#!/bin/bash
2+
3+
# ./env/bin/initialize_osmtm_db
4+
./env/bin/pserve --reload development.ini

0 commit comments

Comments
 (0)