This repository has been archived by the owner on Aug 31, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 10
/
deploy.yaml
153 lines (136 loc) · 4.24 KB
/
deploy.yaml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
---
- hosts: all
remote_user: root
###gather_facts: no
vars:
homedir: "/data/greentea/web"
greentea_user: "greentea"
django_settings: "tttt.settings.production"
elasticsearch: "elasticsearch.docker"
elasticsearch_data_storage: "/usr/share/elasticsearch/data/"
backup_dir: "/var/backup/greentea/"
environment:
DJANGO_SETTINGS_MODULE: "{{ django_settings }}"
tasks:
## Elasticseach instance ###
# only one instance of elastic search can be created
- name: "Stop and backup old version of elasticsearch"
shell: |
COUNT=$( docker ps -aq -f "name={{elasticsearch}}" | wc -l )
[ $COUNT -gt 0 ] && docker stop "{{elasticsearch}}" &&
docker rename "{{elasticsearch}}" "{{elasticsearch}}.$COUNT"
tags:
- renew
- renew-elasticserach
register: out
- debug: var=out.stdout_lines
- name: "Create new Elasticsearch in Docker"
shell: |
docker pull elasticsearch:2
docker run -d -p 9200:9200 -p 9300:9300 -v "{{elasticsearch_data_storage}}":"{{elasticsearch_data_storage}}" --name "{{elasticsearch}}" -h "{{elasticsearch}}" elasticsearch:2
tags:
- renew
- renew-elasticserach
- elasticserach
- name: "create storage for backup database"
file: path={{ backup_dir }} state=directory recurse=yes group={{ greentea_user }} owner={{ greentea_user }}
tags:
- backup_db
- name: "Backup database"
shell: |
source env/bin/activate
TIMESTEMP=$(date +"%Y-%m-%d.%H%M")
DB_NAME=$( python -c "from {{ django_settings }} import DATABASES; print DATABASES['default']['NAME']" )
DB_HOST=$( python -c "from {{ django_settings }} import DATABASES; print DATABASES['default']['HOST']" )
DB_USER=$( python -c "from {{ django_settings }} import DATABASES; print DATABASES['default']['USER']" )
pg_dump "$DB_NAME" -U "$DB_USER" -h "$DB_HOST" | gzip > {{ backup_dir }}/tttt-$TIMESTEMP.db.gz
echo tttt-$TIMESTEMP.db.gz > {{ backup_dir }}/latest
become: yes
become_user: "{{ greentea_user }}"
args:
chdir: "{{ homedir }}"
tags:
- backup_db
- name: "Load latest backup"
command: cat {{ backup_dir }}/latest
register: db_backup_name
tags:
- backup_db
- debug: var=db_backup_name.stdout
tags:
- backup_db
- name: "Fetch backup database"
fetch: src={{backup_dir}}/{{db_backup_name.stdout}} dest=tttt.db.gz flat=yes
tags:
- backup_db
- name: "Update project from Github"
shell: |
git fetch
git reset --hard HEAD
git pull
become: yes
become_user: "{{ greentea_user }}"
args:
chdir: "{{ homedir }}"
tags:
- update
- deploy
## Grean tea instance ##
- name: "Upgrade pip packages"
shell: |
source env/bin/activate
pip freeze > /tmp/update.pip.now
pip freeze | diff /tmp/update.pip.now - >> pip-upgrade.log
pip install --upgrade -r requirement/requirement.txt
become: yes
become_user: greentea
args:
chdir: "{{ homedir }}"
tags:
- upgrade
- name: "Update collect static files"
shell: |
source env/bin/activate &&
python manage.py collectstatic --noinput
become: yes
become_user: greentea
args:
chdir: "{{ homedir }}"
tags:
- deploy
- name: "Stop uwsgi instance of Django"
shell:
uwsgi --stop /run/greentea.pid
tags:
- deploy
- upgrade
- restart
- stop
- name: "Migrate database schema"
shell: |
source env/bin/activate
python manage.py migrate --noinput
become: yes
become_user: greentea
args:
chdir: "{{ homedir }}"
tags:
- deploy
- migrate
- name: "Start uwsgi instance of Django"
shell:
uwsgi -x /etc/uwsgi.d/tttt.conf
tags:
- deploy
- upgrade
- restart
- start
- name: "Check if pid exists and application runs"
shell:
ps aux | grep $(cat /run/greentea.pid) | grep -v grep
tags:
- deploy
- upgrade
- restart
- start
...