Class: Bosh::Cli::ArchiveRepository

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

Instance Method Summary collapse

Constructor Details

#initialize(archive_dir, artifacts_dir, blobstore, resource) ⇒ ArchiveRepository

Returns a new instance of ArchiveRepository


3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
# File 'lib/cli/archive_repository.rb', line 3

def initialize(archive_dir, artifacts_dir, blobstore, resource)
  @archive_dir = archive_dir
  @blobstore = blobstore

  dev_builds_dir = Pathname(@archive_dir).join('.dev_builds', resource.plural_type, resource.name).to_s
  FileUtils.mkdir_p(dev_builds_dir)
  @dev_index = Versions::VersionsIndex.new(dev_builds_dir)

  final_builds_dir = Pathname(@archive_dir).join('.final_builds', resource.plural_type, resource.name).to_s
  FileUtils.mkdir_p(final_builds_dir)
  @final_index = Versions::VersionsIndex.new(final_builds_dir)

  @storage = Versions::LocalArtifactStorage.new(artifacts_dir)
  FileUtils.mkdir_p(artifacts_dir)

  @final_resolver = Versions::VersionFileResolver.new(@storage, @blobstore)
end

Instance Method Details

#install(artifact) ⇒ Object


76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/cli/archive_repository.rb', line 76

def install(artifact)
  fingerprint = artifact.fingerprint
  origin_file = artifact.tarball_path

  tarball_path = @storage.put_file(artifact.sha1, origin_file)

  update_index(
    tarball_path,
    fingerprint,
    artifact.dev_artifact? ? @dev_index : @final_index)

  BuildArtifact.new(artifact.name, artifact.fingerprint, tarball_path, artifact.sha1, artifact.dependencies, artifact.new_version?, artifact.dev_artifact?)
end

#lookup(resource) ⇒ Object


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
46
47
48
49
50
51
52
53
54
55
# File 'lib/cli/archive_repository.rb', line 21

def lookup(resource)
  fingerprint = BuildArtifact.make_fingerprint(resource)

  artifact_info = @final_index[fingerprint]
  if artifact_info && artifact_info['blobstore_id']
    blobstore_id = artifact_info['blobstore_id']
    version = artifact_info['version'] || fingerprint
    sha1 = artifact_info['sha1']

    say("Using final version '#{version}'")
    tarball_path = @final_resolver.find_file(blobstore_id, sha1, "#{resource.singular_type} #{resource.name} (#{version})")

    BuildArtifact.new(resource.name, fingerprint, tarball_path, sha1, resource.dependencies, false, false)
  else
    artifact_info = @dev_index[fingerprint]
    if artifact_info
      if @storage.has_file?(artifact_info['sha1'])
        version = artifact_info['version'] || fingerprint
        say("Using dev version '#{version}'")

        tarball_path = @storage.get_file(artifact_info['sha1'])
        if file_checksum(tarball_path) != artifact_info['sha1']
          raise CorruptedArchive, "#{resource.singular_type} #{resource.name} (#{version}) archive at #{tarball_path} corrupted"
        end

        BuildArtifact.new(resource.name, fingerprint, tarball_path, artifact_info['sha1'], resource.dependencies, false, true)
      end
    end
  end

rescue Bosh::Blobstore::NotFound => e
  raise BlobstoreError, "Final version of '#{resource.name}' not found in blobstore: #{e}"
rescue Bosh::Blobstore::BlobstoreError => e
  raise BlobstoreError, "Blobstore error: #{e}"
end

#promote_from_dev_to_final(artifact) ⇒ Object


90
91
92
93
# File 'lib/cli/archive_repository.rb', line 90

def promote_from_dev_to_final(artifact)
  update_index(artifact.tarball_path, artifact.fingerprint, @final_index)
  artifact.promote_to_final
end

#upload_to_blobstore(artifact) ⇒ Object


57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/cli/archive_repository.rb', line 57

def upload_to_blobstore(artifact)
  artifact_info = @final_index[artifact.fingerprint]
  # todo raise if artifact.dev_artifact?
  return artifact, artifact_info['blobstore_id'] if artifact_info['blobstore_id']

  blobstore_id = nil
  File.open(artifact.tarball_path, 'r') do |f|
    blobstore_id = @blobstore.create(f)
  end

  @final_index.update_version(artifact.fingerprint, {
      'version' => artifact.version,
      'sha1' => artifact.sha1,
      'blobstore_id' => blobstore_id
    })
  artifact = BuildArtifact.new(artifact.name, artifact.fingerprint, artifact.tarball_path, artifact.sha1, artifact.dependencies, artifact.new_version?, false)
  return artifact, blobstore_id
end