Class: Bosh::Cli::ReleaseCompiler

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

Overview

Compiles release tarball based on manifest

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(manifest_file, artifacts_dir, blobstore, package_matches, release_source) ⇒ ReleaseCompiler

Returns a new instance of ReleaseCompiler.

Parameters:

  • manifest_file (String)

    Release manifest path

  • blobstore (Bosh::Blobstore::Client)

    Blobstore client

  • package_matches (Array)

    List of package checksums that director can match

  • release_source (String)

    Release directory



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/cli/release_compiler.rb', line 18

def initialize(manifest_file, artifacts_dir, blobstore, package_matches, release_source)
  @blobstore = blobstore
  @release_source = release_source
  @manifest_path = File.expand_path(manifest_file, @release_source)
  @tarball_path = nil

  @artifacts_dir = artifacts_dir
  @build_dir = Dir.mktmpdir
  @jobs_dir = File.join(@build_dir, "jobs")
  @packages_dir = File.join(@build_dir, "packages")

  @package_matches = Set.new(package_matches)

  FileUtils.mkdir_p(@jobs_dir)
  FileUtils.mkdir_p(@packages_dir)

  @manifest = load_yaml_file(manifest_file)

  @name = @manifest["name"]
  @version = @manifest["version"]
  @packages = @manifest.fetch("packages", []).map { |pkg| OpenStruct.new(pkg) }
  @jobs = @manifest.fetch("jobs", []).map { |job| OpenStruct.new(job) }
  @license = @manifest["license"] ? OpenStruct.new(@manifest["license"]) : nil
end

Instance Attribute Details

#tarball_pathObject



69
70
71
72
# File 'lib/cli/release_compiler.rb', line 69

def tarball_path
  @tarball_path || File.join(File.dirname(@manifest_path),
                             "#{@name}-#{@version}.tgz")
end

Class Method Details

.compile(manifest_file, artifacts_dir, blobstore, package_matches, release_source) ⇒ Object



9
10
11
# File 'lib/cli/release_compiler.rb', line 9

def self.compile(manifest_file, artifacts_dir, blobstore, package_matches, release_source)
  new(manifest_file, artifacts_dir, blobstore, package_matches, release_source).compile
end

Instance Method Details

#artifact_from(resource, type) ⇒ Object



43
44
45
46
# File 'lib/cli/release_compiler.rb', line 43

def artifact_from(resource, type)
  return nil unless resource
  BuildArtifact.new(resource.name, resource.fingerprint, send(:"find_#{type}", resource), resource.sha1, [], nil, nil)
end

#compileObject



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/cli/release_compiler.rb', line 48

def compile
  if exists?
    quit("You already have this version in '#{tarball_path.make_green}'")
  end

  packages = @packages
    .reject { |package| remote_package_exists?(package) }
    .map { |package| artifact_from(package, :package) }
  jobs = @jobs
    .reject { |job| remote_job_exists?(job) }
    .map { |job| artifact_from(job, :job) }
  license = artifact_from(@license, :license)

  archiver = ReleaseArchiver.new(tarball_path, @manifest_path, packages, jobs, license)
  archiver.build
end

#exists?Boolean

Returns:

  • (Boolean)


65
66
67
# File 'lib/cli/release_compiler.rb', line 65

def exists?
  File.exists?(tarball_path)
end

#find_in_indices(final_index, dev_index, build, build_type) ⇒ Object



104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
# File 'lib/cli/release_compiler.rb', line 104

def find_in_indices(final_index, dev_index, build, build_type)
  desc = "#{build.name} (#{build.version})"

  index = final_index
  found_build = find_version_by_sha1(index, build.sha1)

  if found_build.nil?
    index = dev_index
    found_build = find_version_by_sha1(index, build.sha1)
  end

  if found_build.nil?
    say("MISSING".make_red)
    err("Cannot find #{build_type} with checksum '#{build.sha1}'")
  end

  sha1 = found_build["sha1"]
  blobstore_id = found_build["blobstore_id"]

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

  resolver = Versions::VersionFileResolver.new(storage, @blobstore)
  resolver.find_file(blobstore_id, sha1, "#{build_type} #{desc}")
rescue Bosh::Blobstore::BlobstoreError => e
  raise BlobstoreError, "Blobstore error: #{e}"
end

#find_job(job) ⇒ Object



83
84
85
86
87
88
89
90
# File 'lib/cli/release_compiler.rb', line 83

def find_job(job)
  name = job.name
  final_jobs_dir = File.join(@release_source, '.final_builds', 'jobs', name)
  final_index = Versions::VersionsIndex.new(final_jobs_dir)
  dev_jobs_dir = File.join(@release_source, '.dev_builds', 'jobs', name)
  dev_index = Versions::VersionsIndex.new(dev_jobs_dir)
  find_in_indices(final_index, dev_index, job, 'job')
end

#find_license(license) ⇒ Object



92
93
94
95
96
97
98
# File 'lib/cli/release_compiler.rb', line 92

def find_license(license)
  final_dir = File.join(@release_source, '.final_builds', 'license')
  final_index = Versions::VersionsIndex.new(final_dir)
  dev_dir = File.join(@release_source, '.dev_builds', 'license')
  dev_index = Versions::VersionsIndex.new(dev_dir)
  find_in_indices(final_index, dev_index, license, 'license')
end

#find_package(package) ⇒ Object



74
75
76
77
78
79
80
81
# File 'lib/cli/release_compiler.rb', line 74

def find_package(package)
  name = package.name
  final_package_dir = File.join(@release_source, '.final_builds', 'packages', name)
  final_index = Versions::VersionsIndex.new(final_package_dir)
  dev_package_dir = File.join(@release_source, '.dev_builds', 'packages', name)
  dev_index = Versions::VersionsIndex.new(dev_package_dir)
  find_in_indices(final_index, dev_index, package, 'package')
end

#find_version_by_sha1(index, sha1) ⇒ Object



100
101
102
# File 'lib/cli/release_compiler.rb', line 100

def find_version_by_sha1(index, sha1)
  index.select{ |_, build| build['sha1'] == sha1 }.values.first
end

#remote_job_exists?(local_job) ⇒ Boolean

Checks if local job is already known remotely

Parameters:

  • local_job (#name, #version)

Returns:

  • (Boolean)


144
145
146
# File 'lib/cli/release_compiler.rb', line 144

def remote_job_exists?(local_job)
  false
end

#remote_package_exists?(local_package) ⇒ Boolean

Checks if local package is already known remotely

Parameters:

  • local_package (#name, #version)

Returns:

  • (Boolean)


134
135
136
137
138
139
# File 'lib/cli/release_compiler.rb', line 134

def remote_package_exists?(local_package)
  # If checksum is known to director we can always match it
  @package_matches.include?(local_package.sha1) ||                     # !!! Needs test coverage
    (local_package.fingerprint &&                                      # !!! Needs test coverage
     @package_matches.include?(local_package.fingerprint))             # !!! Needs test coverage
end