You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@deltacloud.apache.org by Ben Browning <bb...@redhat.com> on 2010/08/31 22:06:42 UTC

[PATCH 0/2] Deltacloud Core JRuby Compatibility

The following two patches are the steps I had to take to get
deltacloud-core running under JRuby.

Similar to deltacloud-client, the output of rake package will
now produce a -java variant of the gem as well (patch 1/2)

You'll notice that we don't pull in eventmachine, thin, or rerun
gems under JRuby. Because there is no thin gem for JRuby we use
webrick for the deltacloudd command (patch 2/2).

Ben Browning (2):
  Build java and ruby versions of core gem
  Update deltacloudd script to work under jruby.

 server/Rakefile                |   12 ++++-
 server/bin/deltacloudd         |  102 ++++++++++++++++++++++++++--------------
 server/deltacloud-core.gemspec |   14 ++++--
 3 files changed, 86 insertions(+), 42 deletions(-)

-- 
1.7.2.1


Re: [PATCH 0/2] Deltacloud Core JRuby Compatibility

Posted by Benjamin Browning <bb...@redhat.com>.
David,

On Aug 31, 2010, at 6:27 PM, David Lutterkort wrote:
> 
> ACK. Is Webrick the recommended web server under jruby ? I assume under
> torquebox you do something completely different to tie into Tomcat.


Webrick really is only suitable for development. For production deployment, you'd want to use something like TorqueBox, GlassFish, or Tomcat and jruby-rack / warbler. 

For TorqueBox, all you need to do is install the deltacloud-core gem (once the jruby patches are pushed upstream) and create a deployment descriptor in $TORQUEBOX_HOME/jboss/server/default/deploy/deltacloud-rack.yml.

Example below and at http://gist.github.com/559901

---
application:
  RACK_ROOT: $TORQUEBOX_HOME/jruby/lib/ruby/gems/1.8/gems/bbrowning-deltacloud-core-0.0.4-java
  RACK_ENV: production
web:
  context: /deltacloud
environment:
  API_DRIVER: ec2

This sets up deltacloud-core at http://localhost:8080/deltacloud running the ec2 driver.

Ben



Re: [PATCH 0/2] Deltacloud Core JRuby Compatibility

Posted by David Lutterkort <lu...@redhat.com>.
On Tue, 2010-08-31 at 16:06 -0400, Ben Browning wrote:
> You'll notice that we don't pull in eventmachine, thin, or rerun
> gems under JRuby. Because there is no thin gem for JRuby we use
> webrick for the deltacloudd command (patch 2/2).

ACK. Is Webrick the recommended web server under jruby ? I assume under
torquebox you do something completely different to tie into Tomcat.

David



[PATCH 2/2] Update deltacloudd script to work under jruby.

Posted by Ben Browning <bb...@redhat.com>.
A slight rearranging of the script was done to make the
non-jruby-compatible and jruby-compatible code all in one place.
The functionality under non-jruby is unchanged. Under jruby,
webrick is booted instead of thin because thin does not have a
jruby-compatible gem.
---
 server/bin/deltacloudd |  102 +++++++++++++++++++++++++++++++----------------
 1 files changed, 67 insertions(+), 35 deletions(-)

diff --git a/server/bin/deltacloudd b/server/bin/deltacloudd
index 3c91d3e..9d91e1a 100755
--- a/server/bin/deltacloudd
+++ b/server/bin/deltacloudd
@@ -2,7 +2,6 @@
 
 require 'rubygems'
 require 'optparse'
