SlideShare a Scribd company logo
Pythonic Deployment with Fabric 0.9
Corey Oordt
The Washington Times

@coordt
coordt@washingtontimes.com
http://opensource.washingtontimes.com/
Easy Deployment for Different Environments




                                     Testing




                                     Staging




                                    Production
Easy Deployment to Multiple Computers




                                  Production 1




                                  Production 2




                                  Production 3
Installation of Fabric

 $ easy_install paramiko
 $ easy_install pycrypto
 $ easy_install http://git.fabfile.org/cgit.cgi/fabric/
 snapshot/fabric-0.9b1.tar.gz
Fabric is just a command and a python script




            fab                fabfile.py
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'
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'
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)
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.
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.
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_once
            def my_func():
                pass

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

$ fab testing clean:site=basenews
[172.16.12.6] sudo: find /var/websites/basenews -name
'*.pyc' -depth -exec rm {} ;
Password for testuser@192.168.56.3:


Done.
Disconnecting from 172.16.12.6... done.
$
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
Let’s look at an example fabfile.py…
Thank You
Corey Oordt
The Washington Times

@coordt
coordt@washingtontimes.com
http://opensource.washingtontimes.com/

More Related Content

PPTX
DevOps with Fabric
PPTX
A Fabric/Puppet Build/Deploy System
PPTX
Automated Deployment with Fabric
PDF
Fabric-让部署变得简单
ODP
Fabric: A Capistrano Alternative
PDF
Fabric workshop(1) - (MOSG)
PPTX
CoreOS in a Nutshell
ODP
Fabric Fast & Furious edition
DevOps with Fabric
A Fabric/Puppet Build/Deploy System
Automated Deployment with Fabric
Fabric-让部署变得简单
Fabric: A Capistrano Alternative
Fabric workshop(1) - (MOSG)
CoreOS in a Nutshell
Fabric Fast & Furious edition

What's hot (20)

PDF
Software development practices in python
PDF
Launching containers with fleet
PDF
CoreOS : 설치부터 컨테이너 배포까지
PPT
Learn basic ansible using docker
PDF
Ansible, best practices
PDF
CoreOS: Control Your Fleet
PDF
Ansible 實戰:top down 觀點
PDF
Making environment for_infrastructure_as_code
PDF
CoreOSによるDockerコンテナのクラスタリング
PPT
Python Deployment with Fabric
PDF
Object Storage with Gluster
PDF
IT Automation with Ansible
PPTX
Ansible Network Automation session1
PDF
Cookbook testing with KitcenCI and Serverrspec
PDF
Take control of your Jenkins jobs via job DSL.
PDF
Ansible not only for Dummies
PDF
Using Puppet to Create a Dynamic Network - PuppetConf 2013
PDF
Beaker: Automated, Cloud-Based Acceptance Testing - PuppetConf 2014
Software development practices in python
Launching containers with fleet
CoreOS : 설치부터 컨테이너 배포까지
Learn basic ansible using docker
Ansible, best practices
CoreOS: Control Your Fleet
Ansible 實戰:top down 觀點
Making environment for_infrastructure_as_code
CoreOSによるDockerコンテナのクラスタリング
Python Deployment with Fabric
Object Storage with Gluster
IT Automation with Ansible
Ansible Network Automation session1
Cookbook testing with KitcenCI and Serverrspec
Take control of your Jenkins jobs via job DSL.
Ansible not only for Dummies
Using Puppet to Create a Dynamic Network - PuppetConf 2013
Beaker: Automated, Cloud-Based Acceptance Testing - PuppetConf 2014
Ad

Viewers also liked (20)

