Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Added Rack framework support

  • Loading branch information...
commit 6f95737f1e00c5157536bd3869732a4b8f12e6c5 1 parent e6378a1
@krobertson krobertson authored
View
37 staging/lib/vcap/staging/plugin/manifests/rack.yml
@@ -0,0 +1,37 @@
+---
+name: "rack"
+runtimes:
+ - "ruby18":
+ version: "1.8.7" # FIXME change to 1.8.7-p334
+ description: "Ruby 1.8.7"
+ executable: "/usr/bin/ruby"
+ default: true
+ environment:
+ rails_env: "production"
+ bundle_gemfile:
+ rack_env: "production"
+ - "ruby19":
+ version: "1.9.2p180"
+ description: "Ruby 1.9.2"
+ executable: "ruby"
+ environment:
+ rails_env: "production"
+ bundle_gemfile:
+ rack_env: "production"
+app_servers:
+ - "thin":
+ description: "Thin"
+ executable: false # determined during staging
+ default: true
+detection:
+ - "config.ru": true
+ - "config/environment.rb": false # and config/environment.rb must not exist
+staged_services:
+ - "name": "mysql"
+ "version": "*"
+ - "name": "postgresql"
+ "version": "*"
+ - "name": "redis"
+ "version": "2"
+
+# vim: filetype=yaml
View
1  staging/lib/vcap/staging/plugin/manifests/sinatra.yml
@@ -25,6 +25,7 @@ app_servers:
default: true
detection:
- "*.rb": "require 'sinatra'|require \"sinatra\"" # .rb files in the root dir containing a require?
+ - "config.ru": false # use rack if it detects a config.ru
- "config/environment.rb": false # and config/environment.rb must not exist
staged_services:
- "name": "mysql"
View
64 staging/lib/vcap/staging/plugin/rack/plugin.rb
@@ -0,0 +1,64 @@
+class RackPlugin < StagingPlugin
+ include GemfileSupport
+ def framework
+ 'rack'
+ end
+
+ def stage_application
+ Dir.chdir(destination_directory) do
+ create_app_directories
+ copy_source_files
+ compile_gems
+ create_startup_script
+ create_stop_script
+ end
+ end
+
+ def start_command
+ if uses_bundler?
+ "#{local_runtime} ./rubygems/ruby/#{library_version}/bin/bundle exec #{local_runtime} -S rackup $@ config.ru"
+ else
+ "#{local_runtime} -S rackup $@ config.ru"
+ end
+ end
+
+ private
+ def startup_script
+ vars = environment_hash
+ if uses_bundler?
+ vars['PATH'] = "$PWD/app/rubygems/ruby/#{library_version}/bin:$PATH"
+ vars['GEM_PATH'] = vars['GEM_HOME'] = "$PWD/app/rubygems/ruby/#{library_version}"
+ vars['RUBYOPT'] = '-I$PWD/ruby -rstdsync'
+ else
+ vars['RUBYOPT'] = "-rubygems -I$PWD/ruby -rstdsync"
+ end
+ # PWD here is after we change to the 'app' directory.
+ generate_startup_script(vars) do
+ plugin_specific_startup
+ end
+ end
+
+ def stop_script
+ vars = environment_hash
+ generate_stop_script(vars)
+ end
+
+ def plugin_specific_startup
+ cmds = []
+ cmds << "mkdir ruby"
+ cmds << 'echo "\$stdout.sync = true" >> ./ruby/stdsync.rb'
+ cmds.join("\n")
+ end
+
+ # TODO - I'm fairly sure this problem of 'no standard startup command' is
+ # going to be limited to Sinatra and Node.js. If not, it probably deserves
+ # a place in the sinatra.yml manifest.
+ def detect_main_file
+ file = app_files_matching_patterns.first
+ # TODO - Currently staging exceptions are not handled well.
+ # Convert to using exit status and return value on a case-by-case basis.
+ raise "Unable to determine Rack startup command" unless file
+ file
+ end
+end
+
View
5 staging/lib/vcap/staging/plugin/rack/stage
@@ -0,0 +1,5 @@
+#!/usr/bin/env ruby
+require File.expand_path('../../common', __FILE__)
+plugin_class = StagingPlugin.load_plugin_for('rack')
+plugin_class.validate_arguments!
+plugin_class.new(*ARGV).stage_application
View
63 staging/spec/unit/rack_spec.rb
@@ -0,0 +1,63 @@
+require 'spec_helper'
+
+describe "A simple Rack app being staged" do
+ before do
+ app_fixture :sinatra_trivial
+ end
+
+ it "is packaged with a startup script" do
+ stage :rack do |staged_dir|
+ executable = '%VCAP_LOCAL_RUNTIME%'
+ start_script = File.join(staged_dir, 'startup')
+ start_script.should be_executable_file
+ script_body = File.read(start_script)
+ script_body.should == <<-EXPECTED
+#!/bin/bash
+export RACK_ENV="production"
+export RAILS_ENV="production"
+export RUBYOPT="-rubygems -I$PWD/ruby -rstdsync"
+unset BUNDLE_GEMFILE
+mkdir ruby
+echo "\\$stdout.sync = true" >> ./ruby/stdsync.rb
+cd app
+#{executable} -S rackup $@ config.ru > ../logs/stdout.log 2> ../logs/stderr.log &
+STARTED=$!
+echo "$STARTED" >> ../run.pid
+wait $STARTED
+ EXPECTED
+ end
+ end
+
+ describe "when bundled" do
+ before do
+ app_fixture :sinatra_gemfile
+ end
+
+ it "is packaged with a startup script" do
+ stage :rack do |staged_dir|
+ executable = '%VCAP_LOCAL_RUNTIME%'
+ start_script = File.join(staged_dir, 'startup')
+ start_script.should be_executable_file
+ script_body = File.read(start_script)
+ script_body.should == <<-EXPECTED
+#!/bin/bash
+export GEM_HOME="$PWD/app/rubygems/ruby/1.8"
+export GEM_PATH="$PWD/app/rubygems/ruby/1.8"
+export PATH="$PWD/app/rubygems/ruby/1.8/bin:$PATH"
+export RACK_ENV="production"
+export RAILS_ENV="production"
+export RUBYOPT="-I$PWD/ruby -rstdsync"
+unset BUNDLE_GEMFILE
+mkdir ruby
+echo "\\$stdout.sync = true" >> ./ruby/stdsync.rb
+cd app
+#{executable} ./rubygems/ruby/1.8/bin/bundle exec #{executable} -S rackup $@ config.ru > ../logs/stdout.log 2> ../logs/stderr.log &
+STARTED=$!
+echo "$STARTED" >> ../run.pid
+wait $STARTED
+ EXPECTED
+ end
+ end
+ end
+end
+
Please sign in to comment.
Something went wrong with that request. Please try again.