forked from hasadna/Open-Knesset
-
Notifications
You must be signed in to change notification settings - Fork 0
/
fabfile.py
151 lines (103 loc) · 3.47 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
from fabric.api import run, cd, sudo, roles, runs_once, env
from fabric.contrib.files import first
# add a local_fab_settings.py file,
# so that you can access your servers
# but please, don't commit it to git.
try:
from local_fab_settings import *
except ImportError as e:
pass
env.venv_roots = ['/oknesset_web/oknesset/', '/oknesset_data/oknesset/']
env.project_dir = 'Open-Knesset'
env.ok_user = 'oknesset'
def _venv_root():
return first(*env.venv_roots)
def _project_root():
return _venv_root() + env.project_dir
def _activate():
return 'source ' + _venv_root() + 'bin/activate'
def virtualenv(command):
with cd(_project_root()):
sudo(_activate() + ' && ' + command, user=env.ok_user)
# web server stuff
def web_apache_cmd(cmd):
if cmd not in ['start', 'stop', 'restart']:
raise Exception('Unknown apache command %s' % cmd)
sudo('/etc/init.d/apache2 %s' % cmd)
def restart_oknesset():
sudo('supervisorctl restart oknesset')
def _update_commit():
with cd(_project_root()):
sudo(
'git log --pretty=format:"Code Commit: %H <br>Last Update: %cd" -n 1 > templates/last_build.txt',
user=env.ok_user)
def _chown(to_user, directory=env.project_dir):
sudo("chown -R %s %s" % (to_user, directory))
@roles('web')
def deploy_web(buildout=False):
web_apache_cmd('stop')
with cd(_venv_root()):
_chown(env.ok_user)
with cd(env.project_dir):
_git_pull()
if buildout:
_install_requirements()
virtualenv('./manage.py collectstatic --noinput')
_update_commit()
#_chown('www-data')
restart_oknesset()
web_apache_cmd('start')
# db server stuff - should only run once on master db!
@runs_once
def db_migrate_syncdb():
virtualenv('./manage.py migrate')
@roles('db')
def deploy_backend(migration=False, requirements=False):
with cd(_project_root()):
_git_pull()
if requirements:
_install_requirements()
if migration:
db_migrate_syncdb()
@roles('db_master')
def show_cron(as_user=env.ok_user):
sudo('crontab -l', user=as_user)
@roles('db')
def db_show_replication():
# works on both servers
run('ps -ef | grep postgres | grep -e receiver -e sender')
# memcache commands
@roles('web')
@runs_once
def mc_flushall():
#run('echo flush_all | telnet localhost 11211')
virtualenv(
"DJANGO_SETTINGS_MODULE='knesset.settings' " +
"python -c 'from django.core.cache import cache; cache.clear()'"
)
# commands for all servers
def _git_pull(repo='origin', branch='master', as_user=env.ok_user):
sudo("git pull %s %s" % (repo, branch), user=as_user)
def _install_requirements():
virtualenv(
'cd .. && pip install -r ' +
env.project_dir + '/requirements.txt && cd ' + _project_root())
@roles('all')
def all_upgrade_system():
sudo('apt-get update')
sudo('apt-get upgrade')
@roles('all')
def show_updates():
sudo('cat /var/lib/update-notifier/updates-available')
sudo('/usr/lib/update-notifier/update-motd-reboot-required')
@roles('all')
def all_run_cmd(cmd):
run(cmd)
@roles('all')
def all_sudo_cmd(cmd):
sudo(cmd)
def deploy_all(repo='origin', branch='master', install_requirements=False, use_migration=False, reset_memcache=False):
deploy_backend(requirements=install_requirements, migration=use_migration)
deploy_web(buildout=install_requirements)
if reset_memcache:
mc_flushall()