Fabric

24,735 views

Published on

0 Comments
4 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
24,735
On SlideShare
0
From Embeds
0
Number of Embeds
3
Actions
Shares
0
Downloads
35
Comments
0
Likes
4
Embeds 0
No embeds

No notes for slide

Fabric

  1. 1. fabricControl your linux nodes with python
  2. 2. What is it?• Command-line tool for deployment automation and systemadministration as a ssh wrapper• Executes to local or remote✓ local: not a ssh connection. uses python subprocessmodule.✓ run or sudo: when executing against remote, it canexecute as the current user or as current user withsudo rights.
  3. 3. What is it?• Simply specify python functions in fabfile.py• Install it✓ pip install fabric✓ Once installed, fab command is available in your systempath.✓ Start writing your python function in fabfile.py
  4. 4. automation• Daily tasks• Server setup• System update and maintenance• New project creation• Deployment processes
  5. 5. get started• Download and install virtualbox https://www.virtualbox.org• Download and install vagrant http://www.vagrantup.comcd ~/VirtualBoxVMsmkdir debsqueeze64cd debsqueeze64vagrant init debsqueeze64 http://www.emken.biz/vagrant-boxes/debsqueeze64.boxvagrant up
  6. 6. get started• This downloads and creates an image of debian 64-bit onyour computer.• We will use fabric on our local machine to interact withthis debian virtual machine• See http://www.vagrantbox.es to download other preparedboxes.
  7. 7. PythonVirtualenv• Create an isolated python project environmentMac OS X: package management with port or brewsudo port -v install virtualenv_select py27-virtualenvsudo port -v install py27-virtualenvwrappersudo port select --set virtualenv virtualenv27(optional)
  8. 8. PythonVirtualenv• Edit your ~/.bash_profileexport WORKON_HOME=$HOME/.virtualenvsexport PROJECT_HOME=$HOME/worksource `which virtualenvwrapper.sh`• $HOME -- this refers to your user home or “~”(i.e., on my computer,“~” means “/Users/calvin” directory)(optional)
  9. 9. PythonVirtualenv• Create our python virtualenv for our fabric tutorialmkvirtualenv --distribute --no-site-packages learnfabriccd $PROJECT_HOMEgit clone git://github.com/calvinchengx/learnfabric.gitcd learnfabricpip install -r requirements.txt(optional)
  10. 10. learn fabric• Once we have our debian virtual machine and optionallyour python virtualenv ready, we begingit clone git://github.com/calvinchengx/learnfabric.gitcd learnfabricpip install -r requirements.txtcp -rf ~/VirtualBoxVMs/debsqueeze64/.vagrant .• This installs fabric and installs python-vagrant (a wrapperaround vagrant commands so we can programmaticallycontrol our vagrant instance)
  11. 11. learn fabric• Once we have our debian virtual machine and optionallyour python virtualenv ready, we begingit clone git://github.com/calvinchengx/learnfabric.gitcd learnfabricpip install -r requirements.txtcp -rf ~/VirtualBoxVMs/debsqueeze64/.vagrant .• This installs fabric and installs python-vagrant (a wrapperaround vagrant commands so we can programmaticallycontrol our vagrant instance)
  12. 12. fabric functionfab 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. 13. the codeimport 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 ipchanges.# our first fabric function@taskdef mytask(): run(echo $USER)@taskdef mytask(): run(echo $USER)
  14. 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. 15. fabric AUth• All authentication is ssh-based• SSH keys help us avoid typing in passwords - place fabricssh user (env.user)’s public key in remote node’sauthorized_keys• Avoid directly using root user• Give your fabric ssh user sudo rights instead (env.user)
  16. 16. fabric config• from fabric.api import env• fabric environment is simply a dictionary containing hostinformation, roles, user (env.user); and• any other custom information you would like to include.• $HOME/.fabricrc allows custom configuration, e.g.user = ssh_user_namewhere “ssh_user_name” is any value you so desire to passin to env.user when a fab function runs.
  17. 17. Fabric ROLEDEFS# codefrom fabric.api import envenv.roledefs = {‘web’: [‘100.0.0.1’, ‘100.0.0.2’],‘db’: [‘100.0.0.3’],‘media’: [‘100.0.0.4’]}# command linefab -R web mytask
  18. 18. Fabric host(S)# codefrom fabric.api import envenv.roledefs = {‘web’: [‘100.0.0.1’, ‘100.0.0.2’],‘db’: [‘100.0.0.3’],‘media’: [‘100.0.0.4’]}# command linefab -H 100.0.0.1,100.0.0.2 mytask
  19. 19. map roles to tasks# codefrom fabric.api import envfrom fabric.decorators import rolesenv.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 linefab mytask # already mapped to -R web
  20. 20. handling failures• Do not abort upon failure; just give a warning# codefrom fabric.context_managers import settingsdef 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. 21. Lazy sysadmin• A generic command# code@roles(‘all’)def invoke(command):“””Invoke an arbitrary command“””sudo(command)# command linefab invoke:“aptitude update”fab invoke:”aptitude upgrade”
  22. 22. parallel execution• A generic command# code@roles(‘all’)def invoke(command):“””Invoke an arbitrary command“””sudo(command)# command linefab -P invoke:“aptitude update”fab -P invoke:”aptitude upgrade”
  23. 23. python web app
  24. 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 linefab -R all deploy
  25. 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. 26. devops• Bridging the gap between code implementation bydevelopers and deployment to staging and/or live serversby sysadmin• Automation leads to continuous integration (e.g. Jenkins CIServer) and continuous delivery• Support heterogeneous development environment incombination with vagrant (developers test against vagrant,which has same distro as staging and production hosts)
  27. 27. referenceS• My reusable fabric functionshttps://github.com/fabric-colors/fabric-colorshttps://fabric-colors.readthedocs.org/en/latest/index.html• Code that accompanies this set of slideshttps://github.com/calvinchengx/learnfabric• Fabric code and documentationhttps://github.com/fabric/fabrichttp://fabric.readthedocs.org• Multiple vagrant boxes for testing your server confighttp://awaseroot.wordpress.com/2012/05/06/script-for-
  28. 28. q&A• Get in touch @calvinchengx• http://calvinx.com

×