-
Notifications
You must be signed in to change notification settings - Fork 1
/
fabfile.py
211 lines (165 loc) · 6.98 KB
/
fabfile.py
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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
# coding: utf-8
import os
import time
import re
from fabric.api import local
def _get_docker_command(name, ports=[], image=None, rm=True, debug=True):
command = ['docker run',]
if image is None:
image = os.environ.get('IMAGE', 'allmychanges.com')
if rm:
command.append('--rm')
command.extend([
'-t -i',
'-v `pwd`:/app',
'-v `pwd`/logs:/var/log/allmychanges',
'-v `pwd`/tmp:/tmp/allmychanges',
'--net amch',
' '.join('-p ' + p for p in ports)])
if debug:
command.append('-e DEBUG=yes')
# '-e DEV_DOWNLOAD=yes'
command.extend([
'-e REDIS_HOST=redis.allmychanges.com',
'-e MYSQL_HOST=mysql.allmychanges.com',
'-e MYSQL_DATABASE=allmychanges',
'-e MYSQL_USER=root',
'-e MYSQL_PASSWORD=password',
'-e TOOLBAR_TOKEN=12345',
'--name',
name,
image])
command = ' '.join(command)
return command + ' '
def build_docker_image(version):
assert version
tag = 'localhost:5000/allmychanges.com:' + version
local('docker build -t {0} .'.format(tag))
def upload_docker_image(version):
assert version
tag = 'localhost:5000/allmychanges.com:' + version
local('docker build -t {} .'.format(tag))
local('docker push ' + tag)
def shell():
# используем shell2 потому что иначе возникает ошибка
# Error response from daemon: Could not find container for entity id 3f21744e9eddc17e7a4a32ac61993f5fbe3e85e0da908c1d7781d0eb4262d35c
# она должна быть исправлена в версии докера 1.10
# https://github.com/docker/docker/pull/16032
# и описана тут
# https://github.com/docker/docker/issues/17691
local(_get_docker_command('shell2.command.allmychanges.com') + (
'/env/bin/python /app/manage.py '
' shell_plus'))
def dbshell():
local('docker exec -it mysql.allmychanges.com mysql -ppassword allmychanges')
def get_db_from_production():
local('scp clupea:/mnt/yandex.disk/backups/mysql/allmychanges/latest.sql.bz2 dumps/')
local('rm -fr dumps/latest.sql')
local('bunzip2 dumps/latest.sql.bz2')
# local('docker exec -ti mysql.allmychanges.com bash')
local('docker exec mysql.allmychanges.com /dumps/restore.sh')
def runserver():
local(_get_docker_command('runserver.command.allmychanges.com',
ports=['8000:8000']) + (
'/env/bin/python /app/manage.py '
'runserver 0.0.0.0:8000'))
def rungunicorn():
# sudo docker run --rm -ti -e MYSQL_HOST=192.241.207.244 -e DJANGO_SETTINGS_MODULE=allmychanges.settings.production -p 8000:8000 --entrypoint gunicorn edited:latest allmychanges.wsgi:application --bind 0.0.0.0:8000 --access-logfile -
local(_get_docker_command(
'runserver.command.allmychanges.com',
debug=False,
ports=['8000:8000'])
+ (
'gunicorn '
'allmychanges.wsgi:application '
'--bind 0.0.0.0:8000 '
'--access-logfile -'))
def rqworker():
local(_get_docker_command('rqworker.command.allmychanges.com') + (
'/env/bin/python /app/manage.py '
'rqworker '
'default '
'preview '))
def bash(args=''):
if args == 'edit':
rm = False
else:
rm = True
local(_get_docker_command('bash.command.allmychanges.com', rm=rm) + 'bash')
def tail_errors():
local("tail -f logs/django-root.log | jq 'if (.[\"@fields\"].level == \"WARNING\" or .[\"@fields\"].level == \"ERROR\") then . else 0 end | objects'")
def manage(args=''):
if len(args) < 40:
hashed_args = re.sub(ur'[ \:/]+', '_', args)
else:
hashed_args = '.long.command'
name = 'manage.command.allmychanges.com' + hashed_args
local(_get_docker_command(name) +
'/env/bin/python /app/manage.py ' + args)
def dev_extract_history(repo_name):
name = 'dev_extract_history.command.allmychanges.com'
local(_get_docker_command(name) +
'/env/bin/python /app/manage.py dev_extract_history /tmp/allmychanges/' + repo_name)
def test(args=''):
local(_get_docker_command('tests.command.allmychanges.com') + (
'/env/bin/nosetests ') + args)
def test2(args=''):
local('nosetests ' + args)
def test_failed(args=''):
local(_get_docker_command('tests.command.allmychanges.com') + (
'/env/bin/nosetests --with-progressive --failed ') + args)
def test_failed2(args=''):
local(_get_docker_command('tests.command.allmychanges.com') + (
'/env/bin/nosetests --failed ') + args)
def coverage():
local('nosetests --with-coverage --cover-package=allmychanges --cover-html --cover-erase --cover-inclusive --cover-html-dir=static/coverage')
local('ssh back open http://art.dev.allmychanges.com:8000/static/coverage/index.html')
def drop_database():
local('docker rm -fv mysql.allmychanges.com')
def create_database():
start()
get_db_from_production()
manage('migrate')
def start():
# до выполнения надо создать сеть amch
# docker network create amch
containers = _get_docker_containers()
if 'mysql.allmychanges.com' in containers:
local('docker start mysql.allmychanges.com')
else:
local('docker run --net amch --name mysql.allmychanges.com -v `pwd`/dumps:/dumps -e MYSQL_ROOT_PASSWORD=password -d mysql')
print 'Waiting for mysql start'
time.sleep(30)
local('docker exec -it mysql.allmychanges.com mysqladmin -ppassword create allmychanges')
local('docker run --rm -it -v `pwd`:/app --net amch allmychanges.com /env/bin/python /app/manage.py syncdb --migrate')
if 'redis.allmychanges.com' in containers:
local('docker start redis.allmychanges.com')
else:
local('docker run --net amch --name redis.allmychanges.com -d redis')
def stop():
local('docker rm --force -v rqworker.command.allmychanges.com; true')
local('docker rm --force -v runserver.command.allmychanges.com; true')
local('docker stop mysql.allmychanges.com; true')
local('docker stop redis.allmychanges.com; true')
def delete_containers():
containers = (
'rqworker.command.allmychanges.com',
'runserver.command.allmychanges.com',
'mysql.allmychanges.com',
'redis.allmychanges.com')
for container in containers:
local('docker rm --force -v {0}; true'.format(container))
def _get_docker_containers():
import docker
import os
cert_path = os.environ['DOCKER_CERT_PATH']
tls_config = docker.tls.TLSConfig(
client_cert=(os.path.join(cert_path, 'cert.pem'),
os.path.join(cert_path, 'key.pem')),
verify=False,
)
cl = docker.Client(base_url=os.environ['DOCKER_HOST'].replace('tcp://', 'https://'),
tls=tls_config)
containers = cl.containers(all=True)
containers = [c['Names'][0].strip('/') for c in containers]
return containers