From 37a12997628fcab722512f8a6370b92d44e33529 Mon Sep 17 00:00:00 2001 From: Eric Wong Date: Fri, 2 Oct 2009 20:44:03 -0700 Subject: initial revision No tests yet, but the old "gossamer" and "rainbows" branches seem to be basically working. --- bin/rainbows | 166 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 166 insertions(+) create mode 100644 bin/rainbows (limited to 'bin/rainbows') diff --git a/bin/rainbows b/bin/rainbows new file mode 100644 index 0000000..b3f3a26 --- /dev/null +++ b/bin/rainbows @@ -0,0 +1,166 @@ +#!/home/ew/bin/ruby +# -*- encoding: binary -*- +require 'unicorn/launcher' +require 'rainbows' +require 'optparse' + +env = "development" +daemonize = false +listeners = [] +options = { :listeners => listeners } +host, port = Unicorn::Const::DEFAULT_HOST, Unicorn::Const::DEFAULT_PORT +set_listener = false + +opts = OptionParser.new("", 24, ' ') do |opts| + opts.banner = "Usage: #{File.basename($0)} " \ + "[ruby options] [unicorn options] [rackup config file]" + + opts.separator "Ruby options:" + + lineno = 1 + opts.on("-e", "--eval LINE", "evaluate a LINE of code") do |line| + eval line, TOPLEVEL_BINDING, "-e", lineno + lineno += 1 + end + + opts.on("-d", "--debug", "set debugging flags (set $DEBUG to true)") do + $DEBUG = true + end + + opts.on("-w", "--warn", "turn warnings on for your script") do + $-w = true + end + + opts.on("-I", "--include PATH", + "specify $LOAD_PATH (may be used more than once)") do |path| + $LOAD_PATH.unshift(*path.split(/:/)) + end + + opts.on("-r", "--require LIBRARY", + "require the library, before executing your script") do |library| + require library + end + + opts.separator "Unicorn options:" + + # some of these switches exist for rackup command-line compatibility, + + opts.on("-o", "--host HOST", + "listen on HOST (default: #{Unicorn::Const::DEFAULT_HOST})") do |h| + host = h + set_listener = true + end + + opts.on("-p", "--port PORT", + "use PORT (default: #{Unicorn::Const::DEFAULT_PORT})") do |p| + port = p.to_i + set_listener = true + end + + opts.on("-E", "--env ENVIRONMENT", + "use ENVIRONMENT for defaults (default: development)") do |e| + env = e + end + + opts.on("-D", "--daemonize", "run daemonized in the background") do |d| + daemonize = d ? true : false + end + + opts.on("-P", "--pid FILE", "DEPRECATED") do |f| + warn %q{Use of --pid/-P is strongly discouraged} + warn %q{Use the 'pid' directive in the Unicorn config file instead} + options[:pid] = File.expand_path(f) + end + + opts.on("-s", "--server SERVER", + "this flag only exists for compatibility") do |s| + warn "-s/--server only exists for compatibility with rackup" + end + + # Unicorn-specific stuff + opts.on("-l", "--listen {HOST:PORT|PATH}", + "listen on HOST:PORT or PATH", + "this may be specified multiple times", + "(default: #{Unicorn::Const::DEFAULT_LISTEN})") do |address| + listeners << address + end + + opts.on("-c", "--config-file FILE", "Unicorn-specific config file") do |f| + options[:config_file] = File.expand_path(f) + end + + # I'm avoiding Unicorn-specific config options on the command-line. + # IMNSHO, config options on the command-line are redundant given + # config files and make things unnecessarily complicated with multiple + # places to look for a config option. + + opts.separator "Common options:" + + opts.on_tail("-h", "--help", "Show this message") do + puts opts.to_s.gsub(/^.*DEPRECATED.*$/s, '') + exit + end + + opts.on_tail("-v", "--version", "Show version") do + puts "unicorn v#{Unicorn::Const::UNICORN_VERSION}" + exit + end + + opts.parse! ARGV +end + +config = ARGV[0] || "config.ru" +abort "configuration file #{config} not found" unless File.exist?(config) + +if config =~ /\.ru$/ + # parse embedded command-line options in config.ru comments + if File.open(config, "rb") { |fp| fp.sysread(fp.stat.size) } =~ /^#\\(.*)/ + opts.parse! $1.split(/\s+/) + end +end + +require 'pp' if $DEBUG + +app = lambda do || + # require Rack as late as possible in case $LOAD_PATH is modified + # in config.ru or command-line + inner_app = case config + when /\.ru$/ + raw = File.open(config, "rb") { |fp| fp.sysread(fp.stat.size) } + raw.sub!(/^__END__\n.*/, '') + eval("Rack::Builder.new {(#{raw}\n)}.to_app", nil, config) + else + require config + Object.const_get(File.basename(config, '.rb').capitalize) + end + pp({ :inner_app => inner_app }) if $DEBUG + case env + when "development" + Rack::Builder.new do + use Rack::CommonLogger, $stderr + use Rack::ShowExceptions + use Rack::Lint + run inner_app + end.to_app + when "deployment" + Rack::Builder.new do + use Rack::CommonLogger, $stderr + run inner_app + end.to_app + else + inner_app + end +end + +listeners << "#{host}:#{port}" if set_listener + +if $DEBUG + pp({ + :unicorn_options => options, + :app => app, + :daemonize => daemonize, + }) +end + +Unicorn::Launcher.daemonize! if daemonize +Rainbows.run(app, options) -- cgit v1.2.3-24-ge0c7