Created
February 17, 2012 07:46
-
-
Save marazmiki/1851638 to your computer and use it in GitHub Desktop.
The Fabric script (fabfile.py) with deploy common implementation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # -*- coding: utf-8 -*- | |
| from fabric.api import cd, hide, settings, show, path, prefix, lcd, require,\ | |
| prompt, put, get, run, sudo, local, reboot, open_shell, env, output, \ | |
| abort, warn, puts, fastprint, execute | |
| from fabric.contrib.console import confirm | |
| import datetime | |
| import sys | |
| ############################################################################### | |
| # THE CONFIGURATION SECTION. CHANGE YOUR SETTINGS HERE # | |
| ############################################################################### | |
| env.hosts = ['username@hostname.com'] | |
| env.directory = '/home/username/hostname.com/' | |
| env.activate = 'source {directory}.env/bin/activate'.format(directory=env.directory) | |
| env.deploy_user = 'username' | |
| env.settings = '' | |
| ############################################################################### | |
| # YOU DON'T NEED TO EDIT ANYTHING BELOW THIS COMMENTS # | |
| ############################################################################### | |
| def virtualenv(command): | |
| """ | |
| Runs the command in the virtuale environment | |
| """ | |
| with cd(env.directory): | |
| run(env.activate + ' && ' + command) | |
| def manage(command, settings=env.settings): | |
| """ | |
| Runs the Django management command (manage.py) with the given | |
| settings module | |
| """ | |
| with cd(env.directory): | |
| virtualenv('python ./src/manage.py {command} --settings=settings{settings}'.format( | |
| command = command, | |
| settings = settings, | |
| )) | |
| ############################################################################### | |
| # Meta commands # | |
| ############################################################################### | |
| def deploy(settings=env.settings): | |
| local_update_requirements() | |
| local_vcs_sync() | |
| remote_vcs_sync() | |
| remote_update_requirements() | |
| remote_syncdb(settings) | |
| remote_generate_configs(settings) | |
| remote_clean_cache() | |
| remote_restart_service(settings) | |
| ############################################################################### | |
| # Atomic commands # | |
| ############################################################################### | |
| def local_update_requirements(): | |
| """ | |
| Updates the requirements.txt file | |
| """ | |
| local('pip freeze -r {requirements} > {requirements}'.format( | |
| requirements = 'requirements.txt' | |
| )) | |
| def local_vcs_sync(): | |
| """ | |
| Synchronize the local developer copy with GIT repository | |
| """ | |
| with settings(warn_only=True): | |
| add = local('git add .', capture=True) | |
| if not add.failed: | |
| local('git commit -m "{message}"'.format(message="Autocommit "+\ | |
| "via Fabric at {date}".format( | |
| date = datetime.datetime.now().isoformat() | |
| )), capture=True) | |
| pull = local('git pull origin master', capture=True) | |
| push = local('git push origin master', capture=True) | |
| def remote_vcs_sync(): | |
| """ | |
| Update the source code on server | |
| """ | |
| with cd(env.directory): | |
| run('git pull origin master') | |
| def remote_update_requirements(update=False, ignore=False): | |
| """ | |
| Update requirements | |
| """ | |
| virtualenv('pip install {update} {ignore} -r {path}requirements.txt'.format( | |
| path = env.directory, | |
| update = '-U' if update else '', | |
| ignore = '-I' if ignore else '')) | |
| def remote_syncdb(settings=env.settings): | |
| """ | |
| Synchronizes the database via South | |
| """ | |
| manage('syncdb', settings) | |
| manage('migrate', settings) | |
| def remote_generate_configs(settings=env.settings): | |
| """ | |
| Generates configuration files from templates | |
| """ | |
| manage('config_gen', settings) | |
| def remote_restart_service(settings=env.settings): | |
| """ | |
| """ | |
| run('sudo svc -du /etc/service/hostname.com') # @TODO: remove hardcoded | |
| def remote_clean_cache(): | |
| """ | |
| Clean all memcache values | |
| """ | |
| run('echo flush_all | nc localhost 11211') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment