Fabric Deploy Your Symfony2 App: Part I

Sometime ago I heard OpenSky deploys their app with fabric from @jwage, he had an article but I couldn’t make up the details since I was confused a bit by capifony. Now I know why they use it and here is the repo that we can improve together:

I improve this script from https://github.com/fabriceb/sfLive2011 and from jwage blog post http://jwage.com/post/31049115791/deploying-opensky-with-fabric which i did not remember but now that i look at it has some complementing things i will touch on the next part ii of this blog post.

Let’s install it from the mother ship https://github.com/cordoval/Symfony2Fabric:

pip install fabric

Let’s add fabfile.py to the root folder of our app:

from fabric.api import *
from fabric.contrib.files import exists
import os
from time import strftime
repo = 'git@github:username/mysite.git'
path = '/var/www/mysite'
env.use_ssh_config = True
env.sudo_user = 'www-data'
env.hosts = ['grace', 'gospel']
# check read-access to the keys, to be server-independent
keys = ['~/.ssh/id_rsa']
env.key_filename = [key for key in keys if os.access(key, os.R_OK)]
# this tags the sha deploy
def tag_prod():
    tag = "prod/%s" % strftime("%Y/%m-%d-%H-%M-%S")
    local('git tag -a %s -m "Prod"' % tag)
    local('git push --tags')
def install():
    sudo('mkdir -p ' + path)
    with cd(path):
        sudo('git clone ' + repo + ' .')
        sudo('composer install --dev')
        sudo('php app/console doctrine:database:create')
        sudo('php app/console doctrine:migrations:migrate --no-interaction')
def update():
    with cd(path):
        sudo('git remote update')
        sudo('git pull')
        sudo('composer install --dev')
        sudo('php app/console doctrine:migrations:migrate --no-interaction')
#        tag = run('git tag -l prod/* | sort | tail -n1')
#        run('git checkout ' + tag)

def deploy():
    if not exists(path):
#    tag_prod()
#def rollback(num_revs=1):
#    with cd(path):
#        run('git fetch')
#        tag = run('git tag -l prod/* | sort | tail -n' + str(1 + int(num_revs)) + ' | head -n1')
#        run('git checkout ' + tag)

Thanks for your support, I hope to see you on Portland sflive! If you want to see me there please support me as it is very expensive :'(.

6 thoughts on “Fabric Deploy Your Symfony2 App: Part I

  1. Just fixing my previous comment: “Fabric is a great library to make network scripts”. Deploy it’s just one of them 😉

  2. ok, wait a next part, but for current have a lot of questions:

    + what with cache dir permission on install?
    + why just a composer install –dev?
    + in such behaviour update/rollback not effective cause all time you need to real updating pakages instead of capyfony model where you just quick switch between revisions

  3. What happens if any migration fail and you need to rollback?
    Why do you have some lines commented?

    Thanks blog!

  4. I have used both Fabric and Capistrano but nowadays I only use the latter, because it has so much more tools readily available. Especially with Capifony for Symfony2 applications.

    How come you switched from Capistrano to Fabric, Luis?

Leave a Reply to Diego Oliveira Cancel reply

Your email address will not be published. Required fields are marked *