Skip to content

Instantly share code, notes, and snippets.

@hermansc
Created October 29, 2012 09:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hermansc/3972599 to your computer and use it in GitHub Desktop.
Save hermansc/3972599 to your computer and use it in GitHub Desktop.
Fabfile
STAGING = { 'directory': '/var/www/stagingcode/',
'fcgi': 'fcgi-staging.fcgi' }
PROD = {'directory': '/var/www/productioncode/',
'fcgi': 'fcgi-prod.fcgi'}
env.hosts = ['host.domain.com']
env.user = getpass.getuser()
USER_ID = 'productionuser'
@task
def deploy():
env.user = prompt('Username: ', default=env.user)
# Check if we first want to test on staging
staging = confirm("Test on staging first?")
if staging:
git_pull(STAGING['directory'])
update_packages(PROD['directory'])
restart_fcgi(STAGING['fcgi'])
if not confirm("Deploy to staging done. Continue with deploy to production?"):
abort('Aborting production, at your request')
# Pull on the production branch
git_pull(PROD['directory'])
# If we didn't check on staging, we did not update virtualenv
# packages, and need to do this now.
if not staging:
update_packages(PROD['directory'])
# Check if we want to tag the deployment
git_tag(PROD['directory'])
# Restart the fcgi at last.
restart_fcgi(PROD['fcgi'])
def git_pull(repo):
print header("Running: git-pull on directory:\n# %s" % repo)
with cd(repo):
sudo("git fetch origin && git reset --hard origin/master", user=USER_ID)
def git_tag(repo):
if confirm("Give new tag for this deployment?"):
print header("Running: Checking tags")
with cd(repo):
sudo("git tag |tail -n 5", user=USER_ID)
tag = prompt('Give new tag: ')
sudo("git tag %s" % tag, user=USER_ID)
sudo("git push --tags && git push", user=USER_ID)
def update_packages(repo):
print header("Running: pip install in the virtual environment")
with cd(repo + "../"):
sudo("./virtualenv/bin/pip install -r requirements.txt", user=USER_ID)
def restart_fcgi(fcgi_name):
print header("Running: Restart fcgi script: %s" % fcgi_name)
sudo("invoke-rc.d %s restart" % fcgi_name)
def header(text):
return ("#" * 45) + "\n# %s\n" % text + ("#" * 45)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment