Background Image
Table of Contents Table of Contents
Previous Page  186 / 478 Next Page
Information
Show Menu
Previous Page 186 / 478 Next Page
Page Background

1. Download and install pycrypto from the previous URL.

2. pip install Fabric.

Another amazing source of precompiled Python packages for Windows is maintained

by

Christoph Gohlke .

The usual setup is to have a file called

fabfile.py

, whichwill contain one ormore functions

that can later be invoked from a command-line tool called

fab

, like this:

fab function_name,host=SERVER_ADDRESS

That will invoke the function called

function_name

, passing in a connection to the

server at

SERVER_ADDRESS

. There are many other options for specifying usernames and

passwords, which you can find out about using

fab --help

.

Breakdown of a Fabric Script for Our Deployment

The best way to see how it works is with an example.

Here’s one I made earlier ,

auto‐

mating all the deployment steps we’ve been going through. The main function is called

deploy

; that’s the one we’ll invoke from the command line. It uses several helper func‐

tions.

env.host

will contain the server address that we’ve passed in:

deploy_tools/fabfile.py.

from

fabric.contrib.files

import

append

,

exists

,

sed

from

fabric.api

import

env

,

local

,

run

import

random

REPO_URL

=

'https://github.com/hjwp/book-example.git

'

#

def

deploy

():

site_folder

=

'/home/

%s

/sites/

%s

'

%

(

env

.

user

,

env

.

host

)

#

source_folder

=

site_folder

+

'/source'

_create_directory_structure_if_necessary

(

site_folder

)

_get_latest_source

(

source_folder

)

_update_settings

(

source_folder

,

env

.

host

)

_update_virtualenv

(

source_folder

)

_update_static_files

(

source_folder

)

_update_database

(

source_folder

)

You’ll want to update the

REPO_URL

variable with the URL of your own Git repo

on its code sharing site.

env.host

will contain the address of the server we’ve specified at the command

line, eg,

superlists.ottg.eu

.

env.user

will contain the username you’re using to log in to the server.

158

|

Chapter 9: Automating Deployment with Fabric