• Share
  • Email
  • Embed
  • Like
  • Save
  • Private Content
Fabric Fast & Furious edition
 

Fabric Fast & Furious edition

on

  • 482 views

A lighting talk I gave at python Madrid user group on 2014/03/27 about using python fabric beyond the tutorial http://docs.fabfile.org/en/1.8/#tutorial and relates a journey of tips that I have use to ...

A lighting talk I gave at python Madrid user group on 2014/03/27 about using python fabric beyond the tutorial http://docs.fabfile.org/en/1.8/#tutorial and relates a journey of tips that I have use to improve my fabfiles. All is from the documentation.
Download the source file for best viewing (animated gifs ;-) )
About the references and images are from their respective owners

Statistics

Views

Total Views
482
Views on SlideShare
442
Embed Views
40

Actions

Likes
1
Downloads
3
Comments
0

1 Embed 40

https://twitter.com 40

Accessibility

Categories

Upload Details

Uploaded via as OpenOffice

Usage Rights

CC Attribution License

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment

    Fabric Fast & Furious edition Fabric Fast & Furious edition Presentation Transcript

    • FabricFabric F&F editionF&F edition Alejandro E. Brito Monedero @ae_bm
    • Fabric is a Python (2.5-2.7) library and command-line tool for streamlining the use of SSH for application deployment or systems administration tasks.
    • Fabric is a Python (2.5-2.7) library and command-line tool for streamlining the use of SSH for application deployment or systems administration tasks.
    • Fabric is a Python (2.5-2.7) library and command-line tool for streamlining the use of SSH for application deployment or systems administration tasks. It provides a basic suite of operations for executing local or remote shell commands (normally or via sudo) and uploading/downloading files, etc.
    • Start your engines!!!
    • fabfile.py from fabric.api import run def host_type(): run('uname -s')
    • $ fab -H linuxbox host_type [linuxbox] run: uname -s [linuxbox] out: Linux Done. Disconnecting from linuxbox... done.
    • Reaching operating temperature
    • 1
    • Poor's man mcollective / Func $ fab -H web01,web02 -- "cd /var/www && pwd" [web01] Executing task '<remainder>' [web01] run: cd /var/www && pwd [web01] out: /var/www [web01] out: [web02] Executing task '<remainder>' [web02] run: cd /var/www && pwd [web02] out: /var/www [web02] out: Done.
    • 2
    • New tasks aka Task decorator fabfile.py from fabric.api import run, task def ninja(): run('assassinate enemy') @task def salute(): run('hello')
    • $ fab -l Available commands: salute New tasks aka Task decorator
    • 3
    • Documentation proyect01.py from fabric.api import task @task def deploy_stuff(version): '''Deploy the specified version of stuff Keyword arguments: version -- version to deploy Example: $ fab deploy:release_X.Y.Z '''
    • Documentation $ fab -l Available commands: deploy_stuff Deploy the specified version of stuff
    • Documentation $ fab -d deploy_stuff Displaying detailed information for task 'deploy_stuff': Deploy the specified version of stuff Keyword arguments: version -- version to deploy Example: $ fab deploy:release_X.Y.Z Arguments: version
    • 4
    • Execute decorator proyect01.py from fabric.api import cd, execute, sudo, task def update_code(version, code_path, user='root'): '''Update the code using mercurial''' with cd(code_path): sudo('hg pull', user=user) sudo('hg update -r {version} -v'.format(version=version), user=user)
    • Execute decorator proyect01.py @task def deploy_stuff(version): '''Deploy the specified version of stuff ... ''' execute(update_code, hosts=['web01', 'web02'], version=version, user='root', code_path='path')
    • This technique works because tasks that themselves have no host list (this includes the global host list settings) only run one time. If used inside a “regular” task that is going to run on multiple hosts, calls to execute will also run multiple times, resulting in multiplicative numbers of subtask calls – be careful! If you would like your execute calls to only be called once, you may use the runs_once decorator.
    • Execute decorator – Warning! fabfile.py from fabric.api import cd, env, execute, sudo, task env.use_ssh_config = True @task @hosts('web01', 'web02') def alpha(): execute(beta) @hosts('web03', 'web04') def beta(): execute(gamma) def gamma():
    • Execute decorator – Warning! $ fab alpha [web01] Executing task 'alpha' [web03] Executing task 'beta' [web03] run: hostname [web03] out: web03 [web03] out: [web04] Executing task 'beta' [web04] run: hostname [web04] out: web04 [web04] out:
    • Execute decorator – Warning! [web02] Executing task 'alpha' [web03] Executing task 'beta' [web03] run: hostname [web03] out: web03 [web03] out: [web04] Executing task 'beta' [web04] run: hostname [web04] out: web04 [web04] out: Done.
    • FUUUUUU...!!!
    • 5
    • Multiple fabfiles folder ├── fabfile.py ├── proyect01.py ├── proyect02.py └── mercurial.py
    • Multiple fabfiles fabfile.py from fabric.api import env import proyect01 import proyect02 env.use_ssh_config = True env.roledefs = { 'all_web': ['web01', 'web02', 'web03', 'web04',], 'internets-cloud': ['amzn01', 'amzn02', 'goog01', 'goog02',], }
    • Multiple fabfiles proyect01.py from fabric.api import execute, task from mercurial import update_code @task def deploy_stuff(version): '''Deploy the specified version of stuff ... ''' execute(update_code, hosts=['web01', 'web02'], version=version, user='root', code_path='path')
    • Multiple fabfiles proyect02.py from fabric.api import roles, run, task @roles('internets-cloud') @task def take_over_the_world(): '''Accomplish Pinky and the Brain's plan''' run('shutdown -h now')
    • Multiple fabfiles mercurial.py from fabric.api import cd, sudo def update_code(version, code_path, user='root'): '''Update the code using mercurial''' with cd(code_path): sudo('hg pull', user=user) sudo('hg update -r {version} -v'.format(version=version), user=user)
    • Multiple fabfiles $ fab -l Available commands: proyect01.deploy_stuff Deploy the specified version of stuff proyect02.take_over_the_world Accomplish Pinky and the Brain's plan $ fab proyect01.deploy_stuff:release_X.Y.Z ...
    • 6
    • Using fabric API reporter.py #!/usr/bin/env python ... from fabric.api import env, run from fabric.network import disconnect_all ... def check_log(jobid): env.use_ssh_config = True env.host_string = 'hostname' try: run('LANG="C" grep -C 10 {jobid} log'.format(jobid=jobid)) finally: disconnect_all()
    • Questions?
    • Images sources http://upload.wikimedia.org/wikipedia/commons/4/4d/Subaru-WRX-Concept-Car.jpg http://collegecandy.files.wordpress.com/2013/03/screaming.gif?w=615&h=347 http://ultime.pl/uploads/000013/2013/02/engine-start.gif http://www.sabelotodo.org/automovil/imagenes/sistelectrico/relojtemp.png http://bestgifsdotnet.files.wordpress.com/2013/12/rally-accident-car-falling-to-creek1.gif?w=650 http://img.gawkerassets.com/img/18qf904ykzyjigif/ku-xlarge.gif http://images.roadtrafficsigns.com/img/lg/K/Drive-Safely-Sign-K-6732.gif http://img4.wikia.nocookie.net/__cb20071016121318/half-life/en/images/d/dd/Achtung.svg http://www.cleargas.co/default/assets/Image/Racing%20Logo%205.jpg http://mm.motor21.com/Espa %C3%B1ol/Deportes/Motor/Mundial_de_Rallies_WRC/Noticias/77155/kimi-raikkonen.jpg