Continuous Integration
With Fabric
http://docs.fabfile.org

Varun Batra @ Deft Infotech Pvt. Ltd.
I am Lazy!
I need one command to deploy codes.
>fab deploy
That’s what I was talking about 

Varun Batra @ Deft Infotech Pvt. Ltd.
What if something goes wrong?
>fab deploy
This command should handle that as well and report me.

Varun Batra @ Deft Infotech Pvt. Ltd.
Common Principle of CI
• Code Repository (I use GIT)
• Fast & Auto Build
• Self Testing
• ‘Test Environment’ is not ‘Build Environment’
• Auto Deployment: merge to master/deploy branch (GIT), this should
auto deploy it
• Self Sustainable System, e.g. if db is destroyed, it should automatically
recreated by latest backup

Varun Batra @ Deft Infotech Pvt. Ltd.
My Deployment Principle
• Based on my experience
• Based on guys’s ‘better than me’ experience
• Let me know if you can improve because you can

Varun Batra @ Deft Infotech Pvt. Ltd.
Varun Batra @ Deft Infotech Pvt. Ltd.
Why Fabric?
I learned it in half day
It can do what I need
Few seconds installation

Varun Batra @ Deft Infotech Pvt. Ltd.
GIT
>git branch something_cool
>git checkout something_cool
>git commit -a -m ‘what I did?’
>git checkout master
>git merge something_cool
>git push
git checkout -b something_cool combines first two commands
Varun Batra @ Deft Infotech Pvt. Ltd.
Fabric: library + command-line tool
Fabric is a Python (2.5 or higher) library and command-line tool for
streamlining the use of SSH for application deployment or systems
administration tasks.
I copied from their website, I am lazy

Varun Batra @ Deft Infotech Pvt. Ltd.
Fabric Features
• Runs local and remote commands
• Runs normal or sudo commands
• Upload & Download files
• Prompt for input
• Simultaneous deploying in multiple server
• Output in different colors like red(text,bold=False)

Varun Batra @ Deft Infotech Pvt. Ltd.
Fabric combines

fabfile.py

fab

Varun Batra @ Deft Infotech Pvt. Ltd.
There is a catch
each run or sudo call has its own distinct shell session.
run('cd foo' && 'mkdir bar')
is not
run('cd foo')
run('mkdir bar')
is
with('cd foo')
run('mkdir bar')

Varun Batra @ Deft Infotech Pvt. Ltd.
How discussed deployment process should
be?

Varun Batra @ Deft Infotech Pvt. Ltd.
Processing Output
Output of ‘run’ command is stored in ‘url’ variable

Varun Batra @ Deft Infotech Pvt. Ltd.
Deploy with options?
• Passing parameters?
• fab deploy:env=production
• fab deploy:production (in order of params)
• !Passing True is string i.e. ‘True’ not True

• Deploy to many hosts?
• env.hosts = ['user@example1.com', ' user@example2.com ']
• fab –H user@example1.com, user@example2.com deploy

• Show fewer output?
• with hide('output','running'), settings(warn_only=True):

Varun Batra @ Deft Infotech Pvt. Ltd.
@serial VS @parallel

Varun Batra @ Deft Infotech Pvt. Ltd.
More decorators?
• @hosts
• @roles
• @runs_once !caution with @parallel
• @task
• @with_settings
• @serial
• @parallel

Varun Batra @ Deft Infotech Pvt. Ltd.
Context Managers
Remember ‘with’?

with('cd ~/git_repo/'):
run('git pull')
#cd ~/git_repo/ && git pull
with hide('running', 'stdout', 'stderr'):
do_something()
Varun Batra @ Deft Infotech Pvt. Ltd.
Context Manager cont’d..
• cd
• with
• lcd
• path
• prefix
• settings
• show

Varun Batra @ Deft Infotech Pvt. Ltd.
Contributions
• File/directory management
• comment/uncomment, sed, append, contains, exists, upload_template, first

• Project Tools
• rsync_project, upload_project

• Django integration

Varun Batra @ Deft Infotech Pvt. Ltd.
Lets look at fabfile.py

Varun Batra @ Deft Infotech Pvt. Ltd.
But wait..
What about autodeployment?
cron VS service

Varun Batra @ Deft Infotech Pvt. Ltd.
fab prompt_for_any_questions

fab stay_in_touch

Varun Batra
IT Consultant
Deft Infotech Pvt. Ltd.
skype:varunb.deftinfotech
http://varunbatra.com
CodeVarun@gmail.com
https://www.linkedin.com/in/varunbatra/
https://www.facebook.com/VarunBatraIT
https://twitter.com/batravarun

Varun Batra @ Deft Infotech Pvt. Ltd.

