Share
Embed
Fabric: Deploy with Python
Learn how to use Fabric and Fabtools to automate your development and deployment with Python
rick446
March 13, 2014
Transcript
17 slides
-
What is Fabric? • Library and command-line tool • Streamlines
SSH • Use for deployment and sysadmin tasks -
Set up a playground (virtualenv) (master %) rick446@Elrond: ~/src/fabric-demo! $
virtualenv env-fabric! New python executable in env-fabric/bin/python2.7! Also creating executable in env-fabric/bin/python! Installing Setuptools................................................................................ .......................................................................................... ....................................................done.! Installing Pip....................................................................................... .......................................................................................... .......................................................................................... ..........................................................done.! (master %) rick446@Elrond: ~/src/fabric-demo! $ source env-fabric/bin/activate! (env-fabric)(master %) rick446@Elrond: ~/src/fabric-demo! $ pip install fabric! ... lots of stuff ...! Successfully installed fabric paramiko pycrypto ecdsa! Cleaning up...! (env-fabric)(master %) rick446@Elrond: ~/src/fabric-demo! $ -
Hello, Fabric # fab-hello.py! ! def hello():! print 'Hello, Fabric!'
(env-fabric)(master %) rick446@Elrond: ~/src/fabric-demo! $ fab -f fab-hello.py -l! Available commands:! ! hello! (env-fabric)(master %) rick446@Elrond: ~/src/fabric-demo! $ fab -f fab-hello.py hello! Hello, Fabric!! ! Done. -
Command-line Fabric # fab-arguments.py! ! def hello():! print 'Hello, Fabric!'!
! def greet(name):! print 'Hello, {}!'.format(name)! ! def args_n_kwargs(*args, **kwargs):! print 'args: {}'.format(args)! print 'kwargs: {}'.format(kwargs) -
What about • ssh? • deployment? • sysadmin? ! •
For that, we need a bigger playground…. -
What is Vagrant • Bunch of Ruby scripts that help
you manage virtual machines (by default, the free VirtualBox) • [See January’s excellent talk by Daniel Rocco for more details.] • We’ll use it as a deployment playground -
Build the Playground • Prerequisite: Install Virtualbox and Vagrant •
https://www.virtualbox.org/ • http://www.vagrantup.com/ (master) rick446@Elrond: ~/src/fabric-demo! $ vagrant box add saucy64 https://cloud-images.ubuntu.com/vagrant/saucy/current/saucy-server-cloudimg- amd64-vagrant-disk1.box! Downloading or copying the box...! Extracting box...te: 3973k/s, Estimated time remaining: --:--:--)! Successfully added box 'saucy64' with provider ‘virtualbox'!! ! (master) rick446@Elrond: ~/src/fabric-demo! $ vagrant init saucy64! A `Vagrantfile` has been placed in this directory. You are now! ready to `vagrant up` your first virtual environment! Please read! the comments in the Vagrantfile as well as documentation on! `vagrantup.com` for more information on using Vagrant.! ! (env-fabric)(master %) rick446@Elrond: ~/src/fabric-demo! $ vagrant plugin install vagrant-vbguest! ! (master) rick446@Elrond: ~/src/fabric-demo! $ vagrant up! …! [default] Mounting shared folders...! [default] -- /vagrant -
Something simple (env-fabric)(master %) rick446@Elrond: ~/src/fabric-demo! $ vagrant ssh-config >
vagrant-ssh-config! (env-fabric)(master %) rick446@Elrond: ~/src/fabric-demo! $ fab --ssh-config vagrant-ssh-config --set use_ssh_config=True -H default host_type! [default] Executing task 'host_type'! [default] run: uname -s! [default] out: Linux! [default] out:! ! ! Done.! Disconnecting from [email protected]:2222... done. # fabfile.py! ! from fabric import api! ! def host_type():! api.run('uname -s') -
What is Fabtools? • Useful functions to help you write
your Fabric files. • Make it easier to manage system users, packages, databases, etc. • Low-level actions, as well as a higher level interface called fabtools.require. • fabtools.require allows a more declarative style, similar to Chef or Puppet. • Lots of cool stuff, but let’s start with fabtools.vagrant… -
What else can you do? • Target multiple hosts (host
lists or roles) “webservers” • Parallel mode • Compound tasks • Set host list • Execute sub-tasks in parallel -
Image credits • [1] - http://upload.wikimedia.org/wikipedia/ commons/7/7d/ Barack_Obama_with_artistic_gymnastic_McKayl a_Maroney_2.jpg •
[2] - http://pixabay.com/p-91536/?no_redirect