class Bosh::Director::Api::ResourceManager

Public Class Methods

new(blobstore_client=App.instance.blobstores.blobstore) click to toggle source
# File lib/bosh/director/api/resource_manager.rb, line 9
def initialize(blobstore_client=App.instance.blobstores.blobstore)
  @logger = Config.logger
  @blobstore_client = blobstore_client
end

Public Instance Methods

clean_old_tmpfiles() click to toggle source

Deletes all get_resource_path temporary files that are more than 5 minutes old.

# File lib/bosh/director/api/resource_manager.rb, line 41
def clean_old_tmpfiles
  Dir.glob("#{resource_tmpdir}/resource-*").
      select{|f| File.mtime(f) < (Time.now - (60*5)) }.
      each{|f| FileUtils.rm_f(f) }
end
delete_resource(id) click to toggle source

Deletes the resource `id` from the blobstore @param [String] id

# File lib/bosh/director/api/resource_manager.rb, line 64
def delete_resource(id)
  @logger.debug("Deleting #{id} from blobstore...")

  blobstore_resource(id) do |blobstore|
    blobstore.delete(id)
  end

  @logger.debug("Deleted #{id} from blobstore")
end
get_resource(id) click to toggle source

Retrieves the resource `id` from the blobstore and returns the contents of it. @param [String] id @return [String] contents of the blobstore id

# File lib/bosh/director/api/resource_manager.rb, line 50
def get_resource(id)
  @logger.debug("Downloading #{id} from blobstore...")

  blob = nil
  blobstore_resource(id) do |blobstore|
    blob = blobstore.get(id)
  end

  @logger.debug("Downloaded #{id} from blobstore")
  blob
end
get_resource_path(id) click to toggle source

Retrieves the resource `id` from the blobstore and stores it locally, and returns the path to the file. It is the caller's responsibility to delete the resulting file at some point. An easy option is to call clean_old_tmpfiles before each call to this method.

@param [String] id @return [String] path to the contents of the blobstore id

# File lib/bosh/director/api/resource_manager.rb, line 22
def get_resource_path(id)
  blobstore_resource(id) do |blobstore|
    random_name = "resource-#{SecureRandom.uuid}"
    path = File.join(resource_tmpdir, random_name)

    File.open(path, "w") do |f|
      blobstore.get(id, f)
    end

    path
  end
end
resource_tmpdir() click to toggle source

Returns the directory where files created by get_resource_path will be written.

# File lib/bosh/director/api/resource_manager.rb, line 36
def resource_tmpdir
  Dir.tmpdir
end

Private Instance Methods

blobstore_resource(id) { |blobstore_client| ... } click to toggle source
# File lib/bosh/director/api/resource_manager.rb, line 76
def blobstore_resource(id)
  yield @blobstore_client
rescue Bosh::Blobstore::NotFound
  raise ResourceNotFound, "Resource '#{id}' not found in the blobstore"
rescue Bosh::Blobstore::BlobstoreError => e
  raise ResourceError, "Blobstore error accessing resource '#{id}': #{e}"
end