-
Notifications
You must be signed in to change notification settings - Fork 0
/
fabfile.py
153 lines (124 loc) · 4.01 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
#!/usr/bin/env python
from fabric.api import env, run, task, cd, lcd, local, prefix, execute, settings
from fabric.tasks import WrappedCallableTask
from fabric.utils import error
from contextlib import contextmanager as _contextmanager
from sys import executable as sys_executable
import os
from json import load as json_load
from functools import partial
@_contextmanager
def virtualenv():
with env.cd(env.directory):
with prefix(env.activate):
yield
@_contextmanager
def umask(new_mask):
with prefix('umask %s' % (new_mask, )):
yield
@_contextmanager
def custom_cd(cdfunction, mask, path):
with umask(mask):
with cdfunction(path):
yield
@_contextmanager
def shell_env(**ENVS):
with prefix(' && '.join('export %s="%s"' % (k, v) for k,v in ENVS.items())):
yield
def manage_django(command):
with virtualenv():
with shell_env(PYTHONPATH=env['PYTHONPATH']):
env.run('python manage.py %s --settings=%s' % (command, env.django_settings))
class CustomTask(WrappedCallableTask):
def __init__(self, callable, *args, **kwargs):
super(CustomTask, self).__init__(callable, *args, **kwargs)
if env.ssh_config_path and os.path.isfile(os.path.expanduser(env.ssh_config_path)):
env.use_ssh_config = True
if env.host_string == 'localhost' or not env.hosts:
env.pyexecutable = sys_executable
env.cd = partial(custom_cd, lcd, 002)
env.run = local
conffile = 'devenv.json'
else:
env.cd = partial(custom_cd, cd, 002)
env.run = run
if 'production' in env and env.production:
error('TBD')
else:
conffile = 'testenv.json'
if 'conffile' in env:
conffile = env.conffile
with open(conffile) as f:
d = json_load(f)
env.update(d)
env.activate = ''.join(['. ', env.venvpath, '/bin/activate'])
@task(task_class=CustomTask)
def make_venv():
with env.cd(env.directory):
env.run('virtualenv --system-site-packages -p %s %s' % (env.pyexecutable, env.venvpath))
def vars():
env.app = 'freieit'
@task(task_class=CustomTask)
def install_req():
with virtualenv():
env.run('pip install -U -r requirements.txt')
@task(task_class=CustomTask)
def runserver():
manage_django('runserver')
@task(task_class=CustomTask)
def syncdb():
manage_django('syncdb')
manage_django('migrate freieit')
@task(task_class=CustomTask)
def clean():
with env.cd(env.directory):
env.run('rm -f _freieit.db')
env.run('rm -rf _media/')
@task(task_class=CustomTask)
def fixtures():
print ExpertProfile.objects.all()[0]
@task(task_class=CustomTask)
def prepare_migration():
vars()
with settings(warn_only=True):
manage_django('schemamigration %s --auto' % env.app)
env.run('hg add %s/migrations' % env.app)
@task(task_class=CustomTask)
def update():
with env.cd(env.directory):
env.run('hg pull')
env.run('hg update')
@task(task_class=CustomTask)
def deploy():
execute(update)
manage_django('syncdb --noinput')
manage_django('migrate freieit')
@task(task_class=CustomTask)
def shell():
manage_django('shell')
@task(task_class=CustomTask)
def rebuild_venv():
env.run('rm -rf %s' % (env.venvpath, ))
execute(update)
execute(make_venv)
execute(install_req)
@task(task_class=CustomTask)
def collectstatic():
manage_django('collectstatic --noinput')
@task(task_class=CustomTask)
def createsuperuser():
manage_django('createsuperuser')
@task(task_class=CustomTask)
def updateindex():
"updates the haystack search index"
manage_django('update_index')
execute(restart)
@task(task_class=CustomTask)
def rebuildindex():
"rebuilds the complete haystack search index"
manage_django('rebuild_index')
@task(task_class=CustomTask)
def restart():
"restarts the apache django daemon"
with env.cd(env.directory):
env.run('touch ./freieit/apache2/django.wsgi')