/
fabfile.py
91 lines (68 loc) · 2.92 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
import os
from datetime import datetime
from spistresci.settings import SITE_ROOT as DEV_PROJECT_ROOT
from django.conf import settings
from fabric.api import local, run, cd, lcd, env, sudo, get, parallel
from fabric.contrib.console import confirm
from fabric.decorators import runs_once
DEFAULT_DB_SETTINGS = settings.DATABASES['default']
if not DEFAULT_DB_SETTINGS['PASSWORD']:
MYSQL_USER_PASSWD = 'mysql -u%s' % DEFAULT_DB_SETTINGS['USER']
else:
MYSQL_USER_PASSWD = 'mysql -u%s -p\'%s\'' % (DEFAULT_DB_SETTINGS['USER'], DEFAULT_DB_SETTINGS['PASSWORD'])
MYSQL_EXEC_CMD = '%s -e' % MYSQL_USER_PASSWD
env.use_ssh_config = getattr(settings, 'FABRIC_USE_SSH_CONFIG', False)
REPOS = ["backends/spistresci", "backends/egazeciarz", "frontends/egazeciarz",
"frontends/spistresci", "frontends/common", "frontends/ekundelek"]
DEFAULT_SERVER_VIRTUAL_ENV_DIR = '/home/frontend/repo/'
def get_remote_mysql_pass_arg():
"""Return password argument for mysql commands (if pw is set)"""
if env.database['PASSWORD']:
return '-p\'%s\'' % env.database['PASSWORD']
else:
return ''
def staging():
"""Sets up the staging environment for fab remote commands"""
from spistresci.settings.staging import SSH_HOSTS, DATABASES as STAGING_DATABASES
env.user = 'root'
env.hosts = SSH_HOSTS
env.database = STAGING_DATABASES['default']
env.remote_mysql_pw_arg = get_remote_mysql_pass_arg()
env.port = 1337
env.ENV = 'staging'
env.CODE_DIR = DEFAULT_SERVER_VIRTUAL_ENV_DIR
def prod():
"""Sets up the prod environment for fab remote commands"""
from spistresci.settings.prod import SSH_HOSTS, DATABASES as STAGING_DATABASES
env.user = 'root'
env.hosts = SSH_HOSTS
env.database = STAGING_DATABASES['default']
env.remote_mysql_pw_arg = get_remote_mysql_pass_arg()
env.port = 1337
env.ENV = 'prod'
env.CODE_DIR = DEFAULT_SERVER_VIRTUAL_ENV_DIR
def _launch(full=False):
"""Launch new code. Does a git pull, migrate and bounce"""
with cd(env.CODE_DIR):
for repo in REPOS:
path = os.path.join(env.CODE_DIR, repo)
print path
with cd(path):
run('git pull')
if full:
run('sudo pip install -r frontends/spistresci/requirements.pip')
css_path = os.path.join(env.CODE_DIR, 'frontends/spistresci/static/css')
scss_path = os.path.join(env.CODE_DIR, 'frontends/spistresci/static/scss')
run('rm -rf %s && mkdir %s' % (css_path, css_path))
run('sass --update %s:%s' % (scss_path, css_path))
with cd(os.path.join(env.CODE_DIR, 'frontends')):
run('python manage.py collectstatic --noinput')
run('python manage.py collectstatic --noinput -i *.scss')
run('find . -name \*.pyc -delete')
bounce()
def quicklaunch():
_launch(full=False)
def launch():
_launch(full=True)
def bounce():
sudo('sudo service apache2 restart', pty=False)