automated deployments using fabric
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 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 | # -*- coding: utf-8 -*-
__author__ = 'heddevanderheide'
import datetime
import os, sys
from fabric.api import *
from fabric.context_managers import lcd
# todo's:
#
# - implement pip wheel (no need for pypi connections)
# - log level configuration (pip --quiet etc)
# - rolling back (reverse symlinks, remove release dir)
# - local build dir is useless, should all move to /tmp
# Main Configuration
env.hosts = ['www.example.com']
env.user = 'johndoe'
env.key_filename = '/Users/johndoe/.ssh/id_rsa.pub'
# Stage Configuration
class Test(object):
"""
[items]
mappings of django settings that should be parsed
into the local_settings.py (or params.py), the keys
should be prefixed 'settings_'
[stage_root]
the absolute path to your remote stage root
[release_version]
the branch or tag you wish to deploy
[timestamp_format]
the way release directories should look
"""
items = dict(settings_DEBUG=True, settings_ENV='test').items()
stage_root = '/home/johndoe/example.com/test.example.com'
project_name = 'example'
release_version = 'develop'
timestamp_format = '%Y%m%d_%H%M%S'
hosts = env.hosts
user = env.user
password = env.password
class Production(Test):
items = dict(settings_DEBUG=False, settings_ENV='production').items()
stage_root = '/home/johndoe/example.com/example.com'
release_version = 'develop'
# Setup the Configuration Dict
ENV = {
'test': Test(),
'production': Production()
}
# # # # # # # # # # # # # # # # # # # # # # # # # # # # #
# Tasks #
# # # # # # # # # # # # # # # # # # # # # # # # # # # # #
@task
def deploy(env='test'):
try:
env = ENV.get(env)
remote_uname()
release = get_timestamp(env)
create_build_directory(env, release)
create_deploy_directory(env)
create_timestamp_directory(env, release)
checkout_version(env, release)
pack_code(env, release)
upload_code(env, release)
unpack_tarball(env, release)
create_virtualenv(env, release)
pip_install_requirements(env, release)
django_set_envs_for_release(env, release)
with cd('{stage_root}/RELEASES/{release}/src'.format(stage_root=env.stage_root, release=release)):
django_sync_migrate(env, release)
django_compilemessages(env, release)
django_collectstatic(env, release)
switch_symlinks(env, release)
except Exception, e:
print 'ERROR:', e
print 'INFO: rolling back...'
finally:
clean(env)
# # # # # # # # # # # # # # # # # # # # # # # # # # # # #
# Functions #
# # # # # # # # # # # # # # # # # # # # # # # # # # # # #
def remote_uname():
# remote connection indicator
run('uname -a')
def create_build_directory(env, release):
# build dir
local(
'mkdir -p build/{project_name}/{release}'.format(
project_name=env.project_name,
release=release
)
)
def get_timestamp(env):
# returns release timestamp
return datetime.datetime.now().strftime(
'{timestamp_format}'.format(timestamp_format=env.timestamp_format)
)
def create_deploy_directory(env):
# creates the remote release dir if it
# doesn't already exist
run(
'mkdir -p {stage_root}/RELEASES'.format(
stage_root=env.stage_root
)
)
def checkout_version(env, release):
# checkout the version we want to deploy
local(
'git clone . /tmp/{release}'.format(
release=release
)
)
with lcd('/tmp/{release}'.format(release=release)):
local(
'git checkout {version}'.format(
version=env.release_version
)
)
def pack_code(env, release):
# tarball the version designated for deploy
local('rm -rf /tmp/{release}/.git'.format(release=release))
local(
'tar czf build/{project_name}/{version}.tar.gz /tmp/{release}'.format(
project_name=env.project_name,
version=env.release_version,
release=release
)
)
def upload_code(env, release):
# upload tarball to the remote
local('scp build/{project_name}/{release}.tar.gz {user}@{host}:{remote_path}'.format(
project_name=env.project_name,
host=env.hosts[0],
user=env.user,
release=env.release_version,
remote_path='{stage_root}'.format(
stage_root=env.stage_root,
release=env.release_version
)
))
def create_timestamp_directory(env, timestamp):
# create remote timestamp directory for deploy
run(
'mkdir -p {stage_root}/RELEASES/{timestamp}'.format(
stage_root=env.stage_root,
timestamp=timestamp
)
)
def unpack_tarball(env, release):
# unpacks tar release into remote release dir
run(
'tar xvf {stage_root}/{release_version}.tar.gz -C {stage_root}/RELEASES/{release} --strip-components=2'.format(
stage_root=env.stage_root,
release_version=env.release_version,
release=release,
project_name=env.project_name
)
)
def create_virtualenv(env, release):
# creates a virtualenv inside the release dir
run(
'virtualenv {stage_root}/RELEASES/{release}/venv'.format(
stage_root=env.stage_root,
release=release
)
)
def pip_install_requirements(env, release):
# install the requirements.txt using the venv
run(
'{stage_root}/RELEASES/{release}/venv/bin/pip install -r {stage_root}/RELEASES/{release}/requirements.txt --quiet'.format(
stage_root=env.stage_root,
release=release
)
)
def django_set_envs_for_release(env, release):
# setup configured environment variables
# local_settings.py / params.py
path = 'build/{project_name}/{release}'.format(
project_name=env.project_name,
release=release
)
local('touch {path}/local_settings.py'.format(path=path))
f = open('{path}/local_settings.py'.format(path=path), 'w+')
string = "\n".join(
[
"{key} = {value}".format(key=k[9:], value=False if v == '' else repr(v))
for k, v in env.items if k.startswith('settings_')
]
)
f.write(string)
f.close()
local('scp {path}/local_settings.py {user}@{host}:{remote_path}'.format(
path=path,
project_name=env.project_name,
host=env.hosts[0],
user=env.user,
release=env.release_version,
remote_path='{stage_root}/RELEASES/{release}/src/main/'.format(
stage_root=env.stage_root,
release=release
)
))
def django_collectstatic(env, release):
run(
'{stage_root}/RELEASES/{release}/venv/bin/python {stage_root}/RELEASES/{release}/src/manage.py collectstatic --noinput'.format(
stage_root=env.stage_root,
release=release
)
)
def django_compilemessages(env, release):
run(
'{stage_root}/RELEASES/{release}/venv/bin/python {stage_root}/RELEASES/{release}/src/manage.py compilemessages'.format(
stage_root=env.stage_root,
release=release
)
)
def django_sync_migrate(env, release):
run(
'{stage_root}/RELEASES/{release}/venv/bin/python {stage_root}/RELEASES/{release}/src/manage.py syncdb --migrate'.format(
stage_root=env.stage_root,
release=release
)
)
def switch_symlinks(env, release):
symlink_wsgi = '{stage_root}/RELEASES/{release}/src/wsgi.py'.format(
stage_root=env.stage_root,
release=release
)
symlink_current = '{stage_root}/RELEASES/{release}'.format(
stage_root=env.stage_root,
release=release
)
run('touch {stage_root}/RELEASES/{release}/src/VERSION'.format(stage_root=env.stage_root, release=release))
run(
'echo "{release_version}" > {stage_root}/RELEASES/{release}/src/VERSION'.format(
release_version=env.release_version,
stage_root=env.stage_root,
release=release,
project_name=env.project_name
)
)
run(
'ln -fs {symlink} {stage_root}/wsgi.py'.format(
stage_root=env.stage_root,
release=release,
symlink=symlink_wsgi
)
)
run(
'ln -snf {symlink} {stage_root}/current'.format(
stage_root=env.stage_root,
release=release,
symlink=symlink_current
)
)
def clean(env):
run(
'ls -1dt {stage_root}/RELEASES/* | tail -n +6 | xargs rm -rf'.format(
stage_root=env.stage_root
)
)
|
More like this
- Template tag - list punctuation for a list of items by shapiromatron 10 months, 2 weeks ago
- JSONRequestMiddleware adds a .json() method to your HttpRequests by cdcarter 10 months, 3 weeks ago
- Serializer factory with Django Rest Framework by julio 1 year, 5 months ago
- Image compression before saving the new model / work with JPG, PNG by Schleidens 1 year, 6 months ago
- Help text hyperlinks by sa2812 1 year, 7 months ago
Comments
Please login first before commenting.