class Bosh::Director::BlobUtil

Public Class Methods

copy_blob(blobstore_id) click to toggle source
# File lib/bosh/director/blob_util.rb, line 11
def self.copy_blob(blobstore_id)
  # Create a copy of the given blob
  Dir.mktmpdir do |path|
    temp_path = File.join(path, "blob")
    File.open(temp_path, 'w') do |file|
      blobstore.get(blobstore_id, file)
    end
    File.open(temp_path, 'r') do |file|
      blobstore_id = blobstore.create(file)
    end
  end
  blobstore_id
end
create_blob(path) click to toggle source

@param [String] path Path to a file to be uploaded @return [String] Created blob id

# File lib/bosh/director/blob_util.rb, line 7
def self.create_blob(path)
  File.open(path) { |f| blobstore.create(f) }
end
delete_blob(blobstore_id) click to toggle source
# File lib/bosh/director/blob_util.rb, line 25
def self.delete_blob(blobstore_id)
  blobstore.delete(blobstore_id)
end
exists_in_global_cache?(package, cache_key) click to toggle source
# File lib/bosh/director/blob_util.rb, line 49
def self.exists_in_global_cache?(package, cache_key)
  global_cache_filename = [package.name, cache_key].join('-')
  compiled_package_cache_blobstore.exists?(global_cache_filename)
end
fetch_from_global_cache(package, stemcell, cache_key, dependency_key) click to toggle source
# File lib/bosh/director/blob_util.rb, line 54
def self.fetch_from_global_cache(package, stemcell, cache_key, dependency_key)
  global_cache_filename = [package.name, cache_key].join('-')

  blobstore_id = nil
  compiled_package_sha1 = nil

  Dir.mktmpdir do |path|
    blob_path = File.join(path, 'blob')
    begin
      File.open(blob_path, 'wb') do |file|
        compiled_package_cache_blobstore.get(global_cache_filename, file)
      end
    rescue Bosh::Blobstore::NotFound => e
      # if the object is not found in the cache, we ignore it and return nil
      return nil
    end

    File.open(blob_path) do |file|
      blobstore_id = blobstore.create(file)
      compiled_package_sha1 = Digest::SHA1.file(blob_path).hexdigest
    end
  end

  Models::CompiledPackage.create do |p|
    p.package = package
    p.stemcell_os = stemcell.operating_system
    p.stemcell_version = stemcell.version
    p.sha1 = compiled_package_sha1
    p.build = Models::CompiledPackage.generate_build_number(package, stemcell.operating_system, stemcell.version)
    p.blobstore_id = blobstore_id
    p.dependency_key = dependency_key
  end
end
save_to_global_cache(compiled_package, cache_key) click to toggle source
# File lib/bosh/director/blob_util.rb, line 35
def self.save_to_global_cache(compiled_package, cache_key)
  global_cache_filename = [compiled_package.package.name, cache_key].join('-')
  Dir.mktmpdir do |path|
    temp_path = File.join(path, 'blob')
    File.open(temp_path, 'wb') do |file|
      blobstore.get(compiled_package.blobstore_id, file)
    end

    File.open(temp_path) do |file|
      compiled_package_cache_blobstore.create(file, global_cache_filename)
    end
  end
end
verify_blob(blobstore_id, sha1) click to toggle source
# File lib/bosh/director/blob_util.rb, line 29
def self.verify_blob(blobstore_id, sha1)
  sha1 == Digest::SHA1.hexdigest(blobstore.get(blobstore_id))
rescue Bosh::Blobstore::BlobstoreError => e
  return false
end

Private Class Methods

blobstore() click to toggle source
# File lib/bosh/director/blob_util.rb, line 90
def self.blobstore
  App.instance.blobstores.blobstore
end
compiled_package_cache_blobstore() click to toggle source
# File lib/bosh/director/blob_util.rb, line 94
def self.compiled_package_cache_blobstore
  Config.compiled_package_cache_blobstore
end