Permalink
Browse files

script/bootstrap

  • Loading branch information...
technoweenie committed Mar 10, 2011
1 parent dd2f774 commit aa60399c24edd2e566e969375343c39c3aef4eb9
View
@@ -2,3 +2,6 @@
deploy.rb
*~
config/secrets.yml
/bin
vendor/gems
.bundle
View
@@ -0,0 +1,106 @@
is_production = !!ENV['GEM_STRICT']
services_root = File.expand_path('../../', __FILE__)
if is_production
# Verify the environment has been bootstrapped by checking that the
# .bundle/loadpath file exists.
if !File.exist?("#{services_root}/.bundle/loadpath")
warn "WARN The gem environment is out-of-date or has yet to be bootstrapped."
warn " Run script/bootstrap to remedy this situation."
fail "gem environment not configued"
end
else
# Run a more exhaustive bootstrap check in non-production environments by making
# sure the Gemfile matches the .bundle/loadpath file checksum.
#
# Verify the environment has been bootstrapped by checking that the
# .bundle/loadpath file exists.
if !File.exist?("#{services_root}/.bundle/loadpath")
warn "WARN The gem environment is out-of-date or has yet to be bootstrapped."
warn " Runnning script/bootstrap to remedy this situation..."
system "#{services_root}/script/bootstrap --local"
if !File.exist?("#{services_root}/.bundle/loadpath")
warn "WARN The gem environment is STILL out-of-date."
warn " Please contact your network administrator."
fail "gem environment not configued"
end
end
checksum = File.read("#{services_root}/.bundle/checksum").to_i rescue nil
if `cksum <'#{services_root}/Gemfile'`.to_i != checksum
warn "WARN The gem environment is out-of-date or has yet to be bootstrapped."
warn " Runnning script/bootstrap to remedy this situation..."
system "#{services_root}/script/bootstrap --local"
checksum = File.read("#{services_root}/.bundle/checksum").to_i rescue nil
if `cksum <'#{services_root}/Gemfile'`.to_i != checksum
warn "WARN The gem environment is STILL out-of-date."
warn " Please contact your network administrator."
fail "gem environment not configued"
end
end
end
# Disallow use of system gems by default in staging and production environments
# or when the GEM_STRICT environment variable is set. This ensures the gem
# environment is totally isolated to only stuff specified in the Gemfile.
if is_production
ENV['GEM_PATH'] = "#{services_root}/vendor/gems"
ENV['GEM_HOME'] = "#{services_root}/vendor/gems"
elsif !ENV['GEM_PATH'].to_s.include?("#{services_root}/vendor/gems")
ENV['GEM_PATH'] =
["#{services_root}/vendor/gems", ENV['GEM_PATH']].compact.join(':')
end
# put RAILS_ROOT/bin on PATH
binpath = "#{services_root}/bin"
ENV['PATH'] = "#{binpath}:#{ENV['PATH']}" if !ENV['PATH'].include?(binpath)
# Setup bundled gem load path.
paths = File.read("#{services_root}/.bundle/loadpath").split("\n")
paths.each do |path|
next if path =~ /^[ \t]*(?:#|$)/
path = File.join(services_root, path)
$: << path if !$:.include?(path)
end
# Add RAILS_ROOT to load path so you can require config/initializers/file
# and stuff like that.
$:.unshift services_root if !$:.include?(services_root)
$:.unshift *Dir["#{File.dirname(__FILE__)}/../vendor/internal-gems/**/lib"]
# Child processes inherit our load path.
ENV['RUBYLIB'] = $:.compact.join(':')
# stdlib
require 'net/http'
require 'net/https'
require 'net/smtp'
require 'socket'
require 'xmlrpc/client'
require 'openssl'
require 'cgi'
#~ require 'date' # This is needed by the CIA service in ruby 1.8.7 or later
# vendor
require 'basecamp'
require 'rubyforge'
# bundled
require 'mime/types'
require 'xmlsimple'
require 'activesupport'
require 'rack'
require 'sinatra'
require 'tinder'
require 'json'
require 'basecamp'
require 'tmail'
require 'xmpp4r'
require 'xmpp4r-simple'
require 'rubyforge'
require 'oauth'
require 'yammer4r'
require 'mq'
View
@@ -1,21 +1,4 @@
$LOAD_PATH.unshift *Dir["#{File.dirname(__FILE__)}/vendor/**/lib"]
# stdlib
require 'net/http'
require 'net/https'
require 'net/smtp'
require 'socket'
require 'xmlrpc/client'
require 'openssl'
require 'cgi'
#~ require 'date' # This is needed by the CIA service in ruby 1.8.7 or later
# vendor
require 'rubygems'
require 'bundler'
Bundler.require
require 'basecamp'
require 'rubyforge'
require File.expand_path('../config/load', __FILE__)
set :run, true
set :environment, :production
View
@@ -0,0 +1,115 @@
#!/usr/bin/env ruby
#/ Usage: script/bootstrap [<options>]
#/ Bootstraps the gem environment.
#/
#/ Options are passed through to the bundle-install command. In most cases you
#/ won't need these. They're used primarily in production environments.
#/ --local use gems in vendor/cache instead of rubygems.org
#/ --without=<groups> do not install gems in the groups specified
#
# =============================================================================
# Uses bundler to install all gems specified in the Gemfile under vendor/gems,
# records the load path in config/loadpath, and generates bundler-free binstubs
# under bin.
#
# The basic idea is to use bundler to install necessary gems but not
# rely on it to manage the load path at runtime because it's slow. Requiring
# 'bundler/setup' takes ~500ms user CPU time in production and ~1500ms in
# development/test. This makes it unusable in scenarios that require a fast
# boot (e.g., script/gerve, proxymachine daemons, ernie/smoke, etc.). It's also
# a problem in development where it slows tools like rake command line
# completion to a crawl and adds at least a second to single-file test runs.
#
# There's very little reason to use bundler at runtime since everything
# is known at install time. We simply save off the result of the work done by
# bundle/setup and use it until bundle-install is run again.
# show usage message with --help
if ARGV.include?('--help')
system "grep '^#/' <'#{__FILE__}' |cut -c4-"
exit 2
end
# go into the project root because it makes everything easier
root = File.expand_path('../..', __FILE__)
Dir.chdir(root)
# point bundler to the right stuff
ENV['BUNDLE_GEMFILE'] = "#{root}/Gemfile"
ENV['BUNDLE_PATH'] = "#{root}/vendor/gems"
# bring in rubygems and make sure bundler is installed.
require 'rubygems'
begin
require 'bundler'
rescue LoadError => boom
warn "Bundler not found. Install it with `gem install bundler' and try again."
exit 0
end
# record the Gemfile checksum so we can tell if the Gemfile has changed
# since our loadpath was last generated. this is used in config/basic.rb
# to verify the environment is bootstrapped and up-to-date.
checksum = `cksum Gemfile`.to_i
installed = File.read('.bundle/checksum').to_i rescue nil
# run a quick check to see if everything's installed and up-to-date so we can
# skip the install and loadpath generation step if possible.
if checksum == installed && system('bundle check 1>/dev/null 2>&1')
puts "Gem environment up-to-date."
else
# run bundle-install to install any missing gems
argv = ['--no-color', 'install', '--path', 'vendor/gems'] + ARGV
system("bundle", *argv) || begin
warn "bundle executable not found. Ensure bundler is installed (`gem " +
"install bundler`) and that the gem bin path is in your PATH"
exit($?.exitstatus)
end
# load the Gemfile
bundle = Bundler.setup
# extract load paths for each gem and write to the config/loadpath file.
load_paths = []
bundle.gems.each do |gem|
next if gem.name == 'bundler'
gem.load_paths.each do |path|
if path[0, root.size] == root
path = path[(root.size + 1), path.size]
load_paths << path
else
warn "external load path directory detected: #{path}"
end
end
end
# move the loadpath and checksum files into place if everything was installed
# okay and the load path file was written successfully.
File.open('.bundle/loadpath+', 'wb') { |fd| fd.write(load_paths.join("\n")) }
File.rename('.bundle/loadpath+', '.bundle/loadpath')
File.open('.bundle/checksum', 'wb') { |fd| fd.puts(checksum) }
# write binstubs for all executables. we can't use bundler's --binstubs option
# because the generated executables require 'bundler/setup'. the binstubs
# generated here require only config/basic.rb, which sets up the loadpath
# manually using the .bundle/loadpath file.
Dir.mkdir "bin" unless File.directory?("bin")
template = DATA.read
lineno = File.read(__FILE__).count("\n") - template.count("\n")
bundle.gems.each do |spec|
spec.executables.each do |executable|
script = eval('%Q{' + template + '}', binding, __FILE__, lineno)
File.open("bin/#{executable}+", 'wb') { |fd| fd.write(script) }
File.chmod 0755, "bin/#{executable}+"
File.rename("bin/#{executable}+", "bin/#{executable}")
end
end
end
__END__
#!/usr/bin/env #{RbConfig::CONFIG['ruby_install_name']}
#
# This file was generated by script/bootstrap.
require '#{root}/config/load'
load File.join('#{spec.full_gem_path}', '#{spec.bindir}', '#{executable}')

0 comments on commit aa60399

Please sign in to comment.