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