PDF
Ansible on AWS
PPTX
Flask and Paramiko for Python VA
PPTX
Django deployment best practices
PDF
Django rest framework in 20 minuten
KEY
Do more than one thing at the same time, the Python way
PDF
Fabric
ODP
Fabric (python)
TXT
fabfile.py
PDF
Towards Continuous Deployment with Django
PDF
Automation - fabric, django and more
PDF
Two scoops of Django - Security Best Practices
PDF
Fabric - a server management tool from Instagram
PDF
Django REST Framework
PDF
Scaling mysql with python (and Docker).
PDF
Building a platform with Django, Docker, and Salt
PDF
Life in a Queue - Using Message Queue with django
PPT
Fabric
PDF
Django in the Real World
KEY
Scaling Django
PDF
Fabric, Cuisine and Watchdog for server administration in Python
Ansible on AWS
Flask and Paramiko for Python VA
Django deployment best practices
Django rest framework in 20 minuten
Do more than one thing at the same time, the Python way
Fabric
Fabric (python)
fabfile.py
Towards Continuous Deployment with Django
Automation - fabric, django and more
Two scoops of Django - Security Best Practices
Fabric - a server management tool from Instagram
Django REST Framework
Scaling mysql with python (and Docker).
Building a platform with Django, Docker, and Salt
Life in a Queue - Using Message Queue with django
Fabric
Django in the Real World
Scaling Django
Fabric, Cuisine and Watchdog for server administration in Python
Ad

Similar to Pythonic Deployment with Fabric 0.9 (20)

PDF
Bangpypers april-meetup-2012
PPTX
Deployment with Fabric
PDF
Belvedere
PDF
Continuous Delivery: The Next Frontier
PPTX
Tribal Nova Docker workshop
PPTX
One click deployment
PDF
手把手帶你學Docker 03042017
PDF
How to test infrastructure code: automated testing for Terraform, Kubernetes,...
PDF
Deployment Tactics
PPTX
Lessons from running potentially malicious code inside containers
KEY
把鐵路開進視窗裡
PDF
Very Early Review - Rocket(CoreOS)
PDF
AstriCon 2017 - Docker Swarm & Asterisk
PDF
The Docker "Gauntlet" - Introduction, Ecosystem, Deployment, Orchestration
PDF
Infrastructure = code - 1 year later
PPTX
ABCs of docker
PDF
時代在變 Docker 要會:台北 Docker 一日入門篇
PPTX
Docker Starter Pack
PDF
手把手帶你學 Docker 入門篇
PDF
Docker workshop 0507 Taichung
Bangpypers april-meetup-2012
Deployment with Fabric
Belvedere
Continuous Delivery: The Next Frontier
Tribal Nova Docker workshop
One click deployment
手把手帶你學Docker 03042017
How to test infrastructure code: automated testing for Terraform, Kubernetes,...
Deployment Tactics
Lessons from running potentially malicious code inside containers
把鐵路開進視窗裡
Very Early Review - Rocket(CoreOS)
AstriCon 2017 - Docker Swarm & Asterisk
The Docker "Gauntlet" - Introduction, Ecosystem, Deployment, Orchestration
Infrastructure = code - 1 year later
ABCs of docker
時代在變 Docker 要會:台北 Docker 一日入門篇
Docker Starter Pack
手把手帶你學 Docker 入門篇
Docker workshop 0507 Taichung

Recently uploaded (20)

