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, blobstore, package_matches = [], release_dir = nil) ⇒ ReleaseCompiler

Returns a new instance of ReleaseCompiler.

Parameters:

  • manifest_file (String)

    Release manifest path

  • blobstore (Bosh::Blobstore::Client)

    Blobstore client

  • package_matches (Array) (defaults to: [])

    List of package checksums that director can match

  • release_dir (String) (defaults to: nil)

    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, blobstore,
               package_matches = [], release_dir = nil)

  @blobstore = blobstore
  @release_dir = release_dir || Dir.pwd
  @manifest_file = File.expand_path(manifest_file, @release_dir)
  @tarball_path = nil

  @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["packages"].map { |pkg| OpenStruct.new(pkg) }
  @jobs = @manifest["jobs"].map { |job| OpenStruct.new(job) }
end

Instance Attribute Details

#tarball_pathObject



91
92
93
94
# File 'lib/cli/release_compiler.rb', line 91

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

Class Method Details

.compile(manifest_file, blobstore) ⇒ Object



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

def self.compile(manifest_file, blobstore)
  new(manifest_file, blobstore).compile
end

Instance Method Details

#compileObject



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/cli/release_compiler.rb', line 43

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

  FileUtils.cp(@manifest_file, File.join(@build_dir, "release.MF"), :preserve => true)

  header("Copying packages")
  @packages.each do |package|
    say("#{package.name} (#{package.version})".ljust(30), " ")
    if remote_package_exists?(package)
      say("SKIP".make_yellow)
      next
    end
    package_file_path = find_package(package)
    FileUtils.cp(package_file_path,
                 File.join(@packages_dir, "#{package.name}.tgz"),
                 :preserve => true)
  end

  header("Copying jobs")
  @jobs.each do |job|
    say("#{job.name} (#{job.version})".ljust(30), " ")
    if remote_job_exists?(job)
      say("SKIP".make_yellow)
      next
    end
    job_file_path = find_job(job)
    FileUtils.cp(job_file_path,
                 File.join(@jobs_dir, "#{job.name}.tgz"),
                 :preserve => true)
  end

  header("Building tarball")
  Dir.chdir(@build_dir) do
    tar_out = `tar -czf #{tarball_path} . 2>&1`
    unless $?.exitstatus == 0
      raise InvalidRelease, "Cannot create release tarball: #{tar_out}"
    end
    say("Generated #{tarball_path.make_green}")
    say("Release size: #{pretty_size(tarball_path).make_green}")
  end
end

#exists?Boolean

Returns:

  • (Boolean)


87
88
89
# File 'lib/cli/release_compiler.rb', line 87

def exists?
  File.exists?(tarball_path)
end

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



116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
# File 'lib/cli/release_compiler.rb', line 116

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

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

  storage = LocalVersionStorage.new(index.storage_dir)

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

#find_job(job) ⇒ Object



104
105
106
107
108
109
110
# File 'lib/cli/release_compiler.rb', line 104

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

#find_package(package) ⇒ Object



96
97
98
99
100
101
102
# File 'lib/cli/release_compiler.rb', line 96

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

#find_version_by_sha1(index, sha1) ⇒ Object



112
113
114
# File 'lib/cli/release_compiler.rb', line 112

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)


157
158
159
# File 'lib/cli/release_compiler.rb', line 157

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)


147
148
149
150
151
152
# File 'lib/cli/release_compiler.rb', line 147

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