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):
        install()
 
#    tag_prod()
    update()
 
#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 :’(.

Twig Lexer Override for Special Situations

Update: This is coming from the mailing database, but i thought to replicate it because it is something very useful sometimes.
Credits to: Casey Klebba

You can use a custom Twig_Lexer this way:

in config.yml
———–

services:
    twig:
        class: Twig_Environment
        arguments: ['@twig.loader', %twig.options%]
        calls:
            - [addGlobal, ['app','@templating.globals']]
            - [setLexer, ['@custom.lexer']]
    custom.lexer:
        class: Acme\DemoBundle\Twig\CoreLexer
        arguments: ['@twig']

Then:

namespace Acme\DemoBundle\Twig;
 
use \Twig_Environment;
use \Twig_Lexer;
 
class CoreLexer extends Twig_Lexer {
 
    public function __construct(Twig_Environment $twig)
    {
        parent::__construct($twig, array(
            'tag_comment'   => array('{#', '#}'),
            'tag_block'     => array('{%', '%}'),
            'tag_variable'  => array('{{', '}}'),
            'interpolation' => array('#{', '}'),
        ));
    }
}

Very easy now!

–Casey

That is it. Enjoy! And thanks Casey.