Fabric

Calvin Cheng
Calvin ChengI design and code em I work for my app users! :-)
fabric
Control your linux nodes with python
What is it?
• Command-line tool for deployment automation and system
administration as a ssh wrapper
• Executes to local or remote
✓ local: not a ssh connection. uses python subprocess
module.
✓ run or sudo: when executing against remote, it can
execute as the current user or as current user with
sudo rights.
What is it?
• Simply specify python functions in fabfile.py
• Install it
✓ pip install fabric
✓ Once installed, fab command is available in your system
path.
✓ Start writing your python function in fabfile.py
automation
• Daily tasks
• Server setup
• System update and maintenance
• New project creation
• Deployment processes
get started
• Download and install virtualbox https://www.virtualbox.org
• Download and install vagrant http://www.vagrantup.com
cd ~/VirtualBoxVMs
mkdir debsqueeze64
cd debsqueeze64
vagrant init debsqueeze64 http://www.emken.biz/vagrant-
boxes/debsqueeze64.box
vagrant up
get started
• This downloads and creates an image of debian 64-bit on
your computer.
• We will use fabric on our local machine to interact with
this debian virtual machine
• See http://www.vagrantbox.es to download other prepared
boxes.
PythonVirtualenv
• Create an isolated python project environment
Mac OS X: package management with port or brew
sudo port -v install virtualenv_select py27-virtualenv
sudo port -v install py27-virtualenvwrapper
sudo port select --set virtualenv virtualenv27
(optional)
PythonVirtualenv
• Edit your ~/.bash_profile
export WORKON_HOME=$HOME/.virtualenvs
export PROJECT_HOME=$HOME/work
source `which virtualenvwrapper.sh`
• $HOME -- this refers to your user home or “~”
(i.e., on my computer,“~” means “/Users/calvin” directory)
(optional)
PythonVirtualenv
• Create our python virtualenv for our fabric tutorial
mkvirtualenv --distribute --no-site-packages learnfabric
cd $PROJECT_HOME
git clone git://github.com/calvinchengx/learnfabric.git
cd learnfabric
pip install -r requirements.txt
(optional)
learn fabric
• Once we have our debian virtual machine and optionally
our python virtualenv ready, we begin
git clone git://github.com/calvinchengx/learnfabric.git
cd learnfabric
pip install -r requirements.txt
cp -rf ~/VirtualBoxVMs/debsqueeze64/.vagrant .
• This installs fabric and installs python-vagrant (a wrapper
around vagrant commands so we can programmatically
control our vagrant instance)
learn fabric
• Once we have our debian virtual machine and optionally
our python virtualenv ready, we begin
git clone git://github.com/calvinchengx/learnfabric.git
cd learnfabric
pip install -r requirements.txt
cp -rf ~/VirtualBoxVMs/debsqueeze64/.vagrant .
• This installs fabric and installs python-vagrant (a wrapper
around vagrant commands so we can programmatically
control our vagrant instance)
fabric function
fab mytask
[vagrant@127.0.0.1:2222] Executing task 'mytask'
[vagrant@127.0.0.1:2222] run: echo $USER
[vagrant@127.0.0.1:2222] out: vagrant[vagrant@127.0
.0.1:2222] out:
Done.
Disconnecting from vagrant@127.0.0.1:2222... done.
the code
import vagrantfrom fabric.api import env, task, runv =
vagrant.Vagrant()v.up()env.hosts =
[v.user_hostname_port()]env.key_filename =
v.keyfile()env.disable_known_hosts = True # useful when vagrant box ip
changes.
# our first fabric function@taskdef mytask(): run('echo $USER')
@taskdef mytask(): run('echo $USER')
fabric core
• local() runs a command locally, via python subprocess
• run() runs a command remotely, via ssh
• sudo() runs a command remotely as sudo, via ssh
• put() copies a file from local to remote, via ssh
• get() copies a file from remote to local, via ssh
fabric AUth
• All authentication is ssh-based
• SSH keys help us avoid typing in passwords - place fabric
ssh user (env.user)’s public key in remote node’s
authorized_keys
• Avoid directly using root user
• Give your fabric ssh user sudo rights instead (env.user)
fabric config
• from fabric.api import env
• fabric environment is simply a dictionary containing host
information, roles, user (env.user); and
• any other custom information you would like to include.
• $HOME/.fabricrc allows custom configuration, e.g.
user = ssh_user_name
where “ssh_user_name” is any value you so desire to pass
in to env.user when a fab function runs.
Fabric ROLEDEFS
# code
from fabric.api import env
env.roledefs = {
‘web’: [‘100.0.0.1’, ‘100.0.0.2’],
‘db’: [‘100.0.0.3’],
‘media’: [‘100.0.0.4’]
}
# command line
fab -R web mytask
Fabric host(S)
# code
from fabric.api import env
env.roledefs = {
‘web’: [‘100.0.0.1’, ‘100.0.0.2’],
‘db’: [‘100.0.0.3’],
‘media’: [‘100.0.0.4’]
}
# command line
fab -H 100.0.0.1,100.0.0.2 mytask
map roles to tasks
# code
from fabric.api import env
from fabric.decorators import roles
env.roledefs = {
‘web’: [‘100.0.0.1’, ‘100.0.0.2’],
‘db’: [‘100.0.0.3’],
‘media’: [‘100.0.0.4’]
}
@roles(‘web’)
def mytask():
run(‘uptime’)
# command line
fab mytask # already mapped to -R web
handling failures
• Do not abort upon failure; just give a warning
# code
from fabric.context_managers import settings
def mytask():
with settings(warn_only=True):
run(‘rm /var/www/proj/releases/current’)
run(‘ln -s /var/www/proj/releases/deployed
/var/www/proj/releases/current’)
Lazy sysadmin
• A generic command
# code
@roles(‘all’)
def invoke(command):
“””
Invoke an arbitrary command
“””
sudo(command)
# command line
fab invoke:“aptitude update”
fab invoke:”aptitude upgrade”
parallel execution
• A generic command
# code
@roles(‘all’)
def invoke(command):
“””
Invoke an arbitrary command
“””
sudo(command)
# command line
fab -P invoke:“aptitude update”
fab -P invoke:”aptitude upgrade”
python web app
Example deploy
@task(default=True)@set_target_envdef deploy(email=False): """ `fab -R all deploy` or fab -H mysite.com deploy`.
Execute a deployment to the given groups of hosts or host """ if not chk_req(): return if git_branch_check()
or test_host_check(): manage_release('Deployment start') git_archive_and_upload_tar()
pip_requirements() collectstatic(deploy=True) symlink_current() webserver()
migrate_db() # post-deployment tasks manage_release('Deployment end') _releases_cleanup()
email_on_success(trigger=email)
# command line
fab -R all deploy
quick refErence
• fab -H host1.com task
• fab -H host1.com task:arg1,arg2
• fab -P -H localhost, host1.com, host2.com task
• fab -R web task # roles defined by env.roledefs
• fab -R web task:arg1,arg2
• fab -P -R web task
• fab -l
• more options explained via fab --help
devops
• Bridging the gap between code implementation by
developers and deployment to staging and/or live servers
by sysadmin
• Automation leads to continuous integration (e.g. Jenkins CI
Server) and continuous delivery
• Support heterogeneous development environment in
combination with vagrant (developers test against vagrant,
which has same distro as staging and production hosts)
referenceS
• My reusable fabric functions
https://github.com/fabric-colors/fabric-colors
https://fabric-colors.readthedocs.org/en/latest/index.html
• Code that accompanies this set of slides
https://github.com/calvinchengx/learnfabric
• Fabric code and documentation
https://github.com/fabric/fabric
http://fabric.readthedocs.org
• Multiple vagrant boxes for testing your server config
http://awaseroot.wordpress.com/2012/05/06/script-for-
q&A
• Get in touch @calvinchengx
• http://calvinx.com
1 de 28

