Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Fabric: Deploy with Python

rick446
March 13, 2014

Fabric: Deploy with Python

Learn how to use Fabric and Fabtools to automate your development and deployment with Python

rick446

March 13, 2014
Tweet

More Decks by rick446

Other Decks in Technology

Transcript

  1. What is Fabric? • Library and command-line tool • Streamlines

    SSH • Use for deployment and sysadmin tasks
  2. 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! $
  3. 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.
  4. 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)
  5. [1]

  6. What about • ssh? • deployment? • sysadmin? ! •

    For that, we need a bigger playground….
  7. 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
  8. 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
  9. 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')
  10. 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…
  11. 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