Fabric Fast & Furious edition

download Fabric Fast & Furious edition

If you can't read please download the document

description

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

Transcript of Fabric Fast & Furious edition

  • 1. FabricFabric F&F editionF&F edition Alejandro E. Brito Monedero @ae_bm

2. 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. 3. 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. 4. 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. 5. Start your engines!!! 6. fabfile.py from fabric.api import run def host_type(): run('uname -s') 7. $ fab -H linuxbox host_type [linuxbox] run: uname -s [linuxbox] out: Linux Done. Disconnecting from linuxbox... done. 8. Reaching operating temperature 9. 1 10. Poor's man mcollective / Func $ fab -H web01,web02 -- "cd /var/www && pwd" [web01] Executing task '' [web01] run: cd /var/www && pwd [web01] out: /var/www [web01] out: [web02] Executing task '' [web02] run: cd /var/www && pwd [web02] out: /var/www [web02] out: Done. 11. 2 12. New tasks aka Task decorator fabfile.py from fabric.api import run, task def ninja(): run('assassinate enemy') @task def salute(): run('hello') 13. $ fab -l Available commands: salute New tasks aka Task decorator 14. 3 15. 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 ''' 16. Documentation $ fab -l Available commands: deploy_stuff Deploy the specified version of stuff 17. 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 18. 4 19. 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) 20. 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') 21. 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. 22. 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(): 23. 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: 24. 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. 25. FUUUUUU...!!! 26. 5 27. Multiple fabfiles folder fabfile.py proyect01.py proyect02.py mercurial.py 28. 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',], } 29. 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') 30. 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') 31. 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) 32. 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 ... 33. 6 34. 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() 35. Questions? 36. 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