Recomendados

Instruction: dev environment por
Instruction: dev environmentInstruction: dev environment
Instruction: dev environmentSoshi Nemoto
674 visualizações22 slides
Fabric workshop(1) - (MOSG) por
Fabric workshop(1) - (MOSG)Fabric workshop(1) - (MOSG)
Fabric workshop(1) - (MOSG)Soshi Nemoto
533 visualizações29 slides
DevOps(2) : Vagrant - (MOSG) por
DevOps(2) : Vagrant  -  (MOSG)DevOps(2) : Vagrant  -  (MOSG)
DevOps(2) : Vagrant - (MOSG)Soshi Nemoto
793 visualizações14 slides
DevOps(3) : Ansible - (MOSG) por
DevOps(3) : Ansible - (MOSG)DevOps(3) : Ansible - (MOSG)
DevOps(3) : Ansible - (MOSG)Soshi Nemoto
822 visualizações26 slides
Preparation study of_docker - (MOSG) por
Preparation study of_docker  - (MOSG)Preparation study of_docker  - (MOSG)
Preparation study of_docker - (MOSG)Soshi Nemoto
597 visualizações31 slides
Making environment for_infrastructure_as_code por
Making environment for_infrastructure_as_codeMaking environment for_infrastructure_as_code
Making environment for_infrastructure_as_codeSoshi Nemoto
436 visualizações21 slides

Mais conteúdo relacionado

Mais procurados