Continuous Integration with Fabric

  • 1.
  • 2.
    I am Lazy! Ineed one command to deploy codes. >fab deploy That’s what I was talking about  Varun Batra @ Deft Infotech Pvt. Ltd.
  • 3.
    What if somethinggoes wrong? >fab deploy This command should handle that as well and report me. Varun Batra @ Deft Infotech Pvt. Ltd.
  • 4.
    Common Principle ofCI • Code Repository (I use GIT) • Fast & Auto Build • Self Testing • ‘Test Environment’ is not ‘Build Environment’ • Auto Deployment: merge to master/deploy branch (GIT), this should auto deploy it • Self Sustainable System, e.g. if db is destroyed, it should automatically recreated by latest backup Varun Batra @ Deft Infotech Pvt. Ltd.
  • 5.
    My Deployment Principle •Based on my experience • Based on guys’s ‘better than me’ experience • Let me know if you can improve because you can Varun Batra @ Deft Infotech Pvt. Ltd.
  • 6.
    Varun Batra @Deft Infotech Pvt. Ltd.
  • 7.
    Why Fabric? I learnedit in half day It can do what I need Few seconds installation Varun Batra @ Deft Infotech Pvt. Ltd.
  • 8.
    GIT >git branch something_cool >gitcheckout something_cool >git commit -a -m ‘what I did?’ >git checkout master >git merge something_cool >git push git checkout -b something_cool combines first two commands Varun Batra @ Deft Infotech Pvt. Ltd.
  • 9.
    Fabric: library +command-line tool Fabric is a Python (2.5 or higher) library and command-line tool for streamlining the use of SSH for application deployment or systems administration tasks. I copied from their website, I am lazy Varun Batra @ Deft Infotech Pvt. Ltd.
  • 10.
    Fabric Features • Runslocal and remote commands • Runs normal or sudo commands • Upload & Download files • Prompt for input • Simultaneous deploying in multiple server • Output in different colors like red(text,bold=False) Varun Batra @ Deft Infotech Pvt. Ltd.
  • 11.
  • 12.
    There is acatch each run or sudo call has its own distinct shell session. run('cd foo' && 'mkdir bar') is not run('cd foo') run('mkdir bar') is with('cd foo') run('mkdir bar') Varun Batra @ Deft Infotech Pvt. Ltd.
  • 13.
    How discussed deploymentprocess should be? Varun Batra @ Deft Infotech Pvt. Ltd.
  • 14.
    Processing Output Output of‘run’ command is stored in ‘url’ variable Varun Batra @ Deft Infotech Pvt. Ltd.
  • 15.
    Deploy with options? •Passing parameters? • fab deploy:env=production • fab deploy:production (in order of params) • !Passing True is string i.e. ‘True’ not True • Deploy to many hosts? • env.hosts = ['user@example1.com', ' user@example2.com '] • fab –H user@example1.com, user@example2.com deploy • Show fewer output? • with hide('output','running'), settings(warn_only=True): Varun Batra @ Deft Infotech Pvt. Ltd.
  • 16.
    @serial VS @parallel VarunBatra @ Deft Infotech Pvt. Ltd.
  • 17.
    More decorators? • @hosts •@roles • @runs_once !caution with @parallel • @task • @with_settings • @serial • @parallel Varun Batra @ Deft Infotech Pvt. Ltd.
  • 18.
    Context Managers Remember ‘with’? with('cd~/git_repo/'): run('git pull') #cd ~/git_repo/ && git pull with hide('running', 'stdout', 'stderr'): do_something() Varun Batra @ Deft Infotech Pvt. Ltd.
  • 19.
    Context Manager cont’d.. •cd • with • lcd • path • prefix • settings • show Varun Batra @ Deft Infotech Pvt. Ltd.
  • 20.
    Contributions • File/directory management •comment/uncomment, sed, append, contains, exists, upload_template, first • Project Tools • rsync_project, upload_project • Django integration Varun Batra @ Deft Infotech Pvt. Ltd.
  • 21.
    Lets look atfabfile.py Varun Batra @ Deft Infotech Pvt. Ltd.
  • 22.
    But wait.. What aboutautodeployment? cron VS service Varun Batra @ Deft Infotech Pvt. Ltd.
  • 23.
    fab prompt_for_any_questions fab stay_in_touch VarunBatra IT Consultant Deft Infotech Pvt. Ltd. skype:varunb.deftinfotech http://varunbatra.com CodeVarun@gmail.com https://www.linkedin.com/in/varunbatra/ https://www.facebook.com/VarunBatraIT https://twitter.com/batravarun Varun Batra @ Deft Infotech Pvt. Ltd.