forked from Spacelog/Spacelog
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfabfile.py
More file actions
242 lines (205 loc) · 6.68 KB
/
fabfile.py
File metadata and controls
242 lines (205 loc) · 6.68 KB
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
"""
Remote server layout:
PATH/releases -- unpacked versions (versioned by datetime of fabric invocation)
also current & previous doing the obvious thing (as symlinks)
within each, ENV a virtualenv just for that version
PATH/archives -- tgz archives of versions
Use the setup action to build the bits you need.
"""
from fabric.api import *
from fabric.contrib.files import exists
import tempfile
import os
env.branch = "master"
# App choices
def artemis():
env.django_project_name = 'artemis'
env.staging_hosts = ['core.fort'] # old, aww
env.live_hosts = ['spacelog.org']
env.user = 'spacelog'
env.path = '/home/spacelog'
# only one app
artemis()
# Environment choices
def staging():
env.hosts = env.staging_hosts
env.environment = 'staging'
def live():
env.hosts = env.live_hosts
env.environment = 'live'
# tasks
def dirty_deploy():
"""
Do a dirty deploy. For some reason, fab doesn't want to let me pass True through
to the dirty parameter of deploy().
"""
deploy(True)
def deploy(dirty=False):
"""
Deploy the latest version of the site to the servers, install any
required third party modules and then restart the webserver
"""
require('hosts')
require('path')
ponder_release()
export_and_upload_tar_from_git()
if dirty:
copy_previous_virtualenv()
else:
make_release_virtualenv()
prepare_release(dirty)
switch_to(env.release)
restart_webserver()
def setup():
"""
Set up the initial structure for the given user.
"""
require('hosts')
require('path')
run("mkdir releases")
run("mkdir archives")
def switch_to(version):
"""Switch the current (ie live) version"""
require('hosts')
require('path')
if exists('%s/releases/previous' % env.path):
run('rm %s/releases/previous' % env.path)
if exists('%s/releases/current' % env.path):
run('mv %s/releases/current %s/releases/previous' % (env.path, env.path))
run('cd %s; ln -s %s releases/current' % (env.path, version))
env.release = version # in case anything else wants to use it after us
def switch_to_version(version):
"Specify a specific version to be made live"
switch_to(version)
restart_webserver()
# Helpers. These are called by other functions rather than directly
def ponder_release():
import time
env.release = time.strftime('%Y-%m-%dT%H.%M.%S')
def export_and_upload_tar_from_git():
"Create an archive from the git local repo."
require('release', provided_by=[deploy])
export_tgz_from_git()
upload_tar()
def export_tgz_from_git():
"Create an archive from the git local repo."
local("git archive --format=tar --prefix=%(release)s/ %(branch)s | gzip -c > %(release)s.tar.gz" % {
'release': env.release,
'branch': env.branch,
}
)
def upload_tar():
require('release', provided_by=[deploy])
require('path', provided_by=[deploy])
put('%s.tar.gz' % env.release, '%s/archives/' % env.path)
run('cd %s/releases && gzip -dc ../archives/%s.tar.gz | tar xf -' % (env.path, env.release))
local('rm %s.tar.gz' % env.release)
def copy_previous_virtualenv():
"Copy a previous virtualenv, for when making a new one is too much of a PITA"
require('release', provided_by=[deploy])
run(
"cp -a %(path)s/releases/current/ENV %(path)s/releases/%(release)s/ENV" % {
'path': env.path,
'release': env.release,
}
)
def make_release_virtualenv():
"Make a virtualenv and install the required packages into it"
require('release', provided_by=[deploy])
new_release_virtualenv()
update_release_virtualenv()
def new_release_virtualenv():
"Create a new virtualenv, install pip, and upgrade setuptools"
require('release', provided_by=[deploy])
run(
"cd %(path)s/releases/%(release)s; "
"virtualenv ENV; "
"ENV/bin/easy_install pip; "
"ENV/bin/easy_install -U setuptools" % {
'path': env.path,
'release': env.release
}
)
def update_release_virtualenv():
"Install the required packages from the requirements file using pip"
require('release', provided_by=[deploy])
run(
"cd %(path)s/releases/%(release)s; "
"ENV/bin/pip --default-timeout=600 install -r requirements.txt" % {
'path': env.path,
'release': env.release
}
)
def prepare_release(dirty=False):
"Do any release-local build actions."
require('release', provided_by=[deploy])
if dirty:
# basically, don't reindex, but copy the xappydb from the
# currently-running deploy (in ../current)
run(
"make -C %(path)s/releases/%(release)s/ dirty" % {
'environment': env.environment,
'path': env.path,
'project': env.django_project_name,
'release': env.release
}
)
else:
run(
"make -C %(path)s/releases/%(release)s/" % {
'environment': env.environment,
'path': env.path,
'project': env.django_project_name,
'release': env.release
}
)
make_local_settings()
def make_local_settings():
"""
make local_settings.py for both global & website that change
the deployed URLs for static files (both global and mission-specific)
to use env.release in their paths.
then put it up to the release on live
"""
require('release', provided_by=[deploy])
(fd, fname) = tempfile.mkstemp()
os.write(fd, """
# Override the default CDN URLs to use this release's timestamp
# (website)
STATIC_URL = 'http://cdn.spacelog.org/%(release)s/assets/website/'
MISSIONS_STATIC_URL = 'http://cdn.spacelog.org/%(release)s/assets/website/missions/'
""" % {
'release': env.release,
}
)
os.close(fd)
put(
fname,
'%(path)s/releases/%(release)s/website/local_settings.py' % {
'path': env.path,
'release': env.release,
}
)
os.unlink(fname)
(fd, fname) = tempfile.mkstemp()
os.write(fd, """
# Override the default CDN URLs to use this release's timestamp
# (global)
STATIC_URL = 'http://cdn.spacelog.org/%(release)s/assets/global/'
MISSIONS_STATIC_URL = 'http://cdn.spacelog.org/%(release)s/assets/website/missions/'
""" % {
'release': env.release,
}
)
os.close(fd)
put(
fname,
'%(path)s/releases/%(release)s/global/local_settings.py' % {
'path': env.path,
'release': env.release,
}
)
os.unlink(fname)
def restart_webserver():
"Restart the web server"
run("userv root apache2-reload")