Skip to content

Instantly share code, notes, and snippets.

@tzangms
Created January 14, 2015 10:49
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tzangms/8751ca023e956dd9be81 to your computer and use it in GitHub Desktop.
Save tzangms/8751ca023e956dd9be81 to your computer and use it in GitHub Desktop.
my fabfile example
import os.path
from fabric.api import run, env, put
from fabric.context_managers import cd
from fabric.api import local
from datetime import datetime
from cuisine import *
ROOT_PATH = os.path.dirname(__file__)
env.user = 'deploy'
env.password = ''
env.hosts = ['www.myaudiocast.com']
env.PROJECT_PATH = '/home/deploy/projects/myaudiocast/myaudiocast/'
def setup():
package_ensure('build-essential')
package_ensure('python-dev')
package_ensure('python-setuptools')
package_ensure('libxml2-dev')
package_ensure('libjpeg62-dev')
package_ensure('libmysqlclient-dev')
package_ensure('libfreetype6-dev')
package_ensure('libmemcache-dev')
package_ensure('unzip')
user_ensure('deploy', env.password, shell='/bin/bash')
sudo('easy_install pip')
sudo('pip install virtualenv virtualenvwrapper mercurial')
def deploy():
if not dir_exists('/home/deploy/myaudiocast/'):
run('virtualenv myaudiocast')
#rev = str(local('git log --pretty=format:"%h" -1', True))
rev = datetime.now().strftime('%Y%m%d%H%M%S')
release_dir = '/home/deploy/myaudiocast/releases/'
dir_ensure(release_dir)
# upload source
local('git archive --format zip -o latest.zip HEAD')
put('latest.zip', release_dir)
local('rm latest.zip')
with cd('/home/deploy/myaudiocast/releases/'):
run('unzip latest.zip -d {rev}'.format(rev=rev))
run('rm latest.zip')
# create symblink
with cd('/home/deploy/myaudiocast/'):
if file_exists('/home/deploy/myaudiocast/myaudiocast'):
run('rm myaudiocast')
run('ln -nsf releases/{0} myaudiocast'.format(rev))
#
with cd('/home/deploy/myaudiocast/myaudiocast/'):
run('source /home/deploy/myaudiocast/bin/activate; pip install -r requirements.txt')
with cd('/home/deploy/myaudiocast/myaudiocast/'):
production()
run('mv myaudiocast/settings.py myaudiocast/local_settings.py')
put(env.settings_file, '/home/deploy/myaudiocast/myaudiocast/myaudiocast/settings.py')
run('source /home/deploy/myaudiocast/bin/activate; python manage.py syncdb')
run('source /home/deploy/myaudiocast/bin/activate; python manage.py migrate')
run('source /home/deploy/myaudiocast/bin/activate; python manage.py collectstatic --noinput -v0')
restart()
def production(branch='production'):
env.settings_file = os.path.join(ROOT_PATH, 'conf/production_settings.py')
env.BRANCH = branch
def restart():
run('touch /tmp/myaudiocast.reload')
def pip(package):
with cd(env.PROJECT_PATH):
run('pip install -E .. ' + package)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment