Friday, December 10, 2010

How to run Ruby On Rails on Google AppEngine

There is a tutorial https://gist.github.com/671792 by John Woodell,
however it was costumed running in linux environment,
and there is some slightly changes they didnt update their tutorial script,
the script will not work out of box,
So in here i am making a post, especially benefit users who is using Windows, since i am doing this in Windows 7 environment.

PS : i assume u have already had a google app engine account, if u dont, do some bing or google and get one

Specially thanks for the help from Andrew Myers.

1) Install Java Development Kit  6 (JDK6)
make sure you have similar stuff show up in your windows command prompt

>java -version
java version "1.6.0_21"
Java(TM) SE Runtime Environment (build 1.6.0_21-b07)
Java HotSpot(TM) Client VM (build 17.0-b17, mixed mode, sharing)
>javac -version
javac 1.6.0_21

2)  Install AppEngine Java SDK http://code.google.com/appengine/downloads.html
Download the  SDK, and make sure you put the "bin" directory into your system "Path"
















































3) Install Ruby 1.8.7 , not JRuby
Download it http://www.ruby-lang.org/en/downloads/, and install it,
make sure you associate *.rb to be run by ruby  (for windows install, there is the check box)

again open a new command prompt, you should have something like this

>ruby -v
ruby 1.8.7 (2010-08-16 patchlevel 302) [i386-mingw32]
>gem -v
1.3.7

4) Install plugins for ruby
Make sure you open a command prompt as administrator

and type in the following one by one

gem install google-appengine
gem install rails -v "2.3.10"
gem install rails_dm_datastore
gem install activerecord-nulldb-adapter

each one will take a while, so be patient..

5) Make a directory to contain you ROR application
mkdir railsv1
cd railsv1
Or you can use GUI to do it.

6) Copy the code below, save it to a file, for example rails2310_appengine.rb

#!/usr/bin/ruby
#
# Copyright:: Copyright 2009 Google Inc.
# Original Author:: John Woodell (mailto:woodie@google.com)
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#      http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

require 'fileutils'
require 'open-uri'

def composite(source, fragment, index = nil, trim = nil)
  File.open(source, 'r+') do |f|
    lines = f.readlines
    lines = lines[0..trim] unless trim.nil?
    f.pos = 0
    File.open(fragment) do |z|
      section = z.readlines
      if index and index.size < lines.size
        f.print lines[0,index] + section + lines[index..-1]
      else
        f.print lines + section
      end
    end
    f.truncate(f.pos)
  end
  FileUtils.rm fragment
end

def download_file(path, url)
  open(url) do |r|
    FileUtils.mkpath(File.dirname(path))
    open(path,"w"){|f| f.write(r.read) }
  end
end
SET_CMD = RUBY_PLATFORM.include?('mswin32') ? 'set' : 'export'
MORE_GEMS = 'rails_appengine/active_support_vendored'
FILE_BASE = 'http://appengine-jruby.googlecode.com/hg/demos/rails2/'
MOD_FILES = %w{ app/controllers/rails/info_controller.rb public/favicon.ico
                config.ru config/boot_rb config/environment_rb
                config/initializers/gae_init_patch.rb config/database.yml
                script/console.sh script/publish.sh script/server.sh }
# Install Rails 2.3.10
FileUtils.touch 'config.ru'
gemsrc = ARGV[0].eql?('tiny_ds') ? 'Gemfile_td' : 'Gemfile'
download_file("Gemfile", "#{FILE_BASE}#{gemsrc}")
download_file("gems_2310", "#{FILE_BASE}gems_2310")
composite('Gemfile', 'gems_2310', nil, -2)
FileUtils.mkdir_p 'WEB-INF'
download_file("WEB-INF/app.yaml", "#{FILE_BASE}WEB-INF/app.yaml")
system 'appcfg.rb bundle --update .'
# Remove dups and generate Rails app
# Generate rails, and skip APIs to escape the shell
system "rails _2.3.10_ ."
# Fetch configuration files
FileUtils.mkdir_p 'app/controllers/rails'
MOD_FILES.each { |path| download_file(path, "#{FILE_BASE}#{path}") }
if ARGV[0].eql? 'tiny_ds'
  download_file("config/environment_rb", "#{FILE_BASE}config/environment_td")
end
# Merge configs into boot.rb
composite('config/boot.rb', 'config/boot_rb', 108)
# Merge configs into environment.rb
composite('config/environment.rb', 'config/environment_rb', 30)
# install the nulldb adapter
system 'ruby script/plugin install http://svn.avdi.org/nulldb/trunk/'
puts "##"
puts "## Now type 'dev_appserver.rb .'"
puts "##"


and run it, e.g  ruby rails2310_appengine.rb, then you should see something like this:



