Skip to content

Commit ef9485f

Browse files
Merge pull request #422 from mapswipe/continuous_deployment
Continuous deployment
2 parents 7058af6 + e6d5e54 commit ef9485f

File tree

12 files changed

+104
-110
lines changed

12 files changed

+104
-110
lines changed

.travis.yml

Lines changed: 28 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -9,16 +9,25 @@ python:
99
- "3.6"
1010
services:
1111
- docker
12+
addons:
13+
ssh_known_hosts:
14+
- 35.190.148.87 # Development server
15+
env:
16+
- ANSIBLE_CONFIG=ansible/ansible.cfg
1217

1318
before_install:
14-
- openssl aes-256-cbc -K $encrypted_444bb228fd3f_key -iv $encrypted_444bb228fd3f_iv -in .ci-mapswipe-firebase-adminsdk-80fzw-21ddcd1649.json.enc -out mapswipe_workers/serviceAccountKey.json -d
15-
19+
- openssl aes-256-cbc -K $encrypted_444bb228fd3f_key -iv $encrypted_444bb228fd3f_iv -in travis/ci-mapswipe-firebase-adminsdk-80fzw-21ddcd1649.json.enc -out mapswipe_workers/serviceAccountKey.json -d
1620
# Create a mock file for wal-g setup
1721
- touch postgres/serviceAccountKey.json
1822

1923
install:
2024
- pip install --upgrade pip setuptools
2125
- pip install black==19.10b0 flake8==3.8.3 isort==5.5.2
26+
# Install ansible for automated deployment
27+
- sudo apt-get --quiet update
28+
- sudo apt-get --yes install software-properties-common
29+
- sudo apt-add-repository --yes --update ppa:ansible/ansible
30+
- sudo apt-get --yes install ansible
2231

2332
script:
2433
- black --check mapswipe_workers
@@ -29,22 +38,21 @@ script:
2938
- docker-compose run mapswipe_workers python -m unittest discover --verbose --start-directory tests/unittests/
3039
- docker-compose run mapswipe_workers python -m unittest discover --verbose --start-directory tests/integration/
3140

32-
# before_deploy:
33-
# # SSH setup to deploy to server after build.
34-
# - apk add openssh-client
35-
# - mkdir -p ~/.ssh
36-
# - chmod 700 ~/.ssh
37-
# - eval "$(ssh-agent -s)"
38-
# - export KEY_FILE=~/.ssh/api_server_ssh_key
39-
# - echo "$DEPLOYMENT_SERVER_PRIVATE_KEY" | tr -d '\r' > $KEY_FILE
40-
# - chmod 600 $KEY_FILE
41-
# - ssh-add $KEY_FILE > /dev/null
42-
# - ssh-keyscan -H "$DEPLOYMENT_SERVER_IP" >> ~/.ssh/known_hosts
43-
# - chmod 644 ~/.ssh/known_hosts
44-
# - ssh -i $KEY_FILE -o StrictHostKeyChecking=no "mapswipe@$DEPLOYMENT_SERVER_IP"
41+
after_success:
42+
# Deploy to development server using Ansible
43+
# SSH setup to deploy to server after build.
44+
- apk add openssh-client
45+
- eval "$(ssh-agent -s)" #start the ssh agent
46+
- mkdir -p $HOME/.ssh
47+
# - chmod 700 $HOME/.ssh
48+
- cp travis/ssh_config $HOME/.ssh/config
49+
- openssl aes-256-cbc -K $encrypted_c17fa81e6490_key -iv $encrypted_c17fa81e6490_iv -in travis/ssh-private-key.enc -out $HOME/.ssh/private-key -d
50+
- chmod 600 $HOME/.ssh/private-key
51+
- ssh-add $HOME/.ssh/private-key
52+
- chmod +x deploy.sh
4553

46-
# deploy:
47-
# provider: script
48-
# script: bash scripts/deploy.sh
49-
# on:
50-
# branch: dev
54+
deploy:
55+
provider: script
56+
script: bash deploy.sh
57+
on:
58+
branch: dev

ansible/ansible.cfg

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
[defaults]
2+
INVENTORY = inventory.yml
3+
4+
[ssh_connection]
5+
pipelining = True # Reuse connection. Make execution faster.

ansible/inventory.yml

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
all:
2+
hosts:
3+
development:
4+
ansible_host: 35.190.148.87
5+
ansible_user: mapswipe
6+
# Uncomment following line when not using ~/.ssh/config
7+
# ansible_ssh_private_key_file: $HOME/.ssh/private-key
8+
production:
9+
ansible_host: 35.231.228.82
10+
ansible_user: mapswipe
11+
# Uncomment following line when not using ~/.ssh/config
12+
# ansible_ssh_private_key_file: $HOME/.ssh/private-key

ansible/playbook.yml

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
---
2+
- name: Deploy MapSwipe Backend
3+
hosts: development
4+
5+
tasks:
6+
- name: Fetch latest code base from GitHub.
7+
git:
8+
repo: 'https://github.com/mapswipe/python-mapswipe-workers.git'
9+
dest: $HOME/python-mapswipe-workers
10+
version: dev
11+
- name: Rebuild and restart Docker container usind Docker-Compose.
12+
script: $HOME/python-mapswipe-worker/recreate_container.sh

deploy.sh

