Permalink
Browse files

new preflight repo

  • Loading branch information...
Steve Conover
Steve Conover committed Oct 3, 2011
0 parents commit a3fd023985282fda592f84474357ca6a58470c90
Showing with 1,887 additions and 0 deletions.
  1. +1 −0 .gitignore
  2. +1 −0 .rvmrc
  3. +1 −0 .wrong
  4. +6 −0 Gemfile
  5. +21 −0 Gemfile.lock
  6. +25 −0 README.markdown
  7. +149 −0 bin/preflight
  8. BIN gems/bundler-1.0.18.gem
  9. BIN gems/jruby-openssl-0.7.4.gem
  10. +70 −0 spec/basics_spec.rb
  11. +66 −0 spec/bundler_spec.rb
  12. +3 −0 spec/sample_projects/has_gems_via_bundler/Gemfile
  13. +11 −0 spec/sample_projects/has_gems_via_bundler/Gemfile.lock
  14. +14 −0 spec/sample_projects/has_gems_via_bundler/Rakefile
  15. +2 −0 spec/sample_projects/has_gems_via_bundler/config/preflight.yml
  16. 0 spec/sample_projects/has_gems_via_bundler/true}
  17. BIN spec/sample_projects/has_gems_via_bundler/vendor/cache/spruz-0.2.13.gem
  18. +29 −0 spec/sample_projects/no_dependencies/Rakefile
  19. +2 −0 spec/sample_projects/no_dependencies/config/preflight.yml
  20. +3 −0 spec/sample_projects/webapp/Gemfile
  21. +88 −0 spec/sample_projects/webapp/Gemfile.lock
  22. +5 −0 spec/sample_projects/webapp/app/controllers/application_controller.rb
  23. +4 −0 spec/sample_projects/webapp/config.ru
  24. +46 −0 spec/sample_projects/webapp/config/application.rb
  25. +6 −0 spec/sample_projects/webapp/config/boot.rb
  26. +5 −0 spec/sample_projects/webapp/config/environment.rb
  27. +25 −0 spec/sample_projects/webapp/config/environments/development.rb
  28. +35 −0 spec/sample_projects/webapp/config/environments/test.rb
  29. +7 −0 spec/sample_projects/webapp/config/initializers/secret_token.rb
  30. +5 −0 spec/sample_projects/webapp/config/preflight.yml
  31. +60 −0 spec/sample_projects/webapp/config/routes.rb
  32. +36 −0 spec/sample_projects/webapp/log/development.log
  33. +239 −0 spec/sample_projects/webapp/public/index.html
  34. +6 −0 spec/sample_projects/webapp/script/rails
  35. +45 −0 spec/spec_helper.rb
  36. +1 −0 spec/suite.rb
  37. +48 −0 spec/web_spec.rb
  38. +687 −0 web/jetty-init.erb
  39. +88 −0 web/jetty.xml
  40. +47 −0 web/web.xml.erb
