-
Notifications
You must be signed in to change notification settings - Fork 1
/
fabfile.py
286 lines (244 loc) · 9.99 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
"""
WEBFACTION_PASSWORD=thepassword fab install
WEBFACTION_PASSWORD=thepassword fab update
"""
from fabric.api import *
from fabric.contrib.console import confirm
from fabric.contrib.project import rsync_project
from fabric.contrib.files import upload_template, exists
import os
import sys
from fabric.context_managers import path
print __name__
sys.path.append(os.path.dirname(__name__))
env.config_ok = False
def _common():
env.user="TODO"
env.hosts = [env.user + ".webfactional.com"]
env.password = os.getenv("WEBFACTION_PASSWORD")
if not env.password:
env.password = raw_input("Webfaction password:")
env.wsgi_handler = "wsgi:application"
env.app_dir = "/home/%s/webapps/%s" % (env.user, env.app_name)
env.temp_dir = env.app_dir + "/temp"
env.env_dir = env.app_dir + "/env"
env.node_dir = env.app_dir + "/nodejs"
env.conf_dir = env.app_dir + "/conf"
env.virtualenv = env.app_dir + "/env"
env.bin_dir = env.app_dir + "/bin"
env.media_dir = env.app_dir + "/media"
env.static_dir = env.app_dir + "/collected_static"
env.config_ok = True
def MYAPPNAME():
from sites.MYAPPNAMEsite import settings
env.settings = settings
env.domain = "TODO.example.com"
env.app_name = "MYAPPNAME"
env.settings_module = "sites.MYAPPNAMEsite.settings"
_common()
# Only one site so load its setting by default
MYAPPNAME()
def webfaction_login():
print "Logging in to webfaction"
from xmlrpclib import ServerProxy, Error
wfapi = ServerProxy("https://api.webfaction.com/")
print env.user
wf_session_id, _account = wfapi.login(env.user, env.password)
print "logged in", wf_session_id
return wfapi, wf_session_id
def add_cronjob(cronline):
wfapi, wf_session_id = webfaction_login()
try:
wfapi.delete_cronjob(wf_session_id, cronline)
except:
pass
wfapi.create_cronjob(wf_session_id, cronline)
def add_restart_cron():
print "Add restart cron"
add_cronjob("*/1 * * * * %s/start_supervisor.sh start" % (env.bin_dir,))
# def add_notify_cron():
# command = "%s/bin/python ~/webapps/%s/src/manage.py %s --settings=%s" % (env.virtualenv, env.app_name, "send_notifications" ,env.settings_module)
# add_cronjob("0 0 * * * %s" % (command,))
def install_webfaction():
"""
Use the webfaction API to setup the needed apps, websites and
domains. Should be idempotent so it's fine to run it even when
it's not needed.
"""
wfapi, wf_session_id = webfaction_login()
print "Create domain"
wfapi.create_domain(wf_session_id, env.domain, "www")
print "Create apps"
apps_list = wfapi.list_apps(wf_session_id)
for name, type, app_settings in [(env.app_name, "custom_app_with_port",""),
(env.app_name + "_static", "symlink_static_only", env.static_dir),
(env.app_name + "_media", "symlink_static_only", env.media_dir),
(env.app_name + "_supervisor", "custom_app_with_port", "")]:
app = [x for x in apps_list if x["name"] == name]
if app:
print "App", name, "already exists, using it"
else:
print "Creating app", name
wfapi.create_app(wf_session_id, name, type, False, app_settings)
print "Find IP"
# Assuming that only have one server and one main ip I want to use
ip = [x for x in wfapi.list_ips(wf_session_id) if x["is_main"]][0]["ip"]
print ip
def setup_website(name, https):
print "Create website",name
website_list = wfapi.list_websites(wf_session_id)
website = [x for x in website_list if x["name"] == name]
if website:
print "Website already exists, updating"
method = wfapi.update_website
else:
print "Creating website"
method = wfapi.create_website
method(wf_session_id, name, ip, https, ["www." + env.domain, env.domain],
(env.app_name, '/'),
(env.app_name + "_static", '/static'),
(env.app_name + "_media", '/media'))
setup_website(env.app_name + "_http", False)
setup_website(env.app_name + "_https", True)
print "Create DB"
db_list = wfapi.list_dbs(wf_session_id)
db_name = "%s_%s" % (env.user, env.app_name)
db = [x for x in db_list if x["name"] == db_name]
if db:
print "DB already exists, assuming password and type are correct"
else:
wfapi.create_db(wf_session_id, db_name, "postgresql", env.settings.DATABASES["default"]["PASSWORD"])
print "Set media dir acl"
wfapi.set_apache_acl(wf_session_id, env.media_dir, "rwx")
add_restart_cron()
# add_notify_cron()
setup_mailbox()
query_webfaction()
def setup_mailbox():
wfapi, wf_session_id = webfaction_login()
try:
wfapi.create_mailbox(wf_session_id, env.settings.EMAIL_HOST_USER)
except:
print "Failed to create mailbox, assume it's already created"
wfapi.change_mailbox_password(wf_session_id, env.settings.EMAIL_HOST_USER, env.settings.EMAIL_HOST_PASSWORD)
def query_webfaction():
wfapi, wf_session_id = webfaction_login()
print "Query webfaction"
apps = wfapi.list_apps(wf_session_id)
main_app = [x for x in apps if x["name"] == env.app_name][0]
supervisor_app = [x for x in apps if env.app_name + "_supervisor" == x["name"]][0]
env.server_port = main_app["port"]
env.supervisor_port = supervisor_app["port"]
def upload():
rsync_project(local_dir=os.path.dirname(__file__) + "/",
remote_dir="~/webapps/%s/src" % (env.app_name,),
delete=True, exclude=('*local_settings.py*', 'node_modules',))
def start():
run("bash %s/start_supervisor.sh start" % (env.bin_dir,))
def stop():
run("bash %s/start_supervisor.sh stop" % (env.bin_dir,))
def restart():
run("bash %s/start_supervisor.sh restart" % (env.bin_dir,))
def manage(cmd, options=""):
with path(env.bin_dir, "prepend"):
run("%s/bin/python ~/webapps/%s/src/manage.py %s --settings=%s %s" % (env.virtualenv, env.app_name,cmd,env.settings_module, options))
def shell():
manage("shell")
def taillogs():
run("tail -f ~/logs/user/%s_django.log" % (env.app_name))
def migratedb():
manage("syncdb", "--migrate")
def update_supervisord():
upload_template("conf_templates/supervisord.conf",
env.conf_dir + "/supervisord.conf",
{
"user": env.user,
"password": "TODO_SUPERVISOR_PASSWORD", # we're not exposing the port so this isn't so important
"supervisor_port": env.supervisor_port,
"wsgi_handler": env.wsgi_handler,
"port": env.server_port,
"app_name": env.app_name,
"app_dir": env.app_dir,
"bin_dir": env.bin_dir,
"conf_dir": env.conf_dir,
"virtualenv": env.virtualenv,
"settings_module": env.settings_module
})
upload_template("conf_templates/start_supervisor.sh",
env.bin_dir + '/start_supervisor.sh',
{
"user": env.user,
"conf_dir": env.conf_dir,
"virtualenv": env.virtualenv,
"app_name": env.app_name
},
mode=0750,
)
def updatepackages():
run("PATH=/usr/pgsql-9.1/bin/:$PATH ~/webapps/%s/env/bin/pip install -r ~/webapps/%s/src/requirements.txt" % (env.app_name, env.app_name))
def updatenpmpackages():
with cd("~/webapps/%s/src" % (env.app_name,)):
if not exists("node_modules"):
run("mkdir node_modules")
run("%s/npm install" % (env.bin_dir))
# def installredis():
# run("wget http://redis.googlecode.com/files/redis-2.4.4.tar.gz")
# run("tar xzf redis-2.4.4.tar.gz")
# with cd("redis-2.4.4"):
# run("make")
# run("cp src/redis-server src/redis-cli %s" %(env.bin_dir,))
# updateredisconf()
# def updateredisconf():
# upload_template("conf_templates/redis.conf",
# env.conf_dir + "/redis.conf",
# {
# "port": env.redis_port,
# "dir": env.redis_dir
# }
# )
def collectstatic():
manage("collectstatic","--noinput")
def bootstrap_virtualenv():
with cd(env.temp_dir):
run("curl -O https://pypi.python.org/packages/source/v/virtualenv/virtualenv-1.10.tar.gz")
run("tar xzf virtualenv-1.10.tar.gz")
run('python2.7 virtualenv-1.10/virtualenv.py %s' % (env.env_dir))
run("%s/bin/pip install virtualenv-1.10.tar.gz" % (env.env_dir))
run("rm virtualenv-1.10.tar.gz")
run("rm -rf virtualenv-1.10")
def installnodejs():
with cd(env.temp_dir):
run("curl -O http://nodejs.org/dist/v0.10.26/node-v0.10.26.tar.gz")
run("tar xzf node-v0.10.26.tar.gz")
with cd("node-v0.10.26/"):
run("python2.7 configure --prefix=%s" % (env.node_dir))
run("make && make install")
run("ln -s %s/bin/node %s/node" % (env.node_dir, env.bin_dir))
run("ln -s %s/bin/npm %s/npm" % (env.node_dir, env.bin_dir))
def install():
if not env.config_ok:
print "Please specify a site"
return
install_webfaction()
for d in [env.conf_dir, env.bin_dir, env.media_dir, env.temp_dir]:
if not exists(d):
run("mkdir " + d)
if not exists("%s/bin/activate" % (env.env_dir)):
bootstrap_virtualenv()
# installredis()
if not exists("%s/node" % (env.bin_dir)):
installnodejs()
update()
def update():
if not env.config_ok:
print "Please specify a site"
return
query_webfaction()
upload()
updatenpmpackages()
updatepackages()
migratedb()
# updateredisconf()
update_supervisord()
collectstatic()
restart()