-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathfabfile.py
370 lines (315 loc) · 11.5 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
import sys
from fabric.api import env, require, run, sudo, put, cd, lcd, task, abort
from fabric.api import settings as fabric_settings
from fabric.api import local as lrun
from fabric.contrib.console import confirm
from fabric.network import ssh
from fabric.colors import red, green
# ssh.util.log_to_file("paramiko.log", 10)
IMPORT_ERROR = 'Please add the location of \n DEPLOY_KEY, \n STAGE_KEY, \n LOCAL_PROJECT_PATH, \n LOCAL_ENVIRONMENT_PATH in adomattic.conf.fabric.variables'
#importing fabric specific variables
try:
from adomattic.conf.fabric.variables import DEPLOY_KEY, STAGE_KEY, LOCAL_PROJECT_PATH, LOCAL_ENVIRONMENT_PATH
except ImportError:
sys.exit(IMPORT_ERROR)
def local():
"""
Environment settings for local.
Usage:
fab local <task>
"""
env.run = lrun
env.cd = lcd
env.name = 'local'
env.conf_path = 'local'
env.project_root = LOCAL_PROJECT_PATH
env.branch = 'develop'
env.venv_root = LOCAL_ENVIRONMENT_PATH
env.venv = 'source %(venv_root)sbin/activate && ' % env
env.dashboard = '%(project_root)smagneto/dashboard/' % env
env.impressions = '%(project_root)smagneto/impressions/' % env
env.emails = '%(project_root)smagneto/emails/' % env
def stage():
"""
Environment Settings for Staging Server
Usage:
fab stage <task>
"""
env.run = run
env.sudo = sudo
env.cd = cd
env.name = 'ia-stage'
env.conf_path = 'stage'
env.project_root = '/srv/%(name)s/' % env
env.hosts = ['54.213.228.61']
env.user = 'ec2-user'
env.key_filename = STAGE_KEY
# env.no_keys = True
# env.use_ssh_config = False
env.branch = 'develop'
env.venv_root = '/srv/%(name)s/' % env
env.venv = 'source /srv/%(name)s/bin/activate && ' % env
env.dashboard = '/srv/%(name)s/magneto/dashboard/' % env
env.impressions = '/srv/%(name)s/magneto/impressions/' % env
env.emails = '/srv/%(name)s/magneto/emails/' % env
def live():
"""
Environment Settings for Staging Server
Usage:
fab live <task>
"""
env.run = run
env.sudo = sudo
env.cd = cd
env.name = 'ia-live'
env.conf_path = 'live'
env.project_root = '/srv/%(name)s/' % env
env.hosts = ['52.11.62.128']
env.user = 'ec2-user'
env.key_filename = DEPLOY_KEY
# env.no_keys = True
# env.use_ssh_config = False
env.branch = 'master'
env.venv_root = '/srv/%(name)s/' % env
env.venv = 'source /srv/%(name)s/bin/activate && ' % env
env.dashboard = '/srv/%(name)s/magneto/dashboard/' % env
env.impressions = '/srv/%(name)s/magneto/impressions/' % env
env.emails = '/srv/%(name)s/magneto/emails/' % env
if confirm(red('You are about to deploy on live servers, Do you want to continue?'), default=False):
print(red('You selected continue ....'))
else:
abort(green('Perhaps some othertime :)'))
def lab():
"""
Environment Settings for Staging Server
Usage:
fab live <task>
"""
env.run = run
env.sudo = sudo
env.cd = cd
env.name = 'lab'
env.conf_path = 'lab'
env.project_root = '/srv/%(name)s/' % env
env.hosts = ['ec2-54-198-177-190.compute-1.amazonaws.com', ]
env.user = 'ec2-user'
env.key_filename = DEPLOY_KEY
# env.no_keys = True
# env.use_ssh_config = False
env.branch = 'master'
env.venv_root = '/srv/%(name)s/' % env
env.venv = 'source /srv/%(name)s/bin/activate && ' % env
env.dashboard = '/srv/%(name)s/magneto/dashboard/' % env
env.impressions = '/srv/%(name)s/magneto/impressions/' % env
env.emails = '/srv/%(name)s/magneto/emails/' % env
env.git_path = 'https://github.com/innovation-labs/Vader.git'
if confirm(red('You are about to deploy on live servers, Do you want to continue?'), default=False):
print(red('You selected continue ....'))
else:
abort(green('Perhaps some othertime :)'))
def update_envs():
"""
Updates local environment settings to the default repo settings, useful for parallel programming
Usage:
fab local update_envs
"""
with env.cd(env.project_root):
env.run('cp adomattic/conf/%(conf_path)s/settings.py adomattic/settings/local.py' % env)
def apt():
"""
For things that need to be installed via apt-get.
These are installed before requirements.txt in the venv otherwise some python modules won't install properly
"""
env.run('sudo apt-get update')
env.run("sudo apt-get install locate python-setuptools git-core subversion mercurial htop screen byobu gcc")
env.run('sudo apt-get install libjpeg62 libjpeg62-dev zlib1g-dev libfreetype6 libfreetype6-dev python-pycurl-dbg libcurl4-openssl-dev')
env.run('sudo apt-get install build-essential libpq-dev python-dev')
env.run('sudo apt-get install libxml2-dev libxslt-dev')
env.run('sudo apt-get install nginx-full uwsgi uwsgi-plugin-python')
env.run('sudo apt-get install python-pip')
env.run('sudo apt-get install libreadline6 libreadline6-dev libncurses5-dev')
env.run('sudo apt-get install libffi-dev libssl-dev')
# Install Postgres 9.4 (json field)
env.run("""sudo sh -c 'echo "deb http://apt.postgresql.org/pub/repos/apt/ $(lsb_release -cs)-pgdg main" > /etc/apt/sources.list.d/pgdg.list'""")
env.run('sudo apt-get install wget ca-certificates')
env.run('wget --quiet -O - https://www.postgresql.org/media/keys/ACCC4CF8.asc | sudo apt-key add -')
env.run('sudo apt-get update')
env.run('sudo apt-get upgrade')
env.run('sudo apt-get install postgresql-9.4 postgresql-client-9.4 postgresql-contrib-9.4 libpq-dev')
env.run('sudo pip install virtualenv')
# Install latest node and npm
env.run('curl -sL https://deb.nodesource.com/setup_5.x | sudo -E bash -')
env.run('sudo apt-get install -y nodejs')
# Install bower gulp and yo
env.run('sudo npm i -g bower gulp yo')
def yum():
env.sudo('yum -y update')
env.sudo('yum -y groupinstall "Development tools"')
env.sudo('yum -y install zlib-devel')
env.sudo('yum -y install python27-devel python27-tools')
env.sudo('yum -y install python27-pip')
env.sudo('yum -y install ibxml2-devel libxslt-devel geos')
env.sudo('yum -y install freetype-devel freetype-demos libjpeg* pngquant lcms2* libtiff* openjpeg* libwebp-devel tcl-devel tk-devel')
env.sudo('yum install -y gcc openssl-devel libyaml-devel libffi-devel readline-devel zlib-devel gdbm-devel ncurses-devel')
# Adding extra packages
env.sudo('yum-config-manager --enable epel')
env.sudo('yum -y install postgresql94-libs postgresql94-devel')
env.sudo('yum -y install nginx')
env.sudo('curl -sL https://rpm.nodesource.com/setup_5.x | bash -')
env.sudo('yum -y install nodejs')
#env.sudo('yum -y install uwsgi uwsgi-plugin-python')
env.sudo('npm i -g bower gulp yo')
def brew():
print "Warning!!"
print "On a slow internet, this may take time!!"
env.run('brew update')
env.run('brew upgrade')
env.run('brew tap homebrew/bundle')
env.run('brew tap homebrew/dupes')
env.run('brew tap homebrew/versions')
env.run("brew install openssl openjpeg jpeg libpng gd zlib pngquant imagemagick")
env.run("brew install elasticsearch rabbitmq")
env.run("brew install git-flow git-extras")
env.run("brew install geos geoip")
env.run("brew install node")
env.run("brew install openssl wget")
env.run("npm install -g gulp bower")
def prepare():
"""
Prepare your system, install necessary libraries and data
"""
import platform
system = env.run("uname -s")
print system
system = system.lower()
# system = platform.system().lower()
if system == 'darwin':
brew()
get_ipdb()
elif system == 'linux':
# if platform.dist()[0].lower() == 'ubuntu':
# apt()
# else:
# yum()
version = "uname -a"
if "ubuntu" in version.lower():
apt()
else:
yum()
get_ipdb()
else:
sys.exit('Unrecognized system, prepare your system manually')
def virtualenv_setup():
"""
The third step
"""
env.run("/usr/bin/virtualenv --no-site-packages %(venv_root)s" % env)
with env.cd(env.project_root):
env.run("mkdir logs")
env.run("touch logs/error-django.log")
def clone():
"""
This second step of server setup
"""
env.run("sudo mkdir %(project_root)s" % env)
env.run("sudo chown -R ec2-user:ec2-user %(project_root)s" % env)
git_path = getattr(env.git_path)
if git_path:
env.run("git glone --recursive %(git_path)s %(project_root)s" %env)
else:
env.run("git clone --recursive [email protected]:intentaware/Vader.git %(project_root)s" % env)
def git_pull():
"""
pull from git
Usage:
fab <env> git_pull
"""
with env.cd(env.project_root):
env.run('git fetch' % env)
env.run('git checkout %(branch)s; git pull' % env)
env.run('git submodule update --init --recursive' % env)
#env.run('git checkout %(branch)s; git reset --hard origin/%(branch)s' % env)
def install_requirements():
"""
install the environment python packages
Usage:
fab <env> install_requirements
"""
with env.cd(env.project_root):
env.run('%(venv)s pip install -r requirements.txt' % env)
def migrate():
"""
migrates the database
"""
with env.cd(env.project_root):
env.run('%(venv)s python manage.py migrate' % env)
def collect_static():
with env.cd(env.project_root):
env.run('%(venv)s python manage.py collectstatic --noinput -i node_modules' % env)
def uwsgi_install():
"""
first install of uwsgi
"""
with env.cd(env.project_root):
env.run('%(venv)s pip install uwsgi' % env)
def copy_nginx_conf():
"""
update nginx settings for the site and make them available in sites-available
"""
env.sudo('cp /srv/%(name)s/adomattic/conf/%(conf_path)s/nginx/%(conf_path)s.conf /etc/nginx/sites-available/%(conf_path)s.conf' % env)
def restart_uwsgi():
with env.cd(env.project_root):
env.run('touch uwsgi/touch.py')
def npm():
with env.cd(env.dashboard):
env.run('npm install')
with env.cd(env.impressions):
env.run('npm install')
with env.cd(env.emails):
env.run('npm install')
def bower():
with env.cd(env.dashboard):
env.run('bower install --allow-root')
with env.cd(env.impressions):
env.run('bower install --allow-root')
def gulp():
with env.cd(env.dashboard):
env.run('gulp build')
with env.cd(env.impressions):
env.run('gulp aware --%(conf_path)s' % env)
env.run('gulp guages --%(conf_path)s' % env)
with env.cd(env.emails):
env.run('gulp emails')
def clean_pyc():
with env.cd(env.project_root):
env.run('find . -name "*.pyc" -exec rm -rf {} \;')
def setup_magneto():
"""
sets up the front end for environment
"""
npm()
bower()
gulp()
def rabbitmq():
with env.cd(env.project_root):
sudo('service rabbitmq-server restart')
def get_ipdb():
"""
gets the latest ipdb file from maxmind
"""
with env.cd(env.project_root):
env.run('mkdir adomattic/ipdb')
env.run('wget -P adomattic/ipdb/ http://geolite.maxmind.com/download/geoip/database/GeoLite2-City.mmdb.gz')
env.run('gunzip adomattic/ipdb/GeoLite2-City.mmdb.gz')
def deploy():
"""
pull the latest from the repo, and deploy accordingly
"""
git_pull()
install_requirements()
update_envs()
migrate()
bower()
gulp()
collect_static()
restart_uwsgi()