Skip to content

Commit

Permalink
Merge branch 'master' into services-r6
Browse files Browse the repository at this point in the history
Change-Id: I5c3fe671fbd886db48caedc6dc308ebd326167cf
  • Loading branch information
felixhoo committed Nov 3, 2011
2 parents 884bca9 + e1c500f commit 6df3fa3
Show file tree
Hide file tree
Showing 117 changed files with 1,663 additions and 175 deletions.
5 changes: 5 additions & 0 deletions bin/cloud_controller
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,11 @@ opts_parser = OptionParser.new do |opts|
end
opts_parser.parse!(ARGV.dup)

if $cc_config_file.nil? && ENV["CLOUD_FOUNDRY_CONFIG_PATH"]
$cc_config_file = File.join(ENV["CLOUD_FOUNDRY_CONFIG_PATH"], "cloud_controller.yml")
puts "Using config file #{$cc_config_file}"
end

if $cc_config_file
if File.exists?($cc_config_file)
ENV['CLOUD_CONTROLLER_CONFIG'] = $cc_config_file
Expand Down
4 changes: 2 additions & 2 deletions bin/vcap
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ class Component
# Make sure db is setup, this is slow and we should make it faster, but
# should help for now.
if is_cloud_controller?
Dir.chdir("#{File.dirname(__FILE__)}/../cloud_controller") { `rake db:migrate CLOUD_CONTROLLER_CONFIG=#{@configuration_path}` }
Dir.chdir("#{File.dirname(__FILE__)}/../cloud_controller") { `bundle exec rake db:migrate CLOUD_CONTROLLER_CONFIG=#{@configuration_path}` }
end
exec("#{component_start_path}")
end
Expand Down Expand Up @@ -344,7 +344,7 @@ module Run
end

cc_dir = File.expand_path(File.join(DIR, '..', 'cloud_controller'))
run_command("Resetting the CloudController database", "cd #{cc_dir} 2>&1 && rake db:drop 2>&1")
run_command("Resetting the CloudController database", "cd #{cc_dir} 2>&1 && bundle exec rake db:drop 2>&1")
puts

cc_log_dir = File.join(cc_dir, 'log')
Expand Down
5 changes: 3 additions & 2 deletions cloud_controller/Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ gem 'logging', '>= 1.5.0'
# VCAP common components
gem 'vcap_common', :require => ['vcap/common', 'vcap/component'], :path => '../common'
gem 'vcap_logging', :require => ['vcap/logging']
gem 'vcap_staging', '= 0.1.12'
gem 'vcap_staging', '= 0.1.24'

# For queuing staging tasks
gem 'em-hiredis'
Expand All @@ -34,7 +34,7 @@ group :production do
end

# EventMachine and async support
gem 'eventmachine', '~> 0.12.10'
gem 'eventmachine', '~> 0.12.11.cloudfoundry.1'
gem 'thin', '> 1.2'
gem 'em-http-request', '~> 1.0.0.beta.3', :require => 'em-http'
gem 'em-redis', :require => nil
Expand All @@ -57,5 +57,6 @@ group :test do
gem 'mocha'
gem 'ci_reporter'
gem 'sinatra' # For service gateway shims
gem 'delorean'
end

16 changes: 10 additions & 6 deletions cloud_controller/Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ PATH
remote: ../common
specs:
vcap_common (0.99)
eventmachine (~> 0.12.10)
eventmachine (~> 0.12.11.cloudfoundry.1)
logging (>= 1.5.0)
nats
posix-spawn
Expand Down Expand Up @@ -44,9 +44,12 @@ GEM
arel (2.0.9)
bcrypt-ruby (2.1.4)
builder (2.1.2)
chronic (0.6.4)
ci_reporter (1.6.4)
builder (>= 2.1.2)
daemons (1.1.2)
delorean (1.1.0)
chronic
diff-lcs (1.1.2)
em-hiredis (0.1.0)
hiredis (~> 0.3.0)
Expand All @@ -61,7 +64,7 @@ GEM
eventmachine
erubis (2.6.6)
abstract (>= 1.0.0)
eventmachine (0.12.10)
eventmachine (0.12.11.cloudfoundry.1)
hiredis (0.3.2)
http_parser.rb (0.5.1)
i18n (0.5.0)
Expand Down Expand Up @@ -124,7 +127,7 @@ GEM
ruby-hmac (0.4.0)
sinatra (1.2.1)
rack (~> 1.1)
tilt (< 2.0, >= 1.2.2)
tilt (>= 1.2.2, < 2.0)
sqlite3 (1.3.3)
thin (1.2.11)
daemons (>= 1.0.9)
Expand All @@ -138,7 +141,7 @@ GEM
uuidtools (2.1.2)
vcap_logging (0.1.0)
vcap_stager (0.1.3)
vcap_staging (0.1.12)
vcap_staging (0.1.24)
nokogiri (>= 1.4.4)
rake
rspec
Expand All @@ -153,10 +156,11 @@ DEPENDENCIES
SystemTimer
bcrypt-ruby (>= 2.1.4)
ci_reporter
delorean
em-hiredis
em-http-request (~> 1.0.0.beta.3)
em-redis
eventmachine (~> 0.12.10)
eventmachine (~> 0.12.11.cloudfoundry.1)
logging (>= 1.5.0)
mocha
mysql2 (>= 0.2.6)
Expand All @@ -177,5 +181,5 @@ DEPENDENCIES
vcap_common!
vcap_logging
vcap_stager (= 0.1.3)
vcap_staging (= 0.1.12)
vcap_staging (= 0.1.24)
yajl-ruby (>= 0.7.9)
1 change: 0 additions & 1 deletion cloud_controller/app/controllers/application_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,6 @@ def log_exception(e)
end

def render_cloud_error(e)
log_exception(e)
@error = e
render :status => e.status, :json => e.to_json
end
Expand Down
26 changes: 16 additions & 10 deletions cloud_controller/app/controllers/apps_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -53,18 +53,21 @@ def valid_upload_path?(path)
end

def get_uploaded_file
file = nil
if CloudController.use_nginx
path = params[:application_path]
if not valid_upload_path?(path)
CloudController.logger.warn "Illegal path: #{path}, passed to cloud_controller
something is badly misconfigured or insecure!!!"
raise CloudError.new(CloudError::FORBIDDEN)
end
wrapper_class = Class.new do
attr_accessor :path
if path != nil
if not valid_upload_path?(path)
CloudController.logger.warn "Illegal path: #{path}, passed to cloud_controller
something is badly misconfigured or insecure!!!"
raise CloudError.new(CloudError::FORBIDDEN)
end
wrapper_class = Class.new do
attr_accessor :path
end
file = wrapper_class.new
file.path = path
end
file = wrapper_class.new
file.path = path
else
file = params[:application]
end
Expand All @@ -78,8 +81,11 @@ def upload
resources = json_param(:resources)
package = AppPackage.new(@app, file, resources)
@app.latest_bits_from(package)
rescue AppPackageError => e
CloudController.logger.error(e)
raise CloudError.new(CloudError::RESOURCES_PACKAGING_FAILED, e.to_s)
ensure
FileUtils.rm_f(file.path)
FileUtils.rm_f(file.path) if file
end
render :nothing => true, :status => 200
end
Expand Down
3 changes: 1 addition & 2 deletions cloud_controller/app/controllers/user_tokens_controller.rb
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
class UserTokensController < ApplicationController
skip_before_filter :fetch_user_from_token, :only => :create

def create
email = params['email']
password = body_params[:password]
if ::User.valid_login?(email, password)
if ::User.valid_login?(email, password) || (@current_user && @current_user.admin?)
# This could just check the ::User.admins variable, but using this method to support changes in admin? in the future
user = ::User.find_by_email(email)
if AppConfig[:https_required] or (user.admin? and AppConfig[:https_required_for_admins])
Expand Down
12 changes: 10 additions & 2 deletions cloud_controller/app/controllers/users_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,16 @@ def update
end

def info
# FIXME, make sure request matches logged in user!
render :json => { :email => user.email }
target_user = ::User.find_by_email(params['email'])
if target_user
if target_user.email == user.email || @current_user.admin?
render :json => { :email => target_user.email }
else
raise CloudError.new(CloudError::FORBIDDEN)
end
else
raise CloudError.new(CloudError::USER_NOT_FOUND)
end
end

def list
Expand Down
79 changes: 51 additions & 28 deletions cloud_controller/app/models/app_package.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
class AppPackageError < StandardError
end

