Class: Bosh::Cli::VersionFileResolver

Inherits:
Object
  • Object
show all
Defined in:
lib/cli/versions/version_file_resolver.rb

Instance Method Summary collapse

Constructor Details

#initialize(storage, blobstore, tmpdir = Dir.tmpdir) ⇒ VersionFileResolver

Returns a new instance of VersionFileResolver.



4
5
6
7
8
# File 'lib/cli/versions/version_file_resolver.rb', line 4

def initialize(storage, blobstore, tmpdir=Dir.tmpdir)
  @storage = storage
  @blobstore = blobstore
  @tmpdir = tmpdir
end

Instance Method Details

#find_file(blobstore_id, sha1, version, desc) ⇒ Object



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/cli/versions/version_file_resolver.rb', line 10

def find_file(blobstore_id, sha1, version, desc)
  if @storage.has_file?(version)
    say('FOUND LOCAL'.make_green)
    file_path = @storage.get_file(version)
    file_sha1 = Digest::SHA1.file(file_path).hexdigest
    if file_sha1 == sha1
      return file_path
    end
    say("SHA1 `#{file_sha1}' of #{desc} does not match expected SHA1 `#{sha1}'".make_red)
  end

  if blobstore_id.nil?
    err("Cannot find #{desc}")
  end

  say('FOUND REMOTE'.make_yellow)
  say("Downloading #{desc} from blobstore (id=#{blobstore_id})...".make_green)

  tmp_file_path = File.join(@tmpdir, "bosh-tmp-file-#{SecureRandom.uuid}")
  begin
    File.open(tmp_file_path, 'w') do |tmp_file|
      @blobstore.get(blobstore_id, tmp_file)
    end

    file_sha1 = Digest::SHA1.file(tmp_file_path).hexdigest
    if file_sha1 != sha1
      err("SHA1 `#{file_sha1}' of #{desc} from blobstore (id=#{blobstore_id}) " +
        "does not match expected SHA1 `#{sha1}'" +
        'please remove it manually and re-create the release')
    end

    @storage.put_file(version, tmp_file_path)
  ensure
    FileUtils.rm(tmp_file_path, :force => true)
  end
end