Manage osmfr server with fabfile

This commit is contained in:
Yohan Boniface 2013-07-10 23:46:24 +02:00
parent 6dc79db9a3
commit bd6ed636d2
2 changed files with 32 additions and 46 deletions

1
.gitignore vendored
View file

@ -3,3 +3,4 @@
umap/settings/local.py umap/settings/local.py
umap/settings/local/* umap/settings/local/*
docs/_build docs/_build
umap/remote_static

77
fabfile.py vendored
View file

@ -11,31 +11,40 @@ env.requirements_file = 'requirements.pip'
env.restart_sudo = False env.restart_sudo = False
def run_as_umap(*args, **kwargs):
if env.restart_sudo:
kwargs['user'] = "umap"
return sudo(*args, **kwargs)
else:
return run(*args, **kwargs)
#============================================================================== #==============================================================================
# Tasks which set up deployment environments # Tasks which set up deployment environments
#============================================================================== #==============================================================================
@task @task
def live(): def osmfr():
""" """
Use the live deployment environment. OSM-fr servers.
""" """
server = 'umap.org' server = 'osm102.openstreetmap.fr'
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 = '/home/ybon/.virtualenvs/{project_name}'.format(**env) env.virtualenv_dir = '/data/project/umap/.virtualenvs/{project_name}'.format(**env)
env.project_dir = '{virtualenv_dir}/src/{project_name}'.format(**env) env.project_dir = '/data/project/umap/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) env.restart_command = 'touch {project_dir}/umap/wsgi.py'.format(**env)
env.restart_sudo = True
@task @task
def dev(): def dev():
""" """
Use the development deployment environment. Kimsufi dev server.
""" """
server = 'ks3267459.kimsufi.com' server = 'ks3267459.kimsufi.com'
env.roledefs = { env.roledefs = {
@ -133,7 +142,7 @@ def update(action='check'):
""" """
with cd(env.project_dir): with cd(env.project_dir):
remote, dest_branch = env.remote_ref.split('/', 1) remote, dest_branch = env.remote_ref.split('/', 1)
run('git fetch {remote}'.format(remote=remote, run_as_umap('git fetch {remote}'.format(remote=remote,
dest_branch=dest_branch, **env)) dest_branch=dest_branch, **env))
with hide('running', 'stdout'): with hide('running', 'stdout'):
changed_files = run('git diff-index --cached --name-only ' changed_files = run('git diff-index --cached --name-only '
@ -145,10 +154,10 @@ def update(action='check'):
reqs_changed = env.requirements_file in changed_files reqs_changed = env.requirements_file in changed_files
else: else:
reqs_changed = False reqs_changed = False
run('git merge {remote_ref}'.format(**env)) run_as_umap('git merge {remote_ref}'.format(**env))
run('find -name "*.pyc" -delete') run_as_umap('find -name "*.pyc" -delete')
if action == "clean": if action == "clean":
run('git clean -df') run_as_umap('git clean -df')
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).
@ -164,8 +173,6 @@ def collectstatic():
collect_remote_statics() collect_remote_statics()
dj('collectstatic --link --noinput') dj('collectstatic --link --noinput')
dj('compress') dj('compress')
# with cd('{virtualenv_dir}/var/static'.format(**env)):
# fix_permissions()
@task @task
@ -183,11 +190,7 @@ def restart():
""" """
Restart the web service. Restart the web service.
""" """
if env.restart_sudo: run_as_umap(env.restart_command)
cmd = sudo
else:
cmd = run
cmd(env.restart_command)
@task @task
@ -205,10 +208,10 @@ def requirements(name=None, upgrade=False):
"project_dir": env.project_dir, "project_dir": env.project_dir,
"requirements_file": env.requirements_file, "requirements_file": env.requirements_file,
} }
run('{base_command} -r {project_dir}/{requirements_file}'\ run_as_umap('{base_command} -r {project_dir}/{requirements_file}'\
.format(**kwargs)) .format(**kwargs))
else: else:
run('{base_command} {name}'.format( run_as_umap('{base_command} {name}'.format(
base_command=base_command, base_command=base_command,
name=name name=name
)) ))
@ -222,37 +225,19 @@ 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/python {project_dir}/manage.py {dj_command} ' run_as_umap('{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))
def fix_permissions(path='.'):
"""
Fix the file permissions.
"""
if ' ' in path:
full_path = '{path} (in {cwd})'.format(path=path, cwd=env.cwd)
else:
full_path = posixpath.normpath(posixpath.join(env.cwd, path))
puts('Fixing {0} permissions'.format(full_path))
with hide('running'):
system_user = env.system_users.get(env.host)
if system_user:
run('chmod -R g=rX,o= -- {0}'.format(path))
run('chgrp -R {0} -- {1}'.format(system_user, path))
else:
run('chmod -R go= -- {0}'.format(path))
def collect_remote_statics(): def collect_remote_statics():
""" """
Add leaflet and leaflet.draw in a repository watched by collectstatic. Add leaflet and leaflet.draw in a repository watched by collectstatic.
""" """
remote_static_dir = '{project_dir}/{project_name}/remote_static'.format(**env) remote_static_dir = '{project_dir}/{project_name}/remote_static'.format(**env)
run('mkdir -p {0}'.format(remote_static_dir)) run_as_umap('mkdir -p {0}'.format(remote_static_dir))
remote_repositories = { remote_repositories = {
'leaflet': "git://github.com/Leaflet/Leaflet.git@master", 'leaflet': "git://github.com/Leaflet/Leaflet.git@master#v0.6.2",
'draw': "git://github.com/Leaflet/Leaflet.draw.git@master", 'draw': "git://github.com/Leaflet/Leaflet.draw.git@master#v0.2.1",
'hash': "git://github.com/mlevans/leaflet-hash.git@master", 'hash': "git://github.com/mlevans/leaflet-hash.git@master",
'storage': 'git://github.com/yohanboniface/Leaflet.Storage.git@master', 'storage': 'git://github.com/yohanboniface/Leaflet.Storage.git@master',
'edit_in_osm': 'git://github.com/yohanboniface/Leaflet.EditInOSM.git@master', 'edit_in_osm': 'git://github.com/yohanboniface/Leaflet.EditInOSM.git@master',
@ -268,11 +253,11 @@ def collect_remote_statics():
else: else:
ref = branch ref = branch
with hide("running", "stdout"): with hide("running", "stdout"):
exists = run('if [ -d "{0}" ]; then echo 1; fi'.format(subdir)) exists = run_as_umap('if [ -d "{0}" ]; then echo 1; fi'.format(subdir))
if exists: if exists:
with cd(subdir): with cd(subdir):
run('git pull origin {0}'.format(branch)) run_as_umap('git pull origin {0}'.format(branch))
else: else:
run('git clone {0} {1}'.format(repository, subdir)) run_as_umap('git clone {0} {1}'.format(repository, subdir))
with cd(subdir): with cd(subdir):
run('git checkout {0}'.format(ref)) run_as_umap('git checkout {0}'.format(ref))