Download - Ruby And The Cloud

Transcript
Page 1: Ruby And The Cloud

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

Beginner/Intermediate

Page 3: Ruby And The Cloud

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

Clone TinyURL in 40 lines of Ruby code

Page 5: Ruby And The Cloud

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
Page 7: Ruby And The Cloud
Page 8: Ruby And The Cloud

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
Page 10: Ruby And The Cloud

http://github.com/sausheong/snip

Page 11: Ruby And The Cloud

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

QUESTIONS? (BEFORE CONTINUING)

Page 13: Ruby And The Cloud

Deploying Sinatra apps to Heroku

Page 14: Ruby And The Cloud

• 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

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

DebianDebian

MRIMRI

Rails, Sinatra etc

Rails, Sinatra etc

ThinThin

Inside each dyno

Page 17: Ruby And The Cloud

6 steps to deploy

1. Install Heroku gem

1. Create a Rackup file (config.ru)

Page 18: Ruby And The Cloud

6 steps to deploy

3. Create empty git repository, add all files and commit the files

Page 19: Ruby And The Cloud

6 steps to deploy

4. Create the Heroku application using the Heroku gem

Page 20: Ruby And The Cloud

6 steps to deploy

5. Push code to git repository

Page 21: Ruby And The Cloud

6 steps to deploy

6. Go into the Heroku console to create the database

Done!

Page 22: Ruby And The Cloud

QUESTIONS? (BEFORE CONTINUING)

Page 23: Ruby And The Cloud

Deploying Sinatra apps to Google AppEngine with JRuby

Page 24: Ruby And The Cloud

• 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

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

• 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

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

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

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

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

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

Steps to deploy

Remember this? Your app name before

You might want to do this for debugging

Page 33: Ruby And The Cloud

Steps to deploy

6. Create a Rackup configuration file config.ru

Preparation complete!

Page 34: Ruby And The Cloud

Steps to deploy

7. Generate the war file and directory using Warble

Page 35: Ruby And The Cloud

Steps to deploy

• Output:

Page 36: Ruby And The Cloud

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

Steps to deploy

9. Deploy to App Engine!

Page 38: Ruby And The Cloud

Steps to deploy

Page 39: Ruby And The Cloud

QUESTIONS?

Page 40: Ruby And The Cloud

http://tinyclone.saush.com

Page 41: Ruby And The Cloud
Page 42: Ruby And The Cloud

[email protected]://blog.saush.comhttp://github.com/sausheong


Top Related