Class: Bosh::Director::Api::ResourceManager

Inherits:
Object
  • Object
show all
Defined in:
lib/bosh/director/api/resource_manager.rb

Instance Method Summary collapse

Constructor Details

#initialize(blobstore_client = App.instance.blobstores.blobstore) ⇒ ResourceManager

Returns a new instance of ResourceManager.



9
10
11
12
# 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

Instance Method Details

#clean_old_tmpfilesObject

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



41
42
43
44
45
# 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) ⇒ Object

Deletes the resource ‘id` from the blobstore

Parameters:

  • id (String)


64
65
66
67
68
69
70
71
72
# 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) ⇒ String

Retrieves the resource ‘id` from the blobstore and returns the contents of it.

Parameters:

  • id (String)

Returns:

  • (String)

    contents of the blobstore id



50
51
52
53
54
55
56
57
58
59
60
# 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) ⇒ String

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.

Parameters:

  • id (String)

Returns:

  • (String)

    path to the contents of the blobstore id



22
23
24
25
26
27
28
29
30
31
32
33
# 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_tmpdirObject

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



36
37
38
# File 'lib/bosh/director/api/resource_manager.rb', line 36

def resource_tmpdir
  Dir.tmpdir
end