-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathfabfile.py
176 lines (154 loc) · 6.79 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
'''
a fabric fabfile that installs and configures a fresh ubunutu installation
with django running behind nginx via fastcgi. Copy ./.fabricrc.sample to
./.fabricrc, configure it, modify the env.hosts and env.user variables below
then execute:
fab -c ./.fabricrc install
This was inspired by Eric Florenzano's Deploying a Django Site post written
as part of the Django Advent for Django 1.2:
http://djangoadvent.com/1.2/deploying-django-site-using-fastcgi/
'''
from fabric.api import *
from fabric.contrib.console import *
from fabric.contrib.files import *
env.hosts = [''] # list of locations to install
env.user = '' # username at those locations
# ########################################################################
# public tasks
# ########################################################################
def install():
'''install server with required packages and configure the packages'''
_install_core_system()
_install_core_python()
_add_service_user()
_install_django()
_configure_fastcgi()
_configure_daemontools()
_configure_nginx()
def install_celeryd():
'''install a celery daemon'''
celery_context = {
'user': env.django_user,
'site': env.django_site
}
upload_template('./etc/init.d/celeryd', '/etc/init.d/celeryd', context=celery_context, use_sudo=True)
sudo('chown root:root /etc/init.d/celeryd')
sudo('chmod 755 /etc/init.d/celeryd')
upload_template('./etc/default/celeryd', '/etc/default/celeryd', context=celery_context, use_sudo=True)
sudo('chown root:root /etc/default/celeryd')
sudo('chmod 644 /etc/default/celeryd')
sudo('update-rc.d celeryd defaults')
def install_celerybeat():
'''install a celerybeat daemon'''
celery_context = {
'user': env.django_user,
'site': env.django_site
}
upload_template('./etc/init.d/celerybeat', '/etc/init.d/celerybeat', context=celery_context, use_sudo=True)
sudo('chown root:root /etc/init.d/celerybeat')
sudo('chmod 755 /etc/init.d/celerybeat')
upload_template('./etc/default/celerybeat', '/etc/default/celerybeat', context=celery_context, use_sudo=True)
sudo('chown root:root /etc/default/celerybeat')
sudo('chmod 644 /etc/default/celerybeat')
sudo('update-rc.d celerybeat defaults')
def launch():
'''launch the service'''
sudo('svc -u /etc/service/%s' % (env.django_site))
sudo('/etc/init.d/nginx start')
if exists('/etc/init.d/celeryd'):
with settings(warn_only=True):
sudo('/etc/init.d/celeryd start')
if exists('/etc/init.d/celerybeat'):
with settings(warn_only=True):
sudo('/etc/init.d/celerybeat start')
def terminate():
'''terminate the service'''
if exists('/etc/init.d/celerybeat'):
with settings(warn_only=True):
sudo('/etc/init.d/celerybeat stop')
if exists('/etc/init.d/celeryd'):
with settings(warn_only=True):
sudo('/etc/init.d/celeryd stop')
sudo('/etc/init.d/nginx stop')
sudo('svc -d /etc/service/%s' % (env.django_site))
def restart():
'''restart the service'''
terminate()
launch()
def push_ssh_cert():
'''copy ssh key for key based authentication'''
for h in env.hosts:
run('mkdir ~/.ssh; chmod 700 ~/.ssh;')
with hide('running'):
append(open(os.path.expanduser('~/.ssh/id_dsa.pub')).read(), '~/.ssh/authorized_keys')
print 'pushed public key to %s' % (h)
# ########################################################################
# private tasks
# ########################################################################
def _install_core_system():
# core ubuntu packages; all required, note: whois is for adding users
# with mkpasswd
sudo('apt-get --yes install build-essential python-dev python-setuptools nginx daemontools-run whois')
def _install_core_python():
# system wide python pacakages
sudo('easy_install virtualenv')
def _add_service_user():
# add user
result = confirm('Do you wish to add "%s" as a user to the system?' % (env.django_user))
if result:
sudo('adduser --disabled-password --gecos "%s,,," %s' % (env.django_user, env.django_user))
sudo('usermod -p `mkpasswd %s` %s' % (env.django_pswd, env.django_user))
append('%s ALL=(ALL) ALL' % (env.django_user), '/etc/sudoers', use_sudo=True)
def _install_django():
# install and setup a new django project
install_context = {
'user': env.django_user,
'site': env.django_site
}
with cd('/home/%s' % (env.django_user)):
upload_template('./scripts/install_script.sh', 'install_script.sh', context=install_context, use_sudo=True)
sudo('chmod 777 install_script.sh')
with hide('stdout'):
sudo('./install_script.sh', user=env.django_user)
sudo('rm install_script.sh')
def _configure_fastcgi():
# fastcgi
fastcgi_context = {
'user': env.django_user,
'site': env.django_site,
'fastcgi_method': env.fastcgi_method,
'fastcgi_host': env.fastcgi_host,
'fastcgi_port': env.fastcgi_port,
'fastcgi_minspare': env.fastcgi_minspare,
'fastcgi_maxspare': env.fastcgi_maxspare
}
sudo('mkdir -p /etc/service/%s' % (env.django_site))
upload_template('./etc/service/site/run', '/etc/service/%s/run' % (env.django_site), context=fastcgi_context, use_sudo=True)
sudo('chmod +x /etc/service/%s/run' % (env.django_site))
def _configure_daemontools():
# daemontools
# try to use svscan, appears supported in ubuntu 9.10
success = sudo('initctl start svscan')
if not success:
# try to use svscanboot, as defined in django advent article:
# http://djangoadvent.com/1.2/deploying-django-site-using-fastcgi/
sudo('mkdir -p /etc/event.d')
upload_template('./etc/event.d/svscanboot', '/etc/event.d/svscanboot', use_sudo=True)
success = sudo('initctl start svscanboot')
def _configure_nginx():
# configure nginx
nginx_context = {
'site': env.django_site,
'nginx_server_name': env.nginx_server_name,
'nginx_media_alias': env.nginx_media_alias,
'nginx_media_root': env.nginx_media_root,
'nginx_listen': env.nginx_listen
}
with cd('/etc/nginx'):
upload_template('./etc/nginx/fastcgi_params', 'fastcgi_params', use_sudo=True)
upload_template('./etc/nginx/sites-available/site', 'sites-available/%s' % (env.django_site), context=nginx_context, use_sudo=True)
with settings(warn_only=True):
sudo('rm sites-enabled/default')
sudo('rm sites-enabled/%s' % (env.django_site))
sudo('ln -s `pwd`/sites-available/%s sites-enabled/%s' % (env.django_site, env.django_site))
sudo('/etc/init.d/nginx restart')