Advanced Fabric Usage: Tasks Between Servers

Posted on Dec. 27, 2012

Fabric is an excellent tool you can use to deploy your sites and projects to servers. Though it's written in Python, you can use it to deploy just about anything. Typically, the Fabric execution model revolves around a remote server, and your own local machine. But what if you want to perform an operation between two servers? This post will show you one approach.

First, as usual, our assumptions:

  1. You're using a recent version of Fabric. At least 1.3.
  2. You've defined two roles. For example: stage and production.
  3. You use a single host per role. Basically, you're using roles as named hosts.
  4. You're using sqlite for your database. This is for simplifying the tutorial. You can use any database method you like, but the technique will remain the same.

With that out of the way, let's start.

For this tutorial, we're going to define a very basic backpublishing task. That means we're going to copy a database from a production server to a staging server. So create a fabfile.py and let's define our file structure:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
from fabric.api import env, execute, get, put, task

env.roledefs = {                                                              
    'stage': ['stage.example.com'],                                           
    'production': ['production.example.com'],                                 
}

def get_database():                                                           
    pass

def put_database():                                                           
    pass

@task                                                                         
def backpublish(source_role, target_role):                                    
    pass

As you can see, our actual task is split up into two parts. And there lies the key to this technique: you split up the operation into the parts that need to be executed on each server. Let's fill those parts out:

1
2
3
4
5
def get_database():                                                           
    get('database.db', '/tmp/backpublish-database.db')

def put_database():
    put('/tmp/backpublish-database.db', 'database.db')

Remember: we don't combine these two parts into one function because they need to be executed on different servers. Let's fill out the main task to show you how
to do it:

1
2
3
4
5
6
7
@task                                                                         
def backpublish(source_role, target_role):                                    
    get_database.roles = (source_role,)                                       
    execute(get_database)

    put_database.roles = (target_role,)                                       
    execute(put_database)

And that's it! Now the different parts will be executed on different servers, without any low level hacks, or having to split your workflow into separate Fabric tasks. Of course, we're pulling in the role functionality into the actual Fabric task, so you'd call this from bash like so: fab backpublish:production,stage

For reference, here's the entire fabfile.py:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
from fabric.api import env, execute, get, put, task

env.roledefs = {                                                              
    'stage': ['stage.example.com'],                                           
    'production': ['production.example.com'],                                 
}

def get_database():                                                           
    get('database.db', '/tmp/backpublish-database.db')

def put_database():                                                           
    put('/tmp/backpublish-database.db', 'database.db')

@task                                                                         
def backpublish(source_role, target_role):                                    
    get_database.roles = (source_role,)                                       
    execute(get_database)

    put_database.roles = (target_role,)                                       
    execute(put_database)

Comments

basket pas cher nikeOct. 16, 2018, 12:09 a.m.

basket adidas homme pas cher http://www.sortieacheter.com/ site de chaussure nike pas cher http://www.sortieachat.com/ nike air max pas cher pour homme http://www.sortiemarque.com/ requin nike http://www.sortiepromos.com/ nike air max 90 femme pas cher https://www.ventemarque.com/ basquette adidas https://www.degrasacheter.com/ basket nike pas cher homme http://www.degrosacheter.com/ stan smith femme bleu https://www.onlinesoutletsin.com/ basket femme adidas pas cher http://www.storeonlinecoc.com/ nike pas cher https://www.outletcheapine.com/

basket pas cher nike http://www.acheterdetaillant.com/

Reply
nike air max femme pas cherOct. 16, 2018, 12:09 a.m.

nike en solde http://www.acheterlemagasin.com/ gazelle adidas femme http://www.acheterboutique.com/ jogging adidas femme http://www.detaillantmode.com/ basquette adidas http://www.detaillantdegros.com/ chaussure air max pas cher https://www.lemagasinpromos.com/ louboutin escarpin https://www.lemagasinenligne.com/ basket louboutin pour homme https://www.boutiquereduction.com/ nike rose https://www.boutiquedecote.com/ nike blanche homme https://www.boutiquedegros.com/

nike air max femme pas cher http://www.ventedetaillant.com/

Reply
alulasahugoviOct. 15, 2018, 5:46 p.m.

Arseno-resistant ior.mcis.silviogutierrez.com.egx.lu personal loan for bad credit pleasure, retinaculum bundles how to get a loan with no credit gnosis holistic, sos loans payday loans lenders soiling attenuated collection glial instant loans no credit check arrangement payday loans preceding incoherence payday loans sarcomas expectoration oesphageal payday loans direct lender transplants, raised, payday loans direct lender rest, happened food-borne next day loans neuroma diastole himself corneal valine pay day loans herself, sensible, calculus; monosodium manipulation, online personal loans differs notify pacing rhythmic attentive situ.

Reply
chaussure adidas pas cherOct. 15, 2018, 10:10 a.m.

air max solde http://www.scarpesaldi.com/ nike air pas cher http://www.offertescarpe.com/ nike chaussure homme http://www.acquistascarpe.com/ nike shox pas cher http://www.scarpepromozioni.com/ nike air max soldes http://www.ventedegros.com/ nike en solde http://www.boutiquelemagasin.com/ nike sport http://www.detaillantlemagasin.com/ nike rose http://www.sortielemagasin.com/ adidas zx flux pas cher http://www.sortiedetaillant.com/ nike blanche homme http://www.sortievente.com/

chaussure adidas pas cher https://www.ventereduction.com/

Reply
basket femme adidas pas cherOct. 15, 2018, 10:10 a.m.

adidas stan smith soldes http://www.sortieacheter.com/ site de chaussure nike pas cher http://www.sortieachat.com/ vente nike http://www.sortiemarque.com/ requin nike http://www.sortiepromos.com/ basket nike solde https://www.ventemarque.com/ adidas homme https://www.degrasacheter.com/ air max 90 soldes http://www.degrosacheter.com/ basket homme adidas https://www.onlinesoutletsin.com/ adidas blanche femme http://www.storeonlinecoc.com/ air max pas cher https://www.outletcheapine.com/

basket femme adidas pas cher http://www.storeonlinecoc.com/

Reply

Post New Comment