PPTX
Understanding_Digital_Forensics_Presentation.pptx
PDF
Modernizing your data center with Dell and AMD
PDF
TokAI - TikTok AI Agent : The First AI Application That Analyzes 10,000+ Vira...
PDF
Blue Purple Modern Animated Computer Science Presentation.pdf.pdf
PPTX
Effective Security Operations Center (SOC) A Modern, Strategic, and Threat-In...
PDF
Electronic commerce courselecture one. Pdf
PPTX
PA Analog/Digital System: The Backbone of Modern Surveillance and Communication
PDF
Reach Out and Touch Someone: Haptics and Empathic Computing
DOCX
The AUB Centre for AI in Media Proposal.docx
PDF
CIFDAQ's Market Insight: SEC Turns Pro Crypto
PDF
Diabetes mellitus diagnosis method based random forest with bat algorithm
PDF
Per capita expenditure prediction using model stacking based on satellite ima...
PPTX
MYSQL Presentation for SQL database connectivity
PDF
Peak of Data & AI Encore- AI for Metadata and Smarter Workflows
PDF
Bridging biosciences and deep learning for revolutionary discoveries: a compr...
PDF
Optimiser vos workloads AI/ML sur Amazon EC2 et AWS Graviton
PDF
GamePlan Trading System Review: Professional Trader's Honest Take
PDF
Network Security Unit 5.pdf for BCA BBA.
PDF
The Rise and Fall of 3GPP – Time for a Sabbatical?
PPTX
Detection-First SIEM: Rule Types, Dashboards, and Threat-Informed Strategy
Understanding_Digital_Forensics_Presentation.pptx
Modernizing your data center with Dell and AMD
TokAI - TikTok AI Agent : The First AI Application That Analyzes 10,000+ Vira...
Blue Purple Modern Animated Computer Science Presentation.pdf.pdf
Effective Security Operations Center (SOC) A Modern, Strategic, and Threat-In...
Electronic commerce courselecture one. Pdf
PA Analog/Digital System: The Backbone of Modern Surveillance and Communication
Reach Out and Touch Someone: Haptics and Empathic Computing
The AUB Centre for AI in Media Proposal.docx
CIFDAQ's Market Insight: SEC Turns Pro Crypto
Diabetes mellitus diagnosis method based random forest with bat algorithm
Per capita expenditure prediction using model stacking based on satellite ima...
MYSQL Presentation for SQL database connectivity
Peak of Data & AI Encore- AI for Metadata and Smarter Workflows
Bridging biosciences and deep learning for revolutionary discoveries: a compr...
Optimiser vos workloads AI/ML sur Amazon EC2 et AWS Graviton
GamePlan Trading System Review: Professional Trader's Honest Take
Network Security Unit 5.pdf for BCA BBA.
The Rise and Fall of 3GPP – Time for a Sabbatical?
Detection-First SIEM: Rule Types, Dashboards, and Threat-Informed Strategy

Pythonic Deployment with Fabric 0.9

  • 1. Pythonic Deployment with Fabric 0.9 Corey Oordt The Washington Times @coordt coordt@washingtontimes.com http://opensource.washingtontimes.com/
  • 2. Easy Deployment for Different Environments Testing Staging Production
  • 3. Easy Deployment to Multiple Computers Production 1 Production 2 Production 3
  • 4. Installation of Fabric $ easy_install paramiko $ easy_install pycrypto $ easy_install http://git.fabfile.org/cgit.cgi/fabric/ snapshot/fabric-0.9b1.tar.gz
  • 5. Fabric is just a command and a python script fab fabfile.py
  • 6. 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'
  • 7. 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'
  • 8. 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)
  • 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.
  • 10. 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.
  • 11. 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_once def my_func(): pass @roles('web') def my_other_func(): pass
  • 12. Usage Example $ fab testing clean:site=basenews [172.16.12.6] sudo: find /var/websites/basenews -name '*.pyc' -depth -exec rm {} ; Password for testuser@192.168.56.3: Done. Disconnecting from 172.16.12.6... done. $
  • 13. 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
  • 14. Let’s look at an example fabfile.py…
  • 15. Thank You Corey Oordt The Washington Times @coordt coordt@washingtontimes.com http://opensource.washingtontimes.com/

Editor's Notes

  • #3: Fabric allows for easy deployment for different environments. Not just for testing vs. production also for database vs. web.
  • #4: Especially helpful if you need to perform the same tasks on multiple computers, as in a web server farm.
  • #5: There has been a rather substantial change from the current release (0.1.1) and the pending release (0.9). I’ll be showing the pending release.
  • #6: Fabric is just the fab command. It looks for a file named fabfile.py in the current directory, but you can specify a specific file. The fabfile.py contains one or more tasks, written in python, that are executed on one or more hosts.
  • #7: The fabfile typically starts with some environment setup. The env variable is used for standard configs like roledefs and hosts, and anything else you need.
  • #8: One way of handling multiple environments is to set up tasks that set the correct environment settings. Because fabric allows you to chain tasks, you can specify the appropriate environment task before the deployment task
  • #9: A task is standard python, with some fabric-specific commands thrown in. In this example there is prompt, sudo, and use of the env variable. The task is run on every host specified.
  • #10: These are the basic commands that fabric adds.
  • #11: There are some contributed commands, in the contrib module that provide more functionality.
  • #12: You can also decorate your tasks to specify a role or specific set of hosts.
  • #13: In this example, you can see we are chaining the testing environment setup task in front of the clean task. We are passing the site parameter to the clean task. If necessary, it will prompt you for the password for sudo. It will attempt to use the same password on each host.