-
Notifications
You must be signed in to change notification settings - Fork 6
/
fabfile.py
247 lines (191 loc) · 7.26 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
from __future__ import with_statement
from fabric.api import run, env, sudo, cd, prefix, put
from fabric.contrib import django, files
from contextlib import contextmanager as _contextmanager
import os
SSH_HOME = "~/.ssh"
AUTH_KEYS = "~/.ssh/authorized_keys"
PUB_KEY = "~/.ssh/server_rsa.pub"
def test():
env.user = 'readbox'
env.hosts = ['']
env.key_filename = '~/.ssh/server_rsa'
env.activate = 'source ~/readbox_env/bin/activate'
django.settings_module('manhattan.settings_dev')
def deploydb():
env.user = 'root'
env.hosts = ['']
env.key_filename = '~/.ssh/server_rsa'
def deployfirst(host):
env.user = 'root'
env.hosts = host
env.key_filename = '~/.ssh/server_rsa'
def deploy(opt=False):
server_dict = {
'action': '',
}
env.user = 'ubuntu'
if not opt:
env.hosts = server_dict.values()
else:
env.hosts = [server_dict[str(opt)]]
env.activate = 'source ~/readbox_env/bin/activate'
env.key_filename = '~/.ssh/server_rsa'
django.settings_module('manhattan.settings')
@_contextmanager
def virtualenv():
with prefix(env.activate):
yield
def _get_public_key(key_file):
with open(os.path.expanduser(key_file)) as fd:
key = fd.readline().strip()
return key
def add_key(first=False, filename=PUB_KEY):
if first:
sudo('adduser ubuntu')
sudo('adduser ubuntu sudo')
sudo('mkdir /home/ubuntu/.ssh')
sudo('cp .ssh/authorized_keys /home/ubuntu/.ssh/')
sudo('chown ubuntu /home/ubuntu/.ssh/authorized_keys')
sudo('chown ubuntu /home/ubuntu/.ssh')
commands = (
"mkdir -p %s;"
"chmod 700 %s;"
"""echo "%s" >> %s;"""
"chmod 644 %s;"
)
t = (SSH_HOME, SSH_HOME, _get_public_key(filename), AUTH_KEYS, AUTH_KEYS)
command = commands % t
run(command)
if first:
put('config/sshd_config', '/etc/ssh/sshd_config', use_sudo=True)
sudo('service ssh restart')
def pre_setup_webserver():
run('ssh-keygen -t rsa -C "email@example.com"')
run('cat ~/.ssh/id_rsa.pub')
def setup_first():
sudo('apt-get --yes update')
sudo('apt-get --yes upgrade')
sudo('apt-get --yes install python-dev python-pip libevent-dev python-gevent supervisor git libmysqlclient-dev')
sudo('apt-get --yes install python-software-properties build-essential libxml2-dev libxslt-dev libmemcached-dev')
run('git config --global user.email "support@readbox.co"')
run('git config --global user.name "Readbox Server"')
run('git config --global credential.helper cache')
sudo('pip install virtualenv')
run('virtualenv readbox_env')
with virtualenv():
run('easy_install setuptools')
run('pip install gunicorn')
run('pip install gevent')
run('git clone git@github.com:vinceprignano/manhattan.git')
with cd('~/manhattan'):
run('pip install -r requirements.txt')
run('cp manhattan/settings_deploy.py manhattan/settings.py')
with cd('~'):
run('mkdir logs')
def setup_mysql(update=False):
if not update:
sudo('apt-get --yes install python-software-properties')
sudo('apt-key adv --recv-keys --keyserver hkp://keyserver.ubuntu.com:80 0xcbcb082a1bb943db')
sudo("add-apt-repository 'deb http://ftp.osuosl.org/pub/mariadb/repo/5.5/ubuntu precise main'")
put('config/preferences_mariadb', '/etc/apt/preferences.d/mariadb', use_sudo=True)
sudo('apt-get --yes update')
sudo('apt-get --yes upgrade')
sudo('apt-get --yes install mariadb-server')
def setup_elasticsearch():
sudo('apt-get update')
sudo('apt-get --yes install openjdk-7-jre-headless wget')
sudo('wget https://download.elasticsearch.org/elasticsearch/elasticsearch/elasticsearch-0.90.7.deb')
sudo('dpkg -i elasticsearch-0.90.7.deb')
sudo('service elasticsearch start')
def setup_nginx():
sudo('apt-get --yes install nginx chkconfig')
sudo('ln -s /usr/lib/insserv/insserv /sbin/insserv')
put('config/nginx.conf', '/etc/nginx/sites-available/default', use_sudo=True)
sudo('service nginx start')
sudo('chkconfig --add nginx')
sudo('chkconfig nginx on')
def setup_redis():
sudo('apt-get --yes install redis-server')
put('config/redis.conf', '/etc/redis/', use_sudo=True)
sudo('service redis-server stop')
try:
sudo('killall redis-server')
except Exception:
pass
sudo('service redis-server start')
def setup_memcached():
sudo('apt-get --yes install memcached')
def django_syncdb():
with virtualenv():
with cd('~/manhattan'):
run('python manage.py syncdb')
def django_collectstatic(remove=False):
with virtualenv():
with cd('~/manhattan'):
if remove:
run('rm -r STATIC')
run('python manage.py collectstatic')
def django_rebuild_index():
with virtualenv():
with cd('~/manhattan'):
run('python manage.py rebuild_index')
def django_migrate(app):
with virtualenv():
with cd('~/manhattan'):
run('python manage.py migrate ' + app)
def django_schemamigration(app):
with virtualenv():
with cd('~/manhattan'):
run('python manage.py schemamigration ' + app + ' --auto')
def install_requirements(full=False):
with virtualenv():
with cd('~/manhattan'):
if full:
run('pip freeze | xargs pip uninstall -y')
run('pip install -r requirements.txt')
def pull():
with virtualenv():
with cd('~/manhattan'):
run('git reset --hard')
run('git clean -f')
run('git pull')
run('cp manhattan/settings_deploy.py manhattan/settings.py')
def restart(service, num_workers=4):
if service == 'workers':
service = ''
for x in range(0, num_workers):
service += ' worker' + str(x)
sudo('supervisorctl restart ' + service)
def run_gunicorn():
with virtualenv():
with cd('~/manhattan'):
put('config/supervisor/gunicorn.conf', '/etc/supervisor/conf.d/', use_sudo=True)
sudo('supervisorctl reread')
sudo('supervisorctl update')
sudo('supervisorctl start gunicorn')
def run_celerybeat():
with virtualenv():
with cd('~/manhattan'):
put('config/supervisor/celerybeat.conf', '/etc/supervisor/conf.d/', use_sudo=True)
sudo('supervisorctl reread')
sudo('supervisorctl update')
sudo('supervisorctl start celerybeat')
def run_celeryd():
with virtualenv():
with cd('~/manhattan'):
put('config/supervisor/celeryd.conf', '/etc/supervisor/conf.d/', use_sudo=True)
sudo('supervisorctl reread')
sudo('supervisorctl update')
sudo('supervisorctl start celeryd')
def setup_workers(num=0):
with virtualenv():
with cd('~/manhattan'):
with open('config/supervisor/celeryWx.conf') as conf:
text = conf.read()
for x in range(0, int(num)):
files.append('/etc/supervisor/conf.d/worker' + str(num) + '.conf', text.format(x), use_sudo=True)
sudo('supervisorctl reread')
sudo('supervisorctl update')
def status():
sudo('supervisorctl status')