railsv1>ruby rails2310_appengine.rb
      exists
      create  app/controllers
      create  app/helpers
      create  app/models
      create  app/views/layouts
      create  config/environments
      create  config/initializers
      create  config/locales
      create  db
      create  doc
      create  lib
      create  lib/tasks
      create  log
      create  public/images
      create  public/javascripts
      create  public/stylesheets
      create  script/performance
      create  test/fixtures
      create  test/functional
      create  test/integration
      create  test/performance
      create  test/unit
      create  vendor
      create  vendor/plugins
      create  tmp/sessions
      create  tmp/sockets
      create  tmp/cache
      create  tmp/pids
      create  Rakefile
      create  README
      create  app/controllers/application_controller.rb
      create  app/helpers/application_helper.rb
      create  config/database.yml
      create  config/routes.rb
      create  config/locales/en.yml
      create  db/seeds.rb
      create  config/initializers/backtrace_silencers.rb
      create  config/initializers/inflections.rb
      create  config/initializers/mime_types.rb
      create  config/initializers/new_rails_defaults.rb
      create  config/initializers/session_store.rb
      create  config/initializers/cookie_verification_secret.rb
      create  config/environment.rb
      create  config/boot.rb
      create  config/environments/production.rb
      create  config/environments/development.rb
      create  config/environments/test.rb
      create  script/about
      create  script/console
      create  script/dbconsole
      create  script/destroy
      create  script/generate
      create  script/runner
      create  script/server
      create  script/plugin
      create  script/performance/benchmarker
      create  script/performance/profiler
      create  test/test_helper.rb
      create  test/performance/browsing_test.rb
      create  public/404.html
      create  public/422.html
      create  public/500.html
      create  public/index.html
      create  public/favicon.ico
      create  public/robots.txt
      create  public/images/rails.png
      create  public/javascripts/prototype.js
      create  public/javascripts/effects.js
      create  public/javascripts/dragdrop.js
      create  public/javascripts/controls.js
      create  public/javascripts/application.js
      create  doc/README_FOR_APP
      create  log/server.log
      create  log/production.log
      create  log/development.log
      create  log/test.log
+ ./LICENSE
+ ./README
+ ./Rakefile
+ ./init.rb
+ ./lib/active_record/connection_adapters/nulldb_adapter.rb
+ ./lib/nulldb_rspec.rb
+ ./spec/nulldb_spec.rb
+ ./tasks/database.rake
##
## Now type 'dev_appserver.rb .'
##

7) Great, we are almost there, type 'dev_appserver.rb .'  to your command prompt

railsv1>dev_appserver.rb .
=> Bundling gems
Calculating dependencies...
Updating source: http://gems.rubyforge.org
d:/Ruby187/lib/ruby/gems/1.8/gems/bundler08-0.8.5/lib/bundler08/resolver.rb:115:Warning: Gem::Dependency#version_require
ments is deprecated and will be removed on or after August 2010.  Use #requirement
Caching: actionmailer-2.3.10.gem
Caching: actionpack-2.3.10.gem
Caching: activerecord-2.3.10.gem
Caching: activeresource-2.3.10.gem
Caching: activesupport-2.3.10.gem
Downloading addressable-2.2.2.gem
Caching: appengine-apis-0.0.22.gem
Caching: appengine-rack-0.0.12.gem
Downloading bouncy-castle-java-1.5.0145.2.gem
Downloading dm-appengine-0.1.3.gem
Downloading dm-ar-finders-1.0.2.gem
Downloading dm-core-1.0.2.gem
Downloading dm-timestamps-1.0.2.gem
Downloading dm-validations-1.0.2.gem
Downloading extlib-0.9.15.gem
Caching: jruby-jars-1.5.6.gem
Downloading jruby-openssl-0.7.2.gem
Caching: jruby-rack-1.0.4.gem
Downloading lexidecimal-0.0.1.gem
Caching: rack-1.1.0.gem
Caching: rails-2.3.10.gem
Downloading rails_appengine-0.0.6.gem
Downloading rails_dm_datastore-0.2.16.gem
Caching: rake-0.8.7.gem
Installing activesupport (2.3.10)
Installing extlib (0.9.15)
Installing actionmailer (2.3.10)
Installing lexidecimal (0.0.1)
Installing rails_appengine (0.0.6)
Installing bouncy-castle-java (1.5.0145.2)
Installing dm-timestamps (1.0.2)
Installing rack (1.1.0)
Installing actionpack (2.3.10)
Installing jruby-jars (1.5.6)
Installing jruby-openssl (0.7.2)
Installing dm-validations (1.0.2)
Installing rake (0.8.7)
Installing addressable (2.2.2)
Installing dm-core (1.0.2)
Installing dm-appengine (0.1.3)
Installing jruby-rack (1.0.4)
Installing appengine-rack (0.0.12)
Installing appengine-apis (0.0.22)
Installing activerecord (2.3.10)
Installing dm-ar-finders (1.0.2)
Installing rails_dm_datastore (0.2.16)
Installing activeresource (2.3.10)
Installing rails (2.3.10)
Done.
=> Packaging gems
=> Installing appengine-api-1.0-sdk-1.4.0.jar
=> Installing appengine-api-labs-1.4.0.jar
=> Installing appengine-rack.jar
=> Installing bcmail-jdk15-145.jar
=> Installing bcprov-jdk15-145.jar
=> Installing jruby-core-1.5.6.jar
=> Installing jruby-stdlib-1.5.6.jar
=> Installing jopenssl.jar
=> Installing jruby-rack-1.0.4.jar
=> Booting DevAppServer
=> Press Ctrl-C to shutdown server
10/12/2010 6:44:02 AM com.google.apphosting.utils.jetty.JettyLogger info
INFO: Logging to JettyLogger(null) via com.google.apphosting.utils.jetty.JettyLogger
10/12/2010 6:44:07 AM com.google.apphosting.utils.config.AppEngineWebXmlReader readAppEngineWebXml
INFO: Successfully processed E:\xiaomin\projects\appengine-ror\railsv1\WEB-INF/appengine-web.xml
10/12/2010 6:44:07 AM com.google.apphosting.utils.config.AbstractConfigXmlReader readConfigXml
INFO: Successfully processed E:\xiaomin\projects\appengine-ror\railsv1\WEB-INF/web.xml
10/12/2010 6:44:07 AM com.google.apphosting.utils.jetty.JettyLogger info
INFO: jetty-6.1.x
10/12/2010 6:44:39 AM com.google.apphosting.utils.jetty.JettyLogger info
INFO: Started SelectChannelConnector@127.0.0.1:8080
10/12/2010 6:44:39 AM com.google.appengine.tools.development.DevAppServerImpl start
INFO: The server is running at http://localhost:8080/