Lines changed: 4 additions & 88 deletions
Original file line numberDiff line numberDiff line change
@@ -1,88 +1,4 @@
1-
git pull
2-
if [[ $? = 0 ]]; then
3-
echo "success"
4-
else
5-
echo "failure: $?"
6-
exit
7-
fi
8-
9-
python3 test_config.py
10-
if [[ $? = 0 ]]; then
11-
echo "success"
12-
else
13-
echo "failure: $?"
14-
exit
15-
fi
16-
17-
docker-compose build --no-cache postgres
18-
if [[ $? = 0 ]]; then
19-
echo "success"
20-
else
21-
echo "failure: $?"
22-
exit
23-
fi
24-
25-
docker-compose build --no-cache firebase_deploy
26-
if [[ $? = 0 ]]; then
27-
echo "success"
28-
else
29-
echo "failure: $?"
30-
exit
31-
fi
32-
33-
docker-compose build --no-cache mapswipe_workers
34-
if [[ $? = 0 ]]; then
35-
echo "success"
36-
else
37-
echo "failure: $?"
38-
exit
39-
fi
40-
41-
docker-compose build --no-cache manager_dashboard
42-
if [[ $? = 0 ]]; then
43-
echo "success"
44-
else
45-
echo "failure: $?"
46-
exit
47-
fi
48-
49-
docker-compose build --no-cache nginx
50-
if [[ $? = 0 ]]; then
51-
echo "success"
52-
else
53-
echo "failure: $?"
54-
exit
55-
fi
56-
57-
docker-compose build --no-cache api
58-
if [[ $? = 0 ]]; then
59-
echo "success"
60-
else
61-
echo "failure: $?"
62-
exit
63-
fi
64-
65-
docker-compose up -d --force-recreate postgres firebase_deploy mapswipe_workers manager_dashboard nginx api
66-
if [[ $? = 0 ]]; then
67-
echo "success"
68-
else
69-
echo "failure: $?"
70-
exit
71-
fi
72-
73-
docker logs firebase_deploy
74-
if [[ $? = 0 ]]; then
75-
echo "success"
76-
else
77-
echo "failure: $?"
78-
exit
79-
fi
80-
81-
docker ps -a
82-
if [[ $? = 0 ]]; then
83-
echo "success"
84-
else
85-
echo "failure: $?"
86-
exit
87-
fi
88-
1+
#!/bin/bash
2+
# Deploy to dev server.
3+
# This script is called by Travis.
4+
ansible-playbook -i ansible/inventory.yml ansible/playbook.yml

docs/source/dev_setup.md

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,7 @@ There exists a Firebase instance only for Travis.
141141
For the configuration of Travis following environment variables are used:
142142

143143
- FIREBASE_API_KEY
144-
- FIREBASE_DB: test-mapswipe
144+
- FIREBASE_DB
145145
- FIREBASE_TOKEN
146146
- POSTGRES_DB
147147
- POSTGRES_HOST
@@ -153,7 +153,12 @@ For the configuration of Travis following environment variables are used:
153153

154154
Those variables can be definied directly in the repository settings of Travis. For more inofmration refer to: https://docs.travis-ci.com/user/environment-variables/#defining-variables-in-repository-settings
155155

156-
Additionaly a Service Account Key in JSON format is encrypted and added to the GitHub repository using the travis CLI. Read more on that process in the Travis docs: https://docs.travis-ci.com/user/encrypting-files/
156+
Additionaly a Service Account Key in JSON format is encrypted and added to the GitHub repository using the travis CLI. Once Travis runs it will decrypt the Service Account Key. Read more on that process in the Travis docs: https://docs.travis-ci.com/user/encrypting-files/
157+
158+
Once a Travis build succeeds Travis executes an Ansible Playbook to deploy MapSwipe Workers to an already installed and configured server.
159+
For this to work an SSH-Key (with access rights to the server) is also encrypted and added to the GitHub repository. Travis will decrypt the key and Ansible will use it to execute commands defined in the Playbook on the server.
160+
161+
All files encrypted for Travis (Service Account Key, SSH-Key) are stored in the `travis` directory.
157162

158163

159164
## Database Backup

docs/source/tags

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
!_TAG_FILE_FORMAT 2 /extended format; --format=1 will not append ;" to lines/
2+
!_TAG_FILE_SORTED 1 /0=unsorted, 1=sorted, 2=foldcase/
3+
!_TAG_PROGRAM_AUTHOR Darren Hiebert /[email protected]/
4+
!_TAG_PROGRAM_NAME Exuberant Ctags //
5+
!_TAG_PROGRAM_URL http://ctags.sourceforge.net /official site/
6+
!_TAG_PROGRAM_VERSION 5.8 //

recreate_container.sh

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
#!/bin/bash
2+
# This script builds and starts all Docker container for running the Mapswipe ecosystem.
3+
# It is run either manually or by an Ansible Playbook after a successful Travis build.
4+
5+
docker-compose build --no-cache postgres firebase_deploy mapswipe_workers manager_dashboard nginx api
6+
if [[ $? = 0 ]]; then
7+
echo "success"
8+
else
9+
echo "failure: $?"
10+
exit
11+
fi
12+
13+
docker-compose up -d --force-recreate postgres firebase_deploy mapswipe_workers manager_dashboard nginx api
14+
if [[ $? = 0 ]]; then
15+
echo "success"
16+
else
17+
echo "failure: $?"
18+
exit
19+
fi

.ci-mapswipe-firebase-adminsdk-80fzw-21ddcd1649.json.enc renamed to travis/ci-mapswipe-firebase-adminsdk-80fzw-21ddcd1649.json.enc

File renamed without changes.

travis/ssh-private-key.enc

2.53 KB
Binary file not shown.

0 commit comments

Comments
 (0)