-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathfabfile.py
416 lines (306 loc) · 13.6 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
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
import logging
from fabric.api import env, task, sudo, cd, settings, require, prefix
from fabric.contrib.files import upload_template, contains, append, exists
from deploy_settings import *
logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__)
def _add_virtualenv_settings_to_profile(profile_file):
if not exists(profile_file):
logger.info("Creating user profile: {}".format(profile_file))
sudo('touch %s' % profile_file,
user=DEPLOYMENT_USER)
lines_to_append = [
'export WORKON_HOME={workon_home}'.format(workon_home=WORKON_HOME),
'export PROJECT_HOME={remote_deploy_dir}'.format(remote_deploy_dir=REMOTE_DEPLOY_DIR),
'export VIRTUALENVWRAPPER_PYTHON={python_path}'.format(python_path='/usr/bin/python3.5'),
'source /usr/local/bin/virtualenvwrapper.sh',
]
for line in lines_to_append:
if not contains(profile_file, line):
append(profile_file, '\n' + line,
use_sudo=True)
sudo('chown {user}:{group} {file}'
.format(user=DEPLOYMENT_USER,
group=DEPLOYMENT_GROUP,
file=profile_file))
def _load_environment(environment_name):
logger.info('Loading environment...')
if environment_name not in ENVIRONMENTS:
raise ValueError("Incorrect environment name {env_name}".format(env_name=environment_name))
_environment = ENVIRONMENTS[environment_name]
user = input('Please enter user name for deploying: ')
key_filename = input('Please enter path to ssh key file (By default: ~/.ssh/id_rsa): ')
if not key_filename:
key_filename = _environment.get('KEY_FILENAME')
env.user = user
env.hosts = ['{host}:{port}'.format(host=_environment.get('HOST', ''), port=_environment.get('SSH_PORT', ''))]
env.branch = _environment.get('GIT_BRANCH')
env.key_filename = key_filename
env.settings_module = _environment.get('SETTINGS_MODULE')
env.is_production = _environment.get('IS_PRODUCTION')
env.is_certbot_cert = _environment.get('IS_CERTBOT_CERT')
env.steepshotbot_domain = _environment.get('STEEPSHOTBOT_DOMAIN')
def _get_systemd_service_path(service_name):
if not service_name.endswith(('.service', '.unit')):
service_name += '.service'
return '/etc/systemd/system/{}'.format(service_name)
def _is_systemd_service_running(service_name):
with settings(warn_only=True):
status_reply = sudo('systemctl --no-pager --full status %s'
% service_name)
return 'inactive' not in status_reply
def _restart_systemd_service(service_name):
with settings(warn_only=True):
if _is_systemd_service_running(service_name):
sudo('systemctl stop %s' % service_name, user='root')
sudo('systemctl start %s' % service_name, user='root')
def _install_service(service_name, context):
logger.info('Copying systemd services "{service_name}"'.format(service_name=service_name))
remote_service = _get_systemd_service_path(service_name)
local_template = os.path.join(LOCAL_CONF_DIR, service_name)
if not os.path.exists(local_template):
logger.error('Template "{local_template}" does not exist.'.format(local_template=local_template))
raise ValueError()
upload_template(local_template,
remote_service,
context=context,
use_sudo=True,
backup=False)
sudo('chown root:root {remote_service}'.format(remote_service=remote_service))
sudo('systemctl daemon-reload')
sudo('systemctl enable {}'.format(service_name))
@task
def create_non_priveledged_user():
require('user')
logger.info('Creating non priveledged user: {user}'.format(user=DEPLOYMENT_USER))
with settings(warn_only=True):
sudo('adduser --disabled-login --gecos os {deployment_user}'.format(deployment_user=DEPLOYMENT_USER))
sudo('addgroup {deployment_group}'.format(deployment_group=DEPLOYMENT_GROUP))
sudo('adduser {user} {group}'.format(user=DEPLOYMENT_USER, group=DEPLOYMENT_GROUP))
@task
def install_system_packages():
require('user')
logger.info('Installing system packages...')
with settings(warn_only=True):
sudo('add-apt-repository ppa:fkrull/deadsnakes -y')
if env.is_certbot_cert:
sudo('add-apt-repository ppa:certbot/certbot -y')
sudo('wget -q https://www.postgresql.org/media/keys/ACCC4CF8.asc -O - | sudo apt-key add -')
sudo("""sudo sh -c 'echo "deb http://apt.postgresql.org/pub/repos/apt/ `lsb_release -cs`-pgdg main" > /etc/apt/sources.list.d/pgdg.list'""")
sudo('apt-get update')
sudo('apt-get -y --no-upgrade install %s' % ' '.join(UBUNTU_PACKAGES))
@task
def prepare_virtualenv():
require('user')
logger.info("Setting up the virtual environment.")
with settings(warn_only=True):
sudo('pip install virtualenv')
sudo('pip install virtualenvwrapper')
_add_virtualenv_settings_to_profile(USER_PROFILE_FILE)
@task
def create_postgresdb():
"""
Create postgres database and dedicated user
"""
logger.info("Setting the database.")
with settings(warn_only=True):
# Create database user
with prefix("export PGPASSWORD=%s" % DB_PASSWORD):
sudo('psql -c "CREATE ROLE %s WITH CREATEDB CREATEUSER LOGIN ENCRYPTED PASSWORD \'%s\';"' % (DB_USER,
DB_PASSWORD),
user='postgres')
sudo('psql -c "CREATE DATABASE %s WITH OWNER %s"' % (DB_NAME, DB_USER),
user='postgres')
@task
def clearing_project_directory():
require('is_production')
with cd(REMOTE_DEPLOY_DIR), settings(warn_only=True):
if exists(PROJECT_NAME, use_sudo=True):
sudo('rm -rf {project_dir}'.format(project_dir=PROJECT_NAME))
@task
def checkout_repository():
require('is_production')
with cd(REMOTE_DEPLOY_DIR), settings(sudo_user=DEPLOYMENT_USER):
if not exists(PROJECT_NAME, use_sudo=True):
sudo('git clone %s %s' % (REPOSITORY, PROJECT_NAME))
sudo('chown -R {user}:{group} {dir}'.format(
user=DEPLOYMENT_USER,
group=DEPLOYMENT_GROUP,
dir=PROJECT_NAME
))
@task
def create_deploy_dirs(remote_conf_path):
with cd(DEPLOY_DIR):
sudo('mkdir -p logs', user=DEPLOYMENT_USER)
sudo('chown -R {user}:{group} logs/'.format(user=DEPLOYMENT_USER,
group=DEPLOYMENT_GROUP))
sudo('mkdir -p {remote_conf_path}'.format(remote_conf_path=remote_conf_path),
user=DEPLOYMENT_USER)
sudo('chown -R {user}:{group} {remote_conf_path}'.format(user=DEPLOYMENT_USER,
group=DEPLOYMENT_GROUP,
remote_conf_path=remote_conf_path))
@task
def deploy_files():
require('branch')
with cd(DEPLOY_DIR), settings(sudo_user=DEPLOYMENT_USER):
sudo('git fetch')
sudo('git reset --hard')
sudo('git checkout {}'.format(env.branch))
sudo('git pull origin {}'.format(env.branch))
@task
def config_nginx():
if exists('/etc/nginx/sites-available/default'):
with settings(warn_only=True):
sudo('rm /etc/nginx/sites-available/default')
remote_sa_path = '/etc/nginx/sites-available/{project_name}.conf'.format(project_name=PROJECT_NAME)
context = {
'HOST': HOST,
'CURRENT_HOST': CURRENT_HOST,
'DEPLOY_DIR': DEPLOY_DIR,
'GUNI_HOST': GUNI_HOST,
'GUNI_PORT': GUNI_PORT
}
upload_template(template_dir=LOCAL_CONF_DIR,
filename='nginx.conf.j2',
destination=remote_sa_path,
context=context,
backup=False,
mode=0o0644,
use_sudo=True,
use_jinja=True)
sudo('chown root:root {remote_sa_path}'.format(remote_sa_path=remote_sa_path))
sudo('ln -sf %s /etc/nginx/sites-enabled' % remote_sa_path)
@task
def config_gunicorn(remote_conf_path):
context = {
'DEPLOY_DIR': DEPLOY_DIR,
'ENV_PATH': ENVIRONMENT_PATH,
'GUNI_HOST': GUNI_HOST,
'GUNI_PORT': GUNI_PORT,
'GUNI_WORKERS': GUNI_WORKERS,
'GUNI_TIMEOUT': GUNI_TIMEOUT,
'GUNI_GRACEFUL_TIMEOUT': GUNI_GRACEFUL_TIMEOUT,
'USER': DEPLOYMENT_USER,
'GROUP': DEPLOYMENT_GROUP
}
upload_template(os.path.join(LOCAL_CONF_DIR, 'gunicorn.sh'),
remote_conf_path,
context=context,
backup=False,
mode=0o0750,
use_sudo=True)
sudo('chown {user}:{group} {remote_conf_path}/gunicorn.sh'.format(user=DEPLOYMENT_USER,
group=DEPLOYMENT_GROUP,
remote_conf_path=remote_conf_path))
@task
def install_systemd_services():
services = [STEEPSHOTBOT_SERVICE]
common_context = {
'PROJECT_NAME': PROJECT_NAME,
'USER': DEPLOYMENT_USER,
'GROUP': DEPLOYMENT_GROUP,
'DEPLOY_DIR': DEPLOY_DIR
}
for service in services:
_install_service(service, common_context)
@task
def install_virtualenv():
logger.info("Creating clean virtual environment.")
with prefix('source {profile}'.format(profile=USER_PROFILE_FILE)):
with settings(warn_only=True), cd(REMOTE_DEPLOY_DIR):
logger.info('Deleting old virtualenv.')
sudo('rmvirtualenv {virtualenv_name}'.format(virtualenv_name=ENVIRONMENT_NAME),
user=DEPLOYMENT_USER)
logger.info("Creating a new virtualenv.")
sudo('mkvirtualenv {environment_name} -p /usr/bin/python3.5'.format(environment_name=ENVIRONMENT_NAME),
user=DEPLOYMENT_USER)
@task
def config_virtualenv():
remote_postactivate_path = os.path.join(WORKON_HOME,
ENVIRONMENT_NAME,
'bin/postactivate')
postactivate_context = {
'DATABASE_URL': 'postgres://%s:%s@%s:%s/%s' % (DB_USER, DB_PASSWORD, DB_HOST, DB_PORT, DB_NAME),
'IS_PRODUCTION': env.is_production,
'IS_CERTBOT_CERT': env.is_certbot_cert,
'STEEPSHOTBOT_DOMAIN': env.steepshotbot_domain,
'FLASK_APP': FLASK_APP,
'DEPLOY_DIR': DEPLOY_DIR,
'TELEGRAM_BOT_TOKEN': input('Please enter your token: ')
}
upload_template(os.path.join(LOCAL_CONF_DIR, 'postactivate'),
remote_postactivate_path, context=postactivate_context,
backup=False, use_sudo=True)
sudo('chown {user}:{group} {remote_postactivate_path}'.format(user=DEPLOYMENT_USER,
group=DEPLOYMENT_GROUP,
remote_postactivate_path=remote_postactivate_path))
@task
def install_req():
logger.info("Installing python requirements.")
with cd(DEPLOY_DIR), prefix('source %s' % VENV_ACTIVATE):
with settings(sudo_user=DEPLOYMENT_USER):
sudo('pip install --upgrade pip')
# Parameter --upgrade allows install a new packages
# from requirements.txt. For example: golosdata>=0.0.4 was set in requirement.txt,
# if a new version of golosdata appear into pypi repository the package will be upgrade
sudo('pip install --no-cache-dir --upgrade -r {req_file}'
.format(req_file='requirements.txt'))
@task
def clean_pyc():
logger.info("Cleaning .pyc files.")
with cd(DEPLOY_DIR):
sudo("find . -name '*.pyc'")
sudo('find . -name \*.pyc -delete')
@task
def check_nginx_service():
sudo('systemctl --no-pager --full status {service_name}'.format(service_name=NGINX_SERVICE))
@task
def check_steepshotbot_service():
sudo('systemctl --no-pager --full status {service_name}'.format(service_name=STEEPSHOTBOT_SERVICE))
@task
def check_statuses():
with settings(warn_only=True):
services_to_check = [NGINX_SERVICE,
STEEPSHOTBOT_SERVICE]
for service in services_to_check:
sudo('systemctl --no-pager --full status {service_name}'.format(service_name=service))
@task
def restart_nginx_service():
sudo('systemctl stop {service_name}'.format(service_name=NGINX_SERVICE))
sudo('systemctl start {service_name}'.format(service_name=NGINX_SERVICE))
@task
def restart_steepshotbot_service():
sudo('systemctl stop {service_name}'.format(service_name=STEEPSHOTBOT_SERVICE))
sudo('systemctl start {service_name}'.format(service_name=STEEPSHOTBOT_SERVICE))
@task
def restart():
services_to_restart = [NGINX_SERVICE,
STEEPSHOTBOT_SERVICE]
for service_name in services_to_restart:
_restart_systemd_service(service_name)
@task
def production():
_load_environment('PRODUCTION')
@task
def first_time_deploy():
if not exists(REMOTE_DEPLOY_DIR):
create_non_priveledged_user()
install_system_packages()
prepare_virtualenv()
create_postgresdb()
deploy()
@task
def deploy():
remote_conf_path = '%s/conf' % DEPLOY_DIR
clearing_project_directory()
checkout_repository()
create_deploy_dirs(remote_conf_path)
deploy_files()
config_nginx()
config_gunicorn(remote_conf_path)
install_systemd_services()
install_virtualenv()
config_virtualenv()
install_req()
clean_pyc()
restart()