pythonic deployment with fabric 0.9

15
Pythonic Deployment with Fabric 0.9 Corey Oordt The Washington Times @coordt [email protected] http://opensource.washingtontimes.com /

Upload: corey-oordt

Post on 13-May-2015

6.851 views

Category:

Technology


4 download

DESCRIPTION

The slides from my July Django-District presentation. It shows some of the basics of using the new fabric. I have uploaded the example fabfile.py to slideshare as well.

TRANSCRIPT

Page 1: Pythonic Deployment with Fabric 0.9

Pythonic Deployment with Fabric 0.9

Corey OordtThe Washington Times

@[email protected]://opensource.washingtontimes.com/

Page 2: Pythonic Deployment with Fabric 0.9

Easy Deployment for Different Environments

Testing

Staging

Production

Page 3: Pythonic Deployment with Fabric 0.9

Easy Deployment to Multiple Computers

Production 1

Production 2

Production 3

Page 4: Pythonic Deployment with Fabric 0.9

Installation of Fabric

$ easy_install paramiko

$ easy_install pycrypto

$ easy_install http://git.fabfile.org/cgit.cgi/fabric/snapshot/fabric-0.9b1.tar.gz

Page 5: Pythonic Deployment with Fabric 0.9

Fabric is just a command and a python script

fab fabfile.py

Page 6: Pythonic Deployment with Fabric 0.9

The fabfile.py

from fabric.api import *

env.roledefs = {

'web': ['172.16.12.1', '172.16.12.2',],

'media': ['172.16.12.4',],

'staging': ['172.16.12.5',],

'testing': ['172.16.12.6',],

'database': ['172.16.12.7', '172.16.12.8',],

}

env.root_path = '/var/websites'

Page 7: Pythonic Deployment with Fabric 0.9

Define Tasks to Setup Environments

def testing():

env.hosts = env.roledefs['testing']

env.user = 'testuser'

def staging():

env.hosts = env.roledefs['staging']

env.user = 'staginguser'

def production():

env.hosts = env.roledefs['web']

env.user = 'produser'

Page 8: Pythonic Deployment with Fabric 0.9

Task Example

def clean(site=None):

"""Remove .pyc files from a site."""

from os import path

if not site:

site = prompt('Please specify which site(s) to

clean (a comma delimited list is accepted): ', validate=r'^[\w-.,]+$')

site_list = site.split(',')

for item in site_list:

path = path.join(env.root_path, item)

sudo("find %s -name '*.pyc' -depth -exec rm {} \;"

% path)

Page 9: Pythonic Deployment with Fabric 0.9

Fabric Commands

• require: Make sure that certain environment variables are available.

• prompt: Display a prompt to the user and store the input in the given variable. Validation is optional.

• put: Upload files to the remote host(s).

• get: Download a file from a remote host.

• run: Run a shell command on the remote host(s).

• sudo: Run a root privileged shell command command on the remote host(s).

• local: Run a command locally.

Page 10: Pythonic Deployment with Fabric 0.9

Fabric Contrib Commands

• rsync_project: Synchronize a remote directory with the current project directory via rsync.

• upload_project: Upload the current project to a remote system, tar-gzipping during the move.

• exists: Return True if given path exists.

• first: Given one or more file paths, returns first one found, or None.

• upload_template: Render and upload a template text file to a remote host.

• sed: Run a search-and-replace on filename with given regex patterns.

• comment/uncomment: Attempt to (un)comment out all lines in filename matching regex.

• contains: Return True if filename contains text.

• append: Append string (or list of strings) text to filename.

• confirm: Ask user a yes/no question and return their response as True /False.

Page 11: Pythonic Deployment with Fabric 0.9

Fabric Decorators

• hosts: Defines on which host or hosts to execute the wrapped function.

• roles: Defines a list of role names, used to look up host lists.

• runs_once: Prevents wrapped function from running more than once.

@hosts('user1@host1', 'host2', 'user2@host3')@runs_oncedef my_func(): pass

@roles('web')def my_other_func(): pass

Page 12: Pythonic Deployment with Fabric 0.9

Usage Example

$ fab testing clean:site=basenews

[172.16.12.6] sudo: find /var/websites/basenews -name '*.pyc' -depth -exec rm {} \;

Password for [email protected]:

Done.

Disconnecting from 172.16.12.6... done.

$

Page 13: Pythonic Deployment with Fabric 0.9

Example Uses

• Reload web server settings

• Clean .pyc files

• Check out new revision from a SCM repository

• Install packages on servers

• Apply patches

• Restart caching server

• Dump database and restore it locally

Page 14: Pythonic Deployment with Fabric 0.9

Let’s look at an example fabfile.py…