Skip to content

Commit

Permalink
Various changes on fabfile.py
Browse files Browse the repository at this point in the history
  • Loading branch information
yohanboniface committed Aug 1, 2012
1 parent 5c28b68 commit ec57a85
Showing 1 changed file with 49 additions and 38 deletions.
87 changes: 49 additions & 38 deletions fabfile.py
@@ -1,14 +1,13 @@
from fabric.api import task, env, run, local, roles, cd, execute, hide, puts,\
sudo
import posixpath
import re

env.project_name = 'cartefluv'
env.repository = 'git@github.com:yohanboniface/carte_fluviale.git'

env.project_name = 'carte_fluviale'
env.repository = 'git://github.com/yohanboniface/carte-fluviale.git'
env.local_branch = 'master'
env.remote_ref = 'origin/master'
env.requirements_file = 'requirements.pip'
env.restart_command = 'supervisorctl restart {project_name}'.format(**env)
env.restart_sudo = False


Expand All @@ -27,9 +26,10 @@ def live():
'db': [server],
}
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_conf = '{project_name}.conf.local'.format(**env)
env.restart_command = 'circusctl restart {project_name}'.format(**env)


@task
Expand All @@ -43,9 +43,10 @@ def dev():
'db': [server],
}
env.system_users = {server: 'www-data'}
env.virtualenv_dir = '/srv/www/{project_name}'.format(**env)
env.project_dir = '{virtualenv_dir}/src/{project_name}'.format(**env)
env.project_conf = '{project_name}.conf.local'.format(**env)
env.virtualenv_dir = '/home/ybon/.virtualenvs/{project_name}'.format(**env)
env.project_dir = '/home/ybon/dev/{project_name}'.format(**env)
env.project_conf = '{project_name}.conf.dev'.format(**env)
env.restart_command = '{virtualenv_dir}/bin/circusctl restart {project_name}'.format(**env)


# Set the default environment.
Expand All @@ -69,14 +70,17 @@ def bootstrap(action=''):
puts('Assuming {host} has already been bootstrapped since '
'{virtualenv_dir} exists.'.format(**env))
return
sudo('virtualenv {virtualenv_dir}'.format(**env))
if not exists:
sudo('mkdir -p {0}'.format(posixpath.dirname(env.virtualenv_dir)))
sudo('git clone {repository} {project_dir}'.format(**env))
sudo('{virtualenv_dir}/bin/pip install -e {project_dir}'.format(**env))
with cd(env.virtualenv_dir):
sudo('chown -R {user} .'.format(**env))
fix_permissions()
# run('mkvirtualenv {project_name}'.format(**env))
with hide('running', 'stdout'):
project_git_exists = run('if [ -d "{project_dir}" ]; then echo 1; fi'\
.format(**env))
if not project_git_exists:
run('mkdir -p {0}'.format(posixpath.dirname(env.virtualenv_dir)))
run('git clone {repository} {project_dir}'.format(**env))
# 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()
puts('Bootstrapped {host} - database creation needs to be done manually.'\
.format(**env))
Expand Down Expand Up @@ -143,8 +147,8 @@ def update(action='check'):
reqs_changed = False
run('git merge {remote_ref}'.format(**env))
run('find -name "*.pyc" -delete')
run('git clean -df')
fix_permissions()
if action == "clean":
run('git clean -df')
if action == 'force' or reqs_changed:
# Not using execute() because we don't want to run multiple times for
# each role (since this task gets run per role).
Expand All @@ -157,9 +161,10 @@ def collectstatic():
"""
Collect static files from apps and other locations in a single location.
"""
collect_remote_statics()
dj('collectstatic --link --noinput')
with cd('{virtualenv_dir}/var/static'.format(**env)):
fix_permissions()
# with cd('{virtualenv_dir}/var/static'.format(**env)):
# fix_permissions()


@task
Expand All @@ -168,7 +173,7 @@ def syncdb(sync=True, migrate=True):
"""
Synchronize the database.
"""
dj('syncdb --migrate --noinput')
dj('syncdb --noinput')


@task
Expand All @@ -190,23 +195,8 @@ def requirements():
"""
Update the requirements.
"""
run('{virtualenv_dir}/bin/pip install -r {project_dir}/requirements.txt'\
run('{virtualenv_dir}/bin/pip install -r {project_dir}/{requirements_file}'\
.format(**env))
with cd('{virtualenv_dir}/src'.format(**env)):
with hide('running', 'stdout', 'stderr'):
dirs = []
for path in run('ls -db1 -- */').splitlines():
full_path = posixpath.normpath(posixpath.join(env.cwd, path))
if full_path != env.project_dir:
dirs.append(path)
if dirs:
fix_permissions(' '.join(dirs))
with cd(env.virtualenv_dir):
with hide('running', 'stdout'):
match = re.search(r'\d+\.\d+', run('bin/python --version'))
if match:
with cd('lib/python{0}/site-packages'.format(match.group())):
fix_permissions()


#==============================================================================
Expand All @@ -217,7 +207,7 @@ def dj(command):
"""
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))


Expand All @@ -237,3 +227,24 @@ def fix_permissions(path='.'):
run('chgrp -R {0} -- {1}'.format(system_user, path))
else:
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:
puts('"{0}" alread created. Skipping.'.format(subdir))
continue
run('git clone {0} {1}'.format(repository, subdir))

0 comments on commit ec57a85

Please sign in to comment.