Upload
sau-sheong-chang
View
3.473
Download
1
Tags:
Embed Size (px)
DESCRIPTION
How to deploy Ruby Sinatra applications on Heroku and Google App Engine for Java
Citation preview
How to deploy Ruby Sinatra applications to the Heroku and
Google AppEngine cloud platforms
Ruby and the Cloud
Chang Sau SheongGeekcamp SG22 Aug 2009
Beginner/Intermediate
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
Clone TinyURL in 40 lines of Ruby code
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
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
http://github.com/sausheong/snip
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!
QUESTIONS? (BEFORE CONTINUING)
Deploying Sinatra apps to Heroku
• 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
NginxNginx
VarnishVarnishCustom made from Erlang
Custom made from Erlang
Your code runs here
Your code runs here
PostgresPostgres
MemcachedMemcached
Architecture
DebianDebian
MRIMRI
Rails, Sinatra etc
Rails, Sinatra etc
ThinThin
Inside each dyno
6 steps to deploy
1. Install Heroku gem
1. Create a Rackup file (config.ru)
6 steps to deploy
3. Create empty git repository, add all files and commit the files
6 steps to deploy
4. Create the Heroku application using the Heroku gem
6 steps to deploy
5. Push code to git repository
6 steps to deploy
6. Go into the Heroku console to create the database
Done!
QUESTIONS? (BEFORE CONTINUING)
Deploying Sinatra apps to Google AppEngine with JRuby
• 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
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
• 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
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
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
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
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
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
Steps to deploy
Remember this? Your app name before
You might want to do this for debugging
Steps to deploy
6. Create a Rackup configuration file config.ru
Preparation complete!
Steps to deploy
7. Generate the war file and directory using Warble
Steps to deploy
• Output:
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!
Steps to deploy
9. Deploy to App Engine!
Steps to deploy
QUESTIONS?
http://tinyclone.saush.com
[email protected]://blog.saush.comhttp://github.com/sausheong