Example how to rsync and restart services remote server using Fabric


Fabric is great tools in Python which can ease our development. You can start installing it by :

1
sudo pip install fabric

Let create some example cases here.
1. I have remote server which have port SSH (it a must!) and use SSH-key (PEM) for login.
2. I have django apps that located in PROJECT_PATH (eg: ~/htdocs/pricedag)

So, we need create fabfile.py in PROJECT_PATH :

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
""" Deployment PriceDag Project """
import os

from fabric.api import env, sudo, local
from fabric.operations import put, run
from fabric.context_managers import cd

from settings import PROJECT_ROOT

SERVER_PATH = ‘/var/www/pricedag’

def server():
    env.hosts = [‘222.222.222.222’]
    env.user = ‘ubuntu’
    env.key_filename = [‘/home/ubuntu/my-key.pem’]

def restart_webserver():
    """ Restart NGINX & UWSGI
    """
    sudo("stop uwsgi-pricedag")
    sudo("start uwsgi-pricedag")

def syncdb():
    with cd(SERVER_PATH):
        run(‘python manage.py syncdb’)

def deploy():
    # Remove all .pyc files
    local("find . -name *.pyc -delete")
    local("tar -czvf /tmp/pricedag.tar.gz * –exclude=database.db
                                            –exclude=fabfile.py
                                            –exclude=.project
                                            –exclude=.pydevproject")

    put("/tmp/pricedag.tar.gz", "/tmp/")
    run("rm -rf %s" %s" % SERVER_PATH)
    run("mkdir -p %s" % SERVER_PATH)
    run("tar -zxvf /tmp/pricedag.tar.gz -C %s" % SERVER_PATH)
    run("tar -zxvf /tmp/pricedag.tar.gz -C %s" % SERVER_PATH)
    restart_webserver()
    syncdb()

Explanation :

1. Server PATH and login
I set SERVER_PATH for my django application in server.
Then i use “env” to set server ENVIRONMENT.

1
2
3
4
5
6
SERVER_PATH = ‘DJANGO-LOCATION-IN-SERVER’

def server():
    env.hosts = [‘my-server-ip-address’]
    env.user = ‘my-server-username’
    env.key_filename = [‘my-server-ssh-key’]

2. Restart services in Server
We can use “sudo” to execute root command in server.
At this example, I need to restart UWSGI services.

1
2
3
4
5
def restart_webserver():
    """ Restart NGINX & UWSGI
    """
    sudo("stop uwsgi-pricedag")
    sudo("start uwsgi-pricedag")

3. How to syncdb in remote server ?
Yes, we need to do “syncdb” in remote server and it executed by using “with” for moving into selected directory before executing command.

1
2
3
def syncdb():
    with cd(SERVER_PATH):
        run(‘python manage.py syncdb’)

4. Deploy

local() have function to execute command in local environment. For instance, I need to remove all .pyc files and compress my django applications before submitting into server.

put() have function to “rsync/copy” files from local into remote server.

run() have function to do command in remote server.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
def deploy():
    # Remove all .pyc files
    local("find . -name *.pyc -delete")
    local("tar -czvf /tmp/pricedag.tar.gz * –exclude=database.db
                                            –exclude=fabfile.py
                                            –exclude=.project
                                            –exclude=.pydevproject")

    put("/tmp/pricedag.tar.gz", "/tmp/")
    run("rm -rf %s" %s" % SERVER_PATH)
    run("mkdir -p %s" % SERVER_PATH)
    run("tar -zxvf /tmp/pricedag.tar.gz -C %s" % SERVER_PATH)
    run("tar -zxvf /tmp/pricedag.tar.gz -C %s" % SERVER_PATH)
    restart_webserver()
    syncdb()

So, how to execute this code ? It easy! you just go to where fabfile located and do :

1
fab server deploy

Which it have meaning : ”

1
fab <which-environment> <task-to-do>"

Leave a Reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.