8) Finally, go to your browser, and enjoy yourself for your first app-engine ROR,
http://localhost:8080

9) We are not done yet, now it is running on your local machine,
we are just one step a way to have a app up in the cloud.

Let go to your Google AppEngine account















Copy the name of your application, in my case it is "fancyshrimpy" ... what a great name...

10) Edit configure file, and put your application name attach to your app which u had just created

>railsv1\WEB-INF\app.yaml
at the very first line, put your application name there  (they call it application-id)
application: fancyshrimpy

11) finally type "appcfg.rb update ."

you will have something like this:

railsv1>appcfg.rb update .
=> Running AppCfg
Reading application configuration data...
10/12/2010 5:57:00 PM com.google.apphosting.utils.config.AppEngineWebXmlReader readAppEngineWebXml
INFO: Successfully processed .\WEB-INF/appengine-web.xml
10/12/2010 5:57:00 PM com.google.apphosting.utils.config.AbstractConfigXmlReader readConfigXml
INFO: Successfully processed .\WEB-INF/web.xml
Beginning server interaction for fancyshrimpy...
0% Creating staging directory
5% Scanning for jsp files.
20% Scanning files on local disk.
25% Initiating update.
Email: xxxxxxxx@gmail.com
Password for xxxxxxxx@gmail.com:
28% Cloning 7 static files.
31% Cloning 57 application files.
40% Uploading 6 files.
52% Uploaded 1 files.
61% Uploaded 2 files.
68% Uploaded 3 files.
73% Uploaded 4 files.
77% Uploaded 5 files.
80% Uploaded 6 files.
82% Initializing precompilation...
90% Deploying new version.
95% Will check again in 1 seconds.
98% Will check again in 2 seconds.
99% Will check again in 4 seconds.
99% Will check again in 8 seconds.
99% Closing update: new version is ready to start serving.
99% Uploading index definitions.

Update completed successfully.
Success.
Cleaning up temporary files...

Now the very end, go to your app and play with it...
http://fancyshrimpy.appspot.com/env.html

End..

2 comments:

  1. Java and to some degree .Net are the main choices because they have been consistently pegged as the “safe” choice to go with for mid-level project managers in the corporate world. No one was ever fired for choosing Java or Microsoft.

    However, there are many large distributed applications these days that run primarily with technologies like Python, PHP, et al. Even companies like Google and Yahoo are heavily invested in these technologies. Java may be the main choice for enterprise development now, but it’s days are numbered as the only stalwart option to go with.

    Let’s face it, many of these so called “enterprise applications” could easily have been written much faster and with less overhead using technologies like Python, PHP, et al.







    xhtml training

    ReplyDelete
  2. The specific version numbers were important for me.

    I had a bundle error on running "ruby rails2310_appengine.rb", so I downgraded gem:
    gem update --system 1.3.7

    I had a timezone error on launching my server so I added a flag like this:
    dev_appserver.rb --jvm_flag=-Dappengine.user.timezone=UTC .

    ReplyDelete