Class: Bosh::Cli::ReleaseBuilder

Inherits:
Object
  • Object
show all
Includes:
DependencyHelper
Defined in:
lib/cli/release_builder.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from DependencyHelper

#tsort_packages

Constructor Details

#initialize(release, package_artifacts, job_artifacts, license_artifact, name, options = { }) ⇒ ReleaseBuilder

Returns a new instance of ReleaseBuilder

Parameters:


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
# File 'lib/cli/release_builder.rb', line 21

def initialize(release, package_artifacts, job_artifacts, license_artifact, name, options = { })
  @release = release
  @final = options.has_key?(:final) ? !!options[:final] : false
  @commit_hash = options.fetch(:commit_hash, '00000000')
  @uncommitted_changes = options.fetch(:uncommitted_changes, true)
  @packages = package_artifacts # todo
  @jobs = job_artifacts # todo
  @license = license_artifact
  @name = name
  raise 'Release name is blank' if name.blank?

  @timestamp_version = options.fetch(:'timestamp_version', false)
  @version = options.fetch(:version, nil)

  @final_index = Versions::VersionsIndex.new(final_releases_dir)
  @dev_index = Versions::VersionsIndex.new(dev_releases_dir)
  @index = @final ? @final_index : @dev_index
  @release_storage = Versions::LocalArtifactStorage.new(@index.storage_dir)

  if @version && @release_storage.has_file?(release_filename)
    raise ReleaseVersionError.new('Release version already exists')
  end

  @build_dir = Dir.mktmpdir

  in_build_dir do
    FileUtils.mkdir("packages")
    FileUtils.mkdir("jobs")
  end
end

Instance Attribute Details

#build_dirObject (readonly)

Returns the value of attribute build_dir


5
6
7
# File 'lib/cli/release_builder.rb', line 5

def build_dir
  @build_dir
end

#commit_hashObject (readonly)

Returns the value of attribute commit_hash


5
6
7
# File 'lib/cli/release_builder.rb', line 5

def commit_hash
  @commit_hash
end

#jobsObject (readonly)

Returns the value of attribute jobs


5
6
7
# File 'lib/cli/release_builder.rb', line 5

def jobs
  @jobs
end

#licenseObject (readonly)

Returns the value of attribute license


5
6
7
# File 'lib/cli/release_builder.rb', line 5

def license
  @license
end

#nameObject (readonly)

Returns the value of attribute name


5
6
7
# File 'lib/cli/release_builder.rb', line 5

def name
  @name
end

#packagesObject (readonly)

Returns the value of attribute packages


5
6
7
# File 'lib/cli/release_builder.rb', line 5

def packages
  @packages
end

#releaseObject (readonly)

Returns the value of attribute release


5
6
7
# File 'lib/cli/release_builder.rb', line 5

def release
  @release
end

#uncommitted_changesObject (readonly)

Returns the value of attribute uncommitted_changes


5
6
7
# File 'lib/cli/release_builder.rb', line 5

def uncommitted_changes
  @uncommitted_changes
end

#versionString (readonly)

Returns Release version

Returns:

  • (String)

    Release version


53
54
55
# File 'lib/cli/release_builder.rb', line 53

def version
  @version
end

Instance Method Details

#affected_jobsArray<Bosh::Cli::BuildArtifact>

Returns List of job artifacts affected by this release compared to the previous one.

Returns:


68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/cli/release_builder.rb', line 68

def affected_jobs
  result = Set.new(@jobs.select { |job_artifact| job_artifact.new_version? })
  return result.to_a if @packages.empty?

  new_package_names = @packages.map do |package_artifact|
    package_artifact.name if package_artifact.new_version?
  end.compact

  @jobs.each do |job|
    result << job if (new_package_names & job.dependencies).size > 0
  end

  result.to_a
end

#build(options = {}) ⇒ Object

Builds release

Parameters:

  • options (Hash) (defaults to: {})

    Release build options


85
86
87
88
89
90
91
92
93
94
# File 'lib/cli/release_builder.rb', line 85

def build(options = {})
  options = { :generate_tarball => true }.merge(options)

  header("Generating manifest...")
  manifest_path = generate_manifest
  if options[:generate_tarball]
    generate_tarball(manifest_path)
  end
  @build_complete = true
end

#dev_releases_dirObject


170
171
172
# File 'lib/cli/release_builder.rb', line 170

def dev_releases_dir
  File.join(@release.dir, 'dev_releases', @name)
end

#final?Boolean

Returns Is release final?

Returns:

  • (Boolean)

    Is release final?


58
59
60
# File 'lib/cli/release_builder.rb', line 58

def final?
  @final
end

#final_releases_dirObject


166
167
168
# File 'lib/cli/release_builder.rb', line 166

def final_releases_dir
  File.join(@release.dir, 'releases', @name)
end

#generate_manifestObject

Generates release manifest


97
98
99
100
101
102
103
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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
# File 'lib/cli/release_builder.rb', line 97

def generate_manifest
  manifest = {}
  manifest['packages'] = packages.map do |build_artifact|
    {
      'name' => build_artifact.name,
      'version' => build_artifact.version,
      'fingerprint' => build_artifact.fingerprint,
      'sha1' => build_artifact.sha1,
      'dependencies' => build_artifact.dependencies,
    }
  end

  manifest['jobs'] = jobs.map do |build_artifact|
    {
      'name' => build_artifact.name,
      'version' => build_artifact.version,
      'fingerprint' => build_artifact.fingerprint,
      'sha1' => build_artifact.sha1,
    }
  end

  unless @license.nil?
    manifest['license'] = {
      'version' => @license.version,
      'fingerprint' => @license.fingerprint,
      'sha1' => @license.sha1,
    }
  end

  manifest['commit_hash'] = commit_hash
  manifest['uncommitted_changes'] = uncommitted_changes

  unless @name.bosh_valid_id?
    raise InvalidRelease, "Release name '#{@name}' is not a valid BOSH identifier"
  end
  manifest['name'] = @name

  # New release versions are allowed to have the same fingerprint as old versions.
  # For reverse compatibility, random uuids are stored instead.
  @index.add_version(SecureRandom.uuid, { 'version' => version })

  manifest['version'] = version
  manifest_yaml = Psych.dump(manifest)

  say("Writing manifest...")
  File.open(File.join(build_dir, "release.MF"), "w") do |f|
    f.write(manifest_yaml)
  end

  File.open(manifest_path, "w") do |f|
    f.write(manifest_yaml)
  end

  @manifest_generated = true
  manifest_path
end

#generate_tarball(manifest_path = nil) ⇒ Object


154
155
156
157
158
159
160
# File 'lib/cli/release_builder.rb', line 154

def generate_tarball(manifest_path = nil)
  manifest_path ||= generate_manifest
  return if @release_storage.has_file?(release_filename)

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

#manifest_pathObject


178
179
180
# File 'lib/cli/release_builder.rb', line 178

def manifest_path
  File.join(releases_dir, "#{@name}-#{version}.yml")
end

#release_filenameObject


62
63
64
# File 'lib/cli/release_builder.rb', line 62

def release_filename
  "#{@name}-#{@version}.tgz"
end

#releases_dirObject


162
163
164
# File 'lib/cli/release_builder.rb', line 162

def releases_dir
  @final ? final_releases_dir : dev_releases_dir
end

#tarball_pathObject


174
175
176
# File 'lib/cli/release_builder.rb', line 174

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