@@ -0,0 +1 @@
+spec/local_mirror
1 .rvmrc
@@ -0,0 +1 @@
+rvm use ree-1.8.7@package --create
1 .wrong
@@ -0,0 +1 @@
+color
@@ -0,0 +1,6 @@
+source 'https://rubygems.org'
+
+group :test do
+ gem "rspec"
+ gem "open4"
+end
@@ -0,0 +1,21 @@
+GEM
+ remote: https://rubygems.org/
+ specs:
+ diff-lcs (1.1.3)
+ open4 (1.1.0)
+ rspec (2.6.0)
+ rspec-core (~> 2.6.0)
+ rspec-expectations (~> 2.6.0)
+ rspec-mocks (~> 2.6.0)
+ rspec-core (2.6.4)
+ rspec-expectations (2.6.0)
+ diff-lcs (~> 1.1.2)
+ rspec-mocks (2.6.0)
+
+PLATFORMS
+ java
+ ruby
+
+DEPENDENCIES
+ open4
+ rspec
@@ -0,0 +1,25 @@
+Preflight prepares your (j)ruby project for jvm deployment.
+
+1. Create config/preflight.yml in your project
+
+Example config/preflight.yml:
+
+ jruby: "http://mirrors.squareup.com/distfiles/jruby-complete-1.6.4.jar"
+ jetty: "http://mirrors.squareup.com/distfiles/jetty-hightide-7.4.5.v20110725.zip"
+ jruby-rack: "http://mirrors.squareup.com/distfiles/jruby-rack-1.0.10.jar"
+ app_user: finance
+ app_root: /usr/local/finance/finance
+
+2. Running:
+
+ <one day soon there will be a gem install>
+
+ preflight <your project directory>
+
+
+Of note, you'll now have:
+
+* a bin directory, with scripts that run ruby and rake, using jruby and with the gems defined in your project.
+* a vendor/jetty directory, containing everything necessary to run your app using jetty.
+ * You can try your app out by cd'ing into vendor/jetty and running RAILS_ENV=development java -jar start.jar
+ * vendor/jetty/jetty-init is an init script that starts your project. You should symlink /etc/init.d/<appuser>-jetty to this file, and then point monit at /etc/init.d/<appuser>-jetty
@@ -0,0 +1,149 @@
+#!/usr/bin/ruby
+
+require "yaml"
+require "erb"
+require "fileutils"
+include FileUtils
+
+def halt(message)
+ $stderr.puts message
+ exit 1
+end
+
+def x!(cmd)
+ puts cmd if ENV['VERBOSE']
+ system(cmd) || halt("'#{cmd}' failed.")
+end
+
+
+halt("usage: preflight <project_path>") unless ARGV.length==1 && File.directory?(ARGV[0])
+
+@path_to_project = ARGV.shift
+@created = []
+
+config_file = File.join(@path_to_project, "config/preflight.yml")
+halt("#{config_file} not found") unless File.exists?(config_file)
+@preflight_config = YAML.load(ERB.new(File.read(config_file)).result)
+
+unless @preflight_config['jruby']
+ halt(%{Your config/preflight.yml must at least contain a pointer to the desired jruby-complete jar.
+Ex:
+
+jruby: http://jruby.org.s3.amazonaws.com/downloads/1.6.4/jruby-complete-1.6.4.jar
+jetty: http://dist.codehaus.org/jetty/jetty-hightide-7.4.5/jetty-hightide-7.4.5.v20110725.zip
+ })
+end
+
+script_start_time = Time.now
+
+@jruby_jar_file = "vendor/jruby.jar"
+@preflight_dir = File.expand_path(File.join(File.dirname(__FILE__), ".."))
+
+@gem_home = File.expand_path(File.join(@preflight_config['app_root'], "/vendor/bundler_gem"))
+@gem_path = @gem_home + ":file:" + File.expand_path(File.join(@preflight_config['app_root'], @jruby_jar_file)) + "!/META-INF/jruby.home/lib/ruby/gems/1.8"
+
+def jruby!(cmd)
+ x! "cd #{@path_to_project} && java -client -jar #{@jruby_jar_file} #{cmd}"
+end
+
+def download(url, local_file)
+ x! "curl --silent --show-error -o #{local_file} #{url}"
+end
+
+def unzip(file, directory, pattern)
+ x! "unzip -qq #{file} #{pattern} -d #{directory}"
+end
+
+def install_bundler_gem
+ unless File.exists?("#{@path_to_project}/vendor/bundler_gem")
+ jruby! " -S gem install #{@preflight_dir}/gems/bundler-1.0.18.gem -i vendor/bundler_gem --no-rdoc --no-ri"
+ end
+ @created << "#{@path_to_project}/vendor/bundler_gem"
+end
+
+def bundle_install
+ install_bundler_gem
+ jruby! " -e 'require \"rubygems\"; require \"bundler\"; require \"bundler/cli\"; cli=Bundler::CLI.new; cli.options={:deployment=>true}; cli.install'"
+ @created << "#{@path_to_project}/vendor/bundle"
+end
+
+def write_script(script_path, contents)
+ File.open(script_path, "w+") { |f| f << contents }
+ chmod 0700, script_path
+ @created << script_path
+end
+
+def create_ruby_script(ruby_script_path)
+ write_script ruby_script_path, \
+%{#!/bin/bash
+
+cd $(dirname $0)/..
+GEM_HOME="#{@gem_home}" GEM_PATH="#{@gem_path}" java -client -jar #{@jruby_jar_file} $*
+}
+end
+
+def create_rake_script(ruby_script_path)
+ write_script ruby_script_path, \
+%{#!/bin/bash
+
+$(dirname $0)/ruby -S rake $* --trace
+}
+end
+
+def install_jetty(vendor_dir)
+ jetty_dir = File.join(vendor_dir, "jetty")
+ Dir["#{vendor_dir}/jett*"].to_a.each{|f|rm_rf(f)}
+ download @preflight_config['jetty'], File.join(vendor_dir, "jetty.zip")
+ unzip File.join(vendor_dir, "jetty.zip"), vendor_dir, '*/start.jar */lib/jetty*.jar */lib/servl*.jar'
+ rm File.join(vendor_dir, "jetty.zip")
+ mv Dir["#{vendor_dir}/jetty*"].to_a.first, jetty_dir
+ Dir[File.join(jetty_dir, "**/async-rest*")].each{|d|rm_rf(d)}
+ jetty_dir
+end
+
+def install_jruby_rack_jar(vendor_dir)
+ download @preflight_config['jruby-rack'], File.join(vendor_dir, "jruby-rack.jar")
+end
+
+def copy_jetty_xml(jetty_dir)
+ mkdir_p File.join(jetty_dir, "etc")
+ cp File.join(@preflight_dir, "web/jetty.xml"), File.join(jetty_dir, "etc")
+end
+
+def create_web_inf_subdirectory
+ mkdir_p File.join(@path_to_project, "WEB-INF")
+ web_xml_contents = ERB.new(File.read(File.join(@preflight_dir, "web/web.xml.erb"))).result(binding)
+ File.open(File.join(@path_to_project, "WEB-INF/web.xml"), "w+"){|f|f<<web_xml_contents}
+end
+
+def write_jetty_init_script(jetty_dir)
+ jetty_init_contents = ERB.new(File.read(File.join(@preflight_dir, "web/jetty-init.erb"))).result(binding)
+ write_script(File.join(jetty_dir, "jetty-init"), jetty_init_contents)
+end
+
+ruby_script_path = File.join(@path_to_project, "bin/ruby")
+rake_script_path = File.join(@path_to_project, "bin/rake")
+bin_dir = File.join(@path_to_project, "bin")
+vendor_dir = File.join(@path_to_project, "vendor")
+
+mkdir_p File.join(@path_to_project, "bin")
+mkdir_p File.join(@path_to_project, "vendor")
+download @preflight_config['jruby'], File.join(@path_to_project, @jruby_jar_file)
+create_ruby_script ruby_script_path
+create_rake_script rake_script_path
+bundle_install if File.exists?(File.join(@path_to_project, "Gemfile.lock"))
+if @preflight_config['jetty']
+ jetty_dir = install_jetty(vendor_dir)
+ install_jruby_rack_jar vendor_dir #raise if jar not found
+ copy_jetty_xml jetty_dir
+ write_jetty_init_script jetty_dir #raise if app_user isn't specified in config
+
+ create_web_inf_subdirectory
+end
+
+elapsed_seconds = (Time.now - script_start_time).to_i
+
+puts %{Created:
+ #{@created.join("\n ")}
+Time: #{elapsed_seconds} s
+}
Binary file not shown.
Binary file not shown.
@@ -0,0 +1,70 @@
+require "spec_helper"
+
+describe "preflight - basics" do
+ before(:all) do
+ reset
+ @result = x!("bin/preflight spec/sample_projects/no_dependencies")
+ end
+ after(:all) do
+ reset
+ end
+
+ it "will put the jruby jar under vendor" do
+ @result[:stderr].should == ""
+ @result[:exitstatus].should == 0
+ File.exists?("spec/sample_projects/no_dependencies/vendor/jruby.jar").should == true
+ end
+
+ it "creates a rake script" do
+ @result[:stdout].should include("spec/sample_projects/no_dependencies/bin/rake\n")
+ File.exists?("spec/sample_projects/no_dependencies/bin/rake").should == true
+ end
+
+ describe "creates a ruby script that" do
+ it "allows you to execute using the jruby jar." do
+ rake_result = x("spec/sample_projects/no_dependencies/bin/ruby -S rake project_info")
+ rake_result[:stderr].should == ""
+ rake_result[:stdout].should include("Hi, I'm the no_dependencies project")
+ rake_result[:exitstatus].should == 0
+ end
+ end
+
+ describe "makes a rake script that" do
+ it %{ - runs from any directory (properly cd's)
+ - the ruby used is indeed jruby
+ - allows you to execute several tasks in a row
+ - shows the trace of a rake failure
+ - nails the GEM_PATH to within the jar so we don't go 'accidentally' loading gems and such from another ruby env, the result of which is insanity.
+
+ Asserting all this in one test to optimize for test running time.} do
+ absolute_script_path = File.expand_path("spec/sample_projects/no_dependencies/bin/rake project_info another_task load_path gem_path boom")
+ rake_result = x("cd /tmp && #{absolute_script_path} project_info")
+
+ rake_result[:stdout].should include("PWD=#{File.expand_path("spec/sample_projects/no_dependencies")}")
+ rake_result[:stdout].should include("Hi, I'm the no_dependencies project")
+ rake_result[:stdout].should include("RUBY_PLATFORM=java")
+ rake_result[:stdout].should include("jruby.jar!/META-INF")
+ rake_result[:stdout].should include("You ran another task")
+
+ rake_result[:stderr].should include("BOOM")
+ rake_result[:stderr].should include("xxx")
+ rake_result[:stderr].should include("yyy")
+ rake_result[:stderr].should include("zzz")
+ rake_result[:exitstatus].should == 1
+
+ load_path_elements = rake_result[:stdout].split("\n").select{|line|line =~ /^LP--/}
+ load_path_elements.length >= 3
+ load_path_elements.each do |element|
+ element = element.sub("LP-- ", "")
+ (element =~ /META-INF\/jruby\.home/ || element =~ /^\.$/).should >= 0
+ end
+
+ gem_path_elements = rake_result[:stdout].split("\n").select{|line|line =~ /^GP--/}
+ gem_path_elements.length >= 2
+ gem_path_elements.each do |element|
+ element = element.sub("GP-- ", "")
+ (element =~ /META-INF\/jruby\.home/ || element =~ /vendor\/bundler_gem/).should >= 0
+ end
+ end
+ end
+end
@@ -0,0 +1,66 @@
+require "spec_helper"
+
+describe "preflight - bundler and gems" do
+
+ before(:all) do
+ reset
+ rm_rf("spec/sample_projects/has_gems_via_bundler/vendor/bundle")
+ rm_rf("spec/sample_projects/has_gems_via_bundler/vendor/bundler_gem")
+ x!("bin/preflight spec/sample_projects/has_gems_via_bundler")
+ end
+
+ after(:all) do
+ rm_rf("spec/sample_projects/has_gems_via_bundler/vendor/bundle")
+ rm_rf("spec/sample_projects/has_gems_via_bundler/vendor/bundler_gem")
+ end
+
+ describe "presence of the library" do
+ it "installed bundler into vendor/bundler_gem." do
+ files = Dir["spec/sample_projects/has_gems_via_bundler/vendor/bundler_gem/**/*.rb"].to_a.map{|f|File.basename(f)}
+ files.should include("bundler.rb")
+ files.should include("dsl.rb")
+ end
+
+ it "is not accidentally using bundler from another ruby environment." do
+ rake_result = x("spec/sample_projects/has_gems_via_bundler/bin/rake load_path_with_bundler")
+ load_path_elements = rake_result[:stdout].split("\n").select{|line|line =~ /^--/}
+ load_path_elements.length.should >= 3
+ load_path_elements.each do |element|
+ element = element.sub("-- ", "")
+ (element =~ /META-INF\/jruby\.home/ || element =~ /vendor\/bundler_gem/ || element =~ /^\.$/).should >= 0
+ end
+ end
+
+ it "can be used from a script fed to jruby." do
+ rake_result = x(%{echo 'require \\"rubygems\\"; require \\"bundler\\"; puts Bundler::VERSION' | spec/sample_projects/has_gems_via_bundler/bin/ruby})
+ rake_result[:stderr].should == ""
+ rake_result[:stdout].should include("1.0.18")
+ rake_result[:exitstatus].should == 0
+ end
+ end
+
+ describe "gem installation" do
+ it "installs gems into vendor/bundle" do
+ files = Dir["spec/sample_projects/has_gems_via_bundler/vendor/bundle/**/*.rb"].to_a.map{|f|File.basename(f)}
+ files.should include("bijection.rb")
+ files.should include("spruz.rb")
+ files.length.should > 20
+ end
+
+ it "installed gems are available via normal require" do
+ rake_result = x("cd spec/sample_projects/has_gems_via_bundler && " +
+ %{echo 'require \\"rubygems\\"; require \\"bundler/setup\\"; require \\"spruz/bijection\\"; puts Spruz::Bijection.name' | bin/ruby})
+ rake_result[:stderr].should == ""
+ rake_result[:stdout].should == "Spruz::Bijection\n"
+ rake_result[:exitstatus].should == 0
+ end
+
+ it "installed gems are available via Bundler.require" do
+ rake_result = x("cd spec/sample_projects/has_gems_via_bundler && " +
+ %{echo 'require \\"rubygems\\"; require \\"bundler\\"; Bundler.require; puts Spruz::Bijection.name' | bin/ruby})
+ rake_result[:stderr].should == ""
+ rake_result[:stdout].should == "Spruz::Bijection\n"
+ rake_result[:exitstatus].should == 0
+ end
+ end
+end
@@ -0,0 +1,3 @@
+source 'http://rubygems.org'
+
+gem "spruz"
@@ -0,0 +1,11 @@
+GEM
+ remote: http://rubygems.org/
+ specs:
+ spruz (0.2.13)
+
+PLATFORMS
+ java
+ ruby
+
+DEPENDENCIES
+ spruz
@@ -0,0 +1,14 @@
+task :load_path do
+ puts $LOAD_PATH.join("\n")
+end
+
+task :load_path_with_rubygems do
+ require "rubygems"
+ puts $LOAD_PATH.join("\n")
+end
+
+task :load_path_with_bundler do
+ require "rubygems"
+ require "bundler"
+ $LOAD_PATH.each{|path|puts "-- #{path}"}
+end
@@ -0,0 +1,2 @@
+jruby: "file://<%= File.expand_path('spec/local_mirror') %>/jruby-complete-1.6.4.jar"
+app_root: <%= File.expand_path('spec/sample_projects/has_gems_via_bundler') %>
Oops, something went wrong.

0 comments on commit a3fd023

Please sign in to comment.