DevOps(4) : Ansible(2) - (MOSG) por
DevOps(4) : Ansible(2) - (MOSG)DevOps(4) : Ansible(2) - (MOSG)
DevOps(4) : Ansible(2) - (MOSG)Soshi Nemoto
851 visualizações49 slides
Create your very own Development Environment with Vagrant and Packer por
Create your very own Development Environment with Vagrant and PackerCreate your very own Development Environment with Vagrant and Packer
Create your very own Development Environment with Vagrant and Packerfrastel
3.6K visualizações67 slides
EC2 AMI Factory with Chef, Berkshelf, and Packer por
EC2 AMI Factory with Chef, Berkshelf, and PackerEC2 AMI Factory with Chef, Berkshelf, and Packer
EC2 AMI Factory with Chef, Berkshelf, and PackerGeorge Miranda
12.8K visualizações28 slides
Rackspace Hack Night - Vagrant & Packer por
Rackspace Hack Night - Vagrant & PackerRackspace Hack Night - Vagrant & Packer
Rackspace Hack Night - Vagrant & PackerMarc Cluet
2K visualizações39 slides
Vagrant and CentOS 7 por
Vagrant and CentOS 7Vagrant and CentOS 7
Vagrant and CentOS 7Julien Pivotto
15.4K visualizações67 slides
Getting started with Ansible por
Getting started with AnsibleGetting started with Ansible
Getting started with AnsibleIvan Serdyuk
494 visualizações83 slides

Mais procurados(20)

DevOps(4) : Ansible(2) - (MOSG) por Soshi Nemoto
DevOps(4) : Ansible(2) - (MOSG)DevOps(4) : Ansible(2) - (MOSG)
DevOps(4) : Ansible(2) - (MOSG)
Soshi Nemoto851 visualizações
Create your very own Development Environment with Vagrant and Packer por frastel
Create your very own Development Environment with Vagrant and PackerCreate your very own Development Environment with Vagrant and Packer
Create your very own Development Environment with Vagrant and Packer
frastel3.6K visualizações
EC2 AMI Factory with Chef, Berkshelf, and Packer por George Miranda
EC2 AMI Factory with Chef, Berkshelf, and PackerEC2 AMI Factory with Chef, Berkshelf, and Packer
EC2 AMI Factory with Chef, Berkshelf, and Packer
George Miranda12.8K visualizações
Rackspace Hack Night - Vagrant & Packer por Marc Cluet
Rackspace Hack Night - Vagrant & PackerRackspace Hack Night - Vagrant & Packer
Rackspace Hack Night - Vagrant & Packer
Marc Cluet2K visualizações
Vagrant and CentOS 7 por Julien Pivotto
Vagrant and CentOS 7Vagrant and CentOS 7
Vagrant and CentOS 7
Julien Pivotto15.4K visualizações
Getting started with Ansible por Ivan Serdyuk
Getting started with AnsibleGetting started with Ansible
Getting started with Ansible
Ivan Serdyuk494 visualizações
Ansible 實戰:top down 觀點 por William Yeh
Ansible 實戰:top down 觀點Ansible 實戰:top down 觀點
Ansible 實戰:top down 觀點
William Yeh19.6K visualizações
Small Python Tools for Software Release Engineering por pycontw
Small Python Tools for Software Release EngineeringSmall Python Tools for Software Release Engineering
Small Python Tools for Software Release Engineering
pycontw1.8K visualizações
Introduction to Ansible (Pycon7 2016) por Ivan Rossi
Introduction to Ansible (Pycon7 2016)Introduction to Ansible (Pycon7 2016)
Introduction to Ansible (Pycon7 2016)
Ivan Rossi837 visualizações
Automate with Ansible basic (2/e, English) por Chu-Siang Lai
Automate with Ansible basic (2/e, English)Automate with Ansible basic (2/e, English)
Automate with Ansible basic (2/e, English)
Chu-Siang Lai1.1K visualizações
A Fabric/Puppet Build/Deploy System por adrian_nye
A Fabric/Puppet Build/Deploy SystemA Fabric/Puppet Build/Deploy System
A Fabric/Puppet Build/Deploy System
adrian_nye9.2K visualizações
Ansible for beginners por Kuo-Le Mei
Ansible for beginnersAnsible for beginners
Ansible for beginners
Kuo-Le Mei4.9K visualizações
Test-Driven Infrastructure with Ansible, Test Kitchen, Serverspec and RSpec por Martin Etmajer
Test-Driven Infrastructure with Ansible, Test Kitchen, Serverspec and RSpecTest-Driven Infrastructure with Ansible, Test Kitchen, Serverspec and RSpec
Test-Driven Infrastructure with Ansible, Test Kitchen, Serverspec and RSpec
Martin Etmajer43.3K visualizações
IT Automation with Ansible por Rayed Alrashed
IT Automation with AnsibleIT Automation with Ansible
IT Automation with Ansible
Rayed Alrashed15.7K visualizações
Ansible roles done right por Dan Vaida
Ansible roles done rightAnsible roles done right
Ansible roles done right
Dan Vaida1.7K visualizações
Deploying your rails application to a clean ubuntu 10 por Maurício Linhares
Deploying your rails application to a clean ubuntu 10Deploying your rails application to a clean ubuntu 10
Deploying your rails application to a clean ubuntu 10
Maurício Linhares3.8K visualizações
Deployment with Fabric por andymccurdy
Deployment with FabricDeployment with Fabric
Deployment with Fabric
andymccurdy977 visualizações
Puppet Camp NYC 2014: Build a Modern Infrastructure in 45 min! por Puppet
Puppet Camp NYC 2014: Build a Modern Infrastructure in 45 min!Puppet Camp NYC 2014: Build a Modern Infrastructure in 45 min!
Puppet Camp NYC 2014: Build a Modern Infrastructure in 45 min!
Puppet2.1K visualizações
Testing for Ops: Going Beyond the Manifest - PuppetConf 2013 por Puppet
Testing for Ops: Going Beyond the Manifest - PuppetConf 2013Testing for Ops: Going Beyond the Manifest - PuppetConf 2013
Testing for Ops: Going Beyond the Manifest - PuppetConf 2013
Puppet6.6K visualizações

Destaque

2日間Fabricを触った俺が
 色々解説してみる por
2日間Fabricを触った俺が
 色々解説してみる2日間Fabricを触った俺が
 色々解説してみる
2日間Fabricを触った俺が
 色々解説してみるairtoxin Ishii
4.4K visualizações41 slides
Fabric (python) por
Fabric (python)Fabric (python)
Fabric (python)Fanani M. Ihsan
3.5K visualizações10 slides
Fabric por
FabricFabric
FabricJoe_noh
737 visualizações11 slides
fabfile.py por
fabfile.pyfabfile.py
fabfile.pyCorey Oordt
1.4K visualizações3 slides
Fabric-让部署变得简单 por
Fabric-让部署变得简单Fabric-让部署变得简单
Fabric-让部署变得简单Eric Lo
2.5K visualizações16 slides
Automation - fabric, django and more por
Automation - fabric, django and moreAutomation - fabric, django and more
Automation - fabric, django and moreIlian Iliev
11.9K visualizações17 slides

Destaque(20)

2日間Fabricを触った俺が
 色々解説してみる por airtoxin Ishii
2日間Fabricを触った俺が
 色々解説してみる2日間Fabricを触った俺が
 色々解説してみる
2日間Fabricを触った俺が
 色々解説してみる
airtoxin Ishii4.4K visualizações
Fabric (python) por Fanani M. Ihsan
Fabric (python)Fabric (python)
Fabric (python)
Fanani M. Ihsan3.5K visualizações
Fabric por Joe_noh
FabricFabric
Fabric
Joe_noh737 visualizações
fabfile.py por Corey Oordt
fabfile.pyfabfile.py
fabfile.py
Corey Oordt1.4K visualizações
Fabric-让部署变得简单 por Eric Lo
Fabric-让部署变得简单Fabric-让部署变得简单
Fabric-让部署变得简单
Eric Lo2.5K visualizações
Automation - fabric, django and more por Ilian Iliev
Automation - fabric, django and moreAutomation - fabric, django and more
Automation - fabric, django and more
Ilian Iliev11.9K visualizações
Fabric - a server management tool from Instagram por Jay Ren
Fabric - a server management tool from InstagramFabric - a server management tool from Instagram
Fabric - a server management tool from Instagram
Jay Ren1.8K visualizações
DevOps with Fabric por Simone Federici
DevOps with FabricDevOps with Fabric
DevOps with Fabric
Simone Federici786 visualizações
Python Deployment with Fabric por andymccurdy
Python Deployment with FabricPython Deployment with Fabric
Python Deployment with Fabric
andymccurdy34K visualizações
Pythonic Deployment with Fabric 0.9 por Corey Oordt
Pythonic Deployment with Fabric 0.9Pythonic Deployment with Fabric 0.9
Pythonic Deployment with Fabric 0.9
Corey Oordt2.7K visualizações
Scaling mysql with python (and Docker). por Roberto Polli
Scaling mysql with python (and Docker).Scaling mysql with python (and Docker).
Scaling mysql with python (and Docker).
Roberto Polli1.3K visualizações
Mercury por sagemg
MercuryMercury
Mercury
sagemg390 visualizações
PEKA freezing point por Nur Hidayah Othman
PEKA freezing pointPEKA freezing point
PEKA freezing point
Nur Hidayah Othman633 visualizações
Fabric Properties por Azmir Latif Beg
Fabric PropertiesFabric Properties
Fabric Properties
Azmir Latif Beg2.8K visualizações
Freezing-thawing processes study with numerical models por Riccardo Rigon
Freezing-thawing processes study with numerical modelsFreezing-thawing processes study with numerical models
Freezing-thawing processes study with numerical models
Riccardo Rigon737 visualizações
Natural fibers por Mohsin Khattak
Natural fibersNatural fibers
Natural fibers
Mohsin Khattak8.3K visualizações
Presebtation - Cotton fibre por Amit Biswas
Presebtation - Cotton fibrePresebtation - Cotton fibre
Presebtation - Cotton fibre
Amit Biswas1.4K visualizações
properties of fiber por Uneeb Rehman
properties of fiberproperties of fiber
properties of fiber
Uneeb Rehman1.3K visualizações
Vegetable fibers por Bademaw Abate
Vegetable fibersVegetable fibers
Vegetable fibers
Bademaw Abate10K visualizações

