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

What would Jason Bourne do?

Gerhard Lazu
February 08, 2013

What would Jason Bourne do?

Use your shell environment more efficiently, just like Jason Bourne would use a newspaper in a fight.

Gerhard Lazu

February 08, 2013

More Decks by Gerhard Lazu

Other Decks in Programming


  1. What would Jason Bourne do ? @gerhardlazu Lead Engineer howareyou.com

    Friday, 8 March 13 I am Gerhard Lazu, the Lead Engineer for howareyou.com I will be speaking today about Bourne. Jason Bourne is a man of action, He does not have time for hubot or for READMEs, he just... sudo
  2. 1 #!/usr/bin/env bash Friday, 8 March 13 Jason Bourne only

    uses Bourne-Again SHell - he’s so full of himself! He doesn’t like Ruby much, and he hates JavaScript Even his programming is violent!
  3. bash Bourne-Again SHell Friday, 8 March 13 Bash was created

    by Brian Fox in the late 80s, it is the default shell in Linux and Mac OS 10 Bash greatly improves on BourneShell, and picks the best parts from C-Shell and KornShell The name, bash, is a pun on Stephen Bourne, the author of BourneShell
  4. bash the language Friday, 8 March 13 bash is so

    much more than just a command-line interpreter It has loops, conditions, functions & variables It can do background jobs & run sub-shells It even does meta-programming! Yes, it has eval
  5. env a key-value store Friday, 8 March 13 env is

    a key-value store that everybody knows about - but ignores Except Jason Bourne - it’s his key-value store of choice You can set, modify & unset new or pre-defined variables
  6. env not a Heroku add-on Friday, 8 March 13 Just

    to get things clear, Heroku did not invent env, it is not a Heroku add-on They do encourage you to make proper use of env, as does Jason Bourne.
  7. JB walks into your office... 1 [[  -­‐e  $HOME/.env  ]]

     &&  source  $HOME/.env Friday, 8 March 13 So, Jason Bourne walks into your office. What is the first thing that he does? He fires up iTerm and inserts this line in your shell’s profile It reads: “if file exists, source it”
  8. ~/.env 1 load_env() { 2 local _github_user="$(git config --get github.user)"

    3 local _github_token="$(git config --get github.token) 4 5 if [[ -n $_github_user ]] && [[ -n $_github_token ]] 6 then 7 local _github_url="https://raw.github.com/..." 8 source <(curl -fsSL $_github_url) 9 else ... Friday, 8 March 13 The .env file that gets sourced defines and runs a function which in turn sources a remote file from a git repository Lines 2 and 3 read your github user and token, the one set in your global git config Line 5 guards against missing values Lines 7 and 8 load the remote file in your env
  9. ./bin/setup 1 check_if_installed "brew" "http://mxcl.github.com/home 2 3 brew_dependencies=(mysql imagemagick phantomjs

    redis) 4 5 for _brew_dependency in ${brew_dependencies[@]} 6 do 7 brew install "$_brew_dependency" 8 done Friday, 8 March 13 setup is a bash script Line 1 checks if brew is installed, otherwise stops execution and opens page with instructions Line 3 is a list of your app’s system dependencies Lines 5 to 8 install your app’s system dependencies via brew
  10. ./bin/setup 8 ... 9 10 check_if_installed "bundle" "http://gembundler.com/" 11 12

    bundle install --local 13 14 echo "Godspeed Bourne!" Friday, 8 March 13 You would also check for bundler, then install all vendored gems The --local flag will not pull any gems from remote bundle package is your friend, as is vendor/cache Now that your app is setup, how do you start it? You type boot
  11. ./bin/boot 1 if [[ -z $@ ]] 2 then 3

    bundle exec foreman start -f=Procfile.development 4 else 5 $@ 6 fi Friday, 8 March 13 By default, your app and all its components get started via Foreman (Lines 1 to 3) If you pass in any arguments they will be executed as a standalone command. For example boot rails server would only start the rails server
  12. ./bin/build 1 set -e 2 3 setup_dependencies 4 run_tests 5

    tag_for_deploy 6 if_assets_changed && compile_assets 7 _deliver Friday, 8 March 13 This is a bourne build script Line 1 stops this script’s execution if any of the commands exit with a non-zero status Lines 3 to 7 are bash functions, the actual code is not relevant, the intentions however are. Notice line 7, I will get back to it.
  13. ./bin/build $ HOSTS=app.vagrant ./bin/build Friday, 8 March 13 If you

    are running the build script locally, it is likely that you want to deploy to your local VM All you have to do is overwrite the $HOSTS variable By default, successful builds will be pushed to your production instances by Jenkins
  14. PUBLIC Friday, 8 March 13 Jason Bourne uses deliver, a

    pure bash deployment tool deliver has no dependencies It only cares about having enough info in the shell environment to do its job If you want to find out more about deliver, come to my next LRUG talk in March
  15. quick wins Friday, 8 March 13 github’s hubot is impressive,

    as is Netflix’s Asgard Distributing your apps as deb or yum packages is also cool But what can you do right now - like in 5 minutes or less?
  16. env ./script or ./bin bash Friday, 8 March 13 Well,

    you can use env more - the key-value store that is everywhere You can make use of the local script or bin man-drawers Do you remember Jason Bourne’s newspaper-fight? You ARE Jason Bourne, bash IS that newspaper!
  17. What would Jason Bourne do ? @gerhardlazu Lead Engineer howareyou.com

    Friday, 8 March 13 Next time when you are finding yourself typing shell code in Ruby, ask yourself: What would Jason Bourne do?