-
Notifications
You must be signed in to change notification settings - Fork 1
/
fabfile.py
287 lines (241 loc) · 9.48 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
r"""
fabfile for installing Django/nginx/gunicorn onto a fresh Linux server
"""
from fabric.api import local, settings, abort, cd
from fabric import operations
from fabric.contrib.console import confirm
from fabric.contrib.files import sed
from cuisine import run, sudo
from cuisine import select_package, package_ensure
from cuisine import command_check, command_ensure
from cuisine import user_ensure, user_remove
from cuisine import group_ensure, group_user_ensure
from cuisine import dir_ensure, dir_attribs, dir_remove, dir_exists
from cuisine import file_ensure, file_attribs, file_exists
from cuisine import file_link, file_is_link
from cuisine import mode_sudo
from cuisine import upstart_ensure
from cuisine import python_package_ensure_pip
from fabtools import require
import os
def help():
print """Usage:
fab install_django - fab will prompt you for a connection string
([email protected]). It will then SSH to that box and
install django, nginx and gunicorn. DANGER: Only do
this on a clean install!
fab uninstall_django - fab will prompt you for a connection string
([email protected]). It will then SSH to that box and
attempt to uninstall the previously installed django.
Note: all directories created by the installer will be
destroyed!
"""
def install_django():
installer = InstallDjango()
#installer.user_name = r'ec2_user'
installer.run()
def uninstall_django():
installer = UninstallDjango()
installer.run()
## =========
class UtilClass(object):
def __init__(self):
self.distro = run(r'lsb_release -si')
def get_package_manager(self):
# TODO: test on a wider variety of platforms
if self.distro in ('Ubuntu', 'Debian'):
return 'apt'
elif self.distro in ('Fedora'):
return 'yum'
else:
raise EnvironmentError('Unknown distribution')
class InstallerBase(object):
def __init__(self):
self.user_name = 'django'
self.group_name = 'django'
self.www_dir = r'/var/www/django'
self.repo_dir = r'/var/repo/django-chinook'
self.repo_django_root = r'/'
self.django_requirements_txt = r'local-requirements.txt'
self.virtualenv_dir = r'/var/venv/django'
self.repo_url = r'https://bitbucket.org/xeoscript/django-chinook.git'
self.vcs = 'git'
# required by fabric, a dependency of this particular django install
self.install_packages = ['python-dev', 'build-essential']
self.util = UtilClass()
class InstallDjango(InstallerBase):
def run(self):
"""
Do a complete install
"""
#self.upgrade_system()
self.install_prereqs()
self.create_user()
self.checkout_project()
self.create_virtualenv()
self.create_symlink()
self.install_nginx()
self.install_gunicorn()
self.run_tests()
def run_virtualenv(self, command):
with cd(self.virtualenv_dir):
sudo('. bin/activate && ' + command,
user=self.user_name)
def upgrade_system(self):
if self.util.get_package_manager() == 'apt':
sudo(r'apt-get -q update && apt-get -qy upgrade')
elif self.util.get_package_manager() == 'yum':
# TODO: untested
sudo(r'yum update && yum upgrade')
def install_prereqs(self):
select_package(self.util.get_package_manager())
vcs_to_pkg = {
'git': 'git',
'hg': 'mercurial',
}
package_ensure(vcs_to_pkg[self.vcs])
command_ensure('python2.7')
if not command_check('virtualenv'):
package_ensure('python-virtualenv')
for pkg in self.install_packages:
package_ensure(pkg)
def create_user(self):
user_ensure(self.user_name)
group_ensure(self.group_name)
group_user_ensure(self.user_name, self.group_name)
def checkout_project(self):
with mode_sudo():
dir_ensure(self.repo_dir,
owner=self.user_name,
group=self.group_name,
recursive=True,
)
with cd(self.repo_dir):
sudo(r'chown %s:%s .' % (self.user_name, self.group_name))
sudo(r'ls -d * > /dev/null 2>&1 || %s clone -q %s .'
% (self.vcs, self.repo_url),
user=self.user_name)
def create_virtualenv(self):
with mode_sudo():
dir_ensure(self.virtualenv_dir,
owner=self.user_name,
group=self.group_name,
recursive=True,
)
with cd(self.virtualenv_dir):
run('[[ -f bin/activate ]] || virtualenv .',
user=self.user_name)
python_package_ensure_pip(r=os.sep.join((
self.repo_dir,
self.repo_django_root,
self.django_requirements_txt
)))
def create_symlink(self):
(basedir, symlink_location) = self.www_dir.rsplit('/', 1)
with mode_sudo():
dir_ensure(basedir,
recursive=True,
)
with cd(basedir):
with mode_sudo():
file_link(
os.sep.join((
self.repo_dir,
self.repo_django_root
)),
symlink_location)
def install_nginx(self):
require.nginx.proxied_site('example.com',
docroot=self.www_dir,
proxy_url='http://localhost:8000')
def install_gunicorn(self):
self.run_virtualenv('pip -q install gunicorn')
with mode_sudo():
dir_ensure('/var/log/gunicorn/',
owner=self.user_name,
group=self.group_name,
)
if self.util.get_package_manager() == 'apt':
operations.put(
'gunicorn.conf',
'/etc/init/',
use_sudo=True, mode=644)
with mode_sudo():
file_attribs('/etc/init/gunicorn.conf',
mode=700,
owner='root',
group='root',
)
with settings(warn_only=True):
sed('/etc/init/gunicorn.conf',
'\{virtualenv\}',
self.virtualenv_dir,
use_sudo = True,
)
operations.put(
'gunicorn-launcher.sh',
self.virtualenv_dir + '/bin/',
use_sudo=True, mode=750)
with mode_sudo():
file_attribs(self.virtualenv_dir + '/bin/gunicorn-launcher.sh',
mode=700,
owner=self.user_name,
group=self.group_name,
)
with settings(warn_only=True):
sed(self.virtualenv_dir + '/bin/gunicorn-launcher.sh',
'\{virtualenv\}',
self.virtualenv_dir,
use_sudo = True,
)
sed(self.virtualenv_dir + '/bin/gunicorn-launcher.sh',
'\{project\}',
self.www_dir,
use_sudo = True,
)
# install gevent. This is non-critical and might fail so we go to
# warn-only mode
with settings(warn_only=True):
package_ensure('libevent-dev')
self.run_virtualenv('pip -q install gevent')
# # TODO: add -k gevent to gunicorn launcher script
# TODO
upstart_ensure('gunicorn')
def run_tests(self):
# TODO
pass
class UninstallDjango(InstallerBase):
def run(self):
"""
Remove all files and directories
"""
self.uninstall_gunicorn()
self.remove_symlink()
self.remove_project()
self.remove_user()
def uninstall_gunicorn(self):
with mode_sudo():
with settings(warn_only=True):
dir_remove('/var/log/gunicorn', recursive = True)
if (self.virtualenv_dir and
file_exists(self.virtualenv_dir + '/bin/activate')
):
dir_remove(self.virtualenv_dir, recursive = True)
if self.util.get_package_manager() == 'apt':
dir_remove('/etc/init/gunicorn.conf')
def remove_symlink(self):
(basedir, symlink_location) = self.www_dir.rsplit('/', 1)
with settings(warn_only=True):
if file_is_link(self.www_dir):
with mode_sudo():
dir_remove(self.www_dir, recursive = True)
def remove_project(self):
with settings(warn_only=True):
if dir_exists('%s/.%s' % (self.repo_dir, self.vcs)):
with mode_sudo():
dir_remove(self.repo_dir, recursive=True,)
def remove_user(self):
with settings(warn_only=True):
user_remove(self.user_name, rmhome = True)
#group_remove(self.user_name)
# Group_remove not in the cuisine version currently in pip