Permalink
Browse files

made ResourcePool a class

Change-Id: I739f3dc04a36a2ea1ce53560664f13e75052a917
  • Loading branch information...
1 parent 16e4126 commit c69aef40c137b77b6c61f1e8699dda6aa046cb2c @d d committed Feb 7, 2013
@@ -2,12 +2,11 @@
module VCAP::CloudController
class ResourceMatch < RestController::Base
-
def match
return NotAuthorized unless user
# TODO: replace with json_message
descriptors = Yajl::Parser.parse(body)
- matched = ResourcePool.match_resources(descriptors)
+ matched = ResourcePool.instance.match_resources(descriptors)
Yajl::Encoder.encode(matched)
end
@@ -114,7 +114,7 @@ def validate_package_size(uploaded_file, resources)
# Ugh, this stat's all the files that would need to be copied
# from the resource pool. Consider caching sizes in resource pool?
- sizes = ResourcePool.resource_sizes(resources)
+ sizes = ResourcePool.instance.resource_sizes(resources)
total_size += sizes.reduce(0) {|accum, cur| accum + cur["size"] }
validate_size(total_size)
end
@@ -176,11 +176,11 @@ def resolve_path(working_dir, tainted_path)
# Do resource pool synch
def synchronize_pool_with(working_dir, resource_descriptors)
- ResourcePool.add_directory(working_dir)
+ ResourcePool.instance.add_directory(working_dir)
resource_descriptors.each do |descriptor|
create_dir_skeleton(working_dir, descriptor["fn"])
path = resolve_path(working_dir, descriptor["fn"])
- ResourcePool.copy(descriptor, path)
+ ResourcePool.instance.copy(descriptor, path)
end
rescue => e
logger.error "failed synchronizing resource pool with '#{working_dir}' #{e}"
@@ -147,7 +147,8 @@ def self.configure(config)
VCAP::CloudController::MessageBus.instance = mbus
VCAP::CloudController::AccountCapacity.configure(config)
- VCAP::CloudController::ResourcePool.configure(config)
+ VCAP::CloudController::ResourcePool.instance =
+ VCAP::CloudController::ResourcePool.new(config)
VCAP::CloudController::AppPackage.configure(config)
VCAP::CloudController::AppStager.configure(config)
VCAP::CloudController::LegacyStaging.configure(config)
@@ -10,122 +10,124 @@
require "steno"
class VCAP::CloudController::ResourcePool
- class << self
- attr_accessor :minimum_size, :maximum_size
-
- def configure(config = {})
- opts = config[:resource_pool] || {}
- @connection_config = opts[:fog_connection]
- @resource_directory_key = opts[:resource_directory_key] || "cc-resources"
- @minimum_size = opts[:minimum_size] || 0
- @maximum_size = opts[:maximum_size] || 512 * 1024 * 1024 # MB
- end
+ attr_accessor :minimum_size, :maximum_size
- def match_resources(descriptors)
- descriptors.select { |h| resource_known?(h) }
- end
-
- # Adds everything under source directory +dir+ to the resource pool.
- def add_directory(dir)
- unless File.exists?(dir) && File.directory?(dir)
- raise ArgumentError, "Source directory #{dir} is not valid"
- end
+ class << self
+ attr_accessor :instance
+ end
- pattern = File.join(dir, "**", "*")
- files = Dir.glob(pattern, File::FNM_DOTMATCH).select do |f|
- resource_allowed?(f)
- end
+ def initialize(config = {})
+ opts = config[:resource_pool] || {}
+ @connection_config = opts[:fog_connection]
+ @resource_directory_key = opts[:resource_directory_key] || "cc-resources"
+ @minimum_size = opts[:minimum_size] || 0
+ @maximum_size = opts[:maximum_size] || 512 * 1024 * 1024 # MB
+ end
- files.each do |path|
- add_path(path)
- end
- end
+ def match_resources(descriptors)
+ descriptors.select { |h| resource_known?(h) }
+ end
- def add_path(path)
- sha1 = Digest::SHA1.file(path).hexdigest
- key = key_from_sha1(sha1)
- return if resource_dir.files.head(sha1)
-
- File.open(path) do |file|
- resource_dir.files.create(
- :key => key,
- :body => file,
- :public => false,
- )
- end
+ # Adds everything under source directory +dir+ to the resource pool.
+ def add_directory(dir)
+ unless File.exists?(dir) && File.directory?(dir)
+ raise ArgumentError, "Source directory #{dir} is not valid"
end
- def resource_sizes(resources)
- sizes = []
- resources.each do |descriptor|
- key = key_from_sha1(descriptor["sha1"])
- if head = resource_dir.files.head(key)
- entry = descriptor.dup
- entry["size"] = head.content_length
- sizes << entry
- end
- end
- sizes
+ pattern = File.join(dir, "**", "*")
+ files = Dir.glob(pattern, File::FNM_DOTMATCH).select do |f|
+ resource_allowed?(f)
end
- def copy(descriptor, destination)
- if resource_known?(descriptor)
- logger.debug "resource pool sync #{descriptor}"
- overwrite_destination_with!(descriptor, destination)
- else
- logger.warn "resource pool sync error #{descriptor}"
- raise ArgumentError, "Can not copy bits we do not have #{descriptor}"
- end
+ files.each do |path|
+ add_path(path)
end
+ end
- private
+ def add_path(path)
+ sha1 = Digest::SHA1.file(path).hexdigest
+ key = key_from_sha1(sha1)
+ return if resource_dir.files.head(sha1)
- def logger
- @logger ||= Steno.logger("cc.resource_pool")
+ File.open(path) do |file|
+ resource_dir.files.create(
+ :key => key,
+ :body => file,
+ :public => false,
+ )
end
+ end
- def resource_known?(descriptor)
- size = descriptor["size"]
- if size_allowed?(size)
- key = key_from_sha1(descriptor["sha1"])
- resource_dir.files.head(key)
+ def resource_sizes(resources)
+ sizes = []
+ resources.each do |descriptor|
+ key = key_from_sha1(descriptor["sha1"])
+ if head = resource_dir.files.head(key)
+ entry = descriptor.dup
+ entry["size"] = head.content_length
+ sizes << entry
end
end
+ sizes
+ end
- def resource_allowed?(path)
- stat = File.stat(path)
- File.file?(path) && !stat.symlink? && size_allowed?(stat.size)
+ def copy(descriptor, destination)
+ if resource_known?(descriptor)
+ logger.debug "resource pool sync #{descriptor}"
+ overwrite_destination_with!(descriptor, destination)
+ else
+ logger.warn "resource pool sync error #{descriptor}"
+ raise ArgumentError, "Can not copy bits we do not have #{descriptor}"
end
+ end
- def size_allowed?(size)
- size && size > minimum_size && size < maximum_size
- end
+ private
- # Called after we sanity-check the input.
- # Create a new path on disk containing the resource described by +descriptor+
- def overwrite_destination_with!(descriptor, destination)
- FileUtils.mkdir_p File.dirname(destination)
- s3_key = key_from_sha1(descriptor["sha1"])
- s3_file = resource_dir.files.get(s3_key)
- File.open(destination, "w") do |file|
- file.write(s3_file.body)
- end
- end
+ def logger
+ @logger ||= Steno.logger("cc.resource_pool")
+ end
- def connection
- Fog::Storage.new(@connection_config)
+ def resource_known?(descriptor)
+ size = descriptor["size"]
+ if size_allowed?(size)
+ key = key_from_sha1(descriptor["sha1"])
+ resource_dir.files.head(key)
end
+ end
- def resource_dir
- @directory ||= connection.directories.create(
- :key => @resource_directory_key,
- :public => false,
- )
- end
+ def resource_allowed?(path)
+ stat = File.stat(path)
+ File.file?(path) && !stat.symlink? && size_allowed?(stat.size)
+ end
+
+ def size_allowed?(size)
+ size && size > minimum_size && size < maximum_size
+ end
- def key_from_sha1(sha1)
- sha1 = sha1.to_s.downcase
- File.join(sha1[0..1], sha1[2..3], sha1)
+ # Called after we sanity-check the input.
+ # Create a new path on disk containing the resource described by +descriptor+
+ def overwrite_destination_with!(descriptor, destination)
+ FileUtils.mkdir_p File.dirname(destination)
+ s3_key = key_from_sha1(descriptor["sha1"])
+ s3_file = resource_dir.files.get(s3_key)
+ File.open(destination, "w") do |file|
+ file.write(s3_file.body)
end
end
+
+ def connection
+ Fog::Storage.new(@connection_config)
+ end
+
+ def resource_dir
+ @directory ||= connection.directories.create(
+ :key => @resource_directory_key,
+ :public => false,
+ )
+ end
+
+ def key_from_sha1(sha1)
+ sha1 = sha1.to_s.downcase
+ File.join(sha1[0..1], sha1[2..3], sha1)
+ end
end
@@ -4,10 +4,10 @@
module VCAP::CloudController
describe VCAP::CloudController::ResourceMatch do
- include_context "resource pool", ResourcePool
+ include_context "resource pool"
before(:all) do
- ResourcePool.add_directory(@tmpdir)
+ @resource_pool.add_directory(@tmpdir)
end
describe "POST /resources" do
@@ -7,7 +7,7 @@ module VCAP::CloudController
include_context "resource pool"
before(:all) do
- ResourcePool.add_directory(@tmpdir)
+ @resource_pool.add_directory(@tmpdir)
end
describe "PUT /v2/resource_match" do
View
@@ -67,7 +67,7 @@ module VCAP::CloudController
tf.write("A" * 1024)
tf.close
- ResourcePool.add_path(tf.path)
+ ResourcePool.instance.add_path(tf.path)
sha1 = Digest::SHA1.file(tf.path).hexdigest
zipname = File.join(tmpdir, "test.zip")
unzipped_size = create_zip(zipname, 1, 1024)
View
@@ -8,21 +8,21 @@ module VCAP::CloudController
describe "#match_resources" do
before(:all) do
- ResourcePool.add_directory(@tmpdir)
+ @resource_pool.add_directory(@tmpdir)
end
it "should return an empty list when no resources match" do
- res = ResourcePool.match_resources([@dummy_descriptor])
+ res = @resource_pool.match_resources([@dummy_descriptor])
res.should == []
end
it "should return a resource that matches" do
- res = ResourcePool.match_resources([@descriptors.first, @dummy_descriptor])
+ res = @resource_pool.match_resources([@descriptors.first, @dummy_descriptor])
res.should == [@descriptors.first]
end
it "should return many resources that match" do
- res = ResourcePool.match_resources(@descriptors + [@dummy_descriptor])
+ res = @resource_pool.match_resources(@descriptors + [@dummy_descriptor])
res.should == @descriptors
end
end
@@ -33,40 +33,40 @@ module VCAP::CloudController
{ "sha1" => d["sha1"] }
end
- res = ResourcePool.resource_sizes(without_sizes)
+ res = @resource_pool.resource_sizes(without_sizes)
res.should == @descriptors
end
end
describe "#add_path" do
it "should walk the fs tree and add only allowable files" do
- ResourcePool.should_receive(:add_path).exactly(@total_allowed_files).times
- ResourcePool.add_directory(@tmpdir)
+ @resource_pool.should_receive(:add_path).exactly(@total_allowed_files).times
+ @resource_pool.add_directory(@tmpdir)
end
end
describe "#size_allowed?" do
before do
@minimum_size = 5
@maximum_size = 7
- ResourcePool.minimum_size = @minimum_size
- ResourcePool.maximum_size = @maximum_size
+ @resource_pool.minimum_size = @minimum_size
+ @resource_pool.maximum_size = @maximum_size
end
it "should return true for a size between min and max size" do
- ResourcePool.send(:size_allowed?, @minimum_size + 1).should be_true
+ @resource_pool.send(:size_allowed?, @minimum_size + 1).should be_true
end
it "should return false for a size < min size" do
- ResourcePool.send(:size_allowed?, @minimum_size - 1).should be_false
+ @resource_pool.send(:size_allowed?, @minimum_size - 1).should be_false
end
it "should return false for a size > max size" do
- ResourcePool.send(:size_allowed?, @maximum_size + 1).should be_false
+ @resource_pool.send(:size_allowed?, @maximum_size + 1).should be_false
end
it "should return false for a nil size" do
- ResourcePool.send(:size_allowed?, nil).should be_false
+ @resource_pool.send(:size_allowed?, nil).should be_false
end
end
end
Oops, something went wrong.

0 comments on commit c69aef4

Please sign in to comment.