class AppPackage
# This is called with an instance of ActionDispatch::Http::UploadedFile
# but anything that responds to 'path' with a pathname to a zip file is OK.
Expand Down Expand Up @@ -36,28 +39,21 @@ def to_zip
def self.repack_app_in(dir, tmpdir, format)
if format == :zip
target_path = File.join(tmpdir, 'app.zip')
cmd = "cd #{dir}; zip -q -y #{target_path} -r *"
cmd = "cd #{dir}; zip -q -y #{target_path} -r * 2>&1"
else
target_path = File.join(tmpdir, 'app.tgz')
cmd = "cd #{dir}; COPYFILE_DISABLE=true tar -czf #{target_path} *"
cmd = "cd #{dir}; COPYFILE_DISABLE=true tar -czf #{target_path} * 2>&1"
end

timed_section(CloudController.logger, 'repack_app') do
f = Fiber.current
opts = {
:logger => CloudController.logger,
:nobacktrace => true,
:callback => proc { f.resume }
}
VCAP.defer(opts) do
AppPackage.blocking_defer do
output = `#{cmd}`
if $? != 0
target_path = nil
FileUtils.rm_rf(tmpdir)
CloudController.logger.warn("Unable to repack application in #{dir}: #{output} #{$?}")
raise AppPackageError, "Failed repacking application"
end
end
Fiber.yield
end
target_path
end
Expand All @@ -71,6 +67,35 @@ def self.package_dir
end
end

# Yields the current fiber until the supplied block completes execution.
# Propagates any exceptions raised inside the block back to the "calling"
# fiber.
def self.blocking_defer(&blk)
f = Fiber.current

# Executed in a thread reserved by EM for deferred ops
deferred_proc = Proc.new do
begin
retval = blk.call
[:success, retval]
rescue => e
[:error, e]
end
end

# Executed on the main event loop
callback = Proc.new {|result| f.resume(result) }

EM.defer(deferred_proc, callback)

status, retval = Fiber.yield
if status == :success
retval
else
raise retval
end
end

private

def package_dir
Expand All @@ -89,38 +114,36 @@ def unpack_upload
if @uploaded_file
cmd = "unzip -q -d #{working_dir} #{@uploaded_file.path}"
f = Fiber.current
EM.system(cmd) { |output, status|
if status.exitstatus != 0
FileUtils.rm_rf(working_dir)
working_dir = nil
end
f.resume
}
Fiber.yield
EM.system(cmd) do |output, status|
FileUtils.rm_rf(working_dir) if status.exitstatus != 0
f.resume({:status => status, :output => output})
end
unzip_result = Fiber.yield
if unzip_result[:status].exitstatus != 0
CloudController.logger.error("'#{cmd}' exited with status #{unzip_result[:status].exitstatus}")
CloudController.logger.error("Output: '#{unzip_result[:output]}'")
raise AppPackageError, "Failed unzipping application"
end
end
raise "Unable to unpack upload from #{@uploaded_file.path}" if working_dir.nil?
working_dir
end

# Do resource pool synch, needs to be called with a Fiber context
def synchronize_pool_with(working_dir)
timed_section(CloudController.logger, 'process_app_resources') do
f = Fiber.current
opts = {
:logger => CloudController.logger,
:nobacktrace => true,
:callback => proc { f.resume }
}
VCAP.defer(opts) do
AppPackage.blocking_defer do
pool = CloudController.resource_pool
pool.add_directory(working_dir)
@resource_descriptors.each do |descriptor|
target = File.join(working_dir, descriptor[:fn])
pool.copy(descriptor, target)
end
end
Fiber.yield
end
rescue => e
CloudController.logger.error("Failed synchronizing resource pool with '#{working_dir}'")
CloudController.logger.error(e)
raise AppPackageError, "Failed synchronizing resource pool"
end

def bad_resources?
Expand Down
4 changes: 2 additions & 2 deletions cloud_controller/app/models/user_token.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@
class UserToken
class DecodeError < ArgumentError;end
class << self
attr_accessor :token_key
attr_accessor :token_key, :token_expire

def create(user_name)
valid_until = (Time.now.utc + 1.week).to_i
valid_until = (Time.now.utc + token_expire).to_i
new(user_name, valid_until)
end

Expand Down
Loading

0 comments on commit 6df3fa3

Please sign in to comment.