-require 'thin'
 
 options = {
   :env => 'development'
@@ -44,45 +43,78 @@ end
 ENV["API_HOST"] = "localhost" unless ENV["API_HOST"]
 ENV["API_PORT"] = "3001" unless ENV["API_PORT"]
 
+puts "Starting Deltacloud API :: #{ENV["API_DRIVER"]} :: http://#{ENV["API_HOST"]}:#{ENV["API_PORT"]}/api"
+puts
+
 dirname="#{File.dirname(__FILE__)}/.."
+platform = RUBY_PLATFORM[/java/] || 'ruby'
 
-argv_opts = ARGV.clone
-argv_opts << ['start'] unless Thin::Runner.commands.include?(options[0])
-argv_opts << ['--address', ENV["API_HOST"] ]
-argv_opts << ['--port', ENV["API_PORT"] ]
-argv_opts << ['--rackup', 'config.ru' ]
-argv_opts << ['--chdir', dirname ]
-argv_opts << ['-e', options[:env] ]
-argv_opts << ['--threaded', '-D', '--stats', '/stats']
-
-argv_opts.flatten!
-
-if options[:env] == "development"
-  use_rerun = false
-  begin
-    require "rerun"
-    use_rerun = true
-  rescue
-    # Do nothing
-  end
-end
+if platform == 'java'
+  require 'rack'
 
-puts "Starting Deltacloud API :: #{ENV["API_DRIVER"]} :: http://#{ENV["API_HOST"]}:#{ENV["API_PORT"]}/api"
-puts
+  # We can't chdir with webrick so add our root directory
+  # onto the load path
+  $: << dirname
+
+  # Read in config.ru and convert it to an instance of Rack::Builder
+  cfgfile = File.read(File.join(dirname, 'config.ru'))
+  inner_app = eval("Rack::Builder.new {(" + cfgfile + "\n )}.to_app",
+                   nil, 'config.ru')
 
-if use_rerun
-  argv_opts.unshift "thin"
-  command = argv_opts.join(" ")
-  topdir = File::expand_path(File::join(File::dirname(__FILE__), ".."))
-  rerun = Rerun::Runner.new(command, :dir => topdir)
-  rerun.start
-  rerun.join
+  app = Rack::Builder.new {
+    use Rack::CommonLogger # apache-like logging
+    use Rack::Reloader if options[:env] == "development"
+    set :root, dirname # Set Sinatra root since we can't chdir to ../
+    run inner_app
+  }.to_app
+
+  # There's a bug with string ports on JRuby so convert to int
+  # http://jira.codehaus.org/browse/JRUBY-4868
+  port = ENV["API_PORT"].to_i
+
+  puts "=> Ctrl-C to shutdown server"
+  Rack::Handler::WEBrick.run(app,
+                             :Host => ENV["API_HOST"],
+                             :Port => port,
+                             :AccessLog => [])
 else
-  thin = Thin::Runner.new(argv_opts)
+  require 'thin'
+
+  argv_opts = ARGV.clone
+  argv_opts << ['start'] unless Thin::Runner.commands.include?(options[0])
+  argv_opts << ['--address', ENV["API_HOST"] ]
+  argv_opts << ['--port', ENV["API_PORT"] ]
+  argv_opts << ['--rackup', 'config.ru' ]
+  argv_opts << ['--chdir', dirname ]
+  argv_opts << ['-e', options[:env] ]
+  argv_opts << ['--threaded', '-D', '--stats', '/stats']
+
+  argv_opts.flatten!
+
+  if options[:env] == "development"
+    use_rerun = false
+    begin
+      require "rerun"
+      use_rerun = true
+    rescue
+      # Do nothing
+    end
+  end
+
+  if use_rerun
+    argv_opts.unshift "thin"
+    command = argv_opts.join(" ")
+    topdir = File::expand_path(File::join(File::dirname(__FILE__), ".."))
+    rerun = Rerun::Runner.new(command, :dir => topdir)
+    rerun.start
+    rerun.join
+  else
+    thin = Thin::Runner.new(argv_opts)
 
-  begin
-    thin.run!
-  rescue Exception => e
-    puts "ERROR: #{e.message}"
+    begin
+      thin.run!
+    rescue Exception => e
+      puts "ERROR: #{e.message}"
+    end
   end
 end
-- 
1.7.2.1


[PATCH 1/2] Build java and ruby versions of core gem

Posted by Ben Browning <bb...@redhat.com>.
The Rakefile was modified so that 'rake gem' or 'rake package'
builds both the java and ruby versions. If you manually run
'gem build deltacloud-core.gemspec' then the version built
will depend on which interpreter you're using.

The java gem doesn't include eventmachine, thin, or rerun. The
deltacloudd script will need to be modified to run under jruby.
---
 server/Rakefile                |   12 +++++++++---
 server/deltacloud-core.gemspec |   14 ++++++++++----
 2 files changed, 19 insertions(+), 7 deletions(-)

diff --git a/server/Rakefile b/server/Rakefile
index 769e7d0..482b62f 100644
--- a/server/Rakefile
+++ b/server/Rakefile
@@ -43,10 +43,16 @@ Rake::TestTask.new("test") { |t|
   t.warning = false
 }
 
-load 'deltacloud-core.gemspec'
 
-Rake::GemPackageTask.new(@spec) do |pkg|
-  pkg.need_tar = true
+@specs = ['ruby', 'java'].inject({}) do |hash, spec_platform|
+  $platform = spec_platform
+  hash.update(spec_platform => Gem::Specification.load('deltacloud-core.gemspec'))
+end
+
+@specs.values.each do |spec|
+  Rake::GemPackageTask.new(spec) do |pkg|
+    pkg.need_tar = true
+  end
 end
 
 desc "Install API"
diff --git a/server/deltacloud-core.gemspec b/server/deltacloud-core.gemspec
index 8918fdc..c10cdf0 100644
--- a/server/deltacloud-core.gemspec
+++ b/server/deltacloud-core.gemspec
@@ -18,7 +18,7 @@
 
 require 'rake'
 
-@spec=Gem::Specification.new do |s|
+Gem::Specification.new do |s|
   s.author = 'Red Hat, Inc.'
   s.homepage = "http://www.deltacloud.org"
   s.email = 'deltacloud-users@lists.fedorahosted.org'
@@ -57,13 +57,19 @@ require 'rake'
   s.test_files= Dir.glob("tests/*_test.rb")
   s.extra_rdoc_files = Dir["COPYING"]
   s.required_ruby_version = '>= 1.8.1'
+
+  # Rakefile needs to create spec for both platforms (ruby and java), using the
+  # $platform global variable. In all other cases, we figure it out from
+  # RUBY_PLATFORM.
+  s.platform = $platform || RUBY_PLATFORM[/java/] || 'ruby'
+
   s.add_dependency('rake', '>= 0.8.7')
-  s.add_dependency('eventmachine', '>= 0.12.10')
+  s.add_dependency('eventmachine', '>= 0.12.10') if s.platform.to_s == 'ruby'
   s.add_dependency('haml', '>= 2.2.17')
   s.add_dependency('sinatra', '>= 0.9.4')
   s.add_dependency('rack', '>= 1.0.0')
-  s.add_dependency('thin', '>= 1.2.5')
-  s.add_dependency('rerun', '>= 0.5.2')
+  s.add_dependency('thin', '>= 1.2.5') if s.platform.to_s == 'ruby'
+  s.add_dependency('rerun', '>= 0.5.2') if s.platform.to_s == 'ruby'
   s.add_dependency('json', '>= 1.4.3')
   s.add_development_dependency('compass', '>= 0.8.17')
   s.add_development_dependency('nokogiri', '>= 1.4.1')
-- 
1.7.2.1