From fa920eb9cfe743769fb1412e7a8128808d7fde5b Mon Sep 17 00:00:00 2001 From: Martin Mahner Date: Thu, 2 Jun 2011 20:34:41 +0200 Subject: [PATCH] Better fabfile --- fabfile.py | 98 +++++++++++++++++++++++++++++++++++------------------- 1 file changed, 64 insertions(+), 34 deletions(-) diff --git a/fabfile.py b/fabfile.py index e85ce1a..87b4cb5 100644 --- a/fabfile.py +++ b/fabfile.py @@ -1,66 +1,97 @@ from fabric.api import env, local, run, require, cd from fabric.operations import _prefix_commands, _prefix_env_vars +# Host env.disable_known_hosts = True # always fails for me without this -env.hosts = ['pastebin.dev.lincolnloop.com'] -env.root = '/opt/webapps/pastebin' -env.proj_root = env.root + '/src/pastebin' -env.proj_repo = 'git@github.com:myuser/myrepo.git' +env.user = 'root' +env.hosts = ['dpaste.de'] +env.proj_repo = 'git@github.com:bartTC/dpaste.de.git' + +# Paths +env.root = '/opt/webapps/dpaste.de' +env.proj_root = env.root + '/src/dpastede' +env.pid_file = env.root + '/var/gunicorn.pid' +env.proj_bin = env.proj_root + '/pastebin/bin' +env.local_settings = env.proj_root + '/pastebin/conf/local/settings.py' env.pip_file = env.proj_root + '/requirements.pip' +# ============================================================================ +# Git +# ============================================================================ -def deploy(): - """Update source, update pip requirements, syncdb, restart server""" - update() - update_reqs() - syncdb() - restart() - +def push(remote=None, branch=None, reload=False): + """Pushes the local git repo to the given remote and branch. Then pulls it + o n the server.""" + remote = remote or 'origin' + branch = branch or 'master' + local('git push %s %s' % (remote, branch)) + with cd(env.proj_root): + ve_run('git pull %s %s' % (remote, branch)) + if reload: + restart() +def pushr(remote=None, branch=None, reload=True): + push(remote, branch, reload) + def switch(branch): """Switch the repo branch which the server is using""" with cd(env.proj_root): ve_run('git checkout %s' % branch) restart() - def version(): """Show last commit to repo on server""" with cd(env.proj_root): sshagent_run('git log -1') +# ============================================================================ +# Server +# ============================================================================ def restart(): - """Restart Apache process""" - run('touch %s/etc/apache/django.wsgi' % env.root) + """Kill the gunicorn process, Cherokee will start it upon request""" + ve_run('kill `cat %s`' % env.pid_file) +def flush(): + """Flush memcache""" + sshagent_run('/etc/init.d/memcached restart') + +# ============================================================================ +# Django +# ============================================================================ def update_reqs(): """Update pip requirements""" ve_run('yes w | pip install -r %s' % env.pip_file) +def collect(): + manage('collectstatic --noinput') -def update(): - """Updates project source""" - with cd(env.proj_root): - sshagent_run('git pull') +def update(extreme=False): + push() + if extreme: + update_reqs() + collect() + if extreme: + flush() + restart() - -def syncdb(): - """Run syncdb (along with any pending south migrations)""" - ve_run('manage.py syncdb --migrate') - - -def clone(): - """Clone the repository for the first time""" - with cd('%s/src' % env.root): - sshagent_run('git clone %s' % env.proj_repo) - ve_run('pip install -e %s' % env.proj_root) +def debugon(): + """Turn debug mode on for the production server.""" + run("sed -i -e 's/^DEBUG = .*/DEBUG = True/' %s" % env.local_settings) + restart() - with cd('%s/pastebin/conf/local' % env.proj_root): - run('ln -s ../dev/__init__.py') - run('ln -s ../dev/settings.py') +def debugoff(): + """Turn debug mode on for the production server.""" + run("sed -i -e 's/^DEBUG = .*/DEBUG = False/' %s" % env.local_settings) + restart() +# ============================================================================ +# SSH funcs +# ============================================================================ + +def manage(cmd): + return ve_run('python %s/manage.py %s' % (env.proj_bin, cmd)) def ve_run(cmd): """ @@ -70,7 +101,6 @@ def ve_run(cmd): require('root') return sshagent_run('source %s/bin/activate; %s' % (env.root, cmd)) - def sshagent_run(cmd): """ Helper function. @@ -89,4 +119,4 @@ def sshagent_run(cmd): except ValueError: return local( "ssh -A %s@%s '%s'" % (env.user, env.host_string, wrapped_cmd) - ) + ) \ No newline at end of file