-
Notifications
You must be signed in to change notification settings - Fork 9
/
fabfile.py
224 lines (167 loc) · 5.76 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
# -*- coding: utf-8 -*-
"""Fabric file for managing this project.
See: http://www.fabfile.org/
"""
# Standard Library
import os
from contextlib import contextmanager as _contextmanager
from functools import partial
from os.path import dirname, isdir, join
# Third Party Stuff
from fabric import api as fab
from fabric.api import env
from fabric.api import local as fabric_local
local = partial(fabric_local, shell='/bin/bash')
HERE = dirname(__file__)
# ==========================================================================
# Settings
# ==========================================================================
env.project_name = 'nexus'
env.apps_dir = join(HERE, env.project_name)
env.docs_dir = join(HERE, 'docs')
env.static_dir = join(env.apps_dir, 'static')
env.virtualenv_dir = join(HERE, 'venv')
env.requirements_file = join(HERE, 'requirements/development.txt')
env.shell = '/bin/bash -l -i -c'
env.use_ssh_config = True
env.dotenv_path = join(HERE, '.env')
env.config_setter = local
def init(vagrant=False):
"""Prepare a local machine for development."""
install_requirements()
add_pre_commit()
if not os.getenv('CI', 'False').lower() == 'true':
local('createdb %(project_name)s' % env) # create postgres database
manage('migrate')
def install_requirements(file=env.requirements_file):
"""Install project dependencies."""
verify_virtualenv()
# activate virtualenv and install
with virtualenv():
local('pip install -r %s' % file)
def add_pre_commit():
verify_virtualenv()
# activate virtualenv and install pre-commit hooks
with virtualenv():
local('pre-commit install')
def serve_docs(options=''):
"""Start a local server to view documentation changes."""
with fab.lcd(HERE) and virtualenv():
local('mkdocs serve {}'.format(options))
def shell():
manage('shell_plus')
def test(options='--pdb --cov'):
"""Run tests locally. By Default, it runs the test using --ipdb.
You can skip running it using --ipdb by running - `fab test:""`
"""
with virtualenv():
local('flake8 .')
local('pytest %s' % options)
def serve(host='127.0.0.1:8000'):
"""Run local developerment server, making sure that dependencies and
database migrations are upto date.
"""
install_requirements()
migrate()
manage('runserver %s' % host)
def celery():
"""Run local celery, making sure that dependencies and
database migrations are upto date.
"""
install_requirements()
migrate()
local('celery worker -A nexus -B -l INFO --concurrency=2')
def makemigrations(app=''):
"""Create new database migration for an app."""
manage('makemigrations %s' % app)
def migrate():
"""Apply database migrations."""
manage('migrate')
def createapp(appname):
"""fab createapp <appname>
"""
path = join(env.apps_dir, appname)
local('mkdir %s' % path)
manage('startapp %s %s' % (appname, path))
# Enviroments & Deployments
# ---------------------------------------------------------
def dev():
env.host_group = 'dev'
env.remote = 'origin'
env.branch = 'master'
env.hosts = ['dev.nexus.com']
env.dotenv_path = '/home/ubuntu/dev/nexus/.env'
env.config_setter = fab.run
def qa():
env.host_group = 'qa'
env.remote = 'origin'
env.branch = 'qa'
env.hosts = ['qa.nexus.com']
env.dotenv_path = '/home/ubuntu/qa/nexus/.env'
env.config_setter = fab.run
def prod():
env.host_group = 'production'
env.remote = 'origin'
env.branch = 'prod'
env.hosts = ['prod.nexus.com']
env.dotenv_path = '/home/ubuntu/prod/nexus/.env'
env.config_setter = fab.run
def config(action=None, key=None, value=None):
"""Read/write to .env file on local and remote machines.
Usages: fab [prod] config:set,<key>,<value>
fab [prod] config:get,<key>
fab [prod] config:unset,<key>
fab [prod] config:list
"""
import dotenv
command = dotenv.get_cli_string(env.dotenv_path, action, key, value)
env.config_setter('touch %(dotenv_path)s' % env)
if env.config_setter == local:
with virtualenv():
env.config_setter(command)
else:
env.config_setter(command)
restart_servers()
def restart_servers():
services = ['uwsgi-emperor.service', ]
for service in services:
fab.sudo('systemctl restart {0}'.format(service))
def configure(tags='', skip_tags='deploy'):
"""Setup a host using ansible scripts
Usages: fab [prod|qa|dev] configure
"""
fab.require('host_group')
cmd = 'ansible-playbook -i hosts site.yml --limit=%(host_group)s' % env
with fab.lcd('provisioner'):
if tags:
cmd += " --tags '%s'" % tags
if skip_tags:
cmd += " --skip-tags '%s'" % skip_tags
local(cmd)
def deploy():
configure(tags='deploy', skip_tags='')
def deploy_docs():
"""Deploy documentation to server via ansible.
"""
configure(tags='documentation', skip_tags='')
# Helpers
# ---------------------------------------------------------
def manage(cmd, venv=True):
with virtualenv():
local('python manage.py %s' % cmd)
@_contextmanager
def virtualenv():
"""Activates virtualenv context for other commands to run inside it.
"""
with fab.cd(HERE):
with fab.prefix('source %(virtualenv_dir)s/bin/activate' % env):
yield
def verify_virtualenv():
"""This modules check and install virtualenv if it not present.
It also creates local virtualenv directory if it's not present
"""
from distutils import spawn
if not spawn.find_executable('virtualenv'):
local('sudo pip install virtualenv')
if not isdir(env.virtualenv_dir):
local('virtualenv %(virtualenv_dir)s -p $(which python3.6)' % env)