Ruby And The Cloud


Published on

How to deploy Ruby Sinatra applications on Heroku and Google App Engine for Java

Published in: Technology, Education
  • Be the first to comment

No Downloads
Total views
On SlideShare
From Embeds
Number of Embeds
Embeds 0
No embeds

No notes for slide
  • Pre-requisites – you should know Ruby. This is an intermediate level presentation, if you don’t know Ruby it will be hard for you to follow
  • Also talk a bit about myself
  • as of writing, TinyURL is still the king of the hill in the Twitter micro-market of URL shorteners. In June 2009, lexicographers of Oxford University Press sampled 1.5 million tweets and came up with the 500 most frequently used words on Twitter. With a little bit of further analysis, I extracted the numbers that are related to the various URL shorteners mentioned and came up with the approximate market share of the various URL shorteners in Twitter.
  • Do demo here.
  • Ruby And The Cloud

    1. 1. How to deploy Ruby Sinatra applications to the Heroku and Google AppEngine cloud platforms Ruby and the Cloud Chang Sau Sheong Geekcamp SG 22 Aug 2009
    2. 2. Beginner/Intermediate
    3. 3. What I am going to show here <ul><li>Use Sinatra to write a TinyURL clone (Snip) </li></ul><ul><li>Deploy Snip to Heroku </li></ul><ul><li>Deploy Snip to Google AppEngine </li></ul><ul><li>Disclaimer – No affiliation with Heroku or Google, all opinions are my own </li></ul>
    4. 4. Clone TinyURL in 40 lines of Ruby code
    5. 5. URL Shorteners <ul><li>First successful one was TinyURL, started in 2002 </li></ul><ul><li>Generate a short URL for any URL, going to short URL will redirect to the original </li></ul><ul><li>Usage exploded with popularity of Twitter </li></ul><ul><li>Biggest market share today – TinyURL, used to be default for Twitter, second is </li></ul><ul><li>Twitter switched to early May 2009 </li></ul>
    6. 8. Sinatra <ul><li>Rack-based minimalist Ruby web application framework </li></ul>require ‘rubygems’ require ‘sinatra’ get ‘/hi’ do “ Hello Geekcamp!” end <ul><li>Designed and developed by Blake Mizerany, financial support by Heroku </li></ul>
    7. 10.
    8. 11. Some notes <ul><li>Base 36 conversion to reduce number of characters used </li></ul><ul><ul><li>Ruby provides conversion with to_i and to_s </li></ul></ul><ul><li>DataMapper </li></ul><ul><ul><li>ActiveRecord, Sequel also supported </li></ul></ul><ul><ul><li>Use DATABASE_URL environment variable </li></ul></ul><ul><li>Embedded view template with use_in_file_templates! </li></ul>
    10. 13. Deploying Sinatra apps to Heroku
    11. 14. <ul><li>Ruby cloud platform </li></ul><ul><li>Hosted on Amazon’s Elastic Compute Cloud (EC2) </li></ul><ul><li>Fully managed, developer only needs to deploy, all else taken care of </li></ul><ul><li>Full set of command line tools to manage deployment </li></ul><ul><li>Deploy to your own domain or * </li></ul>
    12. 15. Architecture Nginx Varnish Custom made from Erlang Your code runs here Postgres Memcached
    13. 16. Inside each dyno Debian MRI Rails, Sinatra etc Thin
    14. 17. 6 steps to deploy <ul><li>Install Heroku gem </li></ul><ul><li>Create a Rackup file ( </li></ul>
    15. 18. 6 steps to deploy <ul><li>Create empty git repository, add all files and commit the files </li></ul>
    16. 19. 6 steps to deploy <ul><li>Create the Heroku application using the Heroku gem </li></ul>
    17. 20. 6 steps to deploy <ul><li>Push code to git repository </li></ul>
    18. 21. 6 steps to deploy <ul><li>Go into the Heroku console to create the database </li></ul>Done!
    20. 23. Deploying Sinatra apps to Google AppEngine with JRuby
    21. 24. <ul><li>Lets you run your web applications on Google's infrastructure </li></ul><ul><li>Fully managed, only deploy and runs </li></ul><ul><li>Can use own domain name or * </li></ul><ul><li>First released April 2008, first language used Python </li></ul><ul><li>April 2009, Java support released </li></ul>Google App Engine
    22. 25. App Engine for Java <ul><li>Java 6 virtual machine (JVM), App Engine SDK supports Java 5 and later </li></ul><ul><li>Servlet classes, JSPs, static files and data files, deployment descriptor (web.xml), configuration files, in a standard WAR directory structure (not WAR file) </li></ul><ul><li>Persistence using DataStore with JDO and JPA </li></ul><ul><li>Caching using App Engine’s memcache with JCache </li></ul><ul><li>Use Mail service with JavaMail </li></ul><ul><li>Image service, Google Accounts for authentication </li></ul>
    23. 26. <ul><li>JRuby is the Java implementation of the Ruby programming language </li></ul><ul><li>Tightly integrated with Java, able to call Java classes, Ruby 1.8.6 compatible </li></ul><ul><li>Is what we’re going to use to deploy Snip to App Engine </li></ul>
    24. 27. First, some code differences No base 36 conversion Using Ola Bini’s Bumble DataStore adapter, Using DataStore key instead of id
    25. 28. What we need to deploy <ul><li>JRuby 1.3 RC1 (at least) </li></ul><ul><ul><li>Need to split into 2 files because of App Engine’s 10MB file size limit </li></ul></ul><ul><li>Google App Engine for Java SDK </li></ul><ul><li>Warbler – the JRuby WAR packager </li></ul><ul><ul><li>Includes JRuby-Rack </li></ul></ul>
    26. 29. Steps to deploy <ul><li>Install necessary gems (remember to use JRuby and not C Ruby) </li></ul><ul><li>Create a config directory and create the Warbler configuration </li></ul>
    27. 30. Steps to deploy <ul><li>Replace it with this file </li></ul>This is important – your app name We can’t use the default full JRuby library it is too large for GAE/J – max 10MB
    28. 31. Steps to deploy <ul><li>Create a lib folder and add the following library files: </li></ul><ul><ul><li>App Engine Java library - appengine-api-1.0-sdk-1.2.0.jar </li></ul></ul><ul><ul><li>Split jruby-complete-1.3.0RC1.jar to: </li></ul></ul><ul><ul><ul><li>jruby-core-1.3.0RC1.jar and </li></ul></ul></ul><ul><ul><ul><li>jruby-stdlib-1.3.0RC1.jar </li></ul></ul></ul><ul><ul><li>(JRuby-Rack will be added by Warbler later) </li></ul></ul><ul><li>Create an appengine-web.xml deployment configuration file </li></ul>
    29. 32. Steps to deploy Remember this? Your app name before You might want to do this for debugging
    30. 33. Steps to deploy <ul><li>Create a Rackup configuration file </li></ul><ul><ul><li>Preparation complete! </li></ul></ul>
    31. 34. Steps to deploy <ul><li>Generate the war file and directory using Warble </li></ul>
    32. 35. Steps to deploy <ul><li>Output: </li></ul>
    33. 36. Steps to deploy <ul><li>Go into Sinatra code at tmp/war/WEB-INF/gems/gems/sinatra- and remove use_in_file_templates! </li></ul>
    34. 37. Steps to deploy <ul><li>Deploy to App Engine! </li></ul>
    35. 38. Steps to deploy
    36. 39. QUESTIONS?
    37. 40.
    38. 42. [email_address]