| #!/usr/bin/env ruby |
| |
| # Licensed to the Apache Software Foundation (ASF) under one or more |
| # contributor license agreements. See the NOTICE file distributed with |
| # this work for additional information regarding copyright ownership. The |
| # ASF licenses this file to you under the Apache License, Version 2.0 (the |
| # "License"); you may not use this file except in compliance with the |
| # License. You may obtain a copy of the License at |
| # |
| # http://www.apache.org/licenses/LICENSE-2.0 |
| # |
| # Unless required by applicable law or agreed to in writing, software |
| # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| # License for the specific language governing permissions and limitations |
| # under the License. |
| # |
| |
| $:.unshift File.join(File.dirname(__FILE__), '..') |
| |
| require 'rubygems' |
| require 'optparse' |
| |
| #rackup -p 4567 |
| |
| #API_URL=$1 shotgun config.ru -p 4567 -E development |
| # |
| |
| def library_present?(name) |
| begin |
| require name |
| true |
| rescue LoadError |
| false |
| end |
| end |
| |
| options = {} |
| |
| optparse = OptionParser.new do |opts| |
| |
| opts.banner = <<BANNER |
| Usage: |
| start -u <URL> [options] |
| |
| Options: |
| BANNER |
| |
| opts.on( '-u', '--url URL', 'CIMI REST API entry point URL') do |url| |
| ENV["CIMI_API_URL"] ||= url |
| end |
| |
| opts.on( '-r', '--hostname HOSTNAME', |
| 'Bind to HOST address (default: localhost)') do |host| |
| ENV["CIMI_API_HOST"] = host |
| end |
| |
| opts.on( '-p', '--port PORT', 'Use PORT (default: 4001)') do |port| |
| ENV["CIMI_API_PORT"] = port |
| end |
| |
| opts.on( '-e', '--env ENV', 'Environment (default: "development")') { |env| options[:env] = env } |
| end |
| |
| optparse.parse! |
| |
| options[:env] ||= 'development' |
| |
| dirname="#{File.dirname(__FILE__)}/.." |
| |
| have_thin = library_present?('thin') |
| have_rerun = library_present?('rerun') |
| |
| unless have_thin |
| require 'rack' |
| |
| # 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') |
| |
| 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["CIMI_API_PORT"].to_i |
| |
| puts "=> Ctrl-C to shutdown server" |
| Rack::Handler::WEBrick.run(app, |
| :Host => ENV["CIMI_API_HOST"], |
| :Port => port, |
| :AccessLog => []) |
| else |
| argv_opts = ARGV.clone |
| argv_opts << ['start'] |
| argv_opts << ['--address', ENV["CIMI_API_HOST"] ] if ENV['CIMI_API_HOST'] |
| argv_opts << ['--port', ENV["CIMI_API_PORT"] || '4001' ] |
| argv_opts << ['--rackup', File.join(dirname, 'config.ru') ] |
| argv_opts << ['--timeout', ENV["API_TIMEOUT"] || '60'] |
| argv_opts << ['--threaded', '-D' ] |
| |
| argv_opts.flatten! |
| |
| if have_rerun && options[:env] == "development" |
| 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 |
| $:.unshift File.join(dirname) |
| thin = Thin::Runner.new(argv_opts) |
| thin.run! |
| end |
| end |