Similar a Fabric

Continuous Delivery: The Next Frontier por
Continuous Delivery: The Next FrontierContinuous Delivery: The Next Frontier
Continuous Delivery: The Next FrontierCarlos Sanchez
1.6K visualizações57 slides
Devoxx UK 2013: Sandboxing with the Vagrant-Binding API por
Devoxx UK 2013: Sandboxing with the Vagrant-Binding APIDevoxx UK 2013: Sandboxing with the Vagrant-Binding API
Devoxx UK 2013: Sandboxing with the Vagrant-Binding APIHendrik Ebbers
2.5K visualizações27 slides
Getting started with puppet and vagrant (1) por
Getting started with puppet and vagrant (1)Getting started with puppet and vagrant (1)
Getting started with puppet and vagrant (1)Puppet
1.4K visualizações21 slides
Vagrant por
VagrantVagrant
VagrantMichael Peacock
2K visualizações114 slides
Vagrant For DevOps por
Vagrant For DevOpsVagrant For DevOps
Vagrant For DevOpsLalatendu Mohanty
10.5K visualizações18 slides
Omaha (Google Update) server por
Omaha (Google Update) serverOmaha (Google Update) server
Omaha (Google Update) serverDmitry Lyfar
1.4K visualizações27 slides

Similar a Fabric(20)

Continuous Delivery: The Next Frontier por Carlos Sanchez
Continuous Delivery: The Next FrontierContinuous Delivery: The Next Frontier
Continuous Delivery: The Next Frontier
Carlos Sanchez1.6K visualizações
Devoxx UK 2013: Sandboxing with the Vagrant-Binding API por Hendrik Ebbers
Devoxx UK 2013: Sandboxing with the Vagrant-Binding APIDevoxx UK 2013: Sandboxing with the Vagrant-Binding API
Devoxx UK 2013: Sandboxing with the Vagrant-Binding API
Hendrik Ebbers2.5K visualizações
Getting started with puppet and vagrant (1) por Puppet
Getting started with puppet and vagrant (1)Getting started with puppet and vagrant (1)
Getting started with puppet and vagrant (1)
Puppet1.4K visualizações
Vagrant por Michael Peacock
VagrantVagrant
Vagrant
Michael Peacock2K visualizações
Vagrant For DevOps por Lalatendu Mohanty
Vagrant For DevOpsVagrant For DevOps
Vagrant For DevOps
Lalatendu Mohanty10.5K visualizações
Omaha (Google Update) server por Dmitry Lyfar
Omaha (Google Update) serverOmaha (Google Update) server
Omaha (Google Update) server
Dmitry Lyfar1.4K visualizações
Continuous Delivery with Maven, Puppet and Tomcat - ApacheCon NA 2013 por Carlos Sanchez
Continuous Delivery with Maven, Puppet and Tomcat - ApacheCon NA 2013Continuous Delivery with Maven, Puppet and Tomcat - ApacheCon NA 2013
Continuous Delivery with Maven, Puppet and Tomcat - ApacheCon NA 2013
Carlos Sanchez16.9K visualizações
Automation in Cloud por Abhishek Amralkar
Automation in CloudAutomation in Cloud
Automation in Cloud
Abhishek Amralkar282 visualizações
Quick & Easy Dev Environments with Vagrant por Joe Ferguson
Quick & Easy Dev Environments with VagrantQuick & Easy Dev Environments with Vagrant
Quick & Easy Dev Environments with Vagrant
Joe Ferguson1.6K visualizações
Vagrant-Binding JUG Dortmund por Hendrik Ebbers
Vagrant-Binding JUG DortmundVagrant-Binding JUG Dortmund
Vagrant-Binding JUG Dortmund
Hendrik Ebbers1.8K visualizações
Bangpypers april-meetup-2012 por Deepak Garg
Bangpypers april-meetup-2012Bangpypers april-meetup-2012
Bangpypers april-meetup-2012
Deepak Garg528 visualizações
Docker presentasjon java bin por Olve Hansen
Docker presentasjon java binDocker presentasjon java bin
Docker presentasjon java bin
Olve Hansen455 visualizações
Create Development and Production Environments with Vagrant por Brian Hogan
Create Development and Production Environments with VagrantCreate Development and Production Environments with Vagrant
Create Development and Production Environments with Vagrant
Brian Hogan3.5K visualizações
Control your deployments with Capistrano por Ramazan K
Control your deployments with CapistranoControl your deployments with Capistrano
Control your deployments with Capistrano
Ramazan K2.6K visualizações
Virtualization with Vagrant (ua.pycon 2011) por Dmitry Guyvoronsky
Virtualization with Vagrant (ua.pycon 2011)Virtualization with Vagrant (ua.pycon 2011)
Virtualization with Vagrant (ua.pycon 2011)
Dmitry Guyvoronsky1.6K visualizações
Capistrano deploy Magento project in an efficient way por Sylvain Rayé
Capistrano deploy Magento project in an efficient wayCapistrano deploy Magento project in an efficient way
Capistrano deploy Magento project in an efficient way
Sylvain Rayé4.8K visualizações
FreeBSD: Dev to Prod por Sean Chittenden
FreeBSD: Dev to ProdFreeBSD: Dev to Prod
FreeBSD: Dev to Prod
Sean Chittenden1.1K visualizações
About docker in GDG Seoul por Jude Kim
About docker in GDG SeoulAbout docker in GDG Seoul
About docker in GDG Seoul
Jude Kim2.9K visualizações
Minicurso de Vagrant por Leandro Nunes
Minicurso de VagrantMinicurso de Vagrant
Minicurso de Vagrant
Leandro Nunes847 visualizações
Vagrant and chef por Nick Ramirez
Vagrant and chefVagrant and chef
Vagrant and chef
Nick Ramirez547 visualizações

Mais de Calvin Cheng

FOSSASIA 2018 Self-Sovereign Identity with Hyperledger Indy/Sovrin por
FOSSASIA 2018 Self-Sovereign Identity with Hyperledger Indy/SovrinFOSSASIA 2018 Self-Sovereign Identity with Hyperledger Indy/Sovrin
FOSSASIA 2018 Self-Sovereign Identity with Hyperledger Indy/SovrinCalvin Cheng
1.3K visualizações57 slides
Hashgraph as Code por
Hashgraph as CodeHashgraph as Code
Hashgraph as CodeCalvin Cheng
3K visualizações62 slides
Functional Programming for OO Programmers (part 2) por
Functional Programming for OO Programmers (part 2)Functional Programming for OO Programmers (part 2)
Functional Programming for OO Programmers (part 2)Calvin Cheng
2.3K visualizações79 slides
Functional Programming for OO Programmers (part 1) por
Functional Programming for OO Programmers (part 1)Functional Programming for OO Programmers (part 1)
Functional Programming for OO Programmers (part 1)Calvin Cheng
2K visualizações29 slides
iOS Beginners Lesson 4 por
iOS Beginners Lesson 4iOS Beginners Lesson 4
iOS Beginners Lesson 4Calvin Cheng
708 visualizações30 slides
iOS Beginners Lesson 3 por
iOS Beginners Lesson 3iOS Beginners Lesson 3
iOS Beginners Lesson 3Calvin Cheng
626 visualizações30 slides

Mais de Calvin Cheng(15)

FOSSASIA 2018 Self-Sovereign Identity with Hyperledger Indy/Sovrin por Calvin Cheng
FOSSASIA 2018 Self-Sovereign Identity with Hyperledger Indy/SovrinFOSSASIA 2018 Self-Sovereign Identity with Hyperledger Indy/Sovrin
FOSSASIA 2018 Self-Sovereign Identity with Hyperledger Indy/Sovrin
Calvin Cheng1.3K visualizações
Hashgraph as Code por Calvin Cheng
Hashgraph as CodeHashgraph as Code
Hashgraph as Code
Calvin Cheng3K visualizações
Functional Programming for OO Programmers (part 2) por Calvin Cheng
Functional Programming for OO Programmers (part 2)Functional Programming for OO Programmers (part 2)
Functional Programming for OO Programmers (part 2)
Calvin Cheng2.3K visualizações
Functional Programming for OO Programmers (part 1) por Calvin Cheng
Functional Programming for OO Programmers (part 1)Functional Programming for OO Programmers (part 1)
Functional Programming for OO Programmers (part 1)
Calvin Cheng2K visualizações
iOS Beginners Lesson 4 por Calvin Cheng
iOS Beginners Lesson 4iOS Beginners Lesson 4
iOS Beginners Lesson 4
Calvin Cheng708 visualizações
iOS Beginners Lesson 3 por Calvin Cheng
iOS Beginners Lesson 3iOS Beginners Lesson 3
iOS Beginners Lesson 3
Calvin Cheng626 visualizações
iOS Beginners Lesson 2 por Calvin Cheng
iOS Beginners Lesson 2iOS Beginners Lesson 2
iOS Beginners Lesson 2
Calvin Cheng913 visualizações
iOS Beginners Lesson 1 por Calvin Cheng
iOS Beginners Lesson 1iOS Beginners Lesson 1
iOS Beginners Lesson 1
Calvin Cheng725 visualizações
So, you want to build a Bluetooth Low Energy device? por Calvin Cheng
So, you want to build a Bluetooth Low Energy device?So, you want to build a Bluetooth Low Energy device?
So, you want to build a Bluetooth Low Energy device?
Calvin Cheng941 visualizações
Learning iOS and hunting NSZombies in 3 weeks por Calvin Cheng
Learning iOS and hunting NSZombies in 3 weeksLearning iOS and hunting NSZombies in 3 weeks
Learning iOS and hunting NSZombies in 3 weeks
Calvin Cheng1.2K visualizações
Ladypy 01 por Calvin Cheng
Ladypy 01Ladypy 01
Ladypy 01
Calvin Cheng489 visualizações
zhng your vim por Calvin Cheng
zhng your vimzhng your vim
zhng your vim
Calvin Cheng939 visualizações
Django101 geodjango por Calvin Cheng
Django101 geodjangoDjango101 geodjango
Django101 geodjango
Calvin Cheng2K visualizações
Saving Gaia with GeoDjango por Calvin Cheng
Saving Gaia with GeoDjangoSaving Gaia with GeoDjango
Saving Gaia with GeoDjango
Calvin Cheng1.3K visualizações
Agile Apps with App Engine por Calvin Cheng
Agile Apps with App EngineAgile Apps with App Engine
Agile Apps with App Engine
Calvin Cheng537 visualizações

Fabric

  • 1. fabric Control your linux nodes with python
  • 2. What is it? • Command-line tool for deployment automation and system administration as a ssh wrapper • Executes to local or remote ✓ local: not a ssh connection. uses python subprocess module. ✓ run or sudo: when executing against remote, it can execute as the current user or as current user with sudo rights.
  • 3. What is it? • Simply specify python functions in fabfile.py • Install it ✓ pip install fabric ✓ Once installed, fab command is available in your system path. ✓ Start writing your python function in fabfile.py
  • 4. automation • Daily tasks • Server setup • System update and maintenance • New project creation • Deployment processes
  • 5. get started • Download and install virtualbox https://www.virtualbox.org • Download and install vagrant http://www.vagrantup.com cd ~/VirtualBoxVMs mkdir debsqueeze64 cd debsqueeze64 vagrant init debsqueeze64 http://www.emken.biz/vagrant- boxes/debsqueeze64.box vagrant up
  • 6. get started • This downloads and creates an image of debian 64-bit on your computer. • We will use fabric on our local machine to interact with this debian virtual machine • See http://www.vagrantbox.es to download other prepared boxes.
  • 7. PythonVirtualenv • Create an isolated python project environment Mac OS X: package management with port or brew sudo port -v install virtualenv_select py27-virtualenv sudo port -v install py27-virtualenvwrapper sudo port select --set virtualenv virtualenv27 (optional)
  • 8. PythonVirtualenv • Edit your ~/.bash_profile export WORKON_HOME=$HOME/.virtualenvs export PROJECT_HOME=$HOME/work source `which virtualenvwrapper.sh` • $HOME -- this refers to your user home or “~” (i.e., on my computer,“~” means “/Users/calvin” directory) (optional)
  • 9. PythonVirtualenv • Create our python virtualenv for our fabric tutorial mkvirtualenv --distribute --no-site-packages learnfabric cd $PROJECT_HOME git clone git://github.com/calvinchengx/learnfabric.git cd learnfabric pip install -r requirements.txt (optional)
  • 10. learn fabric • Once we have our debian virtual machine and optionally our python virtualenv ready, we begin git clone git://github.com/calvinchengx/learnfabric.git cd learnfabric pip install -r requirements.txt cp -rf ~/VirtualBoxVMs/debsqueeze64/.vagrant . • This installs fabric and installs python-vagrant (a wrapper around vagrant commands so we can programmatically control our vagrant instance)
  • 11. learn fabric • Once we have our debian virtual machine and optionally our python virtualenv ready, we begin git clone git://github.com/calvinchengx/learnfabric.git cd learnfabric pip install -r requirements.txt cp -rf ~/VirtualBoxVMs/debsqueeze64/.vagrant . • This installs fabric and installs python-vagrant (a wrapper around vagrant commands so we can programmatically control our vagrant instance)
  • 12. fabric function fab mytask [vagrant@127.0.0.1:2222] Executing task 'mytask' [vagrant@127.0.0.1:2222] run: echo $USER [vagrant@127.0.0.1:2222] out: vagrant[vagrant@127.0 .0.1:2222] out: Done. Disconnecting from vagrant@127.0.0.1:2222... done.
  • 13. the code import vagrantfrom fabric.api import env, task, runv = vagrant.Vagrant()v.up()env.hosts = [v.user_hostname_port()]env.key_filename = v.keyfile()env.disable_known_hosts = True # useful when vagrant box ip changes. # our first fabric function@taskdef mytask(): run('echo $USER') @taskdef mytask(): run('echo $USER')
  • 14. fabric core • local() runs a command locally, via python subprocess • run() runs a command remotely, via ssh • sudo() runs a command remotely as sudo, via ssh • put() copies a file from local to remote, via ssh • get() copies a file from remote to local, via ssh
  • 15. fabric AUth • All authentication is ssh-based • SSH keys help us avoid typing in passwords - place fabric ssh user (env.user)’s public key in remote node’s authorized_keys • Avoid directly using root user • Give your fabric ssh user sudo rights instead (env.user)
  • 16. fabric config • from fabric.api import env • fabric environment is simply a dictionary containing host information, roles, user (env.user); and • any other custom information you would like to include. • $HOME/.fabricrc allows custom configuration, e.g. user = ssh_user_name where “ssh_user_name” is any value you so desire to pass in to env.user when a fab function runs.
  • 17. Fabric ROLEDEFS # code from fabric.api import env env.roledefs = { ‘web’: [‘100.0.0.1’, ‘100.0.0.2’], ‘db’: [‘100.0.0.3’], ‘media’: [‘100.0.0.4’] } # command line fab -R web mytask
  • 18. Fabric host(S) # code from fabric.api import env env.roledefs = { ‘web’: [‘100.0.0.1’, ‘100.0.0.2’], ‘db’: [‘100.0.0.3’], ‘media’: [‘100.0.0.4’] } # command line fab -H 100.0.0.1,100.0.0.2 mytask
  • 19. map roles to tasks # code from fabric.api import env from fabric.decorators import roles env.roledefs = { ‘web’: [‘100.0.0.1’, ‘100.0.0.2’], ‘db’: [‘100.0.0.3’], ‘media’: [‘100.0.0.4’] } @roles(‘web’) def mytask(): run(‘uptime’) # command line fab mytask # already mapped to -R web
  • 20. handling failures • Do not abort upon failure; just give a warning # code from fabric.context_managers import settings def mytask(): with settings(warn_only=True): run(‘rm /var/www/proj/releases/current’) run(‘ln -s /var/www/proj/releases/deployed /var/www/proj/releases/current’)
  • 21. Lazy sysadmin • A generic command # code @roles(‘all’) def invoke(command): “”” Invoke an arbitrary command “”” sudo(command) # command line fab invoke:“aptitude update” fab invoke:”aptitude upgrade”
  • 22. parallel execution • A generic command # code @roles(‘all’) def invoke(command): “”” Invoke an arbitrary command “”” sudo(command) # command line fab -P invoke:“aptitude update” fab -P invoke:”aptitude upgrade”
  • 24. Example deploy @task(default=True)@set_target_envdef deploy(email=False): """ `fab -R all deploy` or fab -H mysite.com deploy`. Execute a deployment to the given groups of hosts or host """ if not chk_req(): return if git_branch_check() or test_host_check(): manage_release('Deployment start') git_archive_and_upload_tar() pip_requirements() collectstatic(deploy=True) symlink_current() webserver() migrate_db() # post-deployment tasks manage_release('Deployment end') _releases_cleanup() email_on_success(trigger=email) # command line fab -R all deploy
  • 25. quick refErence • fab -H host1.com task • fab -H host1.com task:arg1,arg2 • fab -P -H localhost, host1.com, host2.com task • fab -R web task # roles defined by env.roledefs • fab -R web task:arg1,arg2 • fab -P -R web task • fab -l • more options explained via fab --help
  • 26. devops • Bridging the gap between code implementation by developers and deployment to staging and/or live servers by sysadmin • Automation leads to continuous integration (e.g. Jenkins CI Server) and continuous delivery • Support heterogeneous development environment in combination with vagrant (developers test against vagrant, which has same distro as staging and production hosts)
  • 27. referenceS • My reusable fabric functions https://github.com/fabric-colors/fabric-colors https://fabric-colors.readthedocs.org/en/latest/index.html • Code that accompanies this set of slides https://github.com/calvinchengx/learnfabric • Fabric code and documentation https://github.com/fabric/fabric http://fabric.readthedocs.org • Multiple vagrant boxes for testing your server config http://awaseroot.wordpress.com/2012/05/06/script-for-
  • 28. q&A • Get in touch @calvinchengx • http://calvinx.com