Class: Bosh::Cli::ReleaseTarball
- Includes:
- DependencyHelper, Validation
- Defined in:
- lib/cli/release_tarball.rb
Defined Under Namespace
Classes: TarballArtifact
Instance Attribute Summary collapse
-
#jobs ⇒ Object
readonly
Returns the value of attribute jobs.
-
#packages ⇒ Object
readonly
Returns the value of attribute packages.
-
#release_name ⇒ Object
readonly
Returns the value of attribute release_name.
-
#skipped ⇒ Object
readonly
Mostly for tests.
-
#unpack_dir ⇒ Object
readonly
Mostly for tests.
-
#version ⇒ Object
readonly
Returns the value of attribute version.
Instance Method Summary collapse
- #affected_jobs ⇒ Object
-
#all_release_jobs_unpacked? ⇒ Boolean
verifies that all jobs in release manifest were unpacked.
- #compiled_release? ⇒ Boolean
- #convert_to_old_format ⇒ Object
-
#create_from_unpacked(target_path) ⇒ Object
Creates a new tarball from the current contents of @unpack_dir.
- #exists? ⇒ Boolean
-
#handle_dot_slash_prefix(target) ⇒ Object
This will [add or remove] the ‘./’ when trying to extract a specific file from archive.
-
#initialize(tarball_path) ⇒ ReleaseTarball
constructor
A new instance of ReleaseTarball.
- #job_tarball_path(name) ⇒ Object
- #license ⇒ Object
- #license_resource ⇒ Object
- #manifest ⇒ Object
- #manifest_yaml ⇒ Object
- #package_tarball_path(name) ⇒ Object
-
#perform_validation(options = {}) ⇒ Object
If sparse release is allowed we bypass the requirement of having all jobs and packages in place when we do validation.
- #print_manifest ⇒ Object
- #raw_fast_unpack(target) ⇒ Object
- #raw_unpack(target) ⇒ Object
-
#repack(package_matches = []) ⇒ Object
Repacks tarball according to the structure of remote release Return path to repackaged tarball or nil if repack has failed.
- #replace_manifest(hash) ⇒ Object
-
#safe_fast_unpack(target) ⇒ Object
On machines using GNU based tar command, it should be able to unpack files irrespective of the ./ prefix in the file name.
- #safe_unpack(target) ⇒ Object
-
#unpack ⇒ Object
Unpacks tarball to @unpack_dir, returns true if succeeded, false if failed.
- #unpack_jobs ⇒ Object
- #unpack_license ⇒ Object
- #unpack_manifest ⇒ Object
- #upload_packages?(package_matches = []) ⇒ Boolean
- #validate_jobs(options = {}) ⇒ Object
- #validate_manifest ⇒ Object
- #validate_packages(options = {}) ⇒ Object
Methods included from DependencyHelper
Methods included from Validation
#errors, #reset_validation, #valid?, #validate
Constructor Details
#initialize(tarball_path) ⇒ ReleaseTarball
Returns a new instance of ReleaseTarball.
9 10 11 12 13 14 15 16 17 18 19 20 |
# File 'lib/cli/release_tarball.rb', line 9 def initialize(tarball_path) @tarball_path = File.(tarball_path, Dir.pwd) @unpack_dir = Dir.mktmpdir @jobs = [] @packages = [] if compiled_release? @packages_folder = "compiled_packages" else @packages_folder = "packages" end end |
Instance Attribute Details
#jobs ⇒ Object (readonly)
Returns the value of attribute jobs.
6 7 8 |
# File 'lib/cli/release_tarball.rb', line 6 def jobs @jobs end |
#packages ⇒ Object (readonly)
Returns the value of attribute packages.
6 7 8 |
# File 'lib/cli/release_tarball.rb', line 6 def packages @packages end |
#release_name ⇒ Object (readonly)
Returns the value of attribute release_name.
6 7 8 |
# File 'lib/cli/release_tarball.rb', line 6 def release_name @release_name end |
#skipped ⇒ Object (readonly)
Mostly for tests
7 8 9 |
# File 'lib/cli/release_tarball.rb', line 7 def skipped @skipped end |
#unpack_dir ⇒ Object (readonly)
Mostly for tests
7 8 9 |
# File 'lib/cli/release_tarball.rb', line 7 def unpack_dir @unpack_dir end |
#version ⇒ Object (readonly)
Returns the value of attribute version.
6 7 8 |
# File 'lib/cli/release_tarball.rb', line 6 def version @version end |
Instance Method Details
#affected_jobs ⇒ Object
421 422 423 |
# File 'lib/cli/release_tarball.rb', line 421 def affected_jobs [] end |
#all_release_jobs_unpacked? ⇒ Boolean
verifies that all jobs in release manifest were unpacked
95 96 97 98 99 100 101 |
# File 'lib/cli/release_tarball.rb', line 95 def all_release_jobs_unpacked? return false if manifest_yaml['jobs'].nil? manifest_job_names = manifest_yaml['jobs'].map { |j| j['name'] }.sort unpacked_job_file_names = Dir.glob(File.join(@unpack_dir, 'jobs', '*')).map { |f| File.basename(f, '.*') }.sort unpacked_job_file_names == manifest_job_names end |
#compiled_release? ⇒ Boolean
133 134 135 |
# File 'lib/cli/release_tarball.rb', line 133 def compiled_release? manifest_yaml.has_key?('compiled_packages') end |
#convert_to_old_format ⇒ Object
161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 |
# File 'lib/cli/release_tarball.rb', line 161 def convert_to_old_format step('Converting to old format', "Cannot extract tarball #{@tarball_path}", :fatal) do unpack end manifest_file = File.('release.MF', @unpack_dir) manifest = load_yaml_file(manifest_file) old_format_version = Bosh::Common::Version::ReleaseVersion.parse(manifest['version']).to_old_format manifest['version'] = old_format_version write_yaml(manifest, manifest_file) tmpdir = Dir.mktmpdir repacked_path = File.join(tmpdir, 'release-reformat.tgz') Dir.chdir(@unpack_dir) do exit_success = system("tar", "-czf", repacked_path, ".", out: "/dev/null", err: "/dev/null") return repacked_path if exit_success end end |
#create_from_unpacked(target_path) ⇒ Object
Creates a new tarball from the current contents of @unpack_dir
111 112 113 114 |
# File 'lib/cli/release_tarball.rb', line 111 def create_from_unpacked(target_path) raise "Not unpacked yet!" unless @unpacked SortedReleaseArchiver.new(@unpack_dir).archive(File.(target_path)) end |
#exists? ⇒ Boolean
116 117 118 |
# File 'lib/cli/release_tarball.rb', line 116 def exists? File.exists?(@tarball_path) && File.readable?(@tarball_path) end |
#handle_dot_slash_prefix(target) ⇒ Object
This will [add or remove] the ‘./’ when trying to extract a specific file from archive
65 66 67 68 69 70 71 |
# File 'lib/cli/release_tarball.rb', line 65 def handle_dot_slash_prefix(target) if target =~ /^\.\/.*/ target.sub!(/^\.\//, '') else target.prepend("./") end end |
#job_tarball_path(name) ⇒ Object
143 144 145 146 147 |
# File 'lib/cli/release_tarball.rb', line 143 def job_tarball_path(name) return nil unless valid? unpack File.join(@unpack_dir, 'jobs', "#{name}.tgz") end |
#license ⇒ Object
401 402 403 404 405 406 407 |
# File 'lib/cli/release_tarball.rb', line 401 def license m = Psych.load(manifest) license = m['license'] return nil if !license license['name'] = 'license' TarballArtifact.new(license) end |
#license_resource ⇒ Object
155 156 157 158 159 |
# File 'lib/cli/release_tarball.rb', line 155 def license_resource return nil unless valid? unpack return Resources::License.new(@unpack_dir) end |
#manifest ⇒ Object
120 121 122 123 124 |
# File 'lib/cli/release_tarball.rb', line 120 def manifest return nil unless valid? unpack_manifest File.read(File.join(@unpack_dir, "release.MF")) end |
#manifest_yaml ⇒ Object
126 127 128 129 130 131 |
# File 'lib/cli/release_tarball.rb', line 126 def manifest_yaml return @manifest_yaml unless @manifest_yaml.nil? unpack_manifest manifest_file = File.("release.MF", @unpack_dir) @manifest_yaml = load_yaml_file(manifest_file) end |
#package_tarball_path(name) ⇒ Object
149 150 151 152 153 |
# File 'lib/cli/release_tarball.rb', line 149 def package_tarball_path(name) return nil unless valid? unpack File.join(@unpack_dir, 'packages', "#{name}.tgz") end |
#perform_validation(options = {}) ⇒ Object
If sparse release is allowed we bypass the requirement of having all jobs and packages in place when we do validation. However for jobs and packages that are present we still need to validate checksums
231 232 233 234 235 236 237 238 239 240 241 |
# File 'lib/cli/release_tarball.rb', line 231 def perform_validation( = {}) step("File exists and readable", "Cannot find release file #{@tarball_path}", :fatal) do exists? end validate_manifest if .fetch(:validate_manifest, true) validate_packages() validate_jobs() print_manifest if .fetch(:print_release_info, true) end |
#print_manifest ⇒ Object
425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 |
# File 'lib/cli/release_tarball.rb', line 425 def print_manifest manifest = manifest_yaml say("\nRelease info") say("------------") say("Name: #{manifest["name"]}") say("Version: #{manifest["version"]}") say("\nPackages") if manifest[@packages_folder].empty? say(" - none") end for package in manifest[@packages_folder] say(" - #{package["name"]} (#{package["version"]})") end say("\nJobs") if manifest["jobs"].empty? say(" - none") end for job in manifest["jobs"] say(" - #{job["name"]} (#{job["version"]})") end say("\nLicense") if manifest["license"].nil? || manifest["license"].empty? say(" - none") else say(" - license (#{manifest["license"]["version"]})") end nl end |
#raw_fast_unpack(target) ⇒ Object
73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 |
# File 'lib/cli/release_tarball.rb', line 73 def raw_fast_unpack(target) tar_version, _, _ = Open3.capture3('tar', '--version') case tar_version when /.*gnu.*/i Kernel.system("tar", "-C", @unpack_dir, "-xzf", @tarball_path, "--occurrence", "#{target}", out: "/dev/null", err: "/dev/null") when /.*bsd.*/i if target[-1, 1] == "/" raw_unpack(target) else Kernel.system("tar", "-C", @unpack_dir, "--fast-read", "-xzf", @tarball_path, "#{target}", out: "/dev/null", err: "/dev/null") end else raw_unpack(target) end end |
#raw_unpack(target) ⇒ Object
90 91 92 |
# File 'lib/cli/release_tarball.rb', line 90 def raw_unpack(target) Kernel.system("tar", "-C", @unpack_dir, "-xzf", @tarball_path, "#{target}", out: "/dev/null", err: "/dev/null") end |
#repack(package_matches = []) ⇒ Object
Repacks tarball according to the structure of remote release Return path to repackaged tarball or nil if repack has failed
188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 |
# File 'lib/cli/release_tarball.rb', line 188 def repack(package_matches = []) return nil unless valid? unpack if upload_packages?(package_matches) tmpdir = Dir.mktmpdir repacked_path = File.join(tmpdir, "release-repack.tgz") manifest = load_yaml_file(File.join(@unpack_dir, "release.MF")) local_packages = manifest[@packages_folder] local_jobs = manifest["jobs"] @skipped = 0 Dir.chdir(@unpack_dir) do local_packages.each do |package| say("#{package["name"]} (#{package["version"]})".ljust(30), " ") if package_matches and package_matches.include?(package["sha1"]) || (package["fingerprint"] && package_matches.include?(package["fingerprint"])) say("SKIP".make_green) @skipped += 1 FileUtils.rm_rf(File.join(@packages_folder, "#{package["name"]}.tgz")) else say("UPLOAD".make_red) end end local_jobs.each do |job| say("#{job["name"]} (#{job["version"]})".ljust(30), " ") say("UPLOAD".make_red) end return nil if @skipped == 0 exit_success = system("tar", "-czf", repacked_path, ".", out: "/dev/null", err: "/dev/null") return repacked_path if exit_success end end |
#replace_manifest(hash) ⇒ Object
137 138 139 140 141 |
# File 'lib/cli/release_tarball.rb', line 137 def replace_manifest(hash) return nil unless valid? unpack write_yaml(hash, File.join(@unpack_dir, "release.MF")) end |
#safe_fast_unpack(target) ⇒ Object
On machines using GNU based tar command, it should be able to unpack files irrespective of the ./ prefix in the file name
46 47 48 49 50 51 52 53 |
# File 'lib/cli/release_tarball.rb', line 46 def safe_fast_unpack(target) exit_status = raw_fast_unpack(target) if !exit_status processed_target = handle_dot_slash_prefix(target) exit_status = raw_fast_unpack(processed_target) end exit_status end |
#safe_unpack(target) ⇒ Object
55 56 57 58 59 60 61 62 |
# File 'lib/cli/release_tarball.rb', line 55 def safe_unpack(target) exit_status = raw_unpack(target) if !exit_status processed_target = handle_dot_slash_prefix(target) exit_status = raw_unpack(processed_target) end exit_status end |
#unpack ⇒ Object
Unpacks tarball to @unpack_dir, returns true if succeeded, false if failed
104 105 106 107 108 |
# File 'lib/cli/release_tarball.rb', line 104 def unpack return @unpacked unless @unpacked.nil? exit_success = system("tar", "-C", @unpack_dir, "-xzf", @tarball_path, out: "/dev/null", err: "/dev/null") @unpacked = !!exit_success end |
#unpack_jobs ⇒ Object
28 29 30 31 32 33 34 35 |
# File 'lib/cli/release_tarball.rb', line 28 def unpack_jobs return @unpacked_jobs unless @unpacked_jobs.nil? exit_success = safe_fast_unpack('./jobs/') unless all_release_jobs_unpacked? exit_success = safe_unpack('./jobs/') end @unpacked_jobs = !!exit_success end |
#unpack_license ⇒ Object
37 38 39 40 41 42 |
# File 'lib/cli/release_tarball.rb', line 37 def unpack_license return false if manifest_yaml['license'].nil? return @unpacked_license unless @unpacked_license.nil? exit_success = safe_fast_unpack('./license.tgz') @unpacked_license = !!exit_success end |
#unpack_manifest ⇒ Object
22 23 24 25 26 |
# File 'lib/cli/release_tarball.rb', line 22 def unpack_manifest return @unpacked_manifest unless @unpacked_manifest.nil? exit_success = safe_fast_unpack('./release.MF') @unpacked_manifest = !!exit_success end |
#upload_packages?(package_matches = []) ⇒ Boolean
181 182 183 184 |
# File 'lib/cli/release_tarball.rb', line 181 def upload_packages?(package_matches = []) return true if package_matches.nil? package_matches.uniq.size != manifest_yaml[@packages_folder].map { |p| p['version'] }.uniq.size end |
#validate_jobs(options = {}) ⇒ Object
312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 |
# File 'lib/cli/release_tarball.rb', line 312 def validate_jobs( = {}) allow_sparse = .fetch(:allow_sparse, false) unpack_jobs unpack_license total_jobs = manifest_yaml["jobs"].size step("Checking jobs format", "Jobs are not versioned, please re-create release with current CLI version (or any CLI >= 0.4.4)", :fatal) do total_jobs > 0 && manifest_yaml["jobs"][0].is_a?(Hash) end manifest_yaml["jobs"].each_with_index do |job, i| @jobs << job name = job["name"] version = job["version"] job_file = File.(name + ".tgz", @unpack_dir + "/jobs") job_exists = File.exists?(job_file) step("Read job '%s' (%d of %d), version %s" % [name, i+1, total_jobs, version], "Job '#{name}' not found") do job_exists || allow_sparse end if job_exists job_tmp_dir = Dir.mktmpdir FileUtils.mkdir_p(job_tmp_dir) job_extracted = !!system("tar", "-C", job_tmp_dir, "-xzf", job_file, out: "/dev/null", err: "/dev/null") step("Extract job '#{name}'", "Cannot extract job '#{name}'") do job_extracted end if job_extracted job_manifest_file = File.("job.MF", job_tmp_dir) if File.exists?(job_manifest_file) job_manifest = load_yaml_file(job_manifest_file) end job_manifest_valid = job_manifest.is_a?(Hash) step("Read job '#{name}' manifest", "Invalid job '#{name}' manifest") do job_manifest_valid end if job_manifest_valid && job_manifest["templates"] job_manifest["templates"].each_key do |template| step("Check template '#{template}' for '#{name}'", "No template named '#{template}' for '#{name}'") do File.exists?(File.(template, job_tmp_dir + "/templates")) end end end validate_job_packages = .fetch(:validate_job_packages, true) if validate_job_packages && job_manifest_valid && job_manifest["packages"] job_manifest["packages"].each do |package_name| step("Job '#{name}' needs '#{package_name}' package", "Job '#{name}' references missing package '#{package_name}'") do @available_packages[package_name] || allow_sparse end end end step("Monit file for '#{name}'", "Monit script missing for job '#{name}'") do File.exists?(File.("monit", job_tmp_dir)) || Dir.glob("#{job_tmp_dir}/*.monit").size > 0 end end end end end |
#validate_manifest ⇒ Object
243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 |
# File 'lib/cli/release_tarball.rb', line 243 def validate_manifest step("Extract manifest", "Cannot extract manifest #{@tarball_path}", :fatal) do unpack_manifest end manifest_file = File.("release.MF", @unpack_dir) step("Manifest exists", "Cannot find release manifest", :fatal) do File.exists?(manifest_file) end @manifest_yaml = nil step("Release name/version", "Manifest doesn't contain release name and/or version") do manifest_yaml.is_a?(Hash) && manifest_yaml.has_key?("name") && manifest_yaml.has_key?("version") end @release_name = manifest_yaml["name"] @version = manifest_yaml["version"].to_s @validated = true end |
#validate_packages(options = {}) ⇒ Object
268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 |
# File 'lib/cli/release_tarball.rb', line 268 def validate_packages( = {}) allow_sparse = .fetch(:allow_sparse, false) unpack total_packages = manifest_yaml[@packages_folder].size @available_packages = {} manifest_yaml[@packages_folder].each_with_index do |package, i| @packages << package name = package['name'] package_file = File.(name + ".tgz", File.join(@unpack_dir, @packages_folder)) package_exists = File.exists?(package_file) step("Read package '%s' (%d of %d)" % [name, i+1, total_packages], "Missing package '#{name}'") do package_exists || allow_sparse end if package_exists @available_packages[name] = true end end # Check package dependencies # Note that we use manifest["packages"] here; manifest contains # all packages even if release is sparse, so we can detect problems # even in sparse release tarball. if total_packages > 0 step("Package dependencies", "Package dependencies couldn't be resolved") do begin tsort_packages(manifest_yaml[@packages_folder].inject({}) { |h, p| h[p["name"]] = p["dependencies"] || []; h }) true rescue Bosh::Cli::CircularDependency, Bosh::Cli::MissingDependency => e errors << e. false end end end end |