Working version of fabfile

This commit is contained in:
Yohan Boniface 2012-11-23 19:39:29 +01:00
parent 317170185c
commit ea49f19610

109
fabfile.py vendored
View file

@ -1,15 +1,14 @@
from fabric.api import task, env, run, local, roles, cd, execute, hide, puts,\ from fabric.api import task, env, run, local, roles, cd, execute, hide, puts,\
sudo sudo
import posixpath import posixpath
import re
env.project_name = 'youmap' env.project_name = 'youmap'
env.repository = 'git@github.com:lincolnloop/youmap.git' env.repository = 'https://yohanboniface@bitbucket.org/yohanboniface/youmap_project.git'
env.local_branch = 'master' env.local_branch = 'master'
env.remote_ref = 'origin/master' env.remote_ref = 'origin/master'
env.requirements_file = 'requirements.pip' env.requirements_file = 'requirements.pip'
env.restart_command = 'supervisorctl restart {project_name}'.format(**env) env.restart_sudo = False
env.restart_sudo = True
#============================================================================== #==============================================================================
@ -21,15 +20,16 @@ def live():
""" """
Use the live deployment environment. Use the live deployment environment.
""" """
server = 'youmap.com' server = 'youmap.org'
env.roledefs = { env.roledefs = {
'web': [server], 'web': [server],
'db': [server], 'db': [server],
} }
env.system_users = {server: 'www-data'} env.system_users = {server: 'www-data'}
env.virtualenv_dir = '/srv/www/{project_name}'.format(**env) env.virtualenv_dir = '/home/ybon/.virtualenvs/{project_name}'.format(**env)
env.project_dir = '{virtualenv_dir}/src/{project_name}'.format(**env) env.project_dir = '{virtualenv_dir}/src/{project_name}'.format(**env)
env.project_conf = '{project_name}.settings.local'.format(**env) env.project_conf = '{project_name}.settings.local'.format(**env)
env.restart_command = 'circusctl restart {project_name}'.format(**env)
@task @task
@ -37,15 +37,16 @@ def dev():
""" """
Use the development deployment environment. Use the development deployment environment.
""" """
server = 'youmap.dev.lincolnloop.com' server = 'ks3267459.kimsufi.com'
env.roledefs = { env.roledefs = {
'web': [server], 'web': [server],
'db': [server], 'db': [server],
} }
env.system_users = {server: 'www-data'} env.system_users = {server: 'www-data'}
env.virtualenv_dir = '/srv/www/{project_name}'.format(**env) env.virtualenv_dir = '/home/ybon/.virtualenvs/{project_name}'.format(**env)
env.project_dir = '{virtualenv_dir}/src/{project_name}'.format(**env) env.project_dir = '/home/ybon/dev/{project_name}'.format(**env)
env.project_conf = '{project_name}.conf.local'.format(**env) env.project_conf = '{project_name}.settings.local'.format(**env)
env.restart_command = '{virtualenv_dir}/bin/circusctl restart {project_name}'.format(**env)
# Set the default environment. # Set the default environment.
@ -69,14 +70,17 @@ def bootstrap(action=''):
puts('Assuming {host} has already been bootstrapped since ' puts('Assuming {host} has already been bootstrapped since '
'{virtualenv_dir} exists.'.format(**env)) '{virtualenv_dir} exists.'.format(**env))
return return
sudo('virtualenv {virtualenv_dir}'.format(**env)) # run('mkvirtualenv {project_name}'.format(**env))
if not exists: with hide('running', 'stdout'):
sudo('mkdir -p {0}'.format(posixpath.dirname(env.virtualenv_dir))) project_git_exists = run('if [ -d "{project_dir}" ]; then echo 1; fi'\
sudo('git clone {repository} {project_dir}'.format(**env)) .format(**env))
sudo('{virtualenv_dir}/bin/pip install -e {project_dir}'.format(**env)) if not project_git_exists:
with cd(env.virtualenv_dir): run('mkdir -p {0}'.format(posixpath.dirname(env.virtualenv_dir)))
sudo('chown -R {user} .'.format(**env)) run('git clone {repository} {project_dir}'.format(**env))
fix_permissions() # sudo('{virtualenv_dir}/bin/pip install -e {project_dir}'.format(**env))
# with cd(env.virtualenv_dir):
# sudo('chown -R {user} .'.format(**env))
# fix_permissions()
requirements() requirements()
puts('Bootstrapped {host} - database creation needs to be done manually.'\ puts('Bootstrapped {host} - database creation needs to be done manually.'\
.format(**env)) .format(**env))
@ -143,8 +147,8 @@ def update(action='check'):
reqs_changed = False reqs_changed = False
run('git merge {remote_ref}'.format(**env)) run('git merge {remote_ref}'.format(**env))
run('find -name "*.pyc" -delete') run('find -name "*.pyc" -delete')
if action == "clean":
run('git clean -df') run('git clean -df')
fix_permissions()
if action == 'force' or reqs_changed: if action == 'force' or reqs_changed:
# Not using execute() because we don't want to run multiple times for # Not using execute() because we don't want to run multiple times for
# each role (since this task gets run per role). # each role (since this task gets run per role).
@ -157,9 +161,11 @@ def collectstatic():
""" """
Collect static files from apps and other locations in a single location. Collect static files from apps and other locations in a single location.
""" """
collect_remote_statics()
dj('collectstatic --link --noinput') dj('collectstatic --link --noinput')
with cd('{virtualenv_dir}/var/static'.format(**env)): # dj('compress')
fix_permissions() # with cd('{virtualenv_dir}/var/static'.format(**env)):
# fix_permissions()
@task @task
@ -168,7 +174,7 @@ def syncdb(sync=True, migrate=True):
""" """
Synchronize the database. Synchronize the database.
""" """
dj('syncdb --migrate --noinput') dj('syncdb --noinput')
@task @task
@ -186,27 +192,26 @@ def restart():
@task @task
@roles('web', 'db') @roles('web', 'db')
def requirements(): def requirements(name=None, upgrade=False):
""" """
Update the requirements. Update the requirements.
""" """
run('{virtualenv_dir}/bin/pip install -r {project_dir}/requirements.txt'\ base_command = '{virtualenv_dir}/bin/pip install'.format(virtualenv_dir=env.virtualenv_dir)
.format(**env)) if upgrade:
with cd('{virtualenv_dir}/src'.format(**env)): base_command += ' --upgrade'
with hide('running', 'stdout', 'stderr'): if not name:
dirs = [] kwargs = {
for path in run('ls -db1 -- */').splitlines(): "base_command": base_command,
full_path = posixpath.normpath(posixpath.join(env.cwd, path)) "project_dir": env.project_dir,
if full_path != env.project_dir: "requirements_file": env.requirements_file,
dirs.append(path) }
if dirs: run('{base_command} -r {project_dir}/{requirements_file}'\
fix_permissions(' '.join(dirs)) .format(**kwargs))
with cd(env.virtualenv_dir): else:
with hide('running', 'stdout'): run('{base_command} {name}'.format(
match = re.search(r'\d+\.\d+', run('bin/python --version')) base_command=base_command,
if match: name=name
with cd('lib/python{0}/site-packages'.format(match.group())): ))
fix_permissions()
#============================================================================== #==============================================================================
@ -217,7 +222,7 @@ def dj(command):
""" """
Run a Django manage.py command on the server. Run a Django manage.py command on the server.
""" """
run('{virtualenv_dir}/bin/manage.py {dj_command} ' run('{virtualenv_dir}/bin/python {project_dir}/manage.py {dj_command} '
'--settings {project_conf}'.format(dj_command=command, **env)) '--settings {project_conf}'.format(dj_command=command, **env))
@ -237,3 +242,25 @@ def fix_permissions(path='.'):
run('chgrp -R {0} -- {1}'.format(system_user, path)) run('chgrp -R {0} -- {1}'.format(system_user, path))
else: else:
run('chmod -R go= -- {0}'.format(path)) run('chmod -R go= -- {0}'.format(path))
def collect_remote_statics():
"""
Add leaflet and leaflet.draw in a repository watched by collectstatic.
"""
remote_static_dir = '{project_dir}/{project_name}/remote_static'.format(**env)
run('mkdir -p {0}'.format(remote_static_dir))
remote_repositories = {
'leaflet': "git://github.com/CloudMade/Leaflet.git",
'draw': "git://github.com/jacobtoye/Leaflet.draw.git",
'hash': "git://github.com/mlevans/leaflet-hash.git",
}
with cd(remote_static_dir):
for subdir, repository in remote_repositories.iteritems():
with hide("running", "stdout"):
exists = run('if [ -d "{0}" ]; then echo 1; fi'.format(subdir))
if exists:
with cd(subdir):
run('git pull')
else:
run('git clone {0} {1}'.format(repository, subdir))