forked from 4l1fe/videobase
-
Notifications
You must be signed in to change notification settings - Fork 0
/
fabfile.py
288 lines (202 loc) · 8.22 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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
# coding: utf-8
from fabric.api import env, roles, run, settings, sudo, cd, local
import fabric
from fabtools import require
import fabtools
import os
from string import Template
env.hosts = ['188.226.191.166',]
env.user = 'root'
env.shell = "/bin/bash -c"
def setup():
# Require some Debian/Ubuntu packages
require.deb.packages([
'libxml2-dev',
'mercurial',
'memcached',
'libmemcached-dev',
'zlib1g-dev',
'libssl-dev',
'python-dev',
'build-essential',
'libjpeg-dev',
'libfreetype6-dev',
'zlib1g-dev',
'libpng12-dev',
'libpq-dev'
])
def init_db():
"""
Создает базу данных и пользователя для основного сайта
"""
with settings(sudo_user = "postgres"):
sudo('''echo "CREATE USER pgadmin WITH PASSWORD 'qwerty'; CREATE DATABASE videobase; GRANT ALL PRIVILEGES ON DATABASE videobase to pgadmin;" | psql''')
def init_test_db():
"""
Создает базу данных и пользователя для тест сайта
"""
with settings(sudo_user = "postgres"):
sudo('''echo "CREATE USER pgadmin WITH PASSWORD 'qwerty'; CREATE DATABASE videobase_test; GRANT ALL PRIVILEGES ON DATABASE videobase_test to pgadmin;" | psql''')
def populate_test_db():
"""
Заполняет тестовую базу данных из sql_dump который идет с кодом
"""
with cd('/var/www/videobase_test/sql_dump'):
with settings(sudo_user = "postgres"):
sudo('''psql -d videobase_test -f $(ls -1 *.sql | head -1)''')
def local_db_reset():
'''
Перезаписать локальную базу из репозитория
'''
local('''echo "DROP DATABASE videobase;" | sudo -u postgres psql''')
local('''echo "CREATE USER pgadmin WITH PASSWORD 'qwerty'; CREATE DATABASE videobase; GRANT ALL PRIVILEGES ON DATABASE videobase to pgadmin;" | sudo -u postgres psql''')
local("""cd sql_dump && sudo -u postgres psql -d videobase -f $(ls -1 *.sql | head -1)""")
local('''echo "DROP DATABASE test_base;" | sudo -u postgres psql ''')
local('''echo "CREATE DATABASE test_base; ALTER DATABASE test_base OWNER TO pgadmin;" | sudo -u postgres psql''')
def setup_system_libraries():
'''
'''
pass
def deploy_test_code():
"""
Обновляет или создает код для тестового сайта
ВНИМАНИЕ Скрипт редактирует db.ini для того чтобы ссылаться на тестовую базу данных
"""
with settings(sudo_user = "www-data"):
with cd('/var/www'):
result = str(sudo('ls -1')).strip()
filtered_array=[s for s in result.split('\r\n') if s=='test_base']
if filtered_array:
#sudo("cd videobase_test; git checkout configs/db.ini")
#sudo("cd videobase_test; cat configs/db.ini")
sudo('cd test_base;git pull')
#sudo("cd videobase_test/configs/ && sed -i 's/videobase/videobase_test/g' db.ini")
#sudo("cd videobase_test; cat configs/db.ini")
else:
sudo('git clone [email protected]:developers/videobase.git test_base')
sudo("cd test_base/configs/ && cp db.ini.example db.ini && sed -i 's/videobase/test_base/g' db.ini")
def restart_all():
"""
Перезапуск всех процессов
"""
sudo('service supervisor stop')
sudo('sleep 10;service supervisor start')
sudo('service nginx stop')
sudo('service nginx start')
def initial_test_deploy():
"""
Последовательность команд для выгрузки на новую машину
"""
init_test_db()
deploy_test_code()
populate_test_db()
def delete_test_db():
"""
Удалить тестовую базу данных
"""
with cd('/var/lib/postgresql'):
with settings(sudo_user = "postgres"):
sudo('''echo "DROP DATABASE videobase_test;" | psql''')
def refresh_test_requirements():
with settings(sudo_user = "www-data"):
with cd('/var/www/videobase_test/'):
sudo('/home/virtualenv/videobase_test/bin/pip install -r requirements.txt')
def status():
for k in fabric.state.output:
fabric.state.output[k] = False
fabric.state.output['user']=True
with settings(warn_only=True):
print('SUPERVISOR \n')
print(run("supervisorctl status"))
print("\n NGINX \n")
print(run("ps aux |grep nginx"))
print("\nIs login page is shown on videobase.test.aaysm.com/admin ? ")
print(bool(run('''wget -qO- --header="Host: videobase.test.aaysm.com" localhost:80/admin/ |grep Password | wc -l''')))
for k in fabric.state.output:
fabric.state.output[k] = True
fabric.state.output['debug']=False
def db_migrate_test(appname=''):
'''
Выполняет миграцию базы данных для приложения (app в терминах Django) если указано, или просто m
'''
with settings(sudo_user = "www-data"):
with cd('/var/www/videobase_test/'):
sudo('/home/virtualenv/videobase_test/bin/python manage.py migrate %s --no-initial-data' % appname)
def collect_static():
"""
build/update static files
"""
with settings(sudo_user = "www-data"):
with cd('/var/www/videobase_test/'):
sudo('/home/virtualenv/videobase_test/bin/python manage.py collectstatic --dry-run --noinput')
def deploy():
"""
Обновить код и перезапустить процессы
"""
deploy_test_code()
restart_all()
status()
def project_deploy():
'''
Обновить весь код
'''
setup()
deploy_test_code()
refresh_test_requirements()
db_migrate_test()
collect_static()
restart_all()
status()
def db_flush_test():
"""
Перезаписать тестовую базу данных из дампа
"""
delete_test_db()
init_test_db()
populate_test_db()
def init_if_not_exists_task():
"""
Обновить если пользователя еще нет.
Оставил как заготовку
"""
with settings(sudo_user = "postgres"):
with cd('/var/lib/postgresql'):
if not (str(sudo('''echo "select rolname from pg_roles where rolname = 'pgadmin';" |psql -tA''')).strip()):
init_db()
def scheme():
local('python ./manage.py graph_models -a -g -o current.png')
def show_scheme():
scheme()
local('feh current.png')
def generate_robots_conf(python_interpreter = None, videobase_dir = None, user= None):
if python_interpreter is None:
python_interpreter = local("which python",capture=True)
if videobase_dir is None:
videobase_dir = os.path.abspath('.')
if user is None:
user = local('whoami',capture=True)
robots_list = local('python manage.py list_robots',capture=True).split()
print robots_list
template = Template("""[program:$name]
command=$interpreter manage.py robot_start --site $name
process_name=%(program_name)s ; process_name expr (default %(program_name)s)
numprocs=1 ; number of processes copies to start (def 1)
directory=$workdir ; directory to cwd to before exec (def no cwd)
umask=022 ; umask for process (default None)
autostart=false ; start at supervisord start (default: true)
autorestart=false ; retstart at unexpected quit (default: true)
startretries=1 ; max # of serial start failures (default 3)
user=$user ; setuid to this UNIX account to run the program
redirect_stderr=true ; redirect proc stderr to stdout (default false)
stdout_logfile=/var/log/$name.log""")
config = '\n'.join(template.substitute({'interpreter':python_interpreter,
'user':user,
'workdir': videobase_dir,
'name': robot_name
}) for robot_name in robots_list)
return config
def set_local_robot_config():
with open('configs/robots.conf','w') as fw:
fw.write(generate_robots_conf())
local('sudo cp configs/robots.conf /etc/supervisor/conf.d/robots.conf')
local('sudo service supervisor restart')