forked from honza/django-chef
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfabfile.py
174 lines (140 loc) · 4.54 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
from fabric.api import cd, env, prefix, run, sudo, local, settings
from contextlib import contextmanager
env.roledefs = {
'vagrant': ['[email protected]:2222']
}
env.root_dir = '/var/www/nginx-default'
env.virtualenv = '%s/env' % env.root_dir
env.activate = 'source %s/bin/activate ' % env.virtualenv
env.code_dir = '%s/coolname' % env.root_dir
env.media_dir = '%s/media' % env.root_dir
env.chef_executable = '/var/lib/gems/1.8/bin/chef-solo'
@contextmanager
def _virtualenv():
with prefix(env.activate):
yield
def _vagrant():
result = local('vagrant ssh_config | grep IdentityFile', capture=True)
env.key_filename = result.split()[1]
def create_virtualenv():
with cd(env.root_dir):
sudo('virtualenv env --no-site-packages')
def install_chef():
"""
Install chef-solo on the server
"""
sudo('apt-get update', pty=True)
sudo('apt-get install -y git-core rubygems ruby ruby-dev', pty=True)
sudo('gem install chef --no-ri --no-rdoc', pty=True)
def sync_config():
"""
rsync `deploy/` to the server
"""
local('rsync -av deploy/ %s@%s:/etc/chef' % (env.user, env.hosts[0]))
def provision():
"""
Run chef-solo
"""
sync_config()
sudo('cd /etc/chef && %s -c solo.rb -j node.json' % env.chef_executable, pty=True)
def restart():
"""
Reload nginx/gunicorn
"""
if env.user == 'vagrant':
_vagrant()
with settings(warn_only=True):
with cd(env.code_dir):
pid = sudo('cat gunicorn.pid')
sudo('find . -name "*.pyc" -exec rm {} \;')
if not pid.succeeded:
start_gunicorn()
else:
sudo('kill -HUP %s' % pid)
sudo('/etc/init.d/nginx restart')
def start_gunicorn():
if env.user == 'vagrant':
_vagrant()
with cd(env.code_dir):
with _virtualenv():
if env.user == 'vagrant':
sudo('gunicorn_django -c gunicorn.py --daemon settings_server.py & sleep 3')
else:
sudo('gunicorn_django -c gunicorn-vagrant.py --daemon settings_server.py & sleep 3')
def push():
"""
Update application code on the server
"""
if env.user == 'vagrant':
# Check if a link between /vagrant and /var/www exists
# If not, create it
_vagrant()
with settings(warn_only=True):
result = sudo('ls %s' % env.code_dir)
if not result.succeeded:
sudo('ln -s /vagrant/coolname %s' % env.code_dir)
result = sudo('ls %s' % env.media_dir)
if not result.succeeded:
sudo('ln -s /vagrant/media %s' % env.media_dir)
return
with settings(warn_only=True):
result = local("git push live dev")
# if push didn't work, the repository probably doesn't exist
# 1. create an empty repo
# 2. push to it with -u
# 3. retry
# 4. profit
if not result.succeeded:
result2 = run("ls %s" % env.code_dir)
if not result2.succeeded:
sudo('mkdir %s' % env.code_dir)
with cd("%s" % env.code_dir):
sudo("git init")
sudo("git config --bool receive.denyCurrentBranch false")
local("git push live -u dev")
push()
return
with cd("%s" % env.code_dir):
run('git checkout dev')
def bootstrap():
"""
Init django project
"""
push()
create_virtualenv()
with cd(env.code_dir):
with _virtualenv():
sudo('pip install -r ../deploy_requirements.txt', pty=True)
sudo('python manage.py syncdb --noinput --settings=settings_server', pty=True)
#sudo('python manage.py migrate --settings=settings_server', pty=True)
restart()
def deploy():
"""
Push code, sync, migrate, generate media, restart
"""
push()
with cd(env.code_dir):
with _virtualenv():
sudo('python manage.py syncdb --noinput --settings=settings_server', pty=True)
#sudo('python manage.py migrate --settings=settings_server', pty=True)
sudo('python manage.py collectstatic --settings=settings_server', pty=True)
# Local tasks
def clean():
"""
Remove all .pyc files
"""
local('find . -name "*.pyc" -exec rm {} \;')
def debug():
"""
Find files with debug symbols
"""
clean()
local('grep -ir "print" *')
local('grep -ir "console.log" *')
def todo():
"""
Find all TODO and XXX
"""
clean()
local('grep -ir "TODO" *')
local('grep -ir "XXX" *')