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

Pandora MonroeFeb. 19, 2019, 11:36 a.m.

Different servers are giving the good quality fabric so that people can use it. Mostly there is ninjaessays that are going to show us much in every manner for working purposes.

Reply
nark spencerFeb. 22, 2019, 6:48 a.m.

Thanks for sharing this amazing this amazing photograph of your drawing. Amazing blog post indeed. Share more artistic posts please.Eye Of The Tiger Jacket I would recommend that you should start posting tutorials as well.

Reply
star19March 7, 2019, 10:15 a.m.

Great Post... Nice share for me, maybe nice for all reader of your post.. thanks.
Online cgc
mechanical engineering college in punjab
top MBA college in chandigarh

Reply
fashionMarch 18, 2019, 12:33 p.m.

Excellent information on your blog buy distrito salvaje coat, thank you for taking the time to share with us. Amazing insight you have on this, it’s nice to find a website the details so much information about different artists.

Reply
David MillarApril 3, 2019, 12:11 p.m.

Thank you for sharing this. What is so amazing, God had a plan for everyone, from the beginning. There. Newt Scamander 2 Coat

Reply
Mike RooneyApril 5, 2019, 4:25 p.m.

Your article has proved your hard work and experience you have got in this field. Brilliant. I love it reading. Doom Patrol Matt Bomer Coat

Reply
dsdsdMay 6, 2019, 5:27 a.m.

Il y a toujours un problème dans le processus de vente: le client a une méfiance vis-à-vis du personnel de vente. Ils pensent que les informations obtenues auprès du personnel de vente de replique montre de luxe auront souvent des degrés différents un peu de fraude. Par conséquent, de nombreux clients pensent que les paroles du vendeur peuvent être entendues ou non dans le processus de communication avec le personnel de vente, ne s'en soucient souvent pas trop et même se disputer avec le personnel de vente avec une psychologie rebelle.rolex montre

Reply
david joeMay 16, 2019, 10:19 a.m.

Thanks for Nice and Informative Post. This article is really contains lot more information about This Topic. We have read your all the information some points are also good hollywood jacket and some usually are awesome. Great post I would like to thank you for the efforts you have made in writing this interesting and knowledgeable article.

Reply
Mike RooneyMay 17, 2019, 8:07 p.m.

It would be nice just for the questioner to acknowledge the best answer rather than just leave it to voters. B3 Bomber Jacket

